【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
consul 使用技巧
查看所有注册的服务
consul catalog services
配置consul.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
}
}
启动
/usr/local/consul/consul agent --config-dir=/usr/local/consul/conf/consul.json -ui
提示:如果想添加服务,则修改启动命令,并在conf/目录下添加json配置文件
添加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
健康检查
可以对本机的服务的特定端口进行检查,也可以对其他机器的端口进行检查
[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中systemd
[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 可以请求
[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/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
查看结果
[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递归的删除主路径下所有key
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 https://blog.csdn.net/liuzhuchen/article/details/81913562
来源:oschina
链接:https://my.oschina.net/u/3635512/blog/3143058