咱们经过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