1、consul 是B/C架构。服务端和客户端包是一样的。差别在于启动时候的参数。
--客户端
./consul agent -join=172.29.2.65:8301 -bind=172.29.3.164 -client=172.29.3.164 -data-dir=/app/consul/data -node=client1 -encrypt=eOC89RMstTVHq92WTb8ExQ==
--服务端
nohup consul agent -server -bind=0.0.0.0 -client=0.0.0.0 -bootstrap-expect=1 -data-dir=/app/consul/data/server -node=server1 -encrypt=eOC89RMstTVHq92WTb8ExQ== -ui &
2、常用命令
查看成员
[root@LAPP-V576 ~]# consul members
Node Address Status Type Build Protocol DC Segment
server1 172.29.2.65:8301 alive server 1.0.7 2 dc1
client1 172.29.3.164:8301 alive client 1.0.7 2 dc1
产生公用的秘钥:
[root@LAPP-V576 ~]# consul keygen
hHps7HHRPFvswm5VPeRx1g==
查看版本号:
[root@LAPP-V576 ~]# consul version
Consul v1.0.7
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
4、consul服务查询和post 手工模拟注册服务
手工注册一个服务:
curl -X PUT -d '' http://172.29.3.164:8500/v1/catalog/register
查看当前的node
[root@LAPP-V454 bin]# curl 172.29.3.164:8500/v1/catalog/nodes
[{"ID":"","Node":"client1","Address":"172.29.3.164","Datacenter":"dc1","TaggedAddresses":null,"Meta":null,"CreateIndex":241,"ModifyIndex":471},{"ID":"601cd493-489e-3efa-f6b7-fa7fa1e95caa","Node":"server1","Address":"172.29.2.65","Datacenter":"dc1","TaggedAddresses":{"lan":"172.29.2.65","wan":"172.29.2.65"},"Meta":{"consul-network-segment":""},"CreateIndex":5,"ModifyIndex":6}][root@LAPP-V454 bin]#
查看服务:
[root@LAPP-V454 bin]# dig @172.29.3.164 -p 8600 test01.node.consul SRV
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.17.rc1.el6_4.6 <<>> @172.29.3.164 -p 8600 test01.node.consul SRV
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62327
;; flags: qr aa rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available
;; QUESTION SECTION:
;test01.node.consul. IN SRV
;; Query time: 0 msec
;; SERVER: 172.29.3.164#8600(172.29.3.164)
;; WHEN: Wed Oct 16 16:56:59 2019
;; MSG SIZE rcvd: 36
5、consul服务注册、健康检查和查询
{
"ID": "userServiceId", //服务id
"Name": "userService", //服务名
"Tags": [ //服务的tag,自定义,可以根据这个tag来区分同一个服务名的服务
"primary",
"v1"
],
"Address": "127.0.0.1",//服务注册到consul的IP,服务发现,发现的就是这个IP
"Port": 8000, //服务注册consul的PORT,发现的就是这个PORT
"EnableTagOverride": false,
"Check": { //健康检查部分
"DeregisterCriticalServiceAfter": "90m",
"HTTP": "http://www.baidu.com", //指定健康检查的URL,调用后只要返回20X,consul都认为是健康的
"Interval": "10s" //健康检查间隔时间,每隔10s,调用一次上面的URL
}
}
启动本机上的tomcat
保证
http://172.29.3.164:8080 返回200
注册服务
curl -X PUT -d '{"Datacenter": "dc1","Service": {"Service": "test01", "tags": ["dev"],"address": "172.29.3.164", "Port": 8080
"checks": [ {
"http": "http://172.29.3.164:8080",
"interval": "5s"
}
}}' http://172.29.3.164:8500/v1/catalog/register
查找服务:
curl http://172.29.2.65:8500/v1/catalog/service/test01
[{"ID":"e2f76ac4-4086-f85e-8eeb-d67c7e46e30a","Node":"client1","Address":"172.29.3.164","Datacenter":"dc1","TaggedAddresses":{"lan":"172.29.3.164","wan":"172.29.3.164"},"NodeMeta":{"consul-network-segment":""},"ServiceID":"test01","ServiceName":"test01","ServiceTags":["ceis","font"],"ServiceAddress":"172.29.3.164","ServiceMeta":{},"ServicePort":8080,"ServiceEnableTagOverride":false,"CreateIndex":749,"ModifyIndex":749}][root@LAPP-V454 yyapp]#
返回结果测试:
6、consul 常用其他
注销微服务
curl -s -X PUT "http://$CONSUL_NODE/v1/agent/service/maintenance/$SERVER_ID?enable=true&reason=Deploy_New_Versiont"
sleep 30
curl -s -X PUT "http://$CONSUL_NODE/v1/agent/service/deregister/$SERVER_ID"
curl -s -X PUT "http://172.29.2.65:8500/v1/agent/service/maintenance/test01?enable=true&reason=Deploy_New_Versiont"
curl -s -X PUT "http://172.29.2.65:8500/v1/agent/service/deregister/test01"
测试效果图:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章