10.SpringCloud Bus (訊息匯流排)
阿新 • • 發佈:2021-01-01
1.概述
銜接上一講解的SpringCloud Config 的加深和擴充分散式自動重新整理配置功能Spring Cloud Bus配合Spring Cloud Config使用可以實現配置的動態重新整理是什麼Bus支援兩種訊息代理:RabbitMQ和Kafka能幹嘛為何被稱為匯流排
2.RabbitMQ環境配置
下面是win10,上面安裝rabbitMq的步驟 直接下一步即可,安裝在javaTools資料夾下面安裝Erlang,下載地址:http://erlang.org/download/otp_win64_21.3.exe 安裝RabbitMQ,下載地址https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exe先安裝erlang,直接安裝下一步即可安裝rabbitMq,直接下一步即可安裝完成,開始介面有很多東西
安裝視覺化外掛進入sbin下面cmd
rabbitmq-plugins enable rabbitmq_management
訪問測試,輸入賬號密碼並登入: guest guesthttp://localhost:15672/3.SpringCloud Bus動態重新整理全域性廣播
必須先具備良好的RabbitMQ環境先演示廣播效果,增加複雜度,再以3355為模板再製作一個3377新建cloud-config-client-3377
yml<?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>springcloud2020</artifactId> <groupId>com.chl.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-config-client-3377</artifactId> <dependencies> <!--config 客戶端的依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--mq依賴用於動態重新整理配置--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>com.chl.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>1.0-SNAPSHOT</version> </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> </dependencies> </project>
主啟動server: port: 3377 spring: application: name: config-client cloud: config: label: master name: config profile: dev uri: http://localhost:3344 eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka management: endpoints: web: exposure: include: "*"
@SpringBootApplication
public class ConfigClientMain3377 {
public static void main(String[] args) {
SpringApplication.run( ConfigClientMain3377.class,args);
}
}
業務類@RestController
@RefreshScope //用於重新整理分散式的配置
//可能需要服務中心先請求一次,而且還要運維人員發起一個請求去重新整理相應的客戶端
//例如運維人員改了,還要重新整理客戶端3355 curl -X POST "http://localhost:3355/actuator/refresh
public class ConfigClientController {
//獲取統一配置的資料資訊
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
設計思想
架構1:利用訊息匯流排觸發一個客戶端/bus/refresh,而重新整理所有客戶端的配置架構2:利用訊息匯流排觸發一個服務端ConfigServer的/bus/refresh端點,而重新整理所有客戶端的配置(更加推薦)圖二的架構顯然更加合適,圖一不適合的原因如下
- 打破了微服務的職責單一性,因為微服務本身是業務模組,它本不應該承擔配置重新整理職責
- 破壞了微服務各節點的對等性
- 有一定的侷限性。例如,微服務在遷移時,它的網路地址常常會發生變化,此時如果想要做到自動重新整理,那就會增加更多的修改
給配置中心新增匯流排支援
給cloud-config-center-3344配置中心服務端新增訊息匯流排支援pom<!--mq依賴用於動態重新整理配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
yml 新增rabbitMq的配置server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config: #分支/config-環境名.yml
server: #http://config-3344.com:3344/master/config-dev.yml
git: #githud倉庫的位置 用碼雲不行,識別不了,只能githud
uri: https://github.com/badbad001/springcloud-config.git
search-paths:
- springcloud-config #倉庫名稱
label: master #倉庫的分支
rabbitmq: #rabbit mq的 配置
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
#暴露監控埠,用於重新整理分散式的配置
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
給客戶端新增匯流排支援
給cloud-config-center-3355客戶端新增訊息匯流排支援pom<!--mq依賴用於動態重新整理配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
yml 新增rabbitMq的配置#這個檔名 bootrap.yml 系統級別的 優先於 application.yml(使用者界別)
server:
port: 3355
spring:
application:
name: config-client
cloud:
config: #組裝成就是 http://localhost:3344/master/config-dev.yml 配置的是從哪裡獲取配置資訊
uri: http://localhost:3344
label: master
name: config
profile: dev
rabbitmq: #rabbit mq的 配置
host: localhost
port: 5672 #本地埠這裡有問題, 訪問是這個http://localhost:15672,但是有埠衝突,所以連結額不用15672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
#暴露監控埠,用於重新整理分散式的配置
management:
endpoints:
web:
exposure:
include: "*"
給cloud-config-center-3377客戶端新增訊息匯流排支援pom
<!--mq依賴用於動態重新整理配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
yml 新增rabbitMq的配置server:
port: 3377
spring:
application:
name: config-client
cloud:
config:
label: master
name: config
profile: dev
uri: http://localhost:3344
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
測試
修改Github上配置檔案增加版本號運維工程師 傳送Post請求curl -X POST "http://localhost:3344/actuator/bus-refresh"
一次傳送,處處生效配置中心客戶端
獲取配置資訊,發現都已經重新整理了 一次修改,廣播通知,處處生效
4.SpringCloud Bus動態重新整理定點通知
不想全部通知,只想定點通知- 只通知3355
- 不通知3377
案例說明我們這裡以重新整理執行在3355埠上的config-client為例
- 只通知3355
- 不通知3366
curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"
總結