鵝長微服務發現與治理巨作PolarisMesh實踐-上( 六 )

啟動類ConsumerApplication.java
package cn.itxs;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableFeignClientspublic class ConsumerApplication{public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}}啟動消費者微服務ProviderApplication

鵝長微服務發現與治理巨作PolarisMesh實踐-上

文章插圖
查看控制臺頁面服務列表可以看到提供者微服務已經注冊到北極星中default命名空間
鵝長微服務發現與治理巨作PolarisMesh實踐-上

文章插圖
通過消費者提供控制器訪問接口訪問,http://192.168.44.161:38888/hello/1  , 返回服務提供者的結果,成功實現服務注冊和發現 。
鵝長微服務發現與治理巨作PolarisMesh實踐-上

文章插圖
動態配置示例引入spring-cloud-starter-tencent-polaris-config 實現 Spring Cloud 配置的動態管理,spring-cloud-starter-bootstrap 以便可以支持 bootstrap.yml 的識別與加載 。添加依賴如下:
<dependency><groupId>com.tencent.cloud</groupId><artifactId>spring-cloud-starter-tencent-polaris-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency>在 resources 目錄下創建 bootstrap.yml 文件,并按照如下進行配置
server:port: 48084spring:application:name: config-groupcloud:polaris:address: grpc://192.168.5.52:8091namespace: defaultconfig:auto-refresh: true # auto refresh when config file changedgroups:- name: ${spring.application.name} # group namefiles: [ "config/user.yaml" ]創建配置分組以及配置文件
  • 創建配置分組 config-group
  • 創建配置文件 config/user.yaml , 編輯和發布配置,編輯配置項內容為name: zhangsan
創建提供者微服務演示控制器ConfigController.java
package cn.itxs.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RefreshScopepublic class ConfigController {@Value("${name}")private String name;@RequestMapping("/name")public String name() {return name;}}
鵝長微服務發現與治理巨作PolarisMesh實踐-上

文章插圖
啟動后訪問http://192.168.44.161:48084/name,成功讀到北極星配置中心的配置
鵝長微服務發現與治理巨作PolarisMesh實踐-上

文章插圖
**本人博客網站 **IT小神www.itxiaoshen.com

推薦閱讀