SpringCloud Bus概述
概述
Spring Cloud Bus是Spring Cloud生態(tài)系統(tǒng)中的一個(gè)組件,用于實(shí)現(xiàn)微服務(wù)架構(gòu)中的消息總線。它利用了輕量級(jí)消息代理(如RabbitMQ或Kafka)作為通信中間件,實(shí)現(xiàn)了在分布式系統(tǒng)中的消息傳遞和事件廣播。
Spring Cloud Bus旨在簡(jiǎn)化微服務(wù)架構(gòu)中的配置管理和狀態(tài)同步。它允許將配置更改或狀態(tài)更新廣播到整個(gè)分布式系統(tǒng)中的各個(gè)微服務(wù)實(shí)例。通過(guò)使用消息總線,微服務(wù)可以輕松地獲取最新的配置信息,并且能夠了解其他微服務(wù)的狀態(tài)變化。
作用
-
配置的集中管理: Spring Cloud Bus與Spring Cloud Config(配置中心)集成,可以實(shí)現(xiàn)集中式的配置管理。當(dāng)配置發(fā)生變化時(shí),只需更新配置中心的配置,Spring Cloud Bus會(huì)將新的配置信息廣播給所有訂閱了消息總線的微服務(wù)。
-
快速的配置更新: 通過(guò)使用消息總線,配置的更新可以迅速傳播到整個(gè)分布式系統(tǒng)中的所有微服務(wù)實(shí)例,而無(wú)需每個(gè)微服務(wù)都主動(dòng)去拉取配置。這樣可以減少配置更新的延遲,提高系統(tǒng)的響應(yīng)速度。
-
系統(tǒng)狀態(tài)的同步: Spring Cloud Bus不僅可以用于配置的更新,還可以用于系統(tǒng)狀態(tài)的同步。當(dāng)一個(gè)微服務(wù)的狀態(tài)發(fā)生變化時(shí),它可以通過(guò)消息總線廣播給其他微服務(wù),從而實(shí)現(xiàn)微服務(wù)之間的狀態(tài)同步。
-
可擴(kuò)展的消息傳遞: Spring Cloud Bus提供了靈活的擴(kuò)展機(jī)制,可以與其他Spring Cloud組件無(wú)縫集成。例如,可以與Spring Cloud Stream(消息驅(qū)動(dòng)的微服務(wù))集成,實(shí)現(xiàn)更高級(jí)的消息傳遞模式。
基本原理
Spring Cloud Bus的原理是基于消息代理和事件廣播機(jī)制。
-
消息代理: Spring Cloud Bus使用輕量級(jí)消息代理(如RabbitMQ或Kafka)作為通信中間件。消息代理負(fù)責(zé)接收和分發(fā)消息,并確保消息的可靠傳遞。微服務(wù)通過(guò)連接到消息代理,可以發(fā)送和接收消息。
-
事件廣播: 當(dāng)一個(gè)微服務(wù)的配置發(fā)生變化或狀態(tài)發(fā)生改變時(shí),它會(huì)將這些變化作為消息發(fā)送到消息代理。消息代理將這些消息廣播給所有訂閱了消息總線的微服務(wù)。這樣,其他微服務(wù)就能夠接收到這些變化,并及時(shí)做出相應(yīng)的響應(yīng)。
安裝RabbitMQ
- *安裝Erlang:
下載地址:http://erlang.org/download/otp_win64_21.3.exe,下載完成后點(diǎn)擊安裝即可。 -
安裝RabbitMQ
下載地址:https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.14/rabbitmq-server-3.7.14.exe?????,下載完成后點(diǎn)擊安裝。
安裝RabbitMQ后進(jìn)入sbin目錄下
執(zhí)行命令rabbitmq-plugins enable rabbitmq_management
啟動(dòng)mq,如下圖就啟動(dòng)成功
訪問(wèn)地址http://localhost:15672/ 如下圖
賬號(hào)密碼都為,guest
Demo配置
新建模塊cloud-config-client-3366
pom.xml
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
bootstrap.yml
server:
port: 3366
spring:
application:
name: config-client
cloud:
#Config客戶端配置
config:
label: master #分支名稱
name: config #配置文件名稱
profile: dev #讀取后綴名稱 上述3個(gè)綜合:master分支上config-dev.yml的配置文件被讀取http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地址
# 暴露監(jiān)控端點(diǎn)
management:
endpoints:
web:
exposure:
include: "*"
#服務(wù)注冊(cè)到eureka地址
eureka:
instance:
# 配置eureka的狀態(tài)顯示
hostname: localhost
instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
client: #服務(wù)提供者provider注冊(cè)進(jìn)eureka服務(wù)列表內(nèi)
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka
啟動(dòng)類
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3366 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3366.class, args);
}
}
controller
@RestController
@RefreshScope //Spring Cloud 提供的用于動(dòng)態(tài)刷新配置的注解,會(huì)在調(diào)用 /actuator/refresh 接口時(shí)重新加載配置并更新 configInfo 的值。
public class ConfigClientController {
@Value("${spring.cloud.config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
配置中心3344添加消息總線支持
pom.xml
<!--添加消息總線RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
bootstrap.yml
##rabbitmq相關(guān)配置,暴露bus刷新配置的端點(diǎn)
management:
endpoints: #暴露bus刷新配置的端點(diǎn)
web:
exposure:
include: 'bus-refresh'
客戶端3355,3366添加消息總線支持
pom.xml
<!--添加消息總線RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
bootstrap.yml修改后
server:
port: 3355
spring:
application:
name: config-client
cloud:
#Config客戶端配置
config:
label: master #分支名稱
name: config #配置文件名稱
profile: dev #讀取后綴名稱 上述3個(gè)綜合:master分支上config-dev.yml的配置文件被讀取http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地址
#rabbitmq相關(guān)配置 15672是Web管理界面的端口;5672是MQ訪問(wèn)的端口
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
# 暴露監(jiān)控端點(diǎn)
management:
endpoints:
web:
exposure:
include: "*"
#服務(wù)注冊(cè)到eureka地址
eureka:
instance:
# 配置eureka的狀態(tài)顯示
hostname: localhost
instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
client: #服務(wù)提供者provider注冊(cè)進(jìn)eureka服務(wù)列表內(nèi)
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka
測(cè)試
修改gitee上的配置,config info version由2改為3提交
發(fā)送POST請(qǐng)求來(lái)觸發(fā)刷新
http://localhost:3344/actuator/bus-refresh
查看客戶端結(jié)果
3355和3366均已成功更新
動(dòng)態(tài)刷新定點(diǎn)通知
不想全部通知,只想定點(diǎn)通知:只通知3355,不通知3366
簡(jiǎn)單一句話:指定具體某一個(gè)實(shí)例生效而不是全部
公式:http://localhost:配置中心的端口號(hào)/actuator/bus-refresh/{destination}
案例:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-706379.html
我們這里以刷新運(yùn)行在3355端口上的config-client為例:只通知3355,不通知3366文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-706379.html
http://localhost:3344/actuator/bus-refresh/config-client:3355
到了這里,關(guān)于Spring Cloud學(xué)習(xí)筆記【消息總線-SpringCloud Bus】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!