一、介紹
Spring Cloud Config在分佈式系統中提供服務端和客戶端去支持額外的外部配置。配置服務中心(Config Server)爲所有應用提供各種環境的外部配置。默認採用Git的方式存儲配置文件,因此我們很容易部署修改,有助於對環境配置進行版本管理。
二、構建config server
創建一個spring boot 項目,在pom.xml添加spring-cloud-config-server 依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
在程序的入口SpringCloudConfigServerApplication類加上@EnableConfigServer註解開啓配置服務器。
@EnableConfigServer
@SpringBootApplication
public class SpringCloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}
創建一個application.yml 文件,內容如下
server:
port: 8888
spring:
cloud:
config:
server:
git:
# uri: https://github.com/luosai001/SpringCloudConfigRepo
uri: https://github.com/luosai001/config-repo.git # default uri 當匹配不到倉庫時,用這個默認的uri
search-paths: file # 查找倉庫根目錄和file 目錄, 應用正則表達式
username: luosai001
password: ******
repos: # 配置多個倉庫,各個應用通過模式匹配找到對應的倉庫,讀取配置
project-a:
pattern: spring-cloud-config-client/* # {application}/{profile}
uri: https://github.com/luosai001/SpringCloudConfigRepo.git
project-b:
pattern: project-b/* # {application}/{profile}
uri: https://github.com/luosai001/config-repo.git
search-paths: file # 查找倉庫根目錄和file 目錄, 應用正則表達式
uri是git倉庫,用來存儲配置信息的
username 是git倉庫用戶名
password 是git倉庫密碼
啓動應用 訪問localhost:8888
創建一個遠程倉庫,本示例在github上創建了一個倉庫倉庫地址爲 https://github.com/luosai001/SpringCloudConfigRepo
在本地上傳了一個文件到倉庫,文件名爲spring-cloud-config-client-dev.yml
文件配置信息如下:
server:
port: 8003 #應用端口號
spring:
cloud:
config:
allowOverride: true
failFast: true
overrideNone: true #覆蓋非系統屬性 true不覆蓋
overrideSystemProperties: false #覆蓋系統屬性 false不覆蓋
foo: i am foo,but i changed! changed!changed!
訪問localhost:8888/spring-cloud-config-client/dev
返回信息
{
"name":"spring-cloud-config-client",
"profiles":[
"dev"
],
"label":null,
"version":"cadbabdf73cc54dc8b56afe84d47554efe585cb9",
"state":null,
"propertySources":[
{
"name":"https://github.com/luosai001/SpringCloudConfigRepo/spring-cloud-config-client-dev.yml",
"source":{
"server.port":8003,
"spring.cloud.config.allowOverride":true,
"spring.cloud.config.failFast":true,
"spring.cloud.config.overrideNone":true,
"spring.cloud.config.overrideSystemProperties":false,
"foo":"i am foo,but i changed! changed!changed!"
}
}
]
}
http請求地址和文件映射如下:
- /{application}/{profile}[/{label}
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
普通文本讀取: /{name}/{profile}/{label}/{path}
name: 應用名
profile:激活的配置
lable:哪個分支
path:普通文本文件名
三、創建Config Client
創建一個spring boot 項目 添加依賴spring-cloud-starter-config,spring-boot-starter-web,spring-boot-starter-actuator(刷新配置)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
程序入口類:
@SpringBootApplication
@RestController
@RefreshScope
public class SpringCloudConfigClientApplication {
@Value("${foo}")
String foo ;
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigClientApplication.class, args);
}
@RequestMapping("hi")
public String hi(){
return foo ;
}
}
在這個主入口中,我們將foo通過${foo}注入給foo變量,並返回。 @RefreshScope 指可以刷新配置。如果配置中foo 的值改變了,在本地刷新 /refresh ,再次訪問/hi 就會發現返回的值變了。
創建一個bootstrap.yml文件
spring:
application:
name: spring-cloud-config-client
profiles:
active: dev
cloud:
config:
uri: http://localhost:8888
profile: dev
management:
security:
enabled: false
server:
port: 9000
應用名爲spring-cloud-config-client
激活的文件:dev
uri:配置中心地址
management.security.enabled:false 調用/refresh刷新的時候需要。否則會沒有權限訪問
啓動應用 訪問:localhost:9000/hi 返回配置裏定義的信息。
在遠程外部配置文件中我們開啓了不覆蓋本地系統屬性,不覆蓋本地非系統提供屬性,所以啓動時,遠程外部配置中端口並沒有作爲應用的端口。默認都是覆蓋,即遠程port 8003 會覆蓋本地9000端口。
修改外部配置foo的值,需要手動調用localhost:9000/refresh才能刷新foo的值。如果一個值被多個進程使用,那麼這幾個服務都要手動調用/refresh方法,很麻煩。下一節我們將通過Spring Cloud Bus 來解決這個問題,只需要調用一次即可。
參考文檔 https://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html