SpringCloud_H(消息总线)
1、Bus
- Spring Cloud Bus 配合 Spring Cloud Config 使用可以实现配置的动态刷新
- Spring Cloud Bus 是来将分布式系统中的节点与轻量级消息系统链接起来的框架,它整合了Java的时间处理机制和消息中间件的功能
- Spring Cloud Bus 目前支持RabbitMQ和Kafka
- Spring Cloud Bus 能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、事件推送等,也可以当做微服务之间的通信通道
- 总线
- 在微服务架构系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中素有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称之为消息总线。总线上的各个实例,都可以方便的广播一些需要让其他连接在该主题上的实例都知道的消息
- 基本原理
- ConfigClient实例都监听MQ中同一个topic(默认是springCloudBus)。当一个微服务刷新数据的时候,它会把这个信息放在topic中,这样其他监听同一topic的服务就能得到通知,然后去更新自身的配置
2、动态刷新全局广播
- 利用消息总线触发一个客户端/bus/refresh,而刷新所有客户端的配置
- 利用消息总线触发一个服务端ConfigServer的/bus/refresh端点,而刷新所有客户端的配置
-
第二种架构显然更合适
- 第一种架构打破了微服务的职责单一性,因为微服务本身是业务模块,它本不应该承担配置刷新的职责。破坏了微服务各个节点的对等性
-
新建cloud-config-client-3306
-
maven依赖
<dependencies>
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
- bootstrap.yml
server:
port: 3366
spring:
application:
name: config-client-3366
cloud:
#Config客户端配置
config:
label: master #分支名称
name: application #配置文件名称
profile: dev # 读取后缀名称
uri: http://localhost:3344 # 配置中心地址
#rabbitmq相关配置
rabbitmq:
host: 82.156.13.249
port: 5672
username: admin
password: admin
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
- 启动类
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3366 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3366.class,args);
}
}
- controller
@RestController
@RefreshScope
public class configClientController {
@Value("${config.info}")
private String configInfo;
//获取配置文件中的信息
@GetMapping("/getConfigInfo")
public String getConfigInfo(){
return configInfo;
}
}
- Config服务端3344添加消息总线RabbitMQ支持
<!--添加消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
- application.yml添加rabbitmq配置信息
server:
port: 3344
spring:
application:
name: cloud-config-center #注册进Eureka服务器的微服务名
cloud:
config:
server:
git:
# gitee文件http地址
uri: https://gitee.com/chenminglin/springcloud-config.git
#rabbitmq相关配置
rabbitmq:
host: 82.156.13.249
port: 5672
username: admin
password: admin
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
##rabbitmq相关配置,暴露bus刷新配置的端点
management:
endpoints: #暴露bus刷新配置的端点
web:
exposure:
include: 'bus-refresh'
-
启动服务端3344和客户端3366
-
gitee上仓库中applicaiton.yml文件
- Config服务端3344访问配置文件
- Config客户端3366访问配置文件
- 修改gitee仓库中application.yml
- Config服务端3344访问配置文件
- Config客户端3366访问配置文件
- 向服务端发送POST请求,动态刷新所有客户端
curl -X POST "http://localhost:3344/actuator/bus-refresh"
- Config客户端3366访问配置文件
3、动态刷新定点通知
- 指定具体某一个实例生效而不是全部
- http://{Configf服务端地址}/actuator/bus-refresh/
- /bus/refresh请求不再发送到具体的服务实例上,而是发给config server并通过destination参数类型指定需要更新配置的服务或实例
- 修改gitee仓库中的配置文件,并只让 config-client-3366 服务更新配置
curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client-3366"
分类:
spring
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)