Spring-Cloud-Config消息總線和高可用git
Spring-Cloud-Config是Sping-Cloud下用於分佈式配置管理的組件,分紅了兩個角色Config-Server和Config-Client;Config-Server端集中式存儲/管理配置文件,並對外提供接口方便Config-Client訪問,接口使用HTTP的方式對外提供訪問;Config-Client經過接口獲取配置文件,而後能夠在應用中使用;Config-Server存儲/管理的配置文件能夠來自本地文件,遠程Git倉庫以及遠程Svn倉庫;github
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>2.0.0.RELEASE</version> </dependency>
注:2.0之後的版本須要jdk1.8及以上版本web
Spring-Cloud-Config提供了對多種環境配置文件的支持,好比:開發環境,測試環境,生產環境等;爲了更加全面的模擬,準備三個配置分別以下:spring
config-dev.properties config-test.properties config-pro.properties
分別是開發,測試以及生產的配置文件,內容也比較簡單以下所示:json
foo=hello dev/test/pro
被管理的配置文件能夠來自多個地方,包括:本地文件,遠程Git倉庫以及遠程Svn倉庫,下面分別在resources/application.properties中作配置;bootstrap
spring.application.name=config-server server.port=8888 spring.profiles.active=native spring.cloud.config.server.native.searchLocations=file:E:/github/spring-cloud-config-repo
指定了server端啓動端口爲8888,文件來自E:/github/spring-cloud-config-repo,以上三個文件放在此目錄下緩存
spring.application.name=config-server server.port=8888 spring.profiles.active=git spring.cloud.config.server.git.uri=https://github.com/ksfzhaohui/spring-cloud-config-repo spring.cloud.config.server.git.default-label=master
spring.profiles.active默認值是git,git.uri指定地址,git倉庫default-label默認值是master;服務器
spring.profiles.active=subversion spring.cloud.config.server.svn.uri=https://NJD9YZGJ2-PC.99bill.com:8443/svn/spring-cloud-config-repo spring.cloud.config.server.svn.username=root spring.cloud.config.server.svn.password=root spring.cloud.config.server.svn.default-label=
配置了svn的用戶名和密碼,svn倉庫default-label默認值是trunk,由於此處自建的svn服務器default-label爲空,因此設置爲空值便可;app
@SpringBootApplication @EnableConfigServer public class ConfigServer { public static void main(String[] args) { SpringApplication.run(ConfigServer.class, args); } }
@EnableConfigServer啓動配置服務器;
無論使用以上的哪一種方式配置,均可以經過使用http的方式訪問,http能夠有如下幾種方式請求資源:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
application本實例中對應config;profile表示使用哪一種環境的配置文件,這裏能夠是dev,test,pro;label可選的標籤,git倉庫默認值master,svn倉庫默認值是trunk;
5.1請求http://localhost:8888/config/dev/master,結果以下:
{"name":"config","profiles":["dev"],"label":"master","version":"e9884489051c3b962840ac0a710f0f949a82d0ea","state":null,"propertySources":[{"name":"https://github.com/ksfzhaohui/spring-cloud-config-repo/config-dev.properties","source":{"foo":"hello dev"}}]}
返回結果包含了詳細的信息,最後的source裏面是配置文件內容;
5.2請求http://localhost:8888/config-dev.yml,結果以下:
foo: hello dev
此種方式訪問僅顯示配置文件內容,一樣properties後綴的也僅顯示配置文件內容,只是顯示的格式不同;
5.3更新git上文件內容,請求http://localhost:8888/config-dev.yml,結果以下:
foo: hello dev update
獲取到了最新的內容,其實每次在請求的時候都會去遠程倉庫中更新一下數據,日誌以下:
2018-07-13 09:43:07.606 INFO 14040 --- [nio-8888-exec-7] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@34107ea3: startup date [Fri Jul 13 09:43:07 CST 2018]; root of context hierarchy 2018-07-13 09:43:07.610 INFO 14040 --- [nio-8888-exec-7] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/HUIZHA~1.CFS/AppData/Local/Temp/config-repo-1042810186024067185/config-dev.properties 2018-07-13 09:43:07.611 INFO 14040 --- [nio-8888-exec-7] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@34107ea3: startup date [Fri Jul 13 09:43:07 CST 2018]; root of context hierarchy
把數據更新到本地的Temp路徑下;
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>2.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.0.0.RELEASE</version> </dependency>
在配置文件resources/bootstrap.properties中作以下配置:
spring.application.name=config spring.cloud.config.label=master spring.cloud.config.profile=test spring.cloud.config.uri= http://localhost:8888/ server.port=8889
spring.application.name:對應{application},本實例中是config;
spring.cloud.config.label:對應{label},指定server端配置的分支,此處填master便可;
spring.cloud.config.profile:對應{profile},指定client當前的環境,可選值:dev,test,pro;
spring.cloud.config.uri:server端地址;
server.port:client啓動端口;
@SpringBootApplication public class ConfigClient { public static void main(String[] args) { SpringApplication.run(ConfigClient.class, args); } } @RestController public class HelloController { @Value("${foo}") String foo; @RequestMapping(value = "/hello") public String hello() { return foo; } }
訪問地址:http://localhost:8889/hello,返回結果以下:
hello test
Client端在啓動的時候,能夠發現Server端有拉取配置文件的日誌:
2018-07-13 12:47:36.330 INFO 13884 --- [nio-8888-exec-1] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1917a8ad: startup date [Fri Jul 13 12:47:36 CST 2018]; root of context hierarchy 2018-07-13 12:47:36.399 INFO 13884 --- [nio-8888-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/HUIZHA~1.CFS/AppData/Local/Temp/config-repo-1261377317774171312/config-test.properties 2018-07-13 12:47:36.400 INFO 13884 --- [nio-8888-exec-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1917a8ad: startup date [Fri Jul 13 12:47:36 CST 2018]; root of context hierarchy
更新git中config-test.properties,請求http://localhost:8888/config-test.yml,結果以下:
foo: hello test update
Client請求http://localhost:8889/hello,結果以下:
hello test
能夠發現Server端已經更新,可是Client端沒有獲取到最新的數據,仍是使用的緩存的老數據;
Spring-Cloud-Config提供了多種刷新機制,下面看一下最簡單手動刷新:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.0.0.RELEASE</version> </dependency>
在bootstrap.properties中添加
management.endpoints.web.exposure.include=*
@RefreshScope @RestController public class HelloController { @Value("${foo}") String foo; @RequestMapping(value = "/hello") public String hello() { return foo; } }
@RefreshScope在手動執行刷新的時候會更新此變量
觀察啓動日誌,其中有一條映射以下:
2018-07-13 15:54:16.959 INFO 11372 --- [ main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/refresh],methods=[POST],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Objectorg.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
/actuator/refresh提供了手動刷新的功能,而且必須使用POST方式;
訪問地址:http://localhost:8889/hello,返回結果以下:
hello test
更新git上的配置文件,配置值爲foo=hello test update;
訪問地址:http://localhost:8889/hello,返回結果以下:
hello test
執行手動刷新操做:
C:\curl-7.61.0\I386>curl -X POST http://localhost:8889/actuator/refresh ["config.client.version","foo"]
訪問地址:http://localhost:8889/hello,返回結果以下:
hello test update
在生產環境下不可能每次都去手動觸發refresh,github提供了webhook功能,當某個事件發生時,經過發送http的方式告訴接收方,這樣就能夠在接收到事件的時候觸發refresh請求;
正常狀況下Client會有不少個節點,並且節點會出現上線和下線,如何同時通知每一個節點,Spring-Cloud-Config提供了Spring Cloud Bus來批量處理;
在執行refresh的時候,只會把變更的參數發送給Client端,沒有變更的不會發送,節約了流量;可是若是配置文件被多個不一樣的Client使用,是否會出現不相干的參數會發送給每一個Client;
Server能夠同時加載多個配置文件,Client也能夠支持多個配置文件;
Server端集中管理配置,因此服務的可靠性很重要;
本文分別從Server端和Client端結合實例來介紹Spring-Cloud-Config如何使用,而後大致介紹瞭如何使用更新功能,最後留了幾個待分析的問題,後續進行分析。