跟我學Spring Cloud(Finchley版)-19-配置中心-Spring Cloud Co
為什麽要使用配置中心
- 集中管理配置。一個使用微服務架構的應用系統可能會包含成百上千個微服務,因此集中管理配置是非常有必要的;
- 不同環境,不同配置。例如,數據源配置在不同的環境(開發、測試、預發布、生產等)中是不同的;
- 運行期間可動態調整。例如,我們可根據各個微服務的負載情況,動態調整數據源連接池大小或熔斷閾值,並且在調整配置時不停止微服務;
- 配置修改後可自動更新。如配置內容發生變化,微服務能夠自動更新配置。
Spring Cloud Config簡介
Spring Cloud Config為分布式系統外部化配置提供了服務器端和客戶端的支持,它包括Config Server和Config Client兩部分。由於Config Server和Config Client都實現了對Spring Environment和PropertySource抽象的映射,因此,Spring Cloud Config非常適合Spring應用程序,當然也可與任何其他語言編寫的應用程序配合使用。
Config Server是一個可橫向擴展、集中式的配置服務器,它用於集中管理應用程序各個環境下的配置,默認使用Git存儲配置內容(也可使用Subversion、MySQL、本地文件系統或Vault存儲配置,本博客以Git為例進行講解),因此可以很方便地實現對配置的版本控制與內容審計。
Config Client是Config Server的客戶端,用於操作存儲在Config Server中的配置屬性。引入Spring Cloud Config後的架構如下:
TIPS
Spring Cloud Config的GitHub:<https://github.com/spring-cloud/spring-cloud-config>
編寫Config Server
示例
-
加依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
-
加註解:
@EnableConfigServer
-
寫配置:
server: port: 8080 spring: application: name: microservice-config-server cloud: config: server: git: # Git倉庫地址 uri: https://git.oschina.net/itmuch/spring-cloud-config-repo.git # Git倉庫賬號 username: # Git倉庫密碼 password:
路徑規則
Spring Cloud Config Server提供了RESTful API,可用來訪問存放在Git倉庫中的配置文件。
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
其中的{appliation}、{profile}、{label} 都是占位符。
TIPS
事實上,可使用Spring Cloud Config實現配置的“繼承”與“組合”,舉個例子——
假設有一個應用:microservice-foo
,其profile是dev,那麽其實Spring Cloud Config會查找如下幾個文件:
microservice-foo-dev.yml
microservice-foo.yml
application-dev.yml
application.yml
對於相同屬性的配置,從上至下優先級逐漸遞減;最終獲得的配置屬性是四個文件的組合。由此,不難分析,可如下規劃幾個配置文件:
microservice-foo-dev.yml
作為指定應用在指定profile下的配置文件microservice-foo.yml
作為制定應用在任何profile下都通用的配置文件application-dev.yml
作為所有應用在指定profile下的配置文件application.yml
作為所有應用在任何profile下都通用的配置文件
測試
- 訪問
http://localhost:8080/microservice-foo-dev.yml
可訪問到Git倉庫的microservice-foo-dev.properties
並組合application.properties
。
集成Config Client
編碼
-
加依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
-
加配置:applicaiton.yml
server: port: 8081
-
加配置:bootstrap.yml
spring: application: name: microservice-foo # 對應config server所獲取的配置文件的{application} cloud: config: uri: http://localhost:8080/ profile: dev # profile對應config server所獲取的配置文件中的{profile} label: master # 指定Git倉庫的分支,對應config server所獲取的配置文件的{label}
其中:
spring.application.name:對應Config Server所獲取的配置文件中的{application} ;
spring.cloud.config.uri:指定Config Server的地址,默認是http://localhost:8888 ;
spring.cloud.config.profile:profile對應Config Server所獲取的配置文件中的{profile} ;
spring.cloud.config.label:指定Git倉庫的分支,對應Config Server所獲取配置文件的{label}。
值得註意的是,以上屬性應配置在bootstrap.yml,而不是application.yml中。如果配置在application.yml中,該部分配置就不能正常工作。例如,Config Client會連接spring.cloud.config.uri的默認值
http://localhost:8888
,而並非我們配置的http://localhost:8080/
。Spring Cloud有一個“引導上下文”的概念,這是主應用程序上下文(Application Context)的父上下文。引導上下文負責從配置服務器加載配置屬性,以及解密外部配置文件中的屬性。和主應用程序加載
application.*
(yml或properties)中的屬性不同,引導上下文加載bootstrap.*
中的屬性。配置在bootstrap.*
中的屬性有更高的優先級,因此默認情況下它們不能被本地配置覆蓋。 -
寫代碼
@RestController public class ConfigClientController { @Value("${profile}") private String profile; @GetMapping("/profile") public String hello() { return this.profile; } }
測試
訪問http://localhost:8081/profile
可返回Git倉庫中的配置屬性。
配套代碼
Config Server
- GitHub:<https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-config-server>
- Gitee:<https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-config-server>
Config Client
- GitHub:<https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-config-client>
- Gitee:<https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-config-client>
本文首發
http://www.itmuch.com/spring-cloud/finchley-19/
幹貨分享
跟我學Spring Cloud(Finchley版)-19-配置中心-Spring Cloud Co