鹅长微服务发现与治理巨作PolarisMesh实践-上( 六 )

提供者微服务示例在项目中添加一个provider-service模块 , 在提供者微服务的pom依赖中添加父Maven项目的依赖、 Web 服务依赖、polaris服务注册依赖
<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

经验总结扩展阅读