咱們經過git 把配置文件推送到遠程倉庫作版本控制,當版本發生變化的時候,遠程倉庫經過webhook機制推送消息給 Config Server,Config Server 將修改通知發送到消息總線,而後全部的Config Client 進行配置刷新。
很是巧妙的藉助了Git來作配置文件修改的版本控制。git
public enum Format { /** * Indicates that the configuration specified in consul is of type native key values. */ KEY_VALUE, /** * Indicates that the configuration specified in consul is of property style i.e., * value of the consul key would be a list of key=value pairs separated by new lines. */ PROPERTIES, /** * Indicates that the configuration specified in consul is of YAML style i.e., value * of the consul key would be YAML format */ YAML, /** * Indicates that the configuration specified in consul uses keys as files. * This is useful for tools like git2consul. */ FILES, }
Consul 提供以上的策略,key/value、yaml、properties,能夠很簡單的經過Consule Config 的管理臺進行配置,咱們主要來看FILES,就是咱們也是Cloud Config 同樣,經過Git 來作版本控制,只是用Consul 作配置的分發和修改的通知。
原生的Consul不支持Git來作,須要藉助Consul 社區提供的另一個工程 git2consul
很是簡單就下載就安裝好了。
主要來說一下初始化腳本的 git2consul.jsongithub
{ "version":"1.0", "local_store": "本地倉庫備份地址", "logger":{ "name":"git2consul", "streams":[ { "level":"trace", "type":"rotating-file", "path":"生成日誌路徑/git2consul.log" } ] }, "repos":[ { "name":"pig-config", "url":"遠程倉庫地址", "include_branch_name" : true, //分支信息是否包含到請求中,建議使用 "branches":[ "dev" ], "hooks":[ { "type" : "polling", //更新策略定時刷新的 "interval" : "1" //1分鐘 } ] } ] }
啓動時候指定上邊的腳本web
./git2consul --config-file git2consul.json
spring: application: name: pig-auth cloud: consul: host: localhost port: 8500 config: enabled: true format: FILES watch: enabled: true prefix: ${spring.application}/${spring.profiles.active} profiles: active: dev
OK 已經可使用了 git2consul 來同步你的配置文件啦。spring
如上圖,我配置文件的例子。 json
FILES機制和Spring Cloud Config加載相似,application.yml 會被全部微服務模塊加載公用,對應的application-name.yml 會被對應的微服務加載。app