通過前文講解,至此,微服務架構已經日趨完善——如今已經能夠作一個大型的應用了!然而,隨着項目的迭代,微服務數目每每與日俱增,如何高效地管理配置成爲咱們必須解決的問題。本節來討論如何使用Spring Cloud Config管理配置。git
Spring Cloud Config爲分佈式系統外部化配置提供了服務器端和客戶端的支持,它包括Config Server和Config Client兩部分。因爲Config Server和Config Client都實現了對Spring Environment和PropertySource抽象的映射,所以,Spring Cloud Config很是適合Spring應用程序,固然也可與任何其餘語言編寫的應用程序配合使用。github
Config Server是一個可橫向擴展、集中式的配置服務器,它用於集中管理應用程序各個環境下的配置,默認使用Git存儲配置內容(也可以使用Subversion、MySQL、本地文件系統或Vault存儲配置,本博客以Git爲例進行講解),所以能夠很方便地實現對配置的版本控制與內容審計。spring
Config Client是Config Server的客戶端,用於操做存儲在Config Server中的配置屬性。引入Spring Cloud Config後的架構以下:bootstrap
TIPS服務器
Spring Cloud Config的GitHub:https://github.com/spring-cloud/spring-cloud-config架構
加依賴app
<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
。加依賴
<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
Config Client
http://www.itmuch.com/spring-cloud/finchley-19/