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


<parent><groupId>cn.itxs</groupId><artifactId>spring-cloud-tencent-demo</artifactId><version>1.0-SNAPSHOT</version></parent><!-- 簡單的 Spring Cloud Web 依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 引入 Spring Cloud Tencent 的服務注冊發現依賴 --><dependency><groupId>com.tencent.cloud</groupId><artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency>在provider-service的 resources 目錄下創建 application.yml 文件 , 并按照如下進行配置
server:port: 28888spring:application:name: provider-servicecloud:polaris:# 配置polaris servre地址address: grpc://192.168.5.52:8091discovery:enabled: truestat:enabled: trueport: 28082創建提供者微服務演示控制器ProviderHelloController.java
package cn.itxs.controller;import com.tencent.cloud.polaris.PolarisDiscoveryProperties;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ProviderHelloController {private final PolarisDiscoveryProperties properties;ProviderHelloController(PolarisDiscoveryProperties properties) {this.properties = properties;}@RequestMapping("/hello/{val}")public String echo(@PathVariable String val) {return "Hello PolarisMesh,this is it xiao shen," + val + ", I'm " + properties.getService();}}啟動類ProviderApplication.java
package cn.itxs;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ProviderApplication{public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}}啟動提供者微服務ProviderApplication

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

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

文章插圖
消費者微服務示例與上面服務提供類似,在項目中添加一個consumer-service模塊,在消費者微服務的pom依賴中添加父Maven項目的依賴、 Web 服務依賴、polaris服務注冊依賴
<parent><groupId>cn.itxs</groupId><artifactId>spring-cloud-tencent-demo</artifactId><version>1.0-SNAPSHOT</version></parent><dependencies><!-- 簡單的 Spring Cloud Web 依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 引入 Spring Cloud Tencent 的服務注冊發現依賴 --><dependency><groupId>com.tencent.cloud</groupId><artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId></dependency><!-- 引入 Feign 依賴實現 Feign 調用 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies>在consumer-service的 resources 目錄下創建 application.yml 文件,并按照如下進行配置
server:port: 38888spring:application:name: consumer-servicecloud:polaris:address: grpc://192.168.5.52:8091discovery:enabled: truestat:enabled: trueport: 38082創建Feign接口HelloService.java , 通過feign實現遠程方法的調用
package cn.itxs.service;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.stereotype.Service;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;@Service@FeignClient(name = "provider-service")public interface HelloService {@RequestMapping("/hello/{value}")String hello(@PathVariable("value") String value);}創建提供者微服務演示控制器ProviderHelloController.java
package cn.itxs.controller;import cn.itxs.service.HelloService;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ConsumerHelloController {private final HelloService helloService;ConsumerHelloController(HelloService helloService) {this.helloService = helloService;}@RequestMapping(value = "https://www.huyubaike.com/hello/{val}")public String echo(@PathVariable String val) {return helloService.hello(val);}}

推薦閱讀