跟我学SpringCloud | 第八篇:Spring Cloud Bus 消息总线
SpringCloud系列教程 | 第八篇:Spring Cloud Bus 消息总线
Springboot: 2.1.6.RELEASE
SpringCloud: Greenwich.SR1
如无特殊说明,本系列教程全采用以上版本
前面两篇文章我们聊了Spring Cloud Config配置中心,当我们在更新github上面的配置以后,如果想要获取到最新的配置,需要手动刷新或者利用webhook的机制每次提交代码发送请求来刷新客户端,客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了。使用Spring Cloud Bus(国人很形象的翻译为消息总线,我比较喜欢叫消息巴士)可以完美解决这一问题。
1. Spring Cloud Bus
Spring cloud bus通过轻量消息代理连接各个分布的节点。这会用在广播状态的变化(例如配置变化)或者其他的消息指令。Spring bus的一个核心思想是通过分布式的启动器对spring boot应用进行扩展,也可以用来建立一个多个应用之间的通信频道。目前唯一实现的方式是用AMQP消息代理作为通道,同样特性的设置(有些取决于通道的设置)在更多通道的文档中。
大家可以将它理解为管理和传播所有分布式项目中的消息既可,其实本质是利用了MQ的广播机制在分布式的系统中传播消息,目前常用的有Kafka和RabbitMQ。利用bus的机制可以做很多的事情,其中配置中心客户端刷新就是典型的应用场景之一,我们用一张图来描述bus在配置中心使用的机制。

根据此图我们可以看出利用Spring Cloud Bus做配置更新的步骤:
- 提交代码触发post给客户端A发送bus/refresh
- 客户端A接收到请求从Server端更新配置并且发送给Spring Cloud Bus
- Spring Cloud bus接到消息并通知给其它客户端
- 其它客户端接收到通知,请求Server端获取最新配置
- 全部客户端均获取到最新的配置
2. 项目示例
我们使用上一篇文章中的config-server和config-client来进行改造,mq使用rabbitmq来做示例。
2.1 客户端config-client
2.1.1 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
需要多引入spring-cloud-starter-bus-amqp包,增加对消息总线的支持
2.1.2 配置文件 bootstrap.properties
spring.application.name=spring-cloud-config-client
server.port=8081
spring.cloud.config.name=springcloud-config
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=spring-cloud-config-server
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
management.endpoints.web.exposure.include=*
## 开启消息跟踪
spring.cloud.bus.trace.enabled=true
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=
spring.rabbitmq.password=
配置文件需要增加RebbitMq的相关配置,这样客户端代码就改造完成了。
2.1.3 测试
依次启动eureka,config-serve,config-client。
修改config-client启动配置,同时在8081和8082端口启动服务。
启动完成后,浏览器分别访问连接:http://localhost:8081/hello, http://localhost:8082/hello, 可以发现页面显示的内容都是:hello dev update1,说明客户端都已经读取到了server端的内容。
现在我们更新github上的配置文件,将配置内容改为hello dev update,先访问一下http://localhost:8081/hello,可以看到页面依然显示为:hello dev update1。
我们对端口为8081的服务发送一个/actuator/bus-refresh的POST请求,在win10下使用下面命令来模拟webhook。
curl -X POST http://localhost:8081/actuator/bus-refresh
注意: 在springboot2.x的版本中刷新路径为:/actuator/bus-refresh,在springboot1.5.x的版本中刷新路径为:/bus/refresh。
执行完成后,我们先访问http://localhost:8082/hello,可以看到页面打印内容已经变为:hello dev update,这样说明,我们8081端口的服务已经把更新后的信息通过rabbitmq推送给了8082端口的服务,这样我们就实现了图一中的示例。
2.2 改进版
上面的流程中,虽然我们做到了利用一个消息总线触发刷新,而刷新所有客户端配置的目的,但是这种方式并不合适,如下:
- 打破了微服务的职责单一性。微服务本身是业务模块,它本不应该承担配置刷新的职责。
- 破坏了微服务各节点的对等性。
- 如果客户端ip有变化,这时我们就需要修改WebHook的配置。
我们可以将上面的流程改进一下:

