本文作者:陳剛,叩丁狼高級講師票渠。原創(chuàng)文章,轉(zhuǎn)載請注明出處芬迄。
一.什么是消息總線
在微服務(wù)架構(gòu)中问顷,為了更方便的向微服務(wù)實例廣播消息,我們通常會構(gòu)建一個消息中心禀梳,讓所有的服務(wù)實例都連接上來杜窄,而該消息中心所發(fā)布的消息都會被微服務(wù)實例監(jiān)聽和消費,我們把這種機制叫做消息總線(SpringCloud Bus)算途,在總線上的每個服務(wù)實例都可以去廣播一些讓其他服務(wù)知道的消息塞耕,消息總線可以為微服務(wù)做監(jiān)控,或?qū)崿F(xiàn)應(yīng)用之間的通信郊艘,和其他的一些管理工作荷科,SpringCloud Bus可選的消息組件包括RabbitMQ唯咬,Kafka等纱注,本章節(jié)講述使用 RabbitMQ 作為Spring Cloud的消息組件實現(xiàn)刷新更改微服務(wù)的配置文件畏浆。
二.為什么要用SpringCloud Bus刷新配置
例如有成十上百個微服務(wù)實例,當更改配置時狞贱,需要重啟多個微服務(wù)實例刻获,會非常麻煩。SpringCloud Bus 就能讓這個過程變得非常簡單瞎嬉,當我們Git倉庫中的配置更改不后蝎毡,只需要某個微服務(wù)實例發(fā)送一個POST請求,通過消息組件通知其他微服務(wù)實例重新獲取配置文件氧枣,以達到配置的自動刷新效果沐兵。實現(xiàn)原理如下:
三.實現(xiàn)配置刷新
1.本章節(jié)基于第十章“配置中心”的基礎(chǔ)上進行修改,給 config-client應(yīng)用添加SpringCloud Bus相關(guān)依賴 spring-cloud-starter-bus-amqp和 spring-boot-starter-actuator
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</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-web</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
>2.搭建RabbitMQ服務(wù)便监,不會使用 RabbitMQ 的同學請先百度一下扎谎,RabbitMQ使用非常簡單(體現(xiàn)你們自學能力的時候到了)
>3.修改配置文件 bootstrap.properties,增加RabbitMQ連接配置
分支
spring.cloud.config.label=master
dev開發(fā)環(huán)境配置文件 dev開發(fā)環(huán)境配置文件 ,test測試環(huán)境烧董,pro正式環(huán)境
spring.cloud.config.profile=dev
spring.cloud.config.name=config-client
配置服務(wù)地址
spring.cloud.config.uri=http://localhost:5555/
增加 rabbitmq相關(guān)配置
spring.rabbitmq.host=localhost #mq連接地址
spring.rabbitmq.port=5672 #mq端口
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.cloud.bus.enabled=true #開啟 bus
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh #暴露刷新地址毁靶,完整訪問路徑: xxx/actuator/bus-refresh
>4.在要屬性的配置的配置類上打上 標簽@RefreshScope,否則不能自動刷新
@RestController
@RefreshScope
public class HelloController {
@Value("${notify}") //從遠程配置中取值
private String notify;
@RequestMapping("/hello")
public String hello(){
return notify;
}
}
> 5.啟動 config-server ,再啟動 config-client ,你可以從控制臺看到
> ![image.png](http://upload-images.jianshu.io/upload_images/807144-c7d3006141594a5c?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
>
> 說明RabbitMQ配置起效了訪問 config-client :[http://localhost:7777/hello](http://localhost:7777/hello) 逊移,可以看到響應(yīng)內(nèi)容為 “You are successful for dev” 预吆,接下來修改 Git中的配置 config-client-dev.properties中的 notify為 :
> ![image.png](http://upload-images.jianshu.io/upload_images/807144-82b6216f3506f7c9?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
>
> 6.請求配置刷新地址,使用post請求 :[http://localhost:7777/actuator/bus-refresh](http://localhost:7777/actuator/bus-refresh) 胳泉,如我這里使用了postman進行測試:![image.png](http://upload-images.jianshu.io/upload_images/807144-1dc42d31619e0794?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
>
> 拐叉,觀察控制臺會出現(xiàn)變化,![image.png](http://upload-images.jianshu.io/upload_images/807144-768452955f960ef0?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
>
> 說明刷新的新的配置
> 再次訪問[http://localhost:7777/hello你可以看到](http://localhost:7777/hello%E4%BD%A0%E5%8F%AF%E4%BB%A5%E7%9C%8B%E5%88%B0)
> “You are successful for dev hai bus test” 扇商,已經(jīng)發(fā)生變化凤瘦,說明我們從 git中獲取的配置屬性值已經(jīng)被刷新
![叩丁狼教育.jpg](https://upload-images.jianshu.io/upload_images/807144-e84c35cf93995ff0.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)