$ consul members [-detailed] # 输出信息包括服务的状态、类型、版本、协议等的 # Node Address Status Type Build Protocol DC Segment # apollo 127.0.0.1:8301 alive server 1.3.0 2 dc1 <all>
# DNS API # 通过服务名字 [TAG.]NAME.service.consul,默认情况下在 consul 命名空间下,也可以配置 $ dig @127.0.0.1 -p 8600 web.service.consul # 也可以获取服务/端口 $ dig @127.0.0.1 -p 8600 web.service.consul SRV # 还可以根据 tag 定位服务 $ dig @127.0.0.1 -p 8600 nginx.web.service.consul SRV # HTTP API $ curl http://127.0.0.1:8500/v1/catalog/service/web
更新服务
可以发送通过 SIGHUP 信号给服务重新加载服务配置。
三、连接服务
四、健康检查
五、KV数据
除了提供服务发现、集成健康检查意外,Consul 还提供了一种便捷地使用 KV Store 的方法。可以通过 CLI 和 HTTP API 两种方式。 注:1. 值的大小不能大于512KB ; 2. 更新多个键,可以考虑使用事务。
增加
1 2 3 4
$ consul kv get mysql/port Error! No key exists at: mysql/port $ consul kv put mysql/port 3306 Success! Data written to: mysql/port
读取
1 2 3 4 5 6 7 8 9 10 11 12
$ consul kv get mysql/port 3306 $ consul kv get -detailed mysql/port # CreateIndex 134 # Flags 0 # Key mysql/port # LockIndex 0 # ModifyIndex 134 # Session - # Value 3306 # 获取所有KV $ consul kv get -recurse
更新
1 2 3 4 5 6 7 8
$ consul kv put mysql/port 3307 Success! Data written to: mysql/port # 原子更新 [Check-And-Set] -cas $ consul kv put -cas -modify-index=123 foo bar Success! Data written to: foo
$ consul kv put -cas -modify-index=123 foo bar Error! Did not write to foo: CAS failed
删除
1 2
$ consul kv delete mysql/port $ consul kv delete -recurse mysql