这时Spring Cloud Bus做配置更新步骤如下:
- 提交代码触发post给Server端发送bus/refresh
- Server端接收到请求并发送给Spring Cloud Bus
- Spring Cloud bus接到消息并通知给其它客户端
- 其它客户端接收到通知,请求Server端获取最新配置
- 全部客户端均获取到最新的配置
这样的话我们在server端的代码做一些改动,来支持/actuator/bus-refresh
和上面的client端的改动基本一致
2.2.1 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
需要多引入spring-cloud-starter-bus-amqp包,增加对消息总线的支持
2.2.2 配置文件application.yml
server:
port: 8080
spring:
application:
name: spring-cloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/meteor1993/SpringCloudLearning
search-paths: chapter6/springcloud-config
username:
password:
rabbitmq:
host: 217.0.0。1
port: 5672
username:
password:
management:
endpoints:
web:
exposure:
include: "*"
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
配置文件需要增加RebbitMq的相关配置,actuator开启所有访问。
2.2.3 测试
依次启动eureka,config-serve,config-client。
修改config-client启动配置,同时在8081和8082端口启动服务。
按照上面的测试方式,访问两个客户端测试均可以正确返回信息。同样修改配置文件,将值改为:hello im dev update并提交到仓库中。在win10下使用下面命令来模拟webhook。
curl -X POST http://localhost:8081/actuator/bus-refresh
执行完成后,依次访问两个客户端,返回:hello im dev update。说明三个客户端均已经拿到了最新配置文件的信息,这样我们就实现了上图中的示例。
参考:http://www.ityouknow.com/springcloud/2017/05/26/springcloud-config-eureka-bus.html
跟我学SpringCloud | 第八篇:Spring Cloud Bus 消息总线的更多相关文章
- Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ)
详见:https://www.w3cschool.cn/spring_cloud/spring_cloud-jl8a2ixp.html 上一篇文章,留了一个悬念,Config Client 实现配置的 ...
- spring cloud bus 消息总线 动态刷新配置文件 【actuator 与 RabbitMQ配合完成】
1.前言 单机刷新配置文件,使用actuator就足够了 ,但是 分布式微服务 不可能是单机 ,将会有很多很多的工程 ,无法手动一个一个的发送刷新请求, 因此引入了消息中间件 ,常用的 消息中间件 是 ...
- Spring Cloud Bus 消息总线 RabbitMQ
Spring Cloud Bus将分布式系统中各节点通过轻量级消息代理连接起来. 从而实现例如广播状态改变(例如配置改变)或其他的管理指令. 目前唯一的实现是使用AMQP代理作为传输对象. Sprin ...
- 干货|Spring Cloud Bus 消息总线介绍
继上一篇 干货|Spring Cloud Stream 体系及原理介绍 之后,本期我们来了解下 Spring Cloud 体系中的另外一个组件 Spring Cloud Bus (建议先熟悉 Spri ...
- Spring Cloud 2-Bus 消息总线(九)
Spring Cloud Bus 1.服务端配置 pom.xml application.yml 2.客户端配置 pom.xml application.yml Controller.java 3 ...
- 跟我学SpringCloud | 第二十章:Spring Cloud 之 okhttp
1. 什么是 okhttp ? okhttp 是由 square 公司开源的一个 http 客户端.在 Java 平台上,Java 标准库提供了 HttpURLConnection 类来支持 HTTP ...
- Spring Cloud Stream消息总线
Springcloud 里面对于MQ的整合一个是前一篇的消息总线一个是本文介绍的消息驱动 大体要学习这么几个知识点: 课题:SpringCloud消息驱动Stream1.什么是SpringCloud消 ...
- 跟我学SpringCloud | 第三篇:服务的提供与Feign调用
跟我学SpringCloud | 第三篇:服务的提供与Feign调用 上一篇,我们介绍了注册中心的搭建,包括集群环境吓注册中心的搭建,这篇文章介绍一下如何使用注册中心,创建一个服务的提供者,使用一个简 ...
- SpringCloud学习之Bus消息总线实现配置自动刷新(九)
前面两篇文章我们聊了Spring Cloud Config配置中心,当我们在更新github上面的配置以后,如果想要获取到最新的配置,需要手动刷新或者利用webhook的机制每次提交代码发送请求来刷新 ...
随机推荐
- 开源 自由 java CMS - FreeCMS1.9 积分规则管理
项目地址:http://www.freeteam.cn/ 积分规则管理 管理会员操作时积分处理规则. 1. 积分规则管理 从左側管理菜单点击积分规则进入. 2. 加入积分规则 在积分规则列表下方点击& ...
- 多线程——继承Thread类别
详细java此前使用多线程,让我们来看看下面的问题. 什么是多线程 简单的理解成:cpu"同一时候"运行多个任务,这就是多线程. (究其本质,当涉及到进程和线程的概念.上面 ...
- debian9 安装 odoo11 笔记用 部分内容转载前辈的,在此感谢
1先创建个odoo用户 sudo adduser odoo 2:给root 权限: sudo vi /etc/sudoers 修改文件参考如下: # User privilege specificat ...
- WPF Binding的代码实现
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 【windows】常见的系统环境变量,如%appdata%表示什么意思
原文:[windows]常见的系统环境变量,如%appdata%表示什么意思 1.介绍 %appdata%就代表了C:Users\用户名\AppData\Roaming这个文件夹. “%”是系统变量的 ...
- SQLServer2008-2012开启远程连接的配置方法
一.远程连接端口设置(很关键的一步)1.在服务器上打开SQL Server Configuration Manager.选择SQL Server配置管理器->SQL Server 网络配置-&g ...
- Win8 Metro(C#)数字图像处理--2.40二值图像轮廓提取算法
原文:Win8 Metro(C#)数字图像处理--2.40二值图像轮廓提取算法 [函数名称] 二值图像轮廓提取 ContourExtraction(WriteableBitm ...
- UWP-ListView到底部自动加载更多数据
原文:UWP-ListView到底部自动加载更多数据 ListView绑定的数据当需要“更多”时自动加载 ListView划到底部后,绑定的ObservableCollection列表数据需要加载的更 ...
- EF日志记录,包括记录Linq生成的Sql
<interceptors> <interceptor type="System.Data.Entity.Infrastructure.Interception.Datab ...
- 了解Activity
Android中的activity全都归属于task管理 .task 是多个 activity 的集合,这些 activity 按照启动顺序排队存入一个栈(即“back stack”).android ...