当前位置:网站首页>Bus消息总线
Bus消息总线
2022-07-07 02:40:00 【RB_VER】
概述
Spring Cloud Bus配合spring cloud config使用可以实现配置的动态刷新。
spring cloud bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架。
它整合了Java的事件处理机制和消息中间件的功能。
Bus支持两种消息代理:RabbitMQ和Kafka。
spring cloud bus能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改,事件推送等,也可以当作微服务间的通信通道。
总线:在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以被它称为消息总线。在总线上的各个实例,都可以方便的广播一些需要让其他连接在该主题上的实例都知道的消息。
基本原理:config client实例都监听MQ中同一个topic(默认是SpringCloudBus)。当一个服务刷新数据的时候,它会把这个信息放入到topic中,这样其他监听同一个topic的服务就能得到通知,然后去更新自身配置。
spring cloud bus动态刷新全局广播
设计思想:
利用消息总线触发一个客户端/bus/refresh,而刷新所有客户端的位置

利用消息总线触发一个服务端ConfigServer的/bus/refresh端点,而刷新所有客户端的配置。


第二种架构更为合理,第一种不合理的原因:
- 打破了微服务的职责单一性,因为微服务本身是业务模块,它本不应该承担配置刷新的职责。
- 破坏了微服务各节点的对等性。
- 有一定局限性。
创建cloud-config-center-3344模块作为配置中心服务端。
具体配置参见【微服务】整合spring cloud config。
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
...
application.yml
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: [email protected]:zzyybs/springcloud-config.git
search-paths:
- springcloud-config
label: master
rabbit:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
需要先配置rabbitmq。
新建cloud-config-client-3366模块。
pom.xml依赖
<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>
bootstrap.yml
server:
port: 3366
spring:
application:
name: config-client
cloud:
config:
label: master # 分支名称
name: config # 配置文件名称
profile: dev # 读取后缀名称 上述三个综合:master分支上config-dev.yml的配置文件
uri: http://localhost:3344 # 配置中心地址
rabbit:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defautZone: http://localhost:7001/eureka
management: # 监控暴露端点
endpoints:
web:
exposure:
include: "*"
ConfigClientMain3366
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3366{
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3366.class,args);
}
}
@RestController
@RefreshScope
public class ConfigClientController{
@Value("${server.port}")
private String serverPort;
@Value("${config.info}")
private String configInfo;
@GetMapping("/configinfo")
public String configInfo() {
return "serverPort: "+serverPort+" configInfo: "+configInfo;
}
}
同理新建cloud-config-client-3355模块。
测试:
修改GitHub上配置文件增加版本号:
config:
info: "master branch,springcloud-config/config-dev.yml version=2"
发送post请求:
curl -X POST "http://localhost:3344/actuator/bus-refresh"
访问http://localhost:3366/configinfo,获取配置信息,发现都已经刷新了。
spring cloud bus动态刷新定点通知
是指具体某一个实例生效而不是全部。
http://localhost:配置中心的端口号/actuator/bus-refresh/{destination}
destination=微服务名:端口号
边栏推荐
- 途家、木鸟、美团……民宿暑期战事将起
- Comment les entreprises gèrent - elles les données? Partager les leçons tirées des quatre aspects de la gouvernance des données
- Performance comparison between Ceres solver and g2o
- 2022/07/04学习记录
- Stack and queue-p79-9
- ESXI挂载移动(机械)硬盘详细教程
- 一条慢SQL拖死整个系统
- Can't you really do it when you are 35 years old?
- 肿瘤免疫治疗研究丨ProSci LAG3抗体解决方案
- UIC (configuration UI Engineering) public file library adds 7 industry materials
猜你喜欢

POI export to excel: set font, color, row height adaptation, column width adaptation, lock cells, merge cells

剑指offer-高质量的代码

软件测试到了35岁,真的就干不动了吗?

从零到一,教你搭建「CLIP 以文搜图」搜索服务(二):5 分钟实现原型

Unable to debug screen program with serial port

程序员的日常 | 每日趣闻

What are the classic database questions in the interview?

Matlab / envi principal component analysis implementation and result analysis

带你刷(牛客网)C语言百题(第一天)

POI导出Excel:设置字体、颜色、行高自适应、列宽自适应、锁住单元格、合并单元格...
随机推荐
中英文说明书丨ProSci LAG-3 重组蛋白
屏幕程序用串口无法调试情况
根据IP获取地市
企業如何進行數據治理?分享數據治理4個方面的經驗總結
What are the classic database questions in the interview?
JWT certification
项目实战 五 拟合直线 获得中线
Leite smart home longhaiqi: from professional dimming to full house intelligence, 20 years of focus on professional achievements
Apache ab 压力测试
如何解决数据库插入数据显示SQLSTATE[HY000]: General error: 1364 Field ‘xxxxx‘ doesn‘t have a default value错误
Matlab / envi principal component analysis implementation and result analysis
2022/07/04学习记录
MOS管参数μCox得到的一种方法
C language interview to write a function to find the first occurrence of substring m in string n.
【NOI模拟赛】区域划分(结论,构造)
一条慢SQL拖死整个系统
Ha Qu projection dark horse posture, only half a year to break through the 1000 yuan projector market!
POI导出Excel:设置字体、颜色、行高自适应、列宽自适应、锁住单元格、合并单元格...
DB2获取表信息异常:Caused by: com.ibm.db2.jcc.am.SqlException: [jcc][t4][1065][12306][4.25.13]
LM small programmable controller software (based on CoDeSys) Note 23: conversion of relative coordinates of servo motor operation (stepping motor) to absolute coordinates