【SpringCloud】SpringCloud config分散式配置中心
SpringCloud config分散式配置中心
概述
分散式系統面臨的---配置問題
微服務意味著要將單體應用中的業務拆分成一個個子服務 ,每個服務的粒度相對較小,因此係統中會出現大量的服務。由於每個服務都需要必要的配置資訊財能執行,所以一集中式的、動態的配置管理設施是必不可少的。
SpringCloud提供了ConfigServer來解決這個問題,我們每一個微服務自 己帶著一個application.yml,. 上百個配置檔案的管理..../(ToT)/~~
是什麼
是什麼
SpringCloud Config為微服務架構中的微服務提供集中化的外部配置支援,配置伺服器為各個不同微服務應用的所有環境提供了一箇中心化的外部配置。
怎麼玩
SpringCloud Config分為服務端和客戶端兩部分。
服務端也稱為分散式配置中心,它是-個獨立的微服務應用,睞連線配置伺服器併為客戶端提供獲取配置資訊,加密/解密資訊等訪問介面
客戶端則是通過指定的配置中心來管理應用資源,以吸與業務相關的配置內容,並在啟動的時候從配置中心獲取和載入配置資訊配置伺服器預設採用git來儲存配置資訊,這樣就有助於對環境配置進行版本管理,並且可以通過git客戶端工具方便的管理和訪問配置內容。
能幹嘛
- 集中管理配置檔案
- 不同環境不同配置,動態化的配置更新,分環境比如dev/test/prod/beta/release
- 執行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置檔案,服務會向配置中心同意拉去配置自己的資訊
- 當配置發生改變時,服務不需要重啟即可感知到配置的變化並應用新的配置
- 將配置資訊以REST介面的形式暴露
post/crul訪問重新整理即可...
與GitHub整合配置
由於SpringCloud Config預設使用GIt來儲存配置檔案(也有其他方式,比如支援SVN和本地檔案),但最推薦還是Git,而且使用的是http/https訪問的形式
官網
https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.2.RELEASE/reference/html/
Config服務端配置與測試
用你自己的賬號在GitHub上新建一個名為springcloud-config的心Repository
由上一步獲得剛新建的git地址
- [email protected]:zz
- 說明一下,我之前建立過,所以這裡我直接使用以前的config倉庫 EiletXie/config-repo
本地硬碟目錄上新建git倉庫並clone
注意:這個只是讓你可以本地git修改config-repo倉庫的資訊與該專案關係不大
本地地址: 自己選一個目錄,我這裡選擇的是 E:\IdeaWorkspace\springcloud_sell\config-repo
git命令:git clone git@github
此時在本地碟符下的檔案
- 表示多個環境的配置檔案
- 儲存格式必須為UTF-8
- 如果需要修改,此處模擬運維人員操作git和github
- git add .
- git commit -m "內容"
- git push origin master
新建Module模組cloud-config-center-3344,它即為Cloud的配置中心模組cloudConfig Center
POM
<dependencies>
<!--config server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency><!-- 引用自己定義的api通用包,可以使用Payment支付Entity -->
<groupId>com.eiletxie.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>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
YML
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: [email protected]:eiletxie/config-repo.git #Github上的git倉庫名字
##搜尋目錄.這個目錄指的是github上的目錄
search-paths:
- config-repo
##讀取分支
label: master
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
主啟動類
ConfigCenterMain3344
@EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}
windows下修改hosts檔案,增加對映
127.0.0.1 config-3344.com
測試通過Config微服務是否可以從GitHub是否可以從GitHub上獲取配置內容
啟動服務3344
http://config-3344.com:3344/master/config-dev.yml
讀取配置規則
官網
/{label}/{application}-{profile}.yml
- master分支
- dev分支
/{application}-{profile}.yml
/{application}/{profile}/{/label}
重點配置細節總結
成功實現了SpringCloudConfig通過Github獲取配置資訊
Config客戶端配置與測試
新建cloud-config-client-3355
POM
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency><!-- 引用自己定義的api通用包,可以使用Payment支付Entity -->
<groupId>com.eiletxie.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>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
bootstrap.yml
applicaiton. ym1是使用者級的資源配置項
bootstrap . ym1是系統級的,優先順序更加高
Spring Cloud會建立一個"Bootstrap Context" ,作為Spring應用的Application Context的父上下文。初始化的時候,‘Bootstrap Context'負責從外部源載入配置屬性並解析配置。這兩個上下文共享一個從外部獲取的'Environment'。
"Bootstrap屬性有高優先順序,預設情況下,它們不會被本地配置覆蓋。"Bootstrap context和Application Context有著不同的約定,所以新增了-個bootstrap.yml檔案,保證Bootstrap Context和Application Context配置的分離。
要將Client模組下的application.ym|檔案改為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 #讀取字尾名稱 上訴3個綜合就是 master分支上 config-dev.yml
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
修改config-dev.yml配置並提交到GitHub中,比如加個變數age或者版本號version
主啟動
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
業務類
@RestController
public class ConfigClientController {
// 因為config倉庫以rest形式暴露,所以所有客戶端都可以通過config服務端訪問到github上對應的檔案資訊
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
測試
啟動Config配置中心3344和eureka7001:http://config-3344.com:3344/master/config-dev.yml
啟動3355作為Client準備訪問:
- http://localhost:3355/configInfo
- 注意:啟動前確定github上有config-dev這個檔案,不然會報錯,因為boostrap.yml中的資訊它會檢查
成功實現了客戶端3355訪問SpringCloud Config3344通過GitHub獲取資訊配置
問題隨之而來,分散式配置的動態重新整理問題
- Linux運維修改GitHub上的配置檔案內容做調整
- 重新整理3344,發現ConfigServer配置中心立刻響應
- 重新整理3355,發現ConfigClient客戶端沒有任何響應
- 3355沒有變化除非自己重啟或者重新載入
- 難道每次運維修改配置檔案,客戶端都需要重啟???噩夢OMG
Config客戶端之動態重新整理
避免每次更新配置都要重啟客戶端服務3355
動態重新整理
步驟
修改3355模組
POM引入actuator監控
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
修改YML,暴露監控埠
#暴露監控端點
management:
endpoints:
web:
exposure:
include: "*"
@refreshScope業務類Controller修改
@RefreshScope
@RestController
@RefreshScope
public class ConfigClientController {
// 因為config倉庫以rest形式暴露,所以所有客戶端都可以通過config服務端訪問到github上對應的檔案資訊
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
此時修改github 3344--->3355
http://localhost:3355/configInfo
3355改變沒有??
沒有呀
How
需要運維傳送Post請求重新整理3355
- 必須是POST請求
- curl -X POST "http://localhost:3355/actuator/refresh"
再次
http://localhost:3355/configInfo:ok了
成功實現了客戶端3355重新整理到最新配置內容:避免了服務重啟
想想還有什麼問題
- 假如有多個微服務客戶端3355,3366,3377.。。。
- 每個微服務都要執行一次post請求,手動重新整理?
- 可否廣播,一次通知,處處生效
- 我們想大範圍的自動重新整理求方法