💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
## 一、微服务向zookeeper注册 ### 引入依赖包 在之前的章节已经为大家介绍过微服务向eureka、zookeeper注册的实现过程,大同小异。首先我们需要通过maven坐标引入consul的包。 > 如果项目pom之前存在spring-cloud-starter-netflix-eureka-client,与eureka相关的要删掉,spring-cloud-starter-zookeeper-discovery要删掉。 ~~~ <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ~~~ 因为consul的健康检查端点为'/actuator/health',依赖于spring-boot-starter-actuator,所以对应的maven坐标要一并引入。 ### 配置consul 将host指向consul集群内的任意一个server即可。 ~~~ spring: cloud: consul: #Consul服务注册发现配置 host: 192.168.161.3 port: 8500 discovery: prefer-ip-address: true service-name: ${spring.application.name} ~~~ ### 打开discovery开关 如果关于该注解的理解有问题,请参考《DiscoveryClient服务发现》。 ![](https://img.kancloud.cn/e2/8d/e28d1607154a5f5fd14fa987558b87f0_501x199.png) ### 启动日志 出现如下的一些日志,表示服务注册成功 ![](https://img.kancloud.cn/65/2c/652cdff42f4720b46fcc7ccadc72a49e_1311x153.png) ## 二、验证服务注册结果 可以通过consul的WEB UI界面查看。也可以通过命令行,获取aservice-sms的注册信息,可以使用如下命令。 ~~~ curl -s 127.0.0.1:8500/v1/catalog/service/aservice-sms ~~~ ![](https://img.kancloud.cn/67/ac/67ac97f0285a8e1f2d5908a65522ab7d_1884x341.png) ## 三、FeignClient远程服务名称调整 ![](https://img.kancloud.cn/48/ec/48ecfaf73c4cd718d7836ff514b1014d_702x146.png) 需要注意的是,和zookeeper一样,consul注册的服务名称'[spring.application.name](http://spring.application.name/)',但是并不会将服务名称转成大写,这点与eureka有所区别。所以在使用FeignClient的时候,不能写“ASERVICE-SMS”,而是写“aservice-sms”。 ## 四、接口访问测试 访问aservice-rbac的“/pwd/reset”接口(该接口中远程调用了aservice-sms的“/sms/send”短信发送方法),得到正确结果。说明我们的集群安装及服务注册发现全部可以正确的使用。 ![](https://img.kancloud.cn/2c/58/2c58608057fed2f1eeec6b658adb9a31_1498x577.png) 如果出现401的错误,请看下一节内容。