文章目錄
- 1. 簡介
- 2. 代碼實現
- 2.1 涉及的模塊及整體步驟
- 2.2 源代碼
- 2.3 eureka-server-singleton
- 2.4 config-server-ha
- 2.5 config-client-bus
- 2.6 config-repository
- 3. 驗證
- 3.1 創建SpringBoot啓動類
- 3.1.1 EurekaServerSingletonApplication
- 3.1.2 ConfigServerHaApplication-8772
- 3.1.3 ConfigServerHaApplication-8773
- 3.1.4 ConfigClientBusApplication-8775
- 3.1.5 ConfigClientBusApplication-8776
- 3.2 啓動
- 3.3查看eureka服務信息界面
- 3.4 讀取遠程配置信息
- 4. 思考
- 5. 補充
1. 簡介
Spring Cloud Bus links the nodes of a distributed system with a lightweight message broker. This broker can then be used to broadcast state changes (such as configuration changes) or other management instructions. A key idea is that the bus is like a distributed actuator for a Spring Boot application that is scaled out. However, it can also be used as a communication channel between apps. This project provides starters for either an AMQP broker or Kafka as the transport.
事件、消息總線,用於在集羣(例如,配置變化事件)中傳播狀態變化,可與Spring Cloud Config聯合實現熱部署。
基於RabbitMQ實現上一篇筆記中的配置手動刷新,RabbitMQ的安裝請自行搜索,本片記錄中的RabbitMQ使用的都是默認配置。
2. 代碼實現
2.1 涉及的模塊及整體步驟
2.1.1 涉及的模塊
- eureka-server-singleton:eureka服務發佈註冊中心
- config-server-ha:配置中心服務端,通過指定不同端口啓動兩個實例模擬服務端集羣
- config-client-bus:新建的通過HA版服務配置中心訪問遠程配置信息模塊,也可以使用原有模塊
- config-repository:放置於GitHub的配置,
config-client-bus-test.properties
是對應的本次測試的保存配置信息的文件名稱
2.1.2 整體步驟
- GitHub創建存放配置信息的config-repository目錄與配置信息
config-client-bus-test.properties
,可通過demo中的config-repository
模塊關聯GitHub上的配置。 - 實現eureka-server-singleton:eureka服務發佈註冊中心,與前面沒有任何區別
- 實現config-server-ha:關鍵是啓動Spring Cloud Config Server功能,指定配置倉庫的配置信息
- 實現config-client-bus:從配置倉庫讀取配置信息,引入spring-boot-starter-actuator,spring-cloud-starter-bus-amqp
- 通過爲config-client-bus指定8775、8776端口實現多實例啓動
- 修改Github上遠程配置文件config-client-bus-test.properties,通過8775或者8776刷新配置,再次訪問兩個實例讀取的相同配置項的值,觀察修改是否生效
- config-repository中添加
config-client-bus-test.properties
:放置4步驟的配置信息
2.2 源代碼
2.2.1 Github地址
https://github.com/andyChenHuaYing/spring-cloud-demo
2.2.2 配置信息地址
配置信息在dev-20180827、與master分支都有,但是代碼中使用的是dev-20180827分支的代碼,如果想修改值驗證,記得確認客戶端bootstrap.properties中配置信息與想要訪問的倉庫地址一致。
https://github.com/andyChenHuaYing/spring-cloud-demo/tree/dev-20180827/config-repository
2.3 eureka-server-singleton
與Spring Cloud 之服務發現與調用-Ribbon#2.3 eureka-server-singleton 沒有任何區別
2.4 config-server-ha
與十:Spring Cloud 之配置中心HA版-config 沒有任何區別
2.5 config-client-bus
2.5.1 整體實現
- pom.xml文件中引入依賴
spring-cloud-starter-config
、spring-cloud-starter-netflix-eureka-client
、config-client-bus-test.properties
、spring-boot-starter-actuator
- bootstrap.yml中指定Config Server連接信息以及需要訪問配置中心的具體配置信息
- application-8775.yml、application-8776.yml指定當前模塊的配置信息,通過profile指定不同端口模擬啓動多實例
- ConfigClientBusApplication常規Spring Boot啓動類
2.5.2 pom.xml
<?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>spring-cloud-finchley-demo</artifactId>
<groupId>org.oscar.scd</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-client-bus</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
2.5.3 bootstrap.yml
下方信息與配置中心的配置文件的對應關係見後續驗證部分
spring:
application:
name: config-client-bus
cloud:
config:
label: dev-20180827
profile: test
discovery:
enabled: true
serviceId: config-server-ha
2.5.4 application-8775.yml
application-8776.yml 只是端口不一樣
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
cloud:
bus:
enabled: true
trace:
enabled: true
management:
endpoints:
web:
exposure:
include: bus-refresh
server:
port: 8775
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
2.6 config-repository
保存配置文件
2.6.1 config-client-bus-test.properties配置文件
foo=config-client-bus foo value updated.
3. 驗證
3.1 創建SpringBoot啓動類
簡單創建Spring Boot啓動類即可
3.1.1 EurekaServerSingletonApplication
最簡單的方式添加一個SpringBoot啓動類型的啓動類就行。
3.1.2 ConfigServerHaApplication-8772
3.1.3 ConfigServerHaApplication-8773
參考上一節,修改Active profiles:8773
3.1.4 ConfigClientBusApplication-8775
參考上一節,指定Active profiles:8775。
3.1.5 ConfigClientBusApplication-8776
參考上一節,指定Active profiles:8776。
3.2 啓動
EurekaServerSingletonApplication
ConfigServerHaApplication-8772
ConfigServerHaApplication-8773
ConfigClientBusApplication-8775
ConfigClientBusApplication-8776
3.3查看eureka服務信息界面
3.4 讀取遠程配置信息
3.4.1 查看指定配置項的值
- 瀏覽器中輸入地址:http://localhost:8775/readFooProp
- 預期:讀取的是:https://github.com/andyChenHuaYing/spring-cloud-demo/blob/dev-20180827/config-repository/config-client-bus-test.properties 文件中
foo=config-client-bus foo value updated.
- 實際返回結果:
3.4.2 查看指定配置項修改後的值
-
本地修改資源文件
config-client-bus-test.properties
配置項foo=config-client-bus foo value
,並push到遠程倉庫,查看 https://github.com/andyChenHuaYing/spring-cloud-demo/blob/dev-20180827/config-repository/config-client-bus-test.properties 有沒有成功(這裏可以換成自己的Github測試一下)修改後的值foo=config-client-bus foo value updated again 2018-09-20.
-
再次訪問 http://localhost:8775/readFooProp,返回值不變,值爲:
config-client-bus foo value updated.
-
訪問http://localhost:8775/actuator/bus-refresh 手動刷新配置項,可看到後臺重新請求Github地址,拉取配置。注意:這裏需要使用post請求,並且header中的Content-Type值爲application/json。可以使用Intellij 的HTTP Client。
-
觀察8775控制檯輸出的日子,發現重新拉取了遠程的配置信息.
-
再次訪問 http://localhost:8775/readFooProp,返回值變成修改後的值
config-client-bus foo value
,說明成功讀取到新配置值再次訪問 http://localhost:8775/readFooProp,返回值變成修改後的值config-client-bus foo value
,說明成功讀取到新配置值
-
訪問http://localhost:8776/readFooProp,返回值變成修改後的值
config-client-bus foo value
,說明成功讀取到新配置值
-
通過消息總線實現變更配置信息同步功能生效
4. 思考
- 消息總線是如何接收消息並廣播的
- SpringCloud消息總線還支持哪些消息中間件
- 如果基於消息中線設計一套基於事件驅動的系統架構,需要解決哪些核心關鍵點
5. 補充
5.1 資料
https://springcloud.cc/spring-cloud-bus.html
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_bus.html
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_client.html
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_server.html