還記得在上篇文章中使用Config配置中心的時候,如果遠程配置文件修改了黔寇,那么需要post刷新請求镜粤,配置客戶端才會獲取最新的配置幢踏。這個問題就是如果配置中心修改了髓需,我們需要向每個微服務刷新,大大提高了操作難度房蝉。而消息總線Bus使用消息隊列中的發(fā)布訂閱模式來幫我們解決了這個問題僚匆。
系列文章
SpringCloud(一)-手把手教你創(chuàng)建springcloud微服務父子項目
SpringCloud(二)-手把手教你搭建Eureka Server和Eureka Client
SpringCloud(三)-手把手教你通過Rinbbon實現(xiàn)客戶端負載均衡
SpringCloud(四)-手把手教你使用OpenFeign
SpringCloud(五)-手把手教你使用Hystrix配置服務熔斷和降級以及Hystrix Dashboard
SpringCloud(六)-手把手教你搭建SpringCloud Config配置中心
SpringCloud(七)-手把手教你使用消息總線Bus實現(xiàn)動態(tài)刷新
SpringCloud(八)-手把手教你使用Stream消息驅(qū)動
1. 簡介
Spring Cloud Bus將分布式系統(tǒng)的節(jié)點與輕量級消息代理鏈接。這可以用于廣播狀態(tài)更改(例如配置更改)或其他管理指令搭幻。一個關(guān)鍵的想法是咧擂,Bus就像一個擴展的Spring Boot應用程序的分布式執(zhí)行器,但也可以用作應用程序之間的通信渠道檀蹋。當前唯一的實現(xiàn)是使用AMQP代理作為傳輸松申,但是相同的基本功能集(還有一些取決于傳輸)在其他傳輸?shù)穆肪€圖上。(復制的官方文檔)
簡單的來說通過消息中間件作為傳輸,來廣播命令攻臀。
接下來就是項目案例了焕数,在本例中使用RabbitMQ作為消息中間件纱昧。
2. 安裝RabbitMQ
在筆者的另一篇文章中[RabbitMQ和Kafka:Win10安裝教程] (http://www.reibang.com/p/63d32ab9389f)刨啸,已經(jīng)詳細介紹了如何安裝和啟動RabbitMQ了,如果本機還沒安裝RabbitMQ的同學請參考上篇文章识脆。
啟動成功后的RabbitMQ设联,在瀏覽器中輸入http://localhost:15672/#/ 可以看到如下界面,就說明RabbitMQ安裝和啟動成功灼捂。
3. 配置中心config-server-8600
我們依舊通過config-server-8600項目來作為配置中心离例,要對Bus的支持,我們需要引入一些依賴并修改配置悉稠。
3.1 pom.xml引入依賴
config-server-8600中需要引入消息總線和監(jiān)控中心的依賴
<!--消息總線bus-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- 監(jiān)控中心-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
然后總的pom.xml內(nèi)容如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloudtest</artifactId>
<groupId>com.elio.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-config-server-8600</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--消息總線bus-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- 監(jiān)控中心-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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>
</dependencies>
</project>
3.2 修改application.yml配置文件
需要加入RabbitMQ的相關(guān)配置宫蛆,還有就是暴露刷新接口。
注意:
- 其中rabbitmq的服務端口是5672的猛,而15672支持客戶端的接口耀盗,不要配置錯了。
- bus-refresh 將會是刷新接口的地址卦尊,接下來會通過http://localhost:8600/actuator/bus-refresh 接口來進行刷新
rabbitmq:
host: localhost # rabbitmq主機
port: 5672 # rabbitmq端口叛拷,注意是5672而不是15672
username: guest # 用戶名
password: guest # 密碼
management:
endpoints:
web:
exposure:
include: bus-refresh # 指定刷新地址
然后總的application.yml配置內(nèi)容如下
server:
port: 8600 #端口號
spring:
application:
name: springcloud-config-server # 應用名稱
cloud:
config:
server:
git:
uri: https: #git地址
username: # git賬號名
password: # git賬號密碼
rabbitmq:
host: localhost # rabbitmq主機
port: 5672 # rabbitmq端口,注意是5672而不是15672
username: guest # 用戶名
password: guest # 密碼
eureka:
instance:
instance-id: ${spring.application.name}:${server.port}
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8300/eureka/,http://localhost:8301/eureka/
management:
endpoints:
web:
exposure:
include: bus-refresh # 指定刷新地址
4. 配置客戶端product-provider-8100
完成了配置中心的配置后岂却,接下來就是要配置客戶端product-provider-8100了忿薇。
4.1 修改pom.xml引入依賴
<!--消息總線bus-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- 監(jiān)控中心-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
4.2 修改bootstrap.yml配置文件
bootstrap.yml 只用加入rabbitMQ的相關(guān)配置即可
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
總的bootstrap.yml內(nèi)容如下
spring:
cloud:
config:
name: configtest # 獲取配置文件的名稱
profile: pro # 配置的策略pro-生產(chǎn) test-測試 dev-開發(fā)
label: master # 分支
discovery:
enabled: true # 開啟配置信息發(fā)現(xiàn)
service-id: springcloud-config-server # 指定配置中心的service-id
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
instance:
instance-id: ${spring.application.name}:${server.port}
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8300/eureka/,http://localhost:8301/eureka/
management:
endpoints:
web:
exposure:
include: refresh,health
4.3 確定訪問接口
獲取遠程配置文件的接口是沒有變動的,記得要加上@RefreshScope注解
@RestController
@Slf4j
@RequestMapping("/")
@RefreshScope
public class ProductProviderController {
@Resource
private ProductService productService;
@Value("${spring.application.name}")
private String instantName;
@Value("${server.port}")
private String port;
@Value("${hello}")
private String hello;
@GetMapping("product/provider/get/config/info")
public Result getConfigInfo()throws Exception{
return new Result(200,"",this.hello);
}
}
5. 測試
依次啟動eureka-server-8300躏哩,eureka-server-8301署浩,config-server-8600,product-provider-8100項目后扫尺,接下來進行以下步驟:
- 修改碼云上的configtest-pro.yml文件瑰抵,文件內(nèi)容修改成如下
hello: hello from configtest-pro modified three times
- 然后打開cmd窗口,輸入以下命令
curl -X POST http://localhost:8600/actuator/bus-refresh
- 然后瀏覽器地址中輸入product-provider-8100的訪問接口http://localhost:8100/product/provider/get/config/info
最終測試結(jié)果和預期的一樣器联,在這個例子中筆者只用一個微服務來做測試二汛,其實多個微服務效果一樣,可以通過消息總件的形式拨拓,就能夠?qū)崿F(xiàn)廣播配置文件變動事件了肴颊。
總結(jié)
本文簡單的介紹了如何通過Bus + RabbitMQ來實現(xiàn)動態(tài)刷新,具體的原理和工作流程本文沒有敘述渣磷,希望對只是想通過小實踐來學習的同學有所幫助婿着。