(第十一篇)spring cloud之Bus消息总线
Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架,它整合了Java的事件处理机制和消息中间件的功能。Spring Clud Bus目前支持RabbitMQ和Kafka。Spring Cloud Bus能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、事件推送等,也可以当作微服务间的通信通道。官方文档。
目录
一、概述
Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架,它整合了Java的事件处理机制和消息中间件的功能。Spring Clud Bus目前支持RabbitMQ和Kafka。
Spring Cloud Bus能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、事件推送等,也可以当作微服务间的通信通道。
官方文档
https://spring.io/projects/spring-cloud-bus#overview
二、RabbitMQ环境
win环境安装
1、安装Erlang
25.3.1版本;双击后一直点击下一步即可
下载地址
https://www.erlang.org/patches/otp-25.3.1
2、安装RabbitMQ
版本3.11.7,双击后一直点击下一步即可
下载地址
https://github.com/rabbitmq/rabbitmq-server/releases/tag/v3.11.17
3、启动管理功能
进入到mq的sbin目录,输入命令
rabbitmq-plugins enable rabbitmq_management

4、测试
访问localhost:15672/

三、动态刷新全局广播
解决上一讲SpringCloud Config分布式配置中心无法动态刷新的问题
1、对cloud-config-center模块进行修改
(1)修改pom文件
添加maven坐标
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
(2)修改配置文件
server:
port: 3355
spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地址k
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
management:
endpoints: #暴露bus刷新配置的端点
web:
exposure:
include: 'bus-refresh'
#rabbitmq相关配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
2、对cloud-config-client模块进行修改
根据cloud-config-client模块复制出一个新的模块
(1)修改pom文件
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
(2)修改配置文件
server:
port: 3355
spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地址k
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
#rabbitmq相关配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
3、测试
- 启动eureka、config-center、config-client-1、config-client-2、rabbitMQ
- 修改git上的配置文件,将info改为PHP

- 在center模块访问,可以直接获取到

- 发送post请求curl -X POST "http://localhost:3344/actuator/bus-refresh"
![]()
- 客户端查询,发现可以查询到最新的值


四、指定模块通知
不想全部通知,只想定点通知,只需要在发送post的时候指定某个模块
localhost:配置中心的端口号/actuator/bus-refresh/{destination}
通过destination参数类指定需要更新配置的服务或实例
测试:
- 再次修改git的文件

- 发送post请求 curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"
- 客户端查询,发现只有3355可以查询到最新的值,而3366无法获取最新数据


更多推荐
所有评论(0)