Tips for using consumer
View all registered services
consul catalog services
Configure consumer.json
{ "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 } }
start-up
/usr/local/consul/consul agent --config-dir=/usr/local/consul/conf/consul.json -ui
Tip: if you want to add a service, modify the startup command and add the json configuration file in the conf / directory
Add to json file echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' >/usr/local/consul/conf/web.json //start-up /usr/local/consul/consul agent --config-dir=/usr/local/consul/conf/ -ui //View service [root@70 ~]# consul catalog services consul web //Query by dns dig @127.0.0.1 -p 8600 web.service.consul //Another query method curl -s 127.0.0.1:8500/v1/catalog/service/web |python -m json.tool
Health examination
You can check the specific port of the local service or the ports of other machines
[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}}
systemd in consumer
[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 provides an easy-to-use key / value store. It can be used to maintain dynamic configuration, assist in service coordination, lead election, and do anything developers can think of You can add k/v manually on the page May request
[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=" } ]
Upload k/v
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
View results
[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==" } ] //Get a single key curl -s http://localhost:8500/v1/kv/web/key1 | python -m json.tool
Delete key Deleting a key is also very simple. It is done through the DELETE action. We can DELETE a single key by specifying a full path. Or we can use recurse to DELETE all keys in the main path
curl -X DELETE http://localhost:8500/v1/kv/web/sub?recurse
You can modify a Key by sending the same URL and providing the PUT requests of different message bodies. In addition, Consul provides an operation to check and set to realize the atomic Key modification. This is achieved by adding the latest ModifyIndex in GET with the? cas = parameter. For example, we want to modify "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
In this case, the first CAS update succeeds because ModifyIndex is 502660. The second CAS update fails because ModifyIndex is no longer 502660 after the first update We can also use ModifyIndex to wait for the key value to change. For example, we want to wait for key2 to be modified:
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}]
By providing? index =, we request a ModifyIndex larger than 502663. Although the? wait=5s parameter limits the request for up to 5 seconds, otherwise the current unchanged value will be returned. This can effectively wait for the change of the key. In addition, this function can be used to wait for a group of keys until one of the keys is modified
Reference documents: https://book-consul-guide.vnzmi.com/11_consul_template.html http://www.manongzj.com/blog/5-ntuzvtcyqxnkkmm.html https://blog.csdn.net/liuzhuchen/article/details/81913562