以下列出的CLI命令和API接口,在日常运维和排查客户问题过程中,是非常常用的, 列出以作记录。
参考文档: https://www.consul.io/api-docs
转载请注明🙂,喜欢请一键三连哦 😊
文章目录
一、Consul 常用CLI
1.1 查看服务列表
consul catalog services
1.2 注销服务
consul services deregister -id=:service-id
// :service-id 为用户服务Id
1.3 查看节点成员
consul members
1.4 查看主节点
配合1.3可确认主节点
consul info
二、Consul 常用API
备注 : <:serviceName> 请替换为要查询的服务名称, 如: spring-cloud-provider:serviceName
2.1 注册服务
payload.json
====
{
"ID": "spring-cloud-provider-8080",
"Name": "spring-cloud-provider",
"Tags": ["primary", "v1"],
"Address": "127.0.0.1",
"Port": 8080,
"EnableTagOverride": false,
"Check": {
"DeregisterCriticalServiceAfter": "1m",
"Interval": "10s",
"Timeout": "5s"
"Tcp":"127.0.0.1:8080"
},
"Weights": {
"Passing": 10,
"Warning": 1
}
}
curl \
--request PUT \
--data @payload.json \
http://127.0.0.1:8500/v1/agent/service/register?replace-existing-checks=true
2.2 查看所有注册服务列表
curl 127.0.0.1:8500/v1/catalog/services
2.3 查看服务实例状态(最佳接口)
- 查看健康检查通过的
curl http://127.0.0.1:8500/v1/health/checks/:serviceName?passing |python -m json.tool
- 查看全部
curl http://127.0.0.1:8500/v1/health/checks/:serviceName | python -m json.tool
2.4 查询服务实例列表
- 健康实例(会拉取到未设置check的service)
curl http://127.0.0.1:8500/v1/health/service/:serviceName?passing | python -m json.tool
- 查看全部
curl http://127.0.0.1:8500/v1/health/service/:serviceName | python -m json.tool
2.5 查询注册节点情况
curl http://127.0.0.1:8500/v1/catalog/service/:serviceName | python -m json.tool
2.6 查看Consul集群成员 (常用来测试网络联通性)
curl http://127.0.0.1:8500/v1/agent/members | python -m json.tool
2.7 查询Consul集群Leader(常用来判断集群状态)
curl http://127.0.0.1:8500/v1/status/leader