上一章節,簡單介紹了分佈式配置中心
Spring Cloud Config
的使用。同時,咱們也遺漏了一些問題,好比如何配置實時生效,當服務端地址變動或者集羣部署時,如何指定服務端地址?回想,在服務註冊章節,服務提供者和服務消費者,同時往註冊中心進行註冊和獲取服務地址,而自己註冊中心又支持高可用配置。因此,對於配置中心,咱們也能夠將Server端
和Client端
往註冊中心進行註冊,藉此實現配置中心的服務化,無需指定具體的ip地址,直接根據服務名稱進行調用。html
將配置中心服務化,自己是爲了實現高可用。而實現高可用的手段是不少的,最經常使用的就是負載均衡
。客戶端不直連服務端,而是訪問負載均衡
服務,由負載均衡
來動態選擇須要訪問的服務端。只是Spring Cloud Config
自然的就能進行服務化配置,因此,實際中能夠根據實際的業務需求來進行合理化抉擇的。java
其次,對於使用了git
或者svn
做爲存儲方式時,自己配置倉庫的高可用也是一個須要考慮的事項。自己如github
或者碼雲
這些第三方git
倉庫而言,已經實現了高可用了。但通常上部署的微服務都是內網服務,因此通常上是使用如gitlab
開源的git
倉庫管理系統進行自建,此時就須要考慮自己倉庫的高可用了。git
注意:自己教程爲了避免混淆各知識點,因此都是獨立項目進行實例,而不是在原工程上進行修改。 本章節教程採用了多模塊工程進行構建實例。父類項目名爲:spring-cloud-config-ha
。同時建立服務化的配置文件:my-config-client-ha-dev.properties
和my-config-client-ha-test.properties
github
my-config-client-ha-dev.properties
web
config=this is dev!
my-config-client-ha-dev.properties
spring
config=this is test!
建立子工程:spring-cloud-confg-ha-server
0.加入pom依賴。bootstrap
<!-- config server 依賴 --> <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>
1.配置文件加入註冊中心相關配置。微信
spring.application.name=spring-cloud-config-ha-server server.port=15678 #配置文件git配置 spring.cloud.config.server.git.uri=https://github.com/xie19900123/spring-cloud-learning.git # 搜索路徑,即配置文件的目錄,可配置多個,逗號分隔。默認爲根目錄。 spring.cloud.config.server.git.searchPaths=spring-cloud-config-repo # git用戶名和密碼 針對私有倉庫而言須要填寫 spring.cloud.config.server.git.username= spring.cloud.config.server.git.password= #添加註冊中心配置 # 註冊中心地址 -此爲單機模式 eureka.client.service-url.defaultZone=http://127.0.0.1:1000/eureka # 啓用ip配置 這樣在註冊中心列表中看見的是以ip+端口呈現的 eureka.instance.prefer-ip-address=true # 實例名稱 最後呈現地址:ip:15678 eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
2.啓動類加入@EnableDiscoveryClient
和@EnableConfigServer
,前者開啓服務發現功能,後者聲明一個config server
。app
/** * config server 服務化 * * @author oKong * */ @SpringBootApplication @EnableConfigServer //注意這裏也可以使用@EnableEurekaClient //但因爲springcloud是靈活的,註冊中心支持eureka、consul、zookeeper等 //若寫了具體的註冊中心註解,則當替換成其餘註冊中心時,又須要替換成對應的註解了。 //因此 直接使用@EnableDiscoveryClient 啓動發現。 //這樣在替換註冊中心時,只須要替換相關依賴便可。 @EnableDiscoveryClient @Slf4j public class ConfigServerHaApplication { public static void main(String[] args) throws Exception { SpringApplication.run(ConfigServerHaApplication.class, args); log.info("spring-cloud-config-ha-server啓動!"); } }
關於Eureka
相關知識點,能夠查看:《第二章:服務註冊與發現(Eureka)-上》和《第三章:服務註冊與發現(Eureka)-下》,這裏就不加以闡述了。負載均衡
3.啓動應用,同時啓動Eureka
服務端。訪問下Eureka
服務端地址:http://127.0.0.1:1000/ ,能夠看見服務註冊成功了。
訪問:http://127.0.0.1:15678/my-config-client-ha-dev.properties 能夠看見配置信息了。
建立子工程:spring-cloud-confg-ha-client
0.加入pom依賴。
<!-- config client 依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- eureka client 依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
1.配置文件修改,bootstrap.properties
添加註冊中心配置。 bootstrap.properties
# 設置分支 spring.cloud.config.label=master # 環境變量 spring.cloud.config.profile=dev # 是否使用註冊中心方式進行獲取 spring.cloud.config.discovery.enabled=true # 服務端地址 # 在不使用註冊中心模式下 直接填寫實際地址 #spring.cloud.config.uri=http://127.0.0.1:5678 # 註冊中心應用id spring.cloud.config.discovery.service-id=spring-cloud-config-ha-server #添加註冊中心配置 # 註冊中心地址 -此爲單機模式 eureka.client.service-url.defaultZone=http://127.0.0.1:1000/eureka # 啓用ip配置 這樣在註冊中心列表中看見的是以ip+端口呈現的 eureka.instance.prefer-ip-address=true # 實例名稱 最後呈現地址:ip:15678 eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
application.properties
# 設置應用名稱,須要和配置文件匹配 spring.application.name=my-config-client-ha server.port=15666
注意:註冊中心的相關配置須要放在bootstrap.properties
中,這樣才能利用註冊中心進行服務端服務地址獲取。
2.啓動類,加入@EnableDiscoveryClient
,開啓服務發現功能。
/** * 服務化方式調用config server * * @author oKong * */ @SpringBootApplication @EnableDiscoveryClient @Slf4j public class ConfigClientHaApplication { public static void main(String[] args) throws Exception { SpringApplication.run(ConfigClientHaApplication.class, args); log.info("spring-cloud-config-ha-client啓動!"); } }
3.建立控制層,測試配置參數。
/** * config client 簡單示例 * @author oKong * */ @RestController public class DemoController { @Value("${config}") String config; @GetMapping("/") public String demo() { return "返回的config參數值爲:" + config; } }
4.啓動應用。通常上應用能啓動成功,就說明服務化已經成功了。 啓動時,能夠看見已經往註冊中心去獲取服務端地址了。
2018-10-10 23:15:15.302 INFO 26412 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://192.168.2.102:15678/ 2018-10-10 23:15:20.728 INFO 26412 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=my-config-client-ha, profiles=[dev], label=master, version=f2645253a37db433d806914b1d04d6aba428831c, state=null
此時,咱們訪問:http://127.0.0.1:15666/ ,便可看見配置信息返回了。
在默認狀況下,客戶端是不會自動感知配置的變化的。此時,咱們能夠使用/refresh
端點來進行配置更新。 如今,咱們改造下客戶端。 0.加入端點依賴。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
1.修改下變量使用類,加入@RefreshScope
註解,標記在訪問/refresh
時,進行變量的更新操做。
/** * config client 簡單示例 * @author oKong * */ @RestController @RefreshScope//使用該註解的類,會在接到SpringCloud配置中心配置刷新的時候,自動將新的配置更新到該類對應的字段中。 public class DemoController { @Value("${config}") String config; @GetMapping("/") public String demo() { return "返回的config參數值爲:" + config; } }
重點就是註解@RefreshScope
了。 2.配置文件開啓端點refresh
。這裏須要注意,2.0
以後,默認只開啓了端點info
、health
。其餘的須要經過management.endpoints.web.exposure.include
進行額外配置。
#開啓監控端點 management.endpoints.web.exposure.include=refresh
3.啓動應用,此時,動態修改下遠程倉庫的參數值爲:config=this is dev refresh!!!
, 使用Postman
使用POST
訪問:http://127.0.0.1:15666/actuator/refresh
。
返回值即爲有變更的參數值。
再次訪問:http://127.0.0.1:15666/ 能夠看見已是最新的配置參數值了。
本章節主要講解了如何將配置中心註冊爲一個服務,客戶端像普通的服務消費者同樣,根據服務名便可獲取服務端地址,進而進行參數的獲取。同時講解了當屬性參數有變時,客戶端如何進行感知變化進行參數動態更新。你們應該能夠想到,當咱們客戶端愈來愈多時,一個個去執行
refresh
時不太現實的,雖然咱們能夠經過相似webhook
功能當有提交記錄時,主動去觸發各客戶端的refresh
方法,在前期項目比較少的狀況下,不失爲一個好方法,只須要維護一份待更新的客戶端地址列表便可。但當服務愈來愈多時,維護此列表也是使人頭疼的。此時,咱們能夠使用Spring cloud bus
消息總線進行通知。因爲目前Spring cloud bus
知識點還沒有開始講解,同時做者也比較少使用消息總線,因此待查閱相關以後介紹Spring Cloud bus
章節時,再來進行講解如何使用消息總線
進行全自動的配置更新操做。
目前互聯網上大佬都有分享
SpringCloud
系列教程,內容可能會相似,望多多包涵了。原創不易,碼字不易,還但願你們多多支持。若文中有錯誤之處,還望提出,謝謝。
499452441
lqdevOps
我的博客:http://blog.lqdev.cn
源碼示例:https://github.com/xie19900123/spring-cloud-learning
原文地址:http://blog.lqdev.cn/2018/10/11/SpringCloud/chapter-eight/