文章目錄
- 1、Config
- 1.1、概述簡介
- 1.2、Config服務端配置與測驗
- 1.3、Config客戶端配置與測驗
- 1.4、Config客戶端之動態重繪(手動版)
- 1.4.1、面臨的問題
- 1.4.2、動態重繪配置與測驗
- 2、Bus
- 2.1、概述簡介
- 2.2、全域廣播形式的動態重繪
- 2.2.1、新添加一個客戶端
- 2.2.2、兩種導致全域動態重繪的方式
- 2.2.3、添加訊息總線支持的配置
- 2.2.4、測驗結果
- 2.3、定點通知形式的動態重繪
1、Config
1.1、概述簡介
1. 分布式面臨的問題
- 微服務意味著要將單體應用中的業務拆分成一個個子服務,每個服務的粒度相對較小,因此系統中會出現大量的服務,由于每個服務都需要必要的配置資訊才能運行,所以一套集中式的、動態的配置管理設施是必不可少的,
2. Config是什么

Spring Cloud Config分為服務端和客戶端兩部分,- 服務端也成為分布式配置中心,它是一個獨立的微服務應用,用來連接配置服務器并為客戶端提供獲取配置資訊,加密/解密資訊等訪問介面,
- 客戶端則是通過指定的配置中心來管理應用資源,以及與業務相關的配置內容,并在啟動的時候從配置中心獲取和加載配置資訊配置服務器默認采用
git來存盤配置資訊,這樣就有助于對環境配置進行版本管理,并且可以通過git客戶端工具來方便的管理和訪問配置內容,
3. 能干嘛
- 集中管理組態檔
- 不同環境不同配置,動態化的配置更新,分環境部署比如
dev/test/prod/beta/release - 運行期間動態調整配置,不再需要在每個服務部署的機器上撰寫組態檔,服務會向配置中心統一拉取配置自己的資訊
- 當配置發生變動時,服務不需要重啟即可感知到配置的變化并應用新的配置
- 將配置資訊以REST介面的形式暴露
官網地址
1.2、Config服務端配置與測驗
1. 建Module
Module的名稱為cloud-config-center-3344,
2. 改POM
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.xiao</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>
3. 改YML
server:
port: 3344
spring:
application:
name: cloud-config-center
profiles:
active: dev
cloud:
config:
server:
git:
uri: https://gitee.com/henryxjh/springcloud-config.git # url地址
search-paths: # 搜索路徑
- springcloud-config
label: master # 配置的是主分支
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
4. 主啟動
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}
5. 測驗結果

(1)、其他的訪問路徑格式
/{application}/{profile}[/{label}]
/{application}-{profile}.yml ## 推薦使用這個
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
1.3、Config客戶端配置與測驗
1. 建Module
Module的名稱為cloud-config-client-3355,
2. 改POM
<dependencies>
<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>com.xiao</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>
3. 改YML
server:
port: 3355
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
- 要將
application.yaml檔案名稱改為bootstrap.yaml是很關鍵的,bootstrap.yaml是比application.yaml優先加載的,且它是系統級的,優先級更高, SpringCloud會創建一個Bootstrap Context,作為Spring應用的Application Context的父背景關系,初始化的時候,Bootstrap Context負責從外部源加載配置屬性并決議配置,這兩個背景關系共享一個從外部獲取的Environment,
4. 主啟動
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
5. Controller類
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${spring.application.name}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
6. 測驗結果


- 如圖所示,我們成功的從相應的組態檔中讀取到內容,
1.4、Config客戶端之動態重繪(手動版)
1.4.1、面臨的問題
-
Linux運維修改GitHub上的組態檔內容做調整 -
重繪
3344,發現ConfigServer配置中心立刻回應 -
重繪
3355,發現ConfigServer客戶端沒有任何回應 -
3355沒有變化除非自己重啟或者重新加載
1.4.2、動態重繪配置與測驗
1. POM引入actuator監控
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 在yml檔案中添加以下代碼
management:
endpoints:
web:
exposure:
include: "*"
3. 修改controller類
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${spring.application.name}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
4. 需要發送一個請求重繪客戶端
curl -X POST "http://localhost:3355/actuator/refresh"

5. 測驗結果
(1)、訪問3344埠

(2)、訪問3355埠

2、Bus
2.1、概述簡介
1. 是什么
Spring Cloud Bus是用來將分布式系統的節點與輕量級訊息系統連接起來的框架,它整合了Java的事件處理機制和訊息中間件的功能,Spring Cloud Bus目前支持RabbitMQ和Kafka,- 在微服務架構的系統中,通常會使用輕量級的訊息代理來構建一個公用的訊息主題,并讓系統中所有微服務實體都連接上來,由于該主題中產生的訊息會被所有實體監聽和消費,所以稱它為訊息總線,在總線的各個實體,都可以方便地廣播一些需要讓其他連接在該主題上的實體都知道的訊息,
- 其原理是
ConfigClient實體都監聽MQ中同一個topic默認是SpringCloudBus,當一個服務重繪資料的時候,他會把這個訊息放入到Topic中,這樣其它監聽到同一Topic的服務就能得到通知,然后去更新自身的配置,
2. 能干嘛

Spring Cloud Bus能管理和傳播分布式系統間的訊息,就像一個分布式執行器,可用于廣播狀態更改、時間推送等,也可以當作微服務間的通信通道,
2.2、全域廣播形式的動態重繪
2.2.1、新添加一個客戶端
1. 建Module
Module的名稱為cloud-config-client-3366,
2. 改POM
<dependencies>
<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>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</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>
3. 改YML
server:
port: 3366
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: "*"
4. 主啟動
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3366 {
public static void main(String[] args) {
SpringApplication.run( ConfigClientMain3366.class,args);
}
}
5. controller類
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${spring.application.name}")
private String serverPort;
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return "serverPort:"+serverPort+"\t\n\n configInfo: "+configInfo;
}
}
2.2.2、兩種導致全域動態重繪的方式
- 利用訊息總線觸發一個客戶端
/bus/refresh,而重繪所有客戶端的配置

- 利用訊息總線觸發一個服務端
ConfigServer的/bus/refresh端點,而重繪所有客戶端的配置(更加推薦)

(1)、第二種架構方式更合適的原因
- 打破了微服務的職責單一性,因為微服務本身是業務模塊,它本不應該承擔配置重繪職責
- 破壞了微服務各節點的對等性
- 有一定的局限性,例如,微服務在遷移時,它的網路地址常常會發生變化,此時如果想要做到自動重繪,那就會增加更多的修改,
2.2.3、添加訊息總線支持的配置
1. 給cloud-config-center-3344配置中心服務端添加訊息總線支持
(1)、POM
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
(2)、新添加的YML內容
rabbitmq:
host: localhost
port: 5672
username: admin
password: 123456
2. 給cloud-config-center-3355客戶端添加訊息總線支持
(1)、POM
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
(2)、新添加的YML內容
rabbitmq:
host: localhost
port: 5672
username: admin
password: 123456
3. 給cloud-config-center-3366客戶端添加訊息總線支持
(1)、POM
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
(2)、新添加的YML內容
rabbitmq:
host: localhost
port: 5672
username: admin
password: 123456
2.2.4、測驗結果
- 在發送該請求
curl -X POST "http://localhost:3344/actuator/bus-refresh"之后的測驗結果如下


- 一次修改,廣播通知,處處生效,
2.3、定點通知形式的動態重繪
- 在
2.2小節上述環境,只通知3355不通知3366的請求如下:
curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/413389.html
標籤:java
上一篇:Java資料結構-了解復雜度
