项目源码及相关说明请查看此文:Spring Cloud入门教程-简介
接上一篇文章,这里介绍一下,如何使用Bus 在不停机情况下,刷新配置。
Spring Cloud Bus是用轻量的消息代理将分布式的节点连接起来,可以用于广播配置文件的更改或者服务的监控管理。一个关键的思想就是,消息总线可以为微服务做监控,也可以实现应用程序之间相互通信。 Spring Cloud Bus可选的消息代理线线泡括RabbitMQ、 AMQP和Kaka等。本节讲述的是用 RabbitMQ作为 Spring Cloud的消息组件去刷新更改微服务的配置文件。
为什么需要用 Spring Cloud Bus去刷新配置呢?
如果有几十个微服务,而每一个服务又是多实例,当更改配置时,需要重新启动多个微服务实例,会非常麻烦。 Spring Cloud Bus的一个功能就是让这个过程变得简单,当远程Git仓库的配置更改后,只需要向某一个微服务实例发送一个Post请求,通过消息组件通知其他微
服务实例重新拉取配置文件。
注意:这里需要安装Rabbit ,如何安装这里就不说了,大家自行百度,但是要注意版本的问题。
下面对config-server和config-client 进行改造。
在两个module 的pom.xml dou都加入一下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
这里要说的是,最新版本的依赖和配置都与老版本不同,而且有很大的区别。
添加 rabbit 和bus 配置
config-server application.properties 完整配置如下:
server.port=8797
spring.application.name=config-server
#从本地读取配置文件####################################################
#spring.profiles.active=native
#spring.cloud.config.server.native.search-locations=classpath:/shared
#####################################################################
eureka.client.service-url.defaultZone=http://localhost:8791/eureka/
#spring cloud bus 刷新配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.password=guest
spring.rabbitmq.username=guest
spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh
#从读取远程github配置文件####################################################
spring.cloud.config.server.git.uri=https://github.com/xuweichao/SpringCloud-Demo
spring.cloud.config.server.git.search-paths=SpringcloudConfig
spring.cloud.config.server.git.username=******
spring.cloud.config.server.git.password=******
spring.cloud.config.label=master
config-client bootrap.properties 完整配置如下:
server.port=8798
spring.application.name=config-client
#spring.profiles.active=dev
#spring.cloud.config.uri=http://localhost:8797
#spring.cloud.config.fail-fast=true
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.discovery.service-id=config-server
spring.cloud.config.discovery.enabled=true
spring.cloud.config.fail-fast=true
eureka.client.service-url.defaultZone=http://localhost:8791/eureka/
#spring cloud bus 刷新配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.password=guest
spring.rabbitmq.username=guest
spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
#客户端不开放这个更新接口,在server端开放
#management.endpoints.web.exposure.include=bus-refresh
剩下最重要的一步,就是在你通过
@Value("${xxx}")
读取配置文件中变量的文件中添加@RefreshScope 注解,否则不会改变。
如:
@RefreshScope
@RestController
public class MainController {
@Value("${application.name}")
private String name;
@GetMapping("main")
public String main() {
return name;
}
}
重新启动config-server和两个config-client实例端口分别为8798和8799。
rabbitMQ 会生成三个队列。
向请求http://localhost:8799/main http://localhost:8798/main 都返回“config-client-v1”.
修改github 中eureka-client-dev.properties
application.name=config-client-v2 保存
请求http://localhost:8797/actuator/bus-refresh ,结束后再请求http://localhost:8799/main http://localhost:8798/main 都返回“config-client-v2”.