Spring Cloud Finchley.SR1 的学习与应用 2 - Consul

Spring Cloud Consul

简介

consul是google开源的一个使用go语言开发的服务发现、配置管理中心服务。内置了服务注册与发现框 架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,再也不须要依赖其余工具(好比ZooKeeper等)。服务部署简单,只有一个可运行的二进制的包。每一个节点都须要运行agent,他有两种运行模式server和client。每一个数据中心官方建议须要3或5个server节点以保证数据安全,同时保证server-leader的选举可以正确的进行。node

Consul的安装

1.安装linux

mkdir -p /opt/consul
cd /opt/consul/
wget https://releases.hashicorp.com/consul/1.2.0/consul_1.2.0_linux_amd64.zip
unzip consul_1.2.0_linux_amd64.zip

2.启动consulgit

mkdir -p /opt/consul/config
mkdir -p /opt/consul/data

#启用Gossip加密方式,只须要在启动Agent时设置加密密钥。经过encrypt参数设置密钥:设置的值是包含密钥的配置文件。

$ ./consul keygen
xZpdVT6DUTzQrYyf2B2uCQ==



vim /opt/consul/config/config.json


{
    "advertise_addr": "192.168.2.161",
    "bind_addr": "192.168.2.161",
    "client_addr": "0.0.0.0",
    "domain": "consul",
    "bootstrap_expect": 3,
    "server": true,
    "datacenter": "dc1",
    "data_dir": "/opt/consul/data",
    "encrypt": "xZpdVT6DUTzQrYyf2B2uCQ==",
    "ui": true,
    "enable_syslog": true,
    "performance": {
      "raft_multiplier": 1
    },
    "dns_config": {
        "allow_stale": true,
        "max_stale": "15s"
    },
    "retry_join": [
        "192.168.2.162",
        "192.168.2.163"
    ],
    "retry_interval": "10s",
    "skip_leave_on_interrupt": true,
    "leave_on_terminate": false,
    "rejoin_after_leave": true
}


#启动节点1(server模式)
nohup ./consul agent -config-dir /opt/consul/config -node node1 &
命令 解释 示例
agent 运行一个consul agent consul agent -dev
join 将agent加入到consul集群 consul join IP
members 列出consul cluster集群中的members consul members
leave 将节点移除所在集群 consul leave

consul agent 命令的经常使用选项github

-data-dir 做用:指定agent储存状态的数据目录,这是全部agent都必须的,对于server尤为重要,由于他们必须持久化集群的状态算法

-config-dir 做用:指定service的配置文件和检查定义所在的位置,一般会指定为”某一个路径/consul.d”(一般状况下,.d表示一系列配置文件存放的目录)json

-config-file 做用:指定一个要装载的配置文件,该选项能够配置屡次,进而配置多个配置文件(后边的会合并前边的,相同的值覆盖)bootstrap

-dev 做用:建立一个开发环境下的server节点,该参数配置下,不会有任何持久化操做,即不会有任何数据写入到磁盘,这种模式不能用于生产环境(由于第二条)vim

-bootstrap-expect 做用:该命令通知consul server咱们如今准备加入的server节点个数,该参数是为了延迟日志复制的启动直到咱们指定数量的server节点成功的加入后启动。浏览器

-node 做用:指定节点在集群中的名称,该名称在集群中必须是惟一的(默认采用机器的host),推荐:直接采用机器的IP安全

-bind 做用:指明节点的IP地址,有时候不指定绑定IP,会报Failed to get advertise address: Multiple private IPs found. Please configure one. 的异常

-server 做用:指定节点为server,每一个数据中心(DC)的server数推荐至少为1,至多为5,全部的server都采用raft一致性算法来确保事务的一致性和线性化,事务修改了集群的状态,且集群的状态保存在每一台server上保证可用性,server也是与其余DC交互的门面(gateway)

-client 做用:指定节点为client,指定客户端接口的绑定地址,包括:HTTP、DNS、RPC,默认是127.0.0.1,只容许回环接口访问,若不指定为-server,其实就是-client

-join 做用:将节点加入到集群

-datacenter 做用:指定机器加入到哪个数据中心中 -ui 做用:使得客户端能够直接经过url访问服务端的consul ui

#启动节点2-3(server模式)

#更改advertise_addr、bind_addr、retry_join 参数,启动节点2-3
nohup ./consul agent -config-dir /opt/consul/config -node node2 &

nohup ./consul agent -config-dir /opt/consul/config -node node3 &

打开浏览器,输入http://192.168.2.161:8500/ui/ consul

consul

#查看下集群的状态
[root@node1 consul]# ./consul members
Node   Address             Status  Type    Build  Protocol  DC   Segment
node1  192.168.2.161:8301  alive   server  1.2.0  2         dc1  <all>
node2  192.168.2.162:8301  alive   server  1.2.0  2         dc1  <all>
node3  192.168.2.163:8301  alive   server  1.2.0  2         dc1  <all>

2.安装git2consul

原生的Consul不支持Git来作,须要借助Consul 社区提供的另一个工程git2consul

配置以下:

{
    "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分钟
                }
            ]
        }
    ]
}

个人配置:

{
  "version": "1.0",
  "local_store": "/opt/git2consul/git2consul_cache",
  "logger" : {
    "name" : "git2consul",
    "streams" : [{
      "level": "trace",
      "stream": "process.stdout"
    },
    {
      "level": "debug",
      "type": "rotating-file",
      "path": "/opt/git2consul/logs/git2consul.log"
    }]
  },
  "repos" : [{
    "name" : "woqu_configuration",
    "url" : "https://github.com/zhangorrin/woqu.git",
    "branches" : ["master"],
    "hooks": [{
      "type" : "polling",
      "interval" : "1"
    }]
  }]
}

启动时候指定上边的脚本:

./git2consul --config-file git2consul.json

启动后能够在key/value中看到githup上的配置文件 consul

至此,consul服务端安装已经完成,后续在实际项目中讲解如何使用consul client

相关文章
相关标签/搜索