SpringCloud Bus消息总线简单来说就是springcloud对mq的封装。目前只支持:rabbitmq和kafka。
通常的使用场景:修改了分布式配置中心的配置,通过bus发送消息,所有订阅的服务同时刷新最新配置,也就是实时更新配置文件,不用重启所有微服务。
一、在不使用SpringCloud Bus情况下修改了配置文件,刷新服务配置通常是单台服务器刷新,效率慢。刷新配置:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
server: port: 8081 # 开启服务断点权限 management: endpoint: web: exposure: include: "*"
更新git配置,如当前age=21,更新age=88,调用服务刷新接口:http://127.0.0.1:8081/actuator/refrest
调用Controller验证:http://127.0.0.1:8081/getAge 返回age=88,但是,调用http://127.0.0.1:8082/getAge 返回结果仍然是age=21,所以需要bus总线对所有服务或集群同时刷新。
二、使用SpringCloud Bus情况下修改了配置文件,同时刷新所有服务。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
server: port: 8081 # 开启bus刷新 management: endpoint: web: exposure: include: bus-refresh
更新git配置,如当前age=21,更新age=88,调用服务刷新接口(注意和上面接口不一样):http://127.0.0.1:8081/actuator/bus-refresh
调用Controller验证:http://127.0.0.1:8081/getAge 返回age=88,调用http://127.0.0.1:8082/getAge 返回结果也是age=88,也就是虽然只调用了8081刷新接口,但是8082也同时刷新了最新配置,因为8082消费了mq消息
原理图: