查看全部注册的服务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
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