前言介紹在實際開發(fā)中經常會有一個叫做配置中心的服務,這個服務經過變更參數來動態(tài)刷新線上業(yè)務數據行為配置。比如;行為開關、活動數據、黑白名單、本地/預發(fā)/線上環(huán)境切換等等,這些配置信息往往需要在我們不重啟系統(tǒng)的時候就可以被更新執(zhí)行。那么我們一般會使用具備此類屬性在分布式系統(tǒng)中適合的組件進行開發(fā)配置中心,像是zookeeper、redis發(fā)布訂閱、或者http定時輪許拉取,他們都可以做成統(tǒng)一配置中心服務。而在Spring Cloud Config 中,默認采用 Git 來存儲配置信息,所以使用 Spring Cloud Config 構建的配置服務器,天然就支持對微服務應用配置信息的版本管理,在加上Github的Webhook鉤子服務,可以在我們push等行為操作的時候,自動執(zhí)行我們的http行為,以達到自動刷新配置服務。 環(huán)境準備
案例說明通過在個人Git創(chuàng)建配置服務工程,開啟Webhooks服務添加回調鉤子http://xxx:port/actuator/refresh在更新配置后自動刷新服務配置內容,如圖; 代碼示例itstack-demo-springcloud-06├── itstack-demo-springcloud-config-client │ └── src │ └── main │ ├── java │ │ └── org.itstack.demo │ │ ├── web │ │ │ └── ConfigClientController.java │ │ └── ConfigClientApplication.java │ └── resources │ ├── application.yml │ └── bootstrap.yml └── itstack-demo-springcloud-config-server └── src └── main ├── java │ └── org.itstack.demo │ └── ConfigServerApplication.java └── resources └── application.yml 完整代碼歡迎關注公眾號:bugstack蟲洞棧 回復“SpringCloud專題”進行下載 itstack-demo-springcloud-config-client | 配置獲取客戶端方,提供自動刷新Http
/**
* 微信公眾號:bugstack蟲洞棧 | 沉淀、分享、成長,專注于原創(chuàng)專題案例
* 論壇:http://
* Create by 付政委 on @2019
*/@RestController@RefreshScopepublic class ConfigClientController {@Value("${info.profile:error}")private String profile;@GetMapping("/config")public Mono<String> config() {return Mono.justOrEmpty(profile);}}
/**
* 微信公眾號:bugstack蟲洞棧 | 沉淀、分享、成長,專注于原創(chuàng)專題案例
* 論壇:http://
* Create by 付政委 on @2019
*/@SpringBootApplicationpublic class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}}
spring: application:name: itstack-demo-springcloud-config-client server: port: 9001# /actuator/refresh 這個 Endpoint 暴露出來 management: endpoints:web: exposure:include: refresh
spring:
cloud:config: uri: http://localhost:7397 # 配置中心的具體地址;itstack-demo-springcloud-config-server
name: config-client # 對應 {application} 部分,例如;config-client-dev = 只取最后一個符號'-'之前的
profile: dev # 對應 {profile} 部分
label: master # 對應 {label} 部分,即 Git 的分支。如果配置中心使用的是本地存儲,則該參數無用
#配置文件會被轉換成 Web,訪問規(guī)則如下;
#/{application}/{profile}[/{label}]#/{application}-{profile}.yml
#/{label}/{application}-{profile}.yml
#/{application}-{profile}.properties
#/{label}/{application}-{profile}.properties itstack-demo-springcloud-config-server | 配置提供服務端方,鏈接Git配置工程地址
/**
* 微信公眾號:bugstack蟲洞棧 | 沉淀、分享、成長,專注于原創(chuàng)專題案例
* 論壇:http://
* Create by 付政委 on @2019
*/@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}}
server: port: 7397spring: application:name: itstack-demo-springcloud-config cloud:config: server:git: uri: https://github.com/fuzhengwei/itstack-demo-config # 換成自己的配置Git倉庫的地址,如果沒有可以新建工程地址,也可以克隆我的 search-paths: config-repo # Git倉庫地址下的底層配置文件名稱,如果配置多個用逗號','分割。 # 如果配置中心需要訪問權限,則開啟配置 # spring.cloud.config.server.git.username:Github賬戶 # spring.cloud.config.server.git.password:Github密碼 測試驗證
dev bus 綜上總結
|
|
|