<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
文章插图
查看控制台页面服务列表可以看到提供者微服务已经注册到北极星中default命名空间
文章插图
消费者微服务示例与上面服务提供类似,在项目中添加一个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.javapackage 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);}}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 四十八 SpringCloud微服务实战——搭建企业级开发框架:【移动开发】整合uni-app搭建移动端快速开发框架-使用第三方UI框架
- Dapr实现.Net Grpc服务之间的发布和订阅,并采用WebApi类似的事件订阅方式
- Silky微服务框架之模块
- 一篇文章带你了解轻量级Web服务器——Nginx简单入门
- iphone12有无线充电吗_iphone12可以使用无线充电吗
- 如何转账到支付宝(支付宝不绑卡怎么把钱转到微信)
- Seata 环境搭建
- 四十七 SpringCloud微服务实战——搭建企业级开发框架:【移动开发】整合uni-app搭建移动端快速开发框架-添加Axios并实现登录功能
- 记录一次sshd服务启动失败
- 微信小程序仿手机相册组件——简单版