十四、SpringCloud Bus 消息总线
#
1、概述#
1)是什么?
Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统连接起来的框架
2)能干嘛?
Spring Cloud Bus能够管理和传播分布式系统间的消息,就像一个分布式执行器,用于广播状态更改,事件推送,是微服务之间的通信通道
3)为何被称之为总线
① 在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中的所有微服务实例都连接上来。由于主题中产生的消息会被所有的实例监听和消费,所以称它为消息总线。在总线上的各个实例,都可以方便的广播一些消息给其他链接在总线上的实例。
②、ConfigClient实例都监听MQ中的同一个topic(默认是SpringCloudBus)。当一个服务刷新数据的时候,它会把这个信息放到Topic中,这样其他监听同一Topic的服务就能得到通知,然后去更新自身的配置
2、RabbitMQ的环境配置#
Windows 版
1)安装Erlang
http://erlang.org/download/opt_win64_23.1.exe
2)安装RabbitMQ
https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exe
具体的还有安装过程中遇到的一些错误,我就不写了!
3、SpringCloud Bus动态刷新全局广播#
【注意】 示例架构:一台config-server 3344 两台config-client 3355 3366
1)设计思想
两种设计思想:
①、利用消息总线触发一个客户端的 /bus/refresh,从而刷新所有的客户端配置
②、利用消息总线触发一个服务端 configServer的 /bus/refresh,从而刷新所有的客户端配置
显然图二的架构更加合适,图一不适合的原因有
a、打破了微服务的职责单一性,因为微服务是业务模块,本身不应该承担配置刷新职责
b、破环了微服务节点的对等性
c、有一定的局限性,例如,微服务在迁移是,它的网络地址常常会发生变化,如果想要做到自动刷新,那就会增加更
多的修改
2)配置的实现
3344:
pom:添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
yml
server: port: 3344 spring: application: name: cloud-config-center #注册进Eureka服务器的微服务名 cloud: config: server: git: uri: https://github.com/Coder-Machine/springcloud-config.git #GitHub上面的git仓库名字 ####搜索目录 search-paths: - springcloud-config ####读取分支 label: master # 增加 rabbitmq的相关配置 rabbitmq: host: localhost port: 5672 username: guest password: guest #服务注册到eureka 上 eureka: client: service-url: defaultZone: http://localhost:7001/eureka #暴露 bus 刷新配置的端点 management: endpoints: web: exposure: include: 'bus-refresh'
3355:
pom
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
yml
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 rabbitmq: host: localhost port: 5672 username: guest password: guest #服务注册到eureka地址 eureka: client: service-url: defaultZone: http://localhost:7001/eureka # 暴露监控端点 management: endpoints: web: exposure: include: "*"
3366
pom
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
yml
server: port: 3366 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 rabbitmq: host: localhost port: 5672 username: guest password: guest #服务注册到eureka地址 eureka: client: service-url: defaultZone: http://localhost:7001/eureka # 暴露监控端点 management: endpoints: web: exposure: include: "*"
3)测试验证
①、修改github配置
由 config: info: "master branch,springcloud-config/config-dev.yml version=444444" 改为 config: info: "master branch,springcloud-config/config-dev.yml version=houchen"
②、访问config-server
http://localhost:3344/master/config-dev.yml
访问config-client,此时并未刷新
③、向config-server发送post请求刷新
curl -X POST http://localhost:3344/actuator/busre-fresh
④、查看各个congfig-client的配置是否刷新
http://localhost:3355/configInfo
4、SpringCloud Bus动态刷新定点通知#
不想通知所有的 ConfigClient,只想定点通知:例如( 只通知3355,不通知3366)
1)如何实现
指定具体的某一个实例生效而不是全部
公式:http://localhost:配置中心的端口号/actuator/bus-refresh/{destination}
/bus/refresh请求不在发送到具体的服务实例上,而是发给config server并通过destination参数 指定需要更新配置的服务或者实例;
2)举例
只通知3355:
curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2020-06-19 spring中 属性注入的四种方式
2020-06-19 尚硅谷Docker核心技术 — 有道云笔记