SpringCloud Bus 消息總線
什麼是消息總線
1. 概念
在微服務架構中,一般會使用輕量級的消息代理來構建一個共用的消息主題來鏈接各個微服務實例,
它廣播的消息會被全部在註冊中心的微服務實例監聽和消費,也稱消息總線
2. SpringCloud Bus
SpringCloud中也有對應的解決方案,SpringCloud Bus 將分佈式的節點用輕量的消息代理鏈接起來,
能夠很容易搭建消息總線,配合SpringCloud config 實現微服務應用配置信息的動態更新。
3. 其餘
消息代理屬於中間件。設計代理的目的就是爲了可以從應用程序中傳入消息,並執行一些特別的操做。
開源產品不少如ActiveMQ、Kafka、RabbitMQ、RocketMQ等
目前springCloud僅支持RabbitMQ和Kafka。本文采用RabbitMQ實現這一功能。
搭建分佈式配置中心
1. Config 架構
![clipboard.png clipboard.png](http://static.javashuo.com/static/loading.gif)
當一個系統中的配置文件發生改變的時候,咱們須要從新啓動該服務,才能使得新的配置文件生效,
spring cloud config能夠實現微服務中的全部系統的配置文件的統一管理,
並且還能夠實現當配置文件發生變化的時候,系統會自動更新獲取新的配置。
2. Git 環境搭建
使用 碼雲 環境搭建 git
碼雲環境地址: https://gitee.com/guopf/springcloud_bus
3. Git服務器上傳配置文件
命名規範 服務名稱-版本.yml 例如configclient_dev.yml
![clipboard.png clipboard.png](http://static.javashuo.com/static/loading.gif)
4. 搭建 Eureka 服務註冊中心
具體搭建環境隨後補充,能夠使用我本身部署的 http://47.105.86.222:8100 (配置地址http://47.105.86.222:8100/eureka)
5. 搭建 config-server 服務
1. maven 依賴
<dependencies>
<!-- SpringBoot整合Web組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring-cloud 整合 config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!-- SpringBoot整合eureka客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
2. 配置文件
### 服務地址 端口
server:
port: 8800
### 服務名稱
spring:
application:
name: config_server
cloud:
config:
server:
git:
### git 地址
uri: https://gitee.com/guopf/springcloud_bus.git
username:
password:
### 配置讀取文件夾
search-paths: config
### 分支
label: master
### eureka 配置
eureka:
client:
service-url:
defaultZone: http://47.105.86.222:8100/eureka
register-with-eureka: true
fetch-registry: true
3. 啓動
/**
* @EnableEurekaClient : 開啓 eureka 客戶端
* @EnableConfigServer : 開啓 config 服務端
*
*/
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
搭建 config-client 服務
1. 手動更新
1. maven 依賴
<dependencies>
<!-- SpringBoot整合Web組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!-- SpringBoot整合eureka客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!--核心jar包,集成rabbitMQ 消息總線 bus
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
-->
<!-- actuator監控中心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
2. 配置文件(bootstrap.yml)
### 端口
server:
port: 8801
### eureka 配置中心
eureka:
client:
service-url:
defaultZone: http://47.105.86.222:8100/eureka
fetch-registry: true
register-with-eureka: true
### 配置服務名稱,要和config 配置中心文件保持一致
spring:
application:
name: configclient
cloud:
config:
### 讀取配置
profile: dev
discovery:
###
enabled: true
### config 配置中心配置的服務名稱
service-id: config_server
management:
endpoints:
web:
exposure:
include: "*"
3. 啓動
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
/**
* 在讀取配置文件信息的地方進行添加 @RefreshScope 註解
*/
@RestController
@RefreshScope
public class AppController {
@Value("${userAge}")
private String userAge;
@GetMapping("/userAge")
public String config(){
System.out.println("userAge : " + userAge);
return userAge;
}
}
修改git倉庫中的配置,進行手動更新,post請求
http://127.0.0.1:8801/actuator/refresh 啓動刷新器 從cofnig_server讀取
2. 使用消息總線 bus 更新
1. 添加依賴信息
在 config_server,config_client 中添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- actuator監控中心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 配置文件修改
添加對 rabbbitMQ的配置, rabbitMQ服務和config_server,config_client 在一個服務器上,使用默認配置便可
### rabbitmq 配置信息
rabbitmq:
addresses: 47.105.86.222
username: guest
password: guest
port: 5672
virtual-host: /
3. 刷新
http://127.0.0.1:8801/actuator/bus-refresh post方式