當一個系統中的配置文件發生改變的時候,咱們須要從新啓動該服務,才能使得新的配置文件生效,spring cloud config能夠實現微服務中的全部系統的配置文件的統一管理,並且還能夠實現當配置文件發生變化的時候,系統會自動更新獲取新的配置。git
Spring Cloud Config爲分佈式系統外部化配置提供了服務器端和客戶端的支持,它包括Config Server和Config Client兩部分。Config Server是一個可橫向擴展、集中式的配置服務器,它用於集中管理應用程序各個環境下的配置,默認使用Git存儲配置內容(也可以使用Subversion、本地文件系統或Vault存儲配置),所以能夠方便的實現對配置的版本控制與內容審計。Config Client 是Config Server的客戶端,用於操做存儲在Config Server中的配置屬性。github
新增git倉庫配置中心,地址爲:https://github.com/kongliuyi/config.git,在倉庫中新增長以下配置文件:web
以上端點均可以映射到{application}-{profile}.properties這個配置文件,{application}表示微服務的名稱,{label}對應Git倉庫的分支,默認是 masterspring
其中config-client-dev.properties文件信息以下:bootstrap
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
#端口號 server: port: 7010 ###服務註冊到eureka地址 eureka: client: service-url: defaultZone: http://eureka7001:7001/eureka spring: application: #註冊中心應用名稱 name: config-server cloud: config: server: git: #git環境地址 uri: https://github.com/kongliuyi/config.git ##搜索目錄 search-paths: /
package net.riking.springcloud.configserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableDiscoveryClient @EnableConfigServer //開啓配置中心服務端 public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
啓動工程後,訪問:http://localhost:7010/config-client-dev.properties,能夠看到以下頁面:服務器
新增項目config-client,怕大家把配置中心客戶端服務理解錯,因此這裏解釋一下。配置中心客戶端就是須要用到配置文件的服務,任何微服務均可以稱爲配置中心客戶端(註冊中心除外)app
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
除了默認的application.yml配置文件,還需增長一個bootstrap.yml的配置文件,內容以下:分佈式
#服務啓動端口號 server: port: 9010 #服務名稱(服務註冊到eureka名稱) spring: application: name: config-client #對應config-server所獲取的配置文件的{application} cloud: config: #讀取後綴 對應config-server所獲取的配置文件的{profile} profile: dev label: master #讀取git倉庫分支 對應config-server所獲取的配置文件的{label} #讀取config-server註冊地址 discovery: service-id: config-server enabled: true #客戶端註冊進eureka服務列表內 eureka: client: service-url: defaultZone: http://eureka7001:7001/eureka
注:spring cloud有一個「引導上下文"的概念,這是主應用程序的父上下文。引導上下文負責從配置服務器加載配置屬性,以及解密外部配置文件中的屬性。和主應用程序加載
application.*(yml或 properties)中的屬性不一樣,引導上下文加載(bootstrap.*)中的屬性。配置在 bootstrap.*中的屬性有更高的優先級,所以默認狀況下它們不能被本地配置覆蓋。spring-boot
package net.riking.springcloud.configclient.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/config/client") public class ConfigClientController { @Value("${name}") private String name; @GetMapping public String name() { return name ; } }
package net.riking.springcloud.configclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient ///開啓對EurekaClient的支持,即:做爲Eureka客戶端,高版本可省略 public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
5.驗證微服務
啓動工程後,訪問:http://localhost:9010//config/client,能夠看到以下頁面:
不少場景下,須要在運行期間動態調整配置。若是配置發生了修改,微服務還會不會刷新呢?
1.修改git倉庫config-client-dev.properties配置文件
name=DEV-Charles
2.訪問http://localhost:7010/config-client-dev.properties,輸出以下
3.訪問http://localhost:9010/config/client,輸出以下
答案顯而易見是不會實時刷新,因此在SpringCloud中提供有兩種刷新數據方式,手動刷新配置文件和實時刷新配置文件兩種方式。其中手動方式採用actuator端點刷新數據,而實時刷新採用SpringCloud Bus消息總線
<!--自省和監控的集成功能,這裏的做用是爲配置信息手動刷新作監控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
#開啓監控斷點
management:
endpoints:
web:
exposure:
include: "*"
在Controller上添加註解@RefreshScope,添加這個註解的類會在配置更改時獲得特殊的處理
package net.riking.springcloud.configclient.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/config/client") @RefreshScope//手動刷新配置文件信息 public class ConfigClientController { @Value("${name}") private String name; @GetMapping public String name() { return name ; } }
啓動工程後,訪問:http://localhost:9010//config/client,能夠看到以下頁面:
1.修改git倉庫config-client-dev.properties配置文件
name=Charles
2.訪問http://localhost:7010/config-client-dev.properties,輸出以下
3.訪問http://localhost:9010/config/client,輸出以下
4.發送post請求:http://localhost:9010/refresh 進行手動刷新數據,輸出以下
5.再次訪問http://localhost:9010/config/client,輸出以下
自動刷新我放入下篇文章,點擊進入
之後有時間整理
做爲格式之後在總結。
配置中心技術有不少種,目前我只用過兩種,一種是攜程的阿波羅(apollo),另外一種就是本篇文章中得springCloud config,我將兩種對比一下,對於springCloud config,我推薦微服務少得小公司使用,而對通常微服務多的大公司,我推薦使用攜程的阿波羅,至於緣由,我會在日後開一篇apollo篇中講解