一、Spring Cloud Bus
1. 是什么
- 分布式自动刷新配置。
Spring Cloud Bus
配合 Spring Cloud Config 使用,可以实现配置的动态刷新。
- Spring Cloud Bus 是用来将分布式系统的节点,与轻量级消息系统连接起来的框架。
- 它整合了 Java 的事件处理机制 和 消息中间件 的功能。
- Spring Cloud Bus 目前支持 RabbitMQ 和 Kafka。
2. 能干嘛
- Spring Cloud Bus 能 管理 和 传播 分布式系统间的 消息。
- 就像一个分布式执行器,可用于 广播状态更改、事件推送 等,也可以当作 微服务 间的通信通道。
3. 什么是总线
- 在微服务架构的系统中,通常会使用 轻量级的消息代理 来构建一个 共用的消息主题,并让系统中所有微服务实例都连接上来。
- 由于 该
主题
中产生的消息,会被所有实例 监听 和 消费,所以称它为消息总线
。- 在总线上的各个实例,都可以方便地广播一些,需要让其他连接在该主题上的实例都知道的消息。
- 基本原理。
- Config Client 实例都监听 MQ 中同一个 Topic(默认是
Spring Cloud Bus
)。- 当一个服务刷新数据的时候,它会把这个信息放入到
Topic
中,这样其它监听同一 Topic 的服务就能得到通知,然后去更新自身的配置。
4. WIndows 安装 RabbitMQ
4.1 安装 Erlang
4.2 安装 RabbitMQ
4.3 启动 RabbitMQ
rabbitmq-plugins enable rabbitmq_management
4.4 访问
http://localhost:15672/
用户名:guest,密码:guest
5. 动态刷新全局广播通知
5.1 设计思想
- 利用消息总线,触发一个客户端 Config Client 的
/bus/refresh
端点,而刷新所有客户端的配置。
- 【推荐】利用消息总线,触发一个服务端 Config Server 的
/bus/refresh
端点,而刷新所有客户端的配置。
- 图二的架构显然更加适合,图一不适合的原因如下。
- 打破了微服务的职责单一性,因为微服务本身是业务模块,它本不应该承担配置刷新的职责。
- 破坏了微服务各节点的对等性。
- 有一定的局限性。
例如:微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就会增加更多的修改。
5.2 修改 Module cloud-config-center3344
- 给配置中心服务端,添加消息总线支持。
5.2.1 POM
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
5.2.2 YML
spring:
# RabbitMQ 相关配置,15672是Web管理界面的端口,5672是MQ访问的端口
rabbitmq:
host: 192.168.137.155
port: 5672
username: guest
password: guest
# 暴露监控端点
management:
endpoints:
web:
exposure:
# 暴露bus刷新配置的端点
include: 'bus-refresh'
5.3 修改 Module cloud-config-client3355
- 给客户端,添加消息总线支持。
5.3.1 POM
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
5.3.2 YML
(bootstrap.yml
)
spring:
# RabbitMQ 相关配置,15672是Web管理界面的端口,5672是MQ访问的端口
rabbitmq:
host: 192.168.137.155
port: 5672
username: guest
password: guest
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
5.4 新建 Module cloud-config-client3366
- 给客户端添,加消息总线支持。
5.4.1 POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud-2020</artifactId>
<groupId>com.qs.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-client3366</artifactId>
<dependencies>
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
5.4.2 YML
(bootstrap.yml
)
server:
port: 3366
spring:
application:
name: cloud-config-client
cloud:
# Config客户端配置http://config3344.com:3344/master/config-dev.yml
config:
# 分支名称
label: master
# 配置文件名称
name: config
# 读取后缀名称
profile: dev
# 配置中心地址
uri: http://localhost:3344
# RabbitMQ 相关配置,15672是Web管理界面的端口,5672是MQ访问的端口
rabbitmq:
host: 192.168.137.155
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
# 注册中心地址
defaultZone: http://localhost:7001/eureka
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
5.4.3 主启动
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3366 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3366.class, args);
}
}
5.4.4 ConfigClientController
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${server.port}")
private String serverPort;
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return "serverPort: " + serverPort + "\t\n\nconfigInfo: " + configInfo;
}
}
5.5 测试
- 修改
Github
上配置文件增加版本号。curl -X POST "http://localhost:3344/actuator/bus-refresh"
- 配置中心
http://config3344.com:3344/config-dev.yml- 客户端
http://localhost:3355/configInfo
http://localhost:3366/configInfo
- 一次修改,广播通知,处处生效。
6. 动态刷新定点广播通知
- 不想全部通知,只通知
3355
,不通知3366
。
公式:http://localhost:3344/actuator/bus-refresh/{destination}
/bus/refresh
请求不再发送到具体的服务实例上,而是发给 Config Server,并通过destination
参数类指定需要更新配置的 服务 或 实例。
6.1 测试
- 刷新
3355
。
curl -X POST "http://localhost:3344/actuator/bus-refresh/cloud-config-client:3355"