概念
我們使用配置中心時(shí),當(dāng)配置中心的配置發(fā)生了變化,我們就要發(fā)送一個(gè)post請(qǐng)求給客戶端,讓它重新去拉取新的的配置。當(dāng)客戶端有很多時(shí),并且還是使用同一份配置文件,這樣當(dāng)配置中心的配置發(fā)生改變,我們就得逐個(gè)發(fā)送post請(qǐng)求通知,這樣無疑是很浪費(fèi)人力物力的。
Bus消息總線組件就幫我們解決了這個(gè)問題。他的工作流程是這樣的,當(dāng)配置中心的配置發(fā)生了變化時(shí),我們給其中一個(gè)客戶端發(fā)送post請(qǐng)求,然后client將請(qǐng)求的信息發(fā)送到rabbitmq隊(duì)列中,然后消息隊(duì)列將消息發(fā)送給別的隊(duì)列。
使用
準(zhǔn)備工作
項(xiàng)目基于Spring Cloud 第七章的項(xiàng)目改造。
改造config-client 添加相應(yīng)坐標(biāo)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <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> |
在啟動(dòng)類中添加@RefreshScope注解
@RefreshScope注解只需要寫在需要刷新配置文件的地方,不一定非要在啟動(dòng)類中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @RestController @RefreshScope public class ConfigClientApplication { /** * http://localhost:8881/actuator/bus-refresh */ public static void main(String[] args) { SpringApplication.run(ConfigClientApplication. class , args); } @Value ( "${foo}" ) String foo; @RequestMapping (value = "/hi" ) public String hi(){ return foo; } } |
配置相關(guān)配置
1
2
3
4
5
6
7
8
9
|
spring.rabbitmq.host=localhost spring.rabbitmq.port= 5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest spring.cloud.bus.enabled= true spring.cloud.bus.trace.enabled= true management.endpoints.web.exposure.include=bus-refresh management.security.enabled= false //報(bào)錯(cuò)加上 |
- 依次啟動(dòng)eureka-server、confg-cserver,啟動(dòng)兩個(gè)config-client,端口為:8881、8882。
- 訪問http://localhost:8881/hi 或者h(yuǎn)ttp://localhost:8882/hi 瀏覽器顯示:
foo version 3
- 這時(shí)我們?nèi)ゴa倉庫將foo的值改為“foo version 4”,即改變配置文件foo的值。如果是傳統(tǒng)的做法,需要重啟服務(wù),才能達(dá)到配置文件的更新。此時(shí),我們只需要發(fā)送post請(qǐng)求:http://localhost:8881/actuator/bus-refresh,你會(huì)發(fā)現(xiàn)config-client會(huì)重新讀取配置文件
- 1.5版本的post請(qǐng)求http://localhost:8881/bus/refresh
- 2.0版本的post請(qǐng)求http://localhost:8881/actuator/bus-refresh
- 這時(shí)我們?cè)僭L問http://localhost:8881/hi 或者h(yuǎn)ttp://localhost:8882/hi 瀏覽器顯示:
foo version 4
另外,/actuator/bus-refresh接口可以指定服務(wù),即使用"destination"參數(shù),比如 “/actuator/bus-refresh?destination=customers:**” 即刷新服務(wù)名為customers的所有服務(wù)。 原理圖
當(dāng)git文件更改的時(shí)候,通過pc端用post 向端口為8882的config-client發(fā)送請(qǐng)求/bus/refresh/;此時(shí)8882端口會(huì)發(fā)送一個(gè)消息,由消息總線向其他服務(wù)傳遞,從而使整個(gè)微服務(wù)集群都達(dá)到更新配置文件。
原文鏈接:https://blog.csdn.net/qq_44333590/article/details/121006985