摘自: http://blog.csdn.net/forezp/article/details/69939114 方誌朋微博
一 分佈式配置中心 spring cloud config
spring cloud config 爲分佈式系統中的外部化配置提供服務器和客戶端的支持。
當應用程序經過從開發環境到測試環境和生產環境的部署管道時,您能夠管理這些環境之間的配置,並確保應用程序在遷移時須要運行所需的一切。服務器存儲後端的默認實現使用git,所以它能夠輕鬆支持配置環境的標記版本,而且能夠經過各類工具來訪問內容。
在分佈式系統中,由於服務數量巨多,所以服務配置文件也不少;爲了方便服務配置文件的統一管理,實時更新,因此須要分佈式配置中心組件。在spring cloud 微服務架構中,有服務配置中心組件 spring cloud config,它支持配置服務放置在配置服務的內存中(本地),也支持配置服務放在遠程git倉庫中。
spring cloud config 組件中,包含兩個角色,config server 和 config client ;
二 分佈式配置中心代碼實現
2.1 構建一個 config - server(config - server 是從遠程git倉庫中讀取配置文件的配置)
01. 導包 spring-cloud-starter-config
o2. 配置 .yaml 配置文件
server: port: 8888 spring: application: name: config-server cloud: config: server: git: # 配置git倉庫的地址 uri: https://github.com/forezp/SpringCloudConfig # 配置倉庫的路徑 searchPaths: respo # 訪問git倉庫的用戶名 # username: # 訪問git倉庫的用戶密碼 # password: # 若是git倉庫是公開倉庫,那麼此處的用戶名和密碼能夠不用寫 # 配置倉庫的分支 label: master
03. 在config-server 的程序主入口類上加上註解@EnableConfigServer 註解開啓配置服務器的功能java
@SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
04.啓動程序,訪問 http://localhost:8888/config-client/devgit
{"name":"config-client","profiles":["dev"],"label":null,"version":"a68876a6211369bae723348d5f8c3defe4a55e04","state":null,"propertySources":[{"name":"https://github.com/forezp/SpringCloudConfig/respo/config-client-dev.properties","source":{"democonfigclient.message":"hello spring io","foo":"foo version 2"}}]}
訪問 http://localhost:8888/config-client/dev github
{"name":"config-client","profiles":["dev"],"label":null,"version":"a68876a6211369bae723348d5f8c3defe4a55e04","state":null,"propertySources":[{"name":"https://github.com/forezp/SpringCloudConfig/respo/config-client-dev.properties","source":{"democonfigclient.message":"hello spring io","foo":"foo version 2"}}]}
由於 遠程git倉庫 https://github/forezp/SpringCloudConfig (配置文件中的git地址 uri )中有一個文件 config-client-dev.properties ;web
按照訪問規則 http://localhost:8888/{application}/{profile} --> json 輸出文件中的數據spring
(或者 若是按照 Http://localhost:8888/application-profile.properties(直接訪問文件內容,原樣輸出))json
訪問地址:後端
http://localhost:8888/----不管訪問什麼,這一段是固定不變服務器
若是是訪問某一個文件 config-client/dev----json架構
config-client-dev.properties 直接顯示文件app
若是直接訪問一個文件裏的某一個屬性,如foo foo/dev(http://localhost:8888/foo/dev)
Http請求地址與資源文件映射以下:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
2.2 構建一個 config - client (config - client 從 config - server 中讀取數據)
01. 新建一個spring boot 項目(config - client)
02. 導包 (勾選 web/config client)
<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>
03. 配置 application.yaml 配置文件
(端口:8881;application.name:config-client ; 配置config的訪問地址URI 就是 config - server 的地址及其 label + profile)
server: port: 8881 spring: application: name: config-client cloud: config: label: master profile: dev uri: http://localhost:8888/
04. 在主入口類中直接讀取config-client-dev.properties中的屬性來進行測試 config - client ;
(注: foo = foo version 2 是 config-client-dev.properties 配置文件中的一個屬性)
@SpringBootApplication @RestController public class ConfigClientApplication { @Value("${foo}") private String foo; @RequestMapping("/hi") public String hi(){ return foo; } public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
5. 啓動程序 訪問 http://localhost:8881/hi --> foo version 2
( 在config - client -dev.properties 配置文件中還有一個屬性 democonfigclient.message = Hello spring io );若是在讀取文件時@Value("${ democonfigclient }"),那麼執行結果將是 Hello spring io ;
總結:
config - server 從本地或者從遠程git倉庫讀取被統一管理的配置文件(直接讀取某個文件或者某個文件中的某個屬性);
config - client 則是從 config - server 中獲取的這個配置文件的屬性;