Spring Cloud Config是Spring Cloud團隊創建的一個全新項目,用來為分布式系統中的基礎設施和微服務應用提供集中化的外部配置支持,它分為服務端與客戶端兩個部分。其中服務端也稱為分布式配置中心,它是一個獨立的微服務應用,用來連接配置倉庫并為客戶端提供獲取配置信息、加密/解密信息等訪問接口;而客戶端則是微服務架構中的各個微服務應用或基礎設施,它們通過指定的配置中心來管理應用資源與業務相關的配置內容,并在啟動的時候從配置中心獲取和加載配置信息。Spring Cloud Config實現了對服務端和客戶端中環境變量和屬性配置的抽象映射,所以它除了適用于Spring構建的應用程序之外,也可以在任何其他語言運行的應用程序中使用。由于Spring Cloud Config實現的配置中心默認采用Git來存儲配置信息,所以使用Spring Cloud Config構建的配置服務器,天然就支持對微服務應用配置信息的版本管理,并且可以通過Git客戶端工具來方便的管理和訪問配置內容。當然它也提供了對其他存儲方式的支持,比如:SVN倉庫、本地化文件系統。
在本文中,我們將學習如何構建一個基于Git存儲的分布式配置中心,并對客戶端進行改造,并讓其能夠從配置中心獲取配置信息并綁定到代碼中的整個過程。
準備配置倉庫
準備一個git倉庫,可以在碼云或Github上創建都可以。比如本文準備的倉庫示例:http://git.oschina.net/didispace/config-repo-demo
假設我們讀取配置中心的應用名為
config-client
,那么我們可以在git倉庫中該項目的默認配置文件config-client.yml
:
info:
profile: default
- 為了演示加載不同環境的配置,我們可以在git倉庫中再創建一個針對dev環境的配置文件
config-client-dev.yml
:
info:
profile: dev
構建配置中心
通過Spring Cloud Config來構建一個分布式配置中心非常簡單,只需要三步:
- 創建一個基礎的Spring Boot工程,命名為:
config-server-git
,并在pom.xml
中引入下面的依賴(省略了parent和dependencyManagement部分):
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
- 創建Spring Boot的程序主類,并添加
@EnableConfigServer
注解,開啟Spring Cloud Config的服務端功能。
@EnableConfigServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
- 在
application.yml
中添加配置服務的基本信息以及Git倉庫的相關信息,例如:
spring
application:
name: config-server
cloud:
config:
server:
git:
uri: http://git.oschina.net/didispace/config-repo-demo/
server:
port: 1201
到這里,使用一個通過Spring Cloud Config實現,并使用Git管理配置內容的分布式配置中心就已經完成了。我們可以將該應用先啟動起來,確保沒有錯誤產生,然后再嘗試下面的內容。
如果我們的Git倉庫需要權限訪問,那么可以通過配置下面的兩個屬性來實現;
spring.cloud.config.server.git.username:訪問Git倉庫的用戶名
spring.cloud.config.server.git.password:訪問Git倉庫的用戶密碼
完成了這些準備工作之后,我們就可以通過瀏覽器、POSTMAN或CURL等工具直接來訪問到我們的配置內容了。訪問配置信息的URL與配置文件的映射關系如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
上面的url會映射{application}-{profile}.properties
對應的配置文件,其中{label}
對應Git上不同的分支,默認為master。我們可以嘗試構造不同的url來訪問不同的配置內容,比如,要訪問master分支,config-client應用的dev環境,就可以訪問這個url:http://localhost:1201/config-client/dev/master
,并獲得如下返回:
{
"name": "config-client",
"profiles": [
"dev"
],
"label": "master",
"version": null,
"state": null,
"propertySources": [
{
"name": "http://git.oschina.net/didispace/config-repo-demo/config-client-dev.yml",
"source": {
"info.profile": "dev"
}
},
{
"name": "http://git.oschina.net/didispace/config-repo-demo/config-client.yml",
"source": {
"info.profile": "default"
}
}
]
}
我們可以看到該Json中返回了應用名:config-client,環境名:dev,分支名:master,以及default環境和dev環境的配置內容。
構建客戶端
在完成了上述驗證之后,確定配置服務中心已經正常運作,下面我們嘗試如何在微服務應用中獲取上述的配置信息。
- 創建一個Spring Boot應用,命名為
config-client
,并在pom.xml
中引入下述依賴:
<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>
- 創建Spring Boot的應用主類,具體如下:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
- 創建
bootstrap.yml
配置,來指定獲取配置文件的config-server-git
位置,例如:
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:1201/
profile: default
label: master
server:
port: 2001
上述配置參數與Git中存儲的配置文件中各個部分的對應關系如下:
- spring.application.name:對應配置文件規則中的
{application}
部分 - spring.cloud.config.profile:對應配置文件規則中的
{profile}
部分 - spring.cloud.config.label:對應配置文件規則中的
{label}
部分 - spring.cloud.config.uri:配置中心
config-server
的地址
這里需要格外注意:上面這些屬性必須配置在bootstrap.properties中,這樣config-server中的配置信息才能被正確加載。
在完成了上面你的代碼編寫之后,讀者可以將config-server-git、config-client都啟動起來,然后訪問http://localhost:2001/info ,我們可以看到該端點將會返回從git倉庫中獲取的配置信息:
{
"profile": "default"
}
另外,我們也可以修改config-client的profile為dev來觀察加載配置的變化。
更多Spring Cloud內容請持續關注我的博客更新或在《Spring Cloud微服務實戰》中獲取。
代碼示例
樣例工程將沿用之前在碼云和GitHub上創建的SpringCloud-Learning項目,重新做了一下整理。通過不同目錄來區分Brixton和Dalston的示例。
具體工程說明如下:
- 基于Git倉庫的配置中心:config-server-git
- 使用配置中心的客戶端:config-client