Config 分布式配置中心
概述
微服務意味著要將單體應用中的業務拆分成個個子服務,每個服務的粒度相對較小因此系統中會出現大量的服務
由于每個服務都需要必要的配置資訊才能運行,所以一套集中式的、動態的配置管理設施是必不可少的
Spring Cloud提供了 ConfigServer 來解決這個問題,我們每個微服務自己帶著一個 application.yml,上百個組態檔的管理會導致膨脹
官方地址:https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.2.RELEASE/reference/html/

Spring Cloud Config為微服務架構中的微服務提供集中化的外部配置攴持,配置服務器為各個不同微服務應用的所有環境提供了一個中心化的配置
Spring Cloud Config分為服務端和客戶端兩部分
- 服務端也稱為分布式配置中心,它是獨立的微服務應用,用來連接配置服務器并為客戶端提供獲取配置資訊,加密解密資訊等訪問介面
- 客戶端則是通過指定的配置中心來管理應用資源,以及與業務相關的配置內容,并在啟動的時候從配置中心獲取和加載配置資訊,配置服務器默認采用 git 來存盤配置資訊,這樣就有助于對環境配置進行版本管理,并且可以通過 git 客戶端工具來方便的管理和訪問配置內容
主要分支
- 集中管理組態檔
- 不同環境不同配置,動態化的配置更新,分環境部署比如 dev/test/prod/beta/release
- 運行期間動態調整配置,不再需要在每個服務部署的機器上撰寫組態檔,服務會向配置中心統一拉取配置自己的資訊
- 當配置發生變動時,服務不需要重啟即可感知到配置的變化并應用新的配置
- 將配置資訊以REST介面的形式暴露
- Github整合配置:由于 Spring Cloud Config默認使用Git來存盤組態檔(也有其它方式比如支持SVN和本地檔案),但最推薦的還是Git,而且使用的是http/https訪問的形式
服務端配置
首先要在 Github 上創建一個 Config 倉庫,來配置環境

- 新建一個module
- 修改 pom 依賴
<!-- config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
- 配置 yml
server:
port: 3344
spring:
application:
name: Config-center
cloud:
config:
server:
git:
# Github 上倉庫的名字
uri: https://github.com/odousnag/SpringCloudStudy.git
# 搜索目錄
search-paths:
- springcloud-config
# 讀取分支
label: master
# Eureka
eureka:
client:
service-url:
# 單機版
# defaultZone: http://localhost:7001/eureka/
# 集群版
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
- 主啟動類開啟注解
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterMain {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain.class,args);
}
}
- windows 下更改 hosts 增加映射

- 測驗是否能從 Github 上獲取配置內容

啟動配置中心:http://config3344.com:3344/master/config-dev.yml

7.配置讀寫規則
官網上的配置讀寫規則

常用的三種:
/{label}/{application}-{profile}.properties
master 分支:http://config3344.com:3344/master/config-xxx.yml
dev 分支:http://config3344.com:3344/dev/config-xxx.yml
/{application}/{profile}[/{label}]
http://config3344.com:3344/config-xxx.yml
/{application}-{profile}.yml
http://config3344.com:3344/config/xxx/master
客戶端配置
- 新建一個module
- 修改 pom 依賴
<!-- spring-cloud-starter-config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
- 新建一個 bootstrap.yml
applicaiton.yml 是用戶級的資源配置項,bootstrap.yml是系統級的,優先級更加高
Spring Cloud會創建一個 "Bootstrap Context",作為 Spring應用的 Application Context的父背景關系
初始化的時候, Bootstrap Context 負責從外部源加載配置屬性并決議配置,這兩個背景關系共享—個從外部獲取的 Environment
將 Client 模塊下的 application.yml 檔案改為 bootstrap.yml 這是很關鍵的,因為 bootstrap.yml是比 application.yml 先加載的
bootstrap.yml 優先級高于 application.yml
server:
port: 3355
spring:
application:
name: Config-client
cloud:
# Config 客戶端配置
config:
# 讀取分支
label: master
# 組態檔名稱
name: config
# 讀取名稱后綴
profile: dev
# 配置中心地址
uri: http://localhost:3344
# Eureka
eureka:
client:
service-url:
# 單機版
# defaultZone: http://localhost:7001/eureka/
# 集群版
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
- 修改 application-dev.yml 配置并提交到 Github 中,比如加個變數 age 或者版本號 version
- 業務類測驗
@RestController
public class ClientController {
/**
* 獲取application-dev的資訊
*/
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}

實作了客戶端訪問SpringCloudConfig通過GitHub獲取配置資訊
客戶端動態重繪
動態重繪問題
Linux運維修改GitHub上的組態檔內容做調整,重繪服務端,發現ConfigServer配置中心立刻回應,重繪客戶端,發現ConfigClient客戶端沒有任何回應
修改客戶端
- 引入 actuator 監控依賴
<!-- spring-boot-starter-actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 修改 yml 配置,暴露監控埠
# 暴露監控埠
management:
endpoints:
web:
exposure:
include: "*"
- 業務類修改,增加注解 @RefreshScope
- 發送Post請求重繪 客戶端
curl -X POST "http://localhost:3355/actuator/refresh"

出現的問題:
現在每次更改,都要手動發動請求,這不合適,實作廣播,一次通知,處處修改
下一章到訊息總線會講到
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/282516.html
標籤:Java
上一篇:Mybatis的動態SQL
