本文采用Spring cloud本文爲2.1.8RELEASE,version=Greenwich.SR3
本文基於前面的文章eureka-server的實現。
參考java
在分佈式系統中,因爲服務數量巨多,爲了方便服務配置文件統一管理,因此須要分佈式配置中心組件。Spring Cloud Config爲分佈式系統中的外部化配置提供服務器和客戶端支持。git
本篇涉及項目的結構爲一個Config Server單機模式和適用於連接Git倉庫,一個Config Client經過server來展現配置文件數據。 同時使用兩個bus來模擬配置文件刷新。github
Config-server功能web
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
#加載本地文件配置 spring: application: name: config-server profiles: active: native #加載本地配置 cloud: config: server: native: #加載本地目錄文件 search-locations: /Users/xxxx/config-server server: port: 13081 eureka: instance: hostname: eureka1.client.com lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 10 client: service-url: defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
package spring.cloud.demo.configserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
至此,一個單機本地config-server就搭建完成算法
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
bootstrap.ymlspring
spring: application: name: config-client cloud: config: label: master profile: dev fail-fast: true uri: http://localhost:13081 #經過域名訪問配置中心服務端 discovery: enabled: true eureka: instance: hostname: eureka1.client.com lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 10 client: service-url: defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
客戶端從服務端獲取資源配置的路徑規則:json
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
本文采用第二種規則。bootstrap
配置內容:api
spring: application: name: config-client server: port: 52601 eureka: instance: hostname: eureka1.client.com lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 10 client: service-url: defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/ info: local-config-client-dev
將新建的config-client-dev.yml放到config-server配置的/Users/xxxx/config-server目錄下,若是config-server沒有配置目錄,默認使用resources目錄下。服務器
package spring.cloud.demo.configclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
在config-client建立測試Controller:ConfigClientController
package spring.cloud.demo.configclient.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 測試是否能獲取到配置信息的controller * @auther: maomao * @DateT: 2019-09-17 */ @RestController public class ConfigClientController { @Value("${info}") private String info; @RequestMapping("/config/info") public String info() { return info; } }
分別啓動eureka-server、config-server、config-client。訪問:http://localhost:52601/config/info,顯示以下
證實本地配置文件已經生效。
修改config-server的application.yml配置文件:
##加載本地文件配置 #spring: # application: # name: config-server # profiles: # active: native #加載本地配置 # cloud: # config: # server: # native: #加載本地目錄文件 # search-locations: /Users/fengfujie/config-server #加載遠程git倉庫資源文件 spring: application: name: config-server cloud: config: server: git: # 配置git倉庫的地址 uri: https://github.com/fengfujie25/sping-cloud-config # git倉庫的帳號 username: xxxxxx # git倉庫的密碼 password: xxxxxx server: port: 13081 eureka: instance: hostname: eureka1.client.com lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 10 client: service-url: defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
從新啓動config-server服務。而後刷新http://localhost:52601/config/info地址,顯示以下
證實已經成功獲取了遠程git資源的配置信息。
以上配置都是經過域名訪問的config-server,爲了保證系統的高可用,由於生產環境的配置服務中心都是集羣配置,全部客戶端才經過服務名稱來訪問。
修改config-client的bootstrap.yml
spring: application: name: config-client cloud: config: label: master profile: dev fail-fast: true #uri: http://localhost:13081 #經過域名訪問配置中心服務端 discovery: enabled: true service-id: config-server #經過服務訪問配置中心服務端 eureka: instance: hostname: eureka1.client.com lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 10 client: service-url: defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
spring.cloud.config.discovery.service-id:經過服務名稱訪問配置中心服務端
從新啓動config-client.並訪問http://localhost:5301/config/info,顯示結果同【2.2】則表明配置信息已生效,證實是經過服務名稱訪問的config-server.
至此,spring cloud集成config的配置就所有完成。可是存在一個問題,若是修改遠程git倉庫的資源配置,項目並不會刷新,因此配置信息是不生效的。
動態刷新config-serve配置方式
本文采用比較簡單的原生刷新方式。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
package spring.cloud.demo.configclient.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @auther: maomao * @DateT: 2019-09-17 */ @RestController @RefreshScope public class ConfigClientController { @Value("${info:error}") private String info; @RequestMapping("/config/info") public String info() { return info; } }
此方式同時還要修改@Value註解內容爲@Value("${info:error}"),由於刷新的時候須要配置信息有默認值,不然會報錯。
訪問http://localhost:5301/config/info,看服務是否能夠正常訪問。
而後能夠修改git資源倉庫中的配置信息。
證實refresh已經生效。
此方式每次都須要手動刷新一下才行,比較麻煩。GitHub提供一種Webhooks方法能夠實現不用每次手動刷新。
Payload URL: 觸發後回調的URL
Content type: 數據格式,兩種通常使用json
Secret: 用做給POST的Body加密的字符串,採用HMAC算法
Events: 觸發的事件列表
事件類型 | 描述 |
---|---|
Just the push event |
倉庫有push的時候觸發,默認事件 |
Send me everything | 派我來一切 |
Let me select individual events | 選擇個別事件 |
這樣咱們就能夠利用Webhook的機制去觸發客戶端的更新,可是當客戶端愈來愈多的時候,Webhook機制也不夠優雅,每次增長客戶端都須要改動Webhook也不現實。
其實,Spring cloud給了咱們更好的解決方案-spring cloud bus。
spring cloud bus後續更新。
本文簡單的實現了config-server和config-client的單機和遠程git倉庫的配置的調用以及配置信息的簡單的動態更新。
轉載請註明出處,