git2consul使用踩过的坑

       很久没有更新博客了,可能是最近比较忙了吧(这不能是借口!)。先说正事,最近项目中使用了consul的模块,对于consul的功能简介如下:

  • 服务发现 Consul的客户端可用提供一个服务,比如 api 或者mysql ,另外一些客户端可用使用Consul去发现一个指定服务的提供者.通过DNS或者HTTP应用程序可用很容易的找到他所依赖的服务.
  • 健康检查 Consul客户端可用提供任意数量的健康检查,指定一个服务(比如:webserver是否返回了200 OK 状态码)或者使用本地节点(比如:内存使用是否大于90%). 这个信息可由operator用来监视集群的健康.被服务发现组件用来避免将流量发送到不健康的主机.
  • Key/Value存储 应用程序可用根据自己的需要使用Consul的层级的Key/Value存储.比如动态配置,功能标记,协调,领袖选举等等,简单的HTTP API让他更易于使用.
  • 多数据中心: Consul支持开箱即用的多数据中心.这意味着用户不需要担心需要建立额外的抽象层让业务扩展到多个区域.

具体使用呢,我先贴出我们项目中的配置如下bootstrap.yml:

spring:
  application:
    name: gateway
  cloud:
    consul:
      host: 127.0.0.7
      port: 8500
      config:
        enabled: true
        #acl-token: bdce8853-d22b-5178-39d8-9bdecefba54c
        prefix: configs/tuiodao
        format: FILES
        data-key: ${spring.application.name}.properties
      discovery:
        instance-id: 
${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
        serviceName: ${spring.application.name}

特殊说明:使用consu api形式的注册服务时,一定要使用bootstrap.yml这样的文件,另外consul的文件配置格式这里使用的是 FILES。其实consul支持其他形式如下:

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的启动脚本如下:

nohup /mnt/dat1/soft/consul/consul agent -server -ui -data-dir=/mnt/dat1/soft/consul/tmp -bootstrap -client 0.0.0.0  -bind 127.0.0.1 > /mnt/dat1/soft/consul/info.log &

服务启动之后,可以通过地址:http://127.0.0.1:8500/ui/dc1/nodes 查看已经注册的服务节点信息。

但是,但是,一般生产环境中很少使用ui地址的方式去修改配置,比较不完全嘛,所有就需要 Git2consul 插件来辅助配置文件更新。这里记录下我踩过的坑,还是先贴出我的配置文件git2consul.json信息如下:

{
    "version":"1.0",
    "local_store": "/mnt/dat1/soft/git2consul/git2consul_data", #本地仓库备份地址
    "logger":{
        "name":"git2consul_log",
        "streams":[
            {
                "level":"trace",
                "type":"rotating-file",
                "path":"/mnt/dat1/soft/git2consul/logs/info.log" #生成日志路径
            }
        ]
    },
    "repos":[
        {
            "name":"configs",
            "url":"git@xxx.aaaa:tuodao/configs.git",
            "include_branch_name" : false,#分支信息是否包含到请求中,根据项目的实际路径配置
            "branches":[
                "tuodao"
            ],
            "hooks":[
                {
                  "type" : "polling",#更新策略定时刷新的
                  "interval" : "1"   # 1分钟
                }
            ]
        }
    ]
}

启动脚本如下:

nohup git2consul --endpoint 127.0.0.1 --port 8500 
--config-file /mnt/dat1/soft/git2consul/config/git2consul.json > /mnt/dat1/soft/git2consul/nohup.log &

OK,启动之后会在“/mnt/dat1/soft/git2consul/git2consul_data”目录下生成你的对应分支的git配置文件了。

特殊说明:include_branch_name 该属性由于不仔细,设置成立true,导致consul kv set key的时候,key值里面多了一个分支的路径,如“configs/tuodao/xxx/yyy/application.proterties” ,后来改成false后,生成的key变成了“configs/xxx/yyy/application.proterties”,通过“./consule kv get configs/tuodao/xxx/yyy/application.proterties” 可以查看对应的配置信息了。

其实git2consul配置挺简单的,接下来简单介绍下consul中常用的命令,如下:

 server: 以server身份启动。默认是client
 bootstrap-expect:集群要求的最少server数量,当低于这个数量,集群即失效。
 data-dir:data存放的目录,更多信息请参阅consul数据同步机制
 node:节点id,集群中的每个node必须有一个唯一的名称。默认情况下,Consul使用机器的hostname
 bind:监听的ip地址。默认绑定0.0.0.0,可以不指定。表示Consul监听的地址,而且它必须能够被集群中的其他节点访问。Consul默认会监听第一个private IP,但最好还是提供一个。生产设备上的服务器通常有好几个网卡,所以指定一个不会出错
 client: 客户端的ip地址,0.0.0.0是指谁都可以访问(不加这个,下面的ui :8500无法访问)
 ui: 可以访问UI界面
-config-dir指定配置文件夹,Consul会加载其中的所有文件
-datacenter 指定数据中心名称,默认是dc1

想了解更多,可以参考官网地址https://www.consul.io/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值