Consul Config 使用Git作版本控制的實現

Spring Cloud Config 原理

image

咱們經過git 把配置文件推送到遠程倉庫作版本控制,當版本發生變化的時候,遠程倉庫經過webhook機制推送消息給 Config Server,Config Server 將修改通知發送到消息總線,而後全部的Config Client 進行配置刷新。
很是巧妙的藉助了Git來作配置文件修改的版本控制。git

Consul Config 的FILES 機制

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

bootstarp.yml配置

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

配置細節

image

如上圖,我配置文件的例子。 json

FILES機制和Spring Cloud Config加載相似,application.yml 會被全部微服務模塊加載公用,對應的application-name.yml 會被對應的微服務加載。app

總結

  1. 通過整合Consul Config 已經完成了和Spring Cloud Config 相同的功能,Spring Cloud 微服務使用配置文件過程當中並無太大區別。
  2. 實時刷新機制和前文《Consul微服務的配置中心體驗篇》提到的KEY-VALUE模式沒有什麼區別,git2consul 不只支持webhook 的push,並且能夠輪詢pull,相似於 Apollo 配置中心的部分功能
    1. 關於pig:基於Spring Cloud、oAuth2.0開發基於Vue先後分離的開發平臺,支持帳號、短信、SSO等多種登陸,提供配套視頻開發教程
相關文章
相關標籤/搜索