consul介绍
Consul是一个微服务管理软件。支持多数据中心下,分布式高可用的,服务发现和配置共享。采用 Raft 算法
,用来保证服务的高可用。
参考文献
软件下载:https://www.consul.io/downloads
软件安装
下载软件
wget https://releases.hashicorp.com/consul/1.10.1/consul_1.10.1_linux_amd64.zip
解压软件
unzip consul_1.10.1_linux_amd64.zip
将软件置于系统启动目录
mv consul /usr/local/bin/consul
软件启动
consul agent -server -bootstrap-expect 1 -data-dir /data/consul -node=sn1 -bind=192.168.1.100 -ui -client=0.0.0.0
访问ui界面
访问地址:ip:8500
软件启动说明:
consul必须启动agent才能使用,有两种启动模式server和client。
server用与持久化服务信息,集群官方建议3或5个节点。client只用与于server交互。ui界面可以查看集群情况的。
参数解释:
-bootstrap-expect:集群期望的节点数,只有节点数量达到这个值才会选举leader。
-server: 运行在server模式
-data-dir:指定数据目录,其他的节点对于这个目录必须有读的权限
-node:指定节点的名称
-bind:为该节点绑定一个地址
-config-dir:指定配置文件,定义服务的,默认所有一.json结尾的文件都会读
-enable-script-checks=true:设置检查服务为可用
-datacenter: 数据中心没名称,
-join:加入到已有的集群中
基本使用
常用命令介绍
如果不想用命令操作,当然你也可以通过 ui 界面进行服务管理。
服务管理
注册一个服务
[root@204 ~]# curl -X PUT -d '{"ID":"order_1","Name":"order","Tags":["xdp-\/core.order"],"Address":"192.168.232.201","Port":18307,"Check":{"name":"order_1.check","tcp":"192.168.232.201:18307","interval":"10s","timeout":"2s"}}' http://192.168.169.99:8500/v1/agent/service/register
查询指定服务
[root@204 ~]# curl http://192.168.169.99:8500/v1/health/service/order
[{"Node":{"ID":"0d05756b-e7d3-6fbb-38e4-334de3220fea","Node":"consul1.4.4_client_public_5","Address":"172.17.0.5","Datacenter":"xdp_dc","TaggedAddresses":{"lan":"172.17.0.5","wan":"172.17.0.5"},"Meta":{"consul-network-segment":""},"CreateIndex":229415,"ModifyIndex":229415},"Service":{"ID":"order_1","Service":"order","Tags":["xdp-/core.order"],"Address":"192.168.232.201","Meta":null,"Port":18307,"Weights":{"Passing":1,"Warning":1},"EnableTagOverride":false,"ProxyDestination":"","Proxy":{},"Connect":{},"CreateIndex":229415,"ModifyIndex":229415},"Checks":[{"Node":"consul1.4.4_client_public_5","CheckID":"serfHealth","Name":"Serf Health Status","Status":"passing","Notes":"","Output":"Agent alive and reachable","ServiceID":"","ServiceName":"","ServiceTags":[],"Definition":{},"CreateIndex":229415,"ModifyIndex":229415},{"Node":"consul1.4.4_client_public_5","CheckID":"service:order_1","Name":"order_1.check","Status":"passing","Notes":"","Output":"TCP connect 192.168.232.201:18307: Success","ServiceID":"order_1","ServiceName":"order","ServiceTags":["xdp-/core.order"],"Definition":{},"CreateIndex":229415,"ModifyIndex":303563}]}][root@204 ~]#
查询所有服务
[root@204 ~]# curl http://192.168.169.99:8500/v1/agent/services
{"core.product-/192.168.16.170:8001":{"ID":"core.product-/192.168.16.170:8001","Service":"core.product","Tags":["xdp-/core.product"],"Meta":{},"Port":18306,"Address":"192.168.232.100","Weights":{"Passing":1,"Warning":1},"EnableTagOverride":false},"goods_1":{"ID":"goods_1","Service":"goods","Tags":["xdp-/core.product"],"Meta":{},"Port":18307,"Address":"192.168.232.200","Weights":{"Passing":1,"Warning":1},"EnableTagOverride":false},"test1":{"ID":"test1","Service":"test1name","Tags":["xdp-/core.product"],"Meta":{},"Port":18306,"Address":"192.168.232.100","Weights":{"Passing":1,"Warning":1},"EnableTagOverride":false}}[root@204 ~]#
注销服务
[root@204 ~]# curl -X PUT http://192.168.169.99:8500/v1/agent/service/deregister/order_1
key/value配置中心命令行
查看所有key/value
[root@204 ~]# curl http://192.168.169.99:8500/v1/kv/?recurse
[{"LockIndex":0,"Key":"dd","Flags":0,"Value":"ewoiYiI6ImEiCn0=","CreateIndex":103396,"ModifyIndex":104797},{"LockIndex":0,"Key":"dd/dd/ff/uu","Flags":0,"Value":"NTU1NQ==","CreateIndex":104870,"ModifyIndex":104927},{"LockIndex":0,"Key":"dd/dd/gg","Flags":0,"Value":null,"CreateIndex":104868,"ModifyIndex":104868},{"LockIndex":0,"Key":"dd/ff","Flags":0,"Value":"MTIz","CreateIndex":104864,"ModifyIndex":104864}][root@204 ~]#
查询所有的key/value
[root@204 ~]# curl http://192.168.169.99:8500/v1/kv/?recurse
[{"LockIndex":0,"Key":"dd","Flags":0,"Value":"ewoiYiI6ImEiCn0=","CreateIndex":103396,"ModifyIndex":104797},{"LockIndex":0,"Key":"dd/dd/ff/uu","Flags":0,"Value":"NTU1NQ==","CreateIndex":104870,"ModifyIndex":104927},{"LockIndex":0,"Key":"dd/dd/gg","Flags":0,"Value":null,"CreateIndex":104868,"ModifyIndex":104868},{"LockIndex":0,"Key":"dd/ff","Flags":0,"Value":"MTIz","CreateIndex":104864,"ModifyIndex":104864}]
添加key/value
[root@204 ~]# curl -X PUT -d 'test' http://192.168.169.99:8500/v1/kv/abc/key1
true
说明
key: abc/key1
value:test
查看单个key/value
[root@204 ~]# curl http://192.168.169.99:8500/v1/kv/abc/key1
[{"LockIndex":0,"Key":"abc/key1","Flags":0,"Value":"dGVzdA==","CreateIndex":304012,"ModifyIndex":304012}]
说明
value是test的base64编码(使用base64编码是为了允许非UTF-8的字符)
修改key/value
[root@204 ~]# curl -X PUT -d 'test666' http://192.168.169.99:8500/v1/kv/abc/key1
true
删除key/value
[root@204 ~]# curl -X DELETE http://192.168.169.99:8500/v1/kv/abc/key1
true