002-consul使用技巧

consul 使用技巧

查看全部註冊的服務html

consul catalog services

配置consul.jsonnode

{
 "server": true,
 "datacenter": "testgame",
 "client_addr": "0.0.0.0",
 "advertise_addr":"192.168.83.70",
 "bootstrap_expect": 1,
 "enable_syslog": true,
 "enable_script_checks": true,
 "data_dir": "/usr/local/consul/data",
 "node_name": "consul01",
 "ui":true,
 "recursors" : ["8.8.8.8"],
 "ports" : {
   "dns" : 53,
   "http": 8500,
   "server": 8300
 }
}

啓動python

/usr/local/consul/consul agent --config-dir=/usr/local/consul/conf/consul.json -ui

提示:若是想添加服務,則修改啓動命令,並在conf/目錄下添加json配置文件web

添加json文件
echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' >/usr/local/consul/conf/web.json
啓動
/usr/local/consul/consul agent --config-dir=/usr/local/consul/conf/ -ui
查看服務
[root@70 ~]#  consul catalog services
consul
web
經過dns查詢
dig @127.0.0.1 -p 8600 web.service.consul
另外一種查詢方式
curl -s 127.0.0.1:8500/v1/catalog/service/web |python -m json.tool

健康檢查

能夠對本機的服務的特定端口進行檢查,也能夠對其餘機器的端口進行檢查json

[root@70 conf]# cat 80.json 
{"service": {
    "name": "Faceid",
    "tags": ["extract", "verify", "compare", "idcard"],
    "address": "192.168.83.80",
    "port": 80,
    "check": {
        "id": "api",
           "name": "HTTP API on port 80",
        "http": "http://192.168.83.80:80",
        "interval": "10s",
        "timeout": "1s"
        }
   }
}
[root@70 conf]# cat health.json 
{"service": {
    "name": "checkfor8500",
    "tags": ["extract", "verify", "compare", "idcard"],
    "address": "192.168.83.70",
    "port": 8500,
    "check": {
        "id": "api",
           "name": "HTTP API on port 8500",
        "http": "http://localhost:8500",
        "interval": "10s",
        "timeout": "1s"
        }
   }
}
[root@70 conf]# cat web.json 
{"service": {"name": "web", "tags": ["rails"], "port": 80}}

consul中systemdbootstrap

[Unit]
Description=consul-agent
After=network-online.target

[Service]
User=root
Group=root
ExecStart=/bin/sh -c '/usr/local/consul/consul agent --config-dir=/usr/local/consul/conf/consul.json -ui'
Restart=on-failure
KillSignal=SIGINT

[Install]
WantedBy=multi-user.target

K/V

Consul提供了一個易用的鍵/值存儲.這能夠用來保持動態配置,協助服務協調,領袖選舉,作開發者能夠想到的任何事情 能夠手動在頁面進行添加k/v 能夠請求api

[root@70 conf]# curl -v http://localhost:8500/v1/kv/?recurse |python -m json.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* About to connect() to localhost port 8500 (#0)
*   Trying ::1...
* Connected to localhost (::1) port 8500 (#0)
> GET /v1/kv/?recurse HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8500
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: application/json
< Vary: Accept-Encoding
< X-Consul-Index: 405
< X-Consul-Knownleader: true
< X-Consul-Lastcontact: 0
< Date: Sat, 14 Dec 2019 10:56:28 GMT
< Content-Length: 237
< 
{ [data not shown]
100   237  100   237    0     0  39751      0 --:--:-- --:--:-- --:--:-- 47400
* Connection #0 to host localhost left intact
[
    {
        "CreateIndex": 405,
        "Flags": 0,
        "Key": "50",
        "LockIndex": 0,
        "ModifyIndex": 405,
        "Value": "cHl0aG9uCm15c3FsIDEyMzQ1NgpodHRwczovL3llYXN5LmdpdGJvb2tzLmlvL2RvY2tlcl9wcmFjdGljZS9yZXBvc2l0b3J5L3JlZ2lzdHJ5Lmh0bWwKaGF2YSBhcG9sbG8gZm9sZGVyIApkb2NrZXI="
    }
]

上傳k/vapp

curl -X PUT -d 'test' http://localhost:8500/v1/kv/web/key1
curl -X PUT -d 'test' http://localhost:8500/v1/kv/web/key2?flags=42
curl -X PUT -d 'test'  http://localhost:8500/v1/kv/web/sub/key3

查看結果curl

[root@70 conf]# curl -s http://localhost:8500/v1/kv/web/?recurse | python -m json.tool
[
    {
        "CreateIndex": 952,
        "Flags": 0,
        "Key": "web/key1",
        "LockIndex": 0,
        "ModifyIndex": 952,
        "Value": "dGVzdA=="
    },
    {
        "CreateIndex": 953,
        "Flags": 42,
        "Key": "web/key2",
        "LockIndex": 0,
        "ModifyIndex": 953,
        "Value": "dGVzdA=="
    },
    {
        "CreateIndex": 954,
        "Flags": 0,
        "Key": "web/sub/key3",
        "LockIndex": 0,
        "ModifyIndex": 954,
        "Value": "dGVzdA=="
    }
]
獲取單個key
curl -s http://localhost:8500/v1/kv/web/key1 | python -m json.tool

刪除key 刪除key也很簡單.經過DELETE動做來完成.咱們能夠經過指定完整路徑來刪除一個單獨的key.或者咱們能夠使用?recurse遞歸的刪除主路徑下全部keyide

curl -X DELETE http://localhost:8500/v1/kv/web/sub?recurse

能夠經過發送相同的URL並提供不一樣的消息體的PUT請求去修改一個Key.另外,Consul提供一個檢查並設置的操做,實現原子的Key修改.經過?cas=參數加上GET中最近的ModifyIndex來達到. 例如咱們想修改 「web/key1」:

curl -X PUT -d 'newval' http://localhost:8500/v1/kv/web/key1?cas=502660
true
curl -X PUT -d 'newval' http://localhost:8500/v1/kv/web/key1?cas=502660
false

在這種狀況下,第一次CAS 更新成功由於ModifyIndex是502660.而第二次失敗是由於ModifyIndex在第一次更新後已經不是502660了 咱們也能夠使用ModifyIndex來等待key值的改變.例如咱們想等待key2被修改:

curl "http://localhost:8500/v1/kv/web/key2"
[{"LockIndex":0,"Key":"web/key2","Flags":42,"Value":"dGVzdA==","CreateIndex":502663,"ModifyIndex":502663}]
curl "http://localhost:8500/v1/kv/web/key2?index=502663&wait=5s"
[{"LockIndex":0,"Key":"web/key2","Flags":42,"Value":"dGVzdA==","CreateIndex":502663,"ModifyIndex":502663}]

經過提供 ?index=,咱們請求等待key值有一個比502663更大的ModifyIndex.雖然?wait=5s參數限制了這個請求最多5秒,不然返回當前的未改變的值. 這樣能夠有效的等待key的改變.另外,這個功能能夠用於等待一組key.直到其中的某個key有修改

參考文檔:

https://book-consul-guide.vnzmi.com/11_consul_template.html http://www.manongzj.com/blog/5-ntuzvtcyqxnkkmm.html http://www.javashuo.com/article/p-dknumsnv-cc.html

相關文章
相關標籤/搜索