SpringCloud Bus 消息總線的具體使用
什么是消息總線
1. 概念
在微服務(wù)架構(gòu)中,通常會使用輕量級的消息代理來構(gòu)建一個共用的消息主題來連接各個微服務(wù)實(shí)例, 它廣播的消息會被所有在注冊中心的微服務(wù)實(shí)例監(jiān)聽和消費(fèi),也稱消息總線
2. SpringCloud Bus
SpringCloud中也有對應(yīng)的解決方案,SpringCloud Bus 將分布式的節(jié)點(diǎn)用輕量的消息代理連接起來, 可以很容易搭建消息總線,配合SpringCloud config 實(shí)現(xiàn)微服務(wù)應(yīng)用配置信息的動態(tài)更新。
3. 其他
消息代理屬于中間件。設(shè)計(jì)代理的目的就是為了能夠從應(yīng)用程序中傳入消息,并執(zhí)行一些特別的操作。 開源產(chǎn)品很多如ActiveMQ、Kafka、RabbitMQ、RocketMQ等 目前springCloud僅支持RabbitMQ和Kafka。本文采用RabbitMQ實(shí)現(xiàn)這一功能。
搭建分布式配置中心
1. Config 架構(gòu)

當(dāng)一個系統(tǒng)中的配置文件發(fā)生改變的時候,我們需要重新啟動該服務(wù),才能使得新的配置文件生效,spring cloud config可以實(shí)現(xiàn)微服務(wù)中的所有系統(tǒng)的配置文件的統(tǒng)一管理,而且還可以實(shí)現(xiàn)當(dāng)配置文件發(fā)生變化的時候,系統(tǒng)會自動更新獲取新的配置。
2. Git 環(huán)境搭建
使用 碼云 環(huán)境搭建 git
碼云環(huán)境地址: https://gitee.com/guopf/springcloud_bus
3. Git服務(wù)器上傳配置文件
命名規(guī)范 服務(wù)名稱-版本.yml 例如configclient_dev.yml

4. 搭建 Eureka 服務(wù)注冊中心
具體搭建環(huán)境隨后補(bǔ)充,可以使用我自己部署的 http://47.105.86.222:8100 (配置地址http://47.105.86.222:8100/eureka)
5. 搭建 config-server 服務(wù)
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. 配置文件
### 服務(wù)地址 端口
server:
port: 8800
### 服務(wù)名稱
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 服務(wù)端
*
*/
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
搭建 config-client 服務(wù)
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監(jiān)控中心 -->
<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
### 配置服務(wù)名稱,要和config 配置中心文件保持一致
spring:
application:
name: configclient
cloud:
config:
### 讀取配置
profile: dev
discovery:
###
enabled: true
### config 配置中心配置的服務(wù)名稱
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);
}
}
/**
* 在讀取配置文件信息的地方進(jìn)行添加 @RefreshScope 注解
*/
@RestController
@RefreshScope
public class AppController {
@Value("${userAge}")
private String userAge;
@GetMapping("/userAge")
public String config(){
System.out.println("userAge : " + userAge);
return userAge;
}
}
修改git倉庫中的配置,進(jìn)行手動更新,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監(jiān)控中心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 配置文件修改
添加對 rabbbitMQ的配置, rabbitMQ服務(wù)和config_server,config_client 在一個服務(wù)器上,使用默認(rèn)配置即可
### 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方式
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解SpringBoot如何創(chuàng)建自定義Starter
Spring Boot的自動配置機(jī)制為開發(fā)人員提供了一種輕松集成和配置各種功能的便捷方式,本文將深入探討在Spring Boot中如何創(chuàng)建自定義Starter,為構(gòu)建模塊化且易維護(hù)的應(yīng)用提供有力的支持,需要的朋友可以參考下2024-02-02
Spring Boot命令行啟動添加參數(shù)的三種方式
在命令行中,常見的參數(shù)可以分為三類:選項(xiàng)參數(shù)、非選項(xiàng)參數(shù)和系統(tǒng)參數(shù),本文就來介紹一下Spring Boot命令行三種參數(shù)形式,感興趣的可以了解一下2023-09-09
java中四種生成和解析XML文檔的方法詳解(介紹+優(yōu)缺點(diǎn)比較+示例)
本篇文章主要介紹了四種生成和解析XML文檔的方法,即:DOM、SAX、JDOM和DOM4J,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-11-11

