# 安装 Consul
下载适合你 操作系统的包,它是一个 zip 压缩文件。下载完成后,unzip 解压缩。Consul 只有一个可执行文件 consul
,包里的其他文件都可以删除。把 consul
配置在环境变量 PATH
中。
# 运行 Consul Agent
安装完 Consul 后应该运行 agent,它可以运行在服务端 (server) 或者客户端 (client) 模式下。每个数据中心 (data center) 都至少有一个 server,推荐一个集群 (cluster) 至少有 3 到 5 个 server。因为在故障情况下数据丢失是不可避免的,所以强烈建议不要单机部署。
# 注册服务
# 定义服务
Method | Path | Produces |
---|---|---|
PUT |
/agent/service/register |
application/json |
curl \
--request PUT \
http://192.168.1.6:8500/v1/catalog/service/web
# 查询服务
# DNS API
dig @127.0.0.1 -p 8600 web.service.consul SRV
该 SRV
表示查询该服务运行的端口。
# HTTP API
Method | Path | Produces |
---|---|---|
GET |
/agent/services |
application/json |
curl \
http://192.168.1.6:8500/v1/agent/services
# 注销服务
方法 | 小路 | 生产 |
---|---|---|
PUT |
/agent/service/deregister/:service_id |
application/json |
curl \
--request PUT \
http://127.0.0.1:8500/v1/agent/service/deregister/my-service-id
# API 文档
Consul 提供了 API 和命令行两种模式,通常情况下使用 API,具体可以查看 consul 的 开发者文档。
添加服务 https://developer.hashicorp.com/consul/api-docs/agent/service#register-service
删除服务 https://developer.hashicorp.com/consul/api-docs/agent/service#deregister-service
健康检查 https://developer.hashicorp.com/consul/api-docs/agent/check
列出服务 https://developer.hashicorp.com/consul/api-docs/agent/service#list-services
过滤服务 https://developer.hashicorp.com/consul/api-docs/features/filtering