知識(shí)儲(chǔ)配
Spring Boot對(duì)數(shù)據(jù)文件的加載機(jī)制如下,標(biāo)號(hào)越小優(yōu)先級(jí)越高:
- 在命令行中出傳入的參數(shù)
- SPRING_APPLICATION_JSON中的屬性。SPRING_APPLICATION_JSON是以Json個(gè)格式配置在系統(tǒng)環(huán)境變量中的內(nèi)容
- java:comp/env中的JNDI屬性
- Java的系統(tǒng)屬性,可以通過(guò)
System.getProperties()獲得的內(nèi)容 - 操作系統(tǒng)的環(huán)境變量
- 通過(guò)random.*配置的隨機(jī)屬性
- 位于當(dāng)前應(yīng)用jar包之外,針對(duì)不同{profile}環(huán)境的配置文件內(nèi)容,例如application-{profile}.properties或是YAML定義的配置文件
- 位于當(dāng)前應(yīng)用jar包之內(nèi),針對(duì)不同{profile}環(huán)境的配置文件內(nèi)容,例如application-{profile}.properties或是YAML定義的配置文件
- 位于當(dāng)前應(yīng)用jar包之外的application.properties和YAML配置內(nèi)容
- 位于當(dāng)前應(yīng)用jar包之內(nèi)的application.properties和YAML配置內(nèi)容
- 在@Configuration注解修改的類中,通過(guò)@PropertySource注解定義的屬性
- 應(yīng)用默認(rèn)屬性,使用SpringApplication.setDefaultProperties定義的內(nèi)容
由7和9都是從jar包之外讀取配置文件,實(shí)現(xiàn)外部配置化的原理即此。我們?cè)诠こ讨械呐渲镁妥兊姆浅8蓛?,只需在本地防止開發(fā)需要的配置即可。
概述
Spring Cloud Config分為服務(wù)端和客戶端。
服務(wù)端是一個(gè)獨(dú)立的微服務(wù)應(yīng)用,其連接配置倉(cāng)庫(kù)并為其他微服務(wù)提供獲取配置信息的接口。
客戶端則是微服務(wù)架構(gòu)中的各個(gè)微服務(wù)應(yīng)用或基礎(chǔ)設(shè)施。
配置倉(cāng)庫(kù)默認(rèn)采用Git,天然支持對(duì)配置信息的版本管理。
服務(wù)端
1. 依賴:
<dependencies>
<dependency>
<groupId>org.springframeword.cloud</groud>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencied>
2. 注解:
@EnableConfigServer
3. application.properties:
# 在Config Server的依賴jar包下有一個(gè)configserver.yml的文件,提供了默認(rèn)的配置(包含倉(cāng)庫(kù))
# 自報(bào)家名
spring.application.name=config-server
# 訪問(wèn)端口號(hào)
server.port=7001
spring.cloud.config.server.git.uri=xxx.git # 配置Git倉(cāng)庫(kù)的位置
spring.cloud.config.server.git.searchPaths=xxx #配置倉(cāng)庫(kù)路徑下的相對(duì)搜索位置,可以配置多個(gè)
spring.cloud.config.server.git.username=xxx # 訪問(wèn)Git倉(cāng)庫(kù)的用戶名
spring.cloud.config.server.git.password=xxx # 訪問(wèn)Git倉(cāng)庫(kù)的用戶密碼
【注】:第5步中的{applicaiton}、{profile}、{label}可以在配置git倉(cāng)庫(kù)的路徑中使用。其中,若Git的分支和標(biāo)簽名包含/,則{label}參數(shù)在HTTP的URL中應(yīng)使用"(_)"代替,詳見《Spring Cloud微服務(wù)實(shí)戰(zhàn)》的P276。
4. 倉(cāng)庫(kù)下文件
- xxx.properties
- xxx-dev.properties
- xxx-test.properties
- xxx-pro.properties
5. 通過(guò)http訪問(wèn),訪問(wèn)規(guī)則如下 :
- /{application}-{profile}.yml:默認(rèn)分支下的文件
- /{application}-{profile}.properties:默認(rèn)分支下的文件
- /{label}/{application}-{profile}.yml:指定分支下的文件
- /{label}/{application}-{profile}.properties:指定分支下的文件
- /{application}/{profile}[/{label}]:文件于分支
其中,各含義如下:
- {application}:倉(cāng)庫(kù)里配置文件名(文件名可以包含-)
- {profile}:倉(cāng)庫(kù)里配置文件名的環(huán)境(如dev、test、pro)
- {label}:分支,缺省為master
以上規(guī)則會(huì)映射{application}-{profile}.properties的配置文件,如訪問(wèn)http://xxx:7001/{application}/{profile}[/{label}]
客戶端
1. 依賴:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
2. 新建bootstrap.properties
# 自報(bào)家門
spring.application.name=xxx
server.port=7002
# 我要dev環(huán)境的配置文件
spring.cloud.config.profile=dev
# 我要xxx分支上的配置文件
spring.cloud.config.label=master
# 服務(wù)端的訪問(wèn)路徑
spring.cloud.config.uri=http ://xxx:7001/
如果將以上內(nèi)容寫在application.properties文件中,從spring cloud config 配置中心讀取數(shù)據(jù)報(bào):Fetching config from server at: http://localhost:8888錯(cuò)誤
bootstrap.properties文件的更多介紹參見官網(wǎng)介紹,配合著這里看
3. 在代碼中使用配置參數(shù)
@Value("${key}")
或
environment.getProperty("kye","defaultValue")
補(bǔ)充
客戶端應(yīng)用從配置管理中獲取配置信息遵從下面的執(zhí)行流程:
- 應(yīng)用啟動(dòng)時(shí),根據(jù)bootstrap.properties中配置的應(yīng)用名{application}、環(huán)境名{profile}、分支名{label},向Config Server請(qǐng)求獲取配置信息
- Config Server根據(jù)自己維護(hù)的Git倉(cāng)庫(kù)信息和客戶端傳遞過(guò)來(lái)的配置定位信息去查找配置信息
- 通過(guò)git clone命令將找到的配置信息下載到Config Server的文件系統(tǒng)中
- Confit Server創(chuàng)建Spring的ApplicationContext實(shí)例,并從Git本地倉(cāng)庫(kù)中加載配置文件,最后將這些配置內(nèi)容讀取出來(lái)返回給客戶端應(yīng)用
- 客戶端應(yīng)用在獲得外部配置文件后加載到客戶端的ApplicationContext實(shí)例,該配置內(nèi)容的優(yōu)先級(jí)高于客戶端Jar包內(nèi)部的配置內(nèi)容,所以在Jar包中重復(fù)的內(nèi)容將不再被加載
Config Server巧妙地通過(guò) git clone將配置信息存于本地,起到了緩存作用,即使當(dāng)Git服務(wù)端無(wú)法訪問(wèn)的時(shí)候, 依然可以取Config Server中的緩存內(nèi)容進(jìn)行使用。
Config Server支持多倉(cāng)庫(kù)的配置,以及倉(cāng)庫(kù)子目錄的配置,詳見《Spring Cloud微服務(wù)實(shí)戰(zhàn)》的P277-278。
Config Server支持自定義本地git倉(cāng)庫(kù)的路徑,通過(guò)spring.cloud.config.server.git.basedir來(lái)指定。
Config Server的健康監(jiān)測(cè)詳見《Spring Cloud微服務(wù)實(shí)戰(zhàn)》的P280。
Config Server支持為所有客戶端提供相同配置的能力,通過(guò)spring.cloud.config.server.overrides.xxx=xxx來(lái)指定。
Config Server可以使用Spring Security組件增加其安全性。方式如下:
- Config Server增加依賴
<dependency>
<groupId>org.springframeword.boot</groud>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- Config Server 在application.properties中增加配置
spring.security.user.name=user
spring.security.user.password=123
- Config Client在bootstrap.properties中增加配置
spring.cloud.config.username=user
spring.cloud.config.password=123
Config Server可以對(duì)敏感配置進(jìn)行加密解密操作,詳見《Spring Cloud微服務(wù)實(shí)戰(zhàn)》的P282-285。
Config Server和Config Client都可以服務(wù)化(通過(guò)Eureka),詳見《Spring Cloud微服務(wù)實(shí)戰(zhàn)》的P287-290。
Config Client向Config Server請(qǐng)求配置信息的時(shí)候,支持失敗快速響應(yīng)與重試策略,詳見《Spring Cloud微服務(wù)實(shí)戰(zhàn)》的P290-292。
當(dāng)配置改變的時(shí)候,Config Client具備動(dòng)態(tài)刷新(無(wú)需重啟)的能力,方法如下:
- 引入依賴,該依賴包含了/refresh端點(diǎn)的實(shí)現(xiàn),用于實(shí)現(xiàn)客戶端應(yīng)用配置信息的重新獲取與刷新
<dependency>
<groupId>org.springframeword.boot</groud>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 修改Git倉(cāng)庫(kù)的配置新建
- (POST)調(diào)用Client的/refresh接口
- 可以使用Spring Cloud Bus來(lái)實(shí)現(xiàn)以消息總線的方式進(jìn)行配置變更的通知...
遇到的一些問(wèn)題
- Spring Boot項(xiàng)目啟動(dòng)遇到Process finished with exit code 0
- Eureka和Config Server的啟動(dòng)順序問(wèn)題
- Config Server 如何實(shí)現(xiàn)高可用
- Config Server 如何實(shí)現(xiàn)高可用
他山之石
為什么需要分布式配置中心?
一篇好TM長(zhǎng)的關(guān)于配置中心的文章
阿里云ACM:云原生配置管理利器
“野生”Java程序員學(xué)習(xí)的道路
Spring Cloud Config 規(guī)范