一、簡介
在分布式系統中,由於服務數量巨多,為了方便服務配置文件統一管理,實時更新,所以需要分布式配置中心組件。市面上開源的配置中心有很多,BAT每家都出過,360的QConf、淘寶的diamond、百度的disconf都是解決這類問題。國外也有很多開源的配置中心Apache的Apache Commons Configuration、owner、cfg4j等等。在Spring Cloud中,有分布式配置中心組件spring cloud config ,它支持配置服務放在配置服務的內存中(即本地),也支持放在遠程Git倉庫中。在spring cloud config 組件中,分兩個角色,一是config server,二是config client。
一個配置中心提供的核心功能
- 提供服務端和客戶端支持
- 集中管理各環境的配置文件
- 配置文件修改之后,可以快速的生效
- 可以進行版本管理
- 支持大的並發查詢
- 支持各種語言
Spring Cloud Config可以完美的支持以上所有的需求。
Spring Cloud Config項目是一個解決分布式系統的配置管理方案。它包含了Client和Server兩個部分,server提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client通過接口獲取數據、並依據此數據初始化自己的應用。Spring cloud使用git或svn存放配置文件,默認情況下使用git,我們先以git為例做一套示例。
二、構建Config Server
創建一個spring-boot項目,取名為config-server,pom.xml中引入依賴:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--表示為web工程--> <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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
新建入口類BootApplication:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class BootApplication { public static void main(String[] args) { SpringApplication.run(BootApplication.class, args); } }
application.properties:
server.port=7010
spring.cloud.config.server.default-application-name=config-server
# 配置git倉庫地址
spring.cloud.config.server.git.uri=https://github.com/s***w*/myspringcloudconfig
# 配置倉庫路徑
spring.cloud.config.server.git.search-paths=myconfigpath
# 配置倉庫的分支
spring.cloud.config.label=master
# 訪問git倉庫的用戶名
spring.cloud.config.server.git.username=xxxxoooo
# 訪問git倉庫的用戶密碼 如果Git倉庫為公開倉庫,可以不填寫用戶名和密碼,如果是私有倉庫需要填寫
spring.cloud.config.server.git.password=xxxxoooo
遠程倉庫https://github.com/shaweiwei/myspringcloudconfig/ 中有個文件config-client-dev.properties文件中有一個屬性:
myww=myww version 2
啟動程序:訪問http://localhost:7010/myww/dev
證明配置服務中心可以從遠程程序獲取配置信息。
http請求地址和資源文件映射如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
三、構建一個config client
重新創建一個springboot項目,取名為config-client,其pom文件引入依賴:
<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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
其配置文件bootstrap.properties:
# 和git里的文件名對應
spring.application.name=config-client
# 遠程倉庫的分支
spring.cloud.config.label=master
# dev 開發環境配置文件 | test 測試環境 | pro 正式環境
# 和git里的文件名對應
spring.cloud.config.profile=dev
# 指明配置服務中心的網址
spring.cloud.config.uri= http://localhost:7010/
server.port=7020
程序的入口類,寫一個API接口“/hi”,返回從配置中心讀取的foo變量的值,代碼如下:
import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class BootApplication { public static void main(String[] args) { SpringApplication.run(BootApplication.class, args); } @Value("${myww}") // git配置文件里的key String myww; @RequestMapping(value = "/hi") public String hi(){ return myww; } }
打開網址訪問:http://localhost:7020/hi,網頁顯示:
myww version 2
這就說明,config-client從config-server獲取了foo的屬性,而config-server是從git倉庫讀取的,如圖:
四、擴展
我們在進行一些小實驗,手動修改service-config-dev.properties中配置信息提交到github,再次請求會看到還是用的舊的配置,這是為什么?因為spirngboot項目只有在啟動的時候才會獲取配置文件的值,修改github信息后,client端並沒有再次去獲取,所以導致這個問題。如何去解決這個問題呢?
本文源碼:http://download.csdn.net/download/u013081610/10152869