1. 程式人生 > 其它 >consul-常用命令

consul-常用命令

1、consul 是B/C架構。服務端和客戶端包是一樣的。差別在於啟動時候的引數。

1 2 3 4 5 --客戶端 ./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  <all>
client1  172.29.3.164:8301  alive   client  1.0.7  2         dc1  <default>


產生公用的祕鑰:

[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"

# 等待30秒
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"

測試效果圖: