Consul:服务发现与健康检查,微服务架构的注册中心
微服务架构里,服务 A 要调用服务 B,但 B 的 IP 随时可能变(容器重启、扩缩容)------这就是服务发现要解决的问题。Consul 是 HashiCorp 出品的服务注册与发现工具,还集成了健康检查、KV 存储、Service Mesh 功能,是 Docker/K8s 之外最常用的微服务基础设施组件之一。
Consul 核心功能
- 服务注册与发现:服务启动时注册到 Consul,其他服务通过 Consul 查询地址
- 健康检查:Consul 定期检测服务是否健康,自动从注册表移除故障实例
- KV 存储:轻量级配置中心,存储配置数据
- DNS 接口 :通过 DNS 查询服务地址(
service.consul域名) - 多数据中心:跨机房的服务发现和流量管理
服务器配置
- 单节点(开发/测试):1 核 1GB
- 生产集群(高可用):3 节点或 5 节点,每节点 2 核 2GB
Consul 推荐奇数节点集群,3 节点可以容忍 1 个节点故障,5 节点可以容忍 2 个。
我把 3 节点 Consul 集群部署在雨云服务器 rainyun+com 的 3 台 2 核 2G 机型上,注册填码 2026off 领 5 折券,3 台服务器组集群性价比很高。
单节点 Docker 部署(开发环境)
bash
mkdir -p ~/consul/{config,data}
yaml
# docker-compose.yml
version: "3.8"
services:
consul:
image: consul:1.17
container_name: consul
command: agent -server -bootstrap-expect=1 -ui -client=0.0.0.0
environment:
CONSUL_BIND_INTERFACE: eth0
ports:
- "8500:8500" # HTTP API + Web UI
- "8600:8600/udp" # DNS
- "8301:8301" # LAN gossip
- "8302:8302" # WAN gossip
volumes:
- ./config:/consul/config
- consul_data:/consul/data
restart: unless-stopped
volumes:
consul_data:
bash
docker compose up -d
# 查看 Web UI
# http://服务器IP:8500
生产集群部署(3节点)
在 3 台服务器上分别运行:
bash
# 所有节点统一配置
cat > /etc/consul.d/consul.json << EOF
{
"datacenter": "dc1",
"data_dir": "/opt/consul",
"log_level": "INFO",
"node_name": "consul-01", // 每台服务器改为不同名字
"server": true,
"bootstrap_expect": 3,
"bind_addr": "{{ GetInterfaceIP \"eth0\" }}",
"client_addr": "0.0.0.0",
"retry_join": [
"consul-01-IP",
"consul-02-IP",
"consul-03-IP"
],
"ui_config": {
"enabled": true
},
"acl": {
"enabled": true,
"default_policy": "deny",
"enable_token_persistence": true
}
}
EOF
sudo systemctl enable --now consul
服务注册
方式一:配置文件注册
bash
# 在需要注册的服务所在节点
cat > /etc/consul.d/web-api.json << 'EOF'
{
"service": {
"name": "web-api",
"id": "web-api-01",
"port": 8080,
"tags": ["production", "v2"],
"check": {
"http": "http://localhost:8080/health",
"interval": "10s",
"timeout": "3s",
"deregister_critical_service_after": "30s"
}
}
}
EOF
consul reload
方式二:HTTP API 注册
bash
# 注册服务
curl -X PUT http://consul:8500/v1/agent/service/register \
-H "Content-Type: application/json" \
-d '{
"Name": "payment-service",
"ID": "payment-001",
"Port": 9000,
"Tags": ["prod"],
"Check": {
"TCP": "localhost:9000",
"Interval": "10s",
"Timeout": "3s"
}
}'
服务发现
DNS 方式
bash
# 通过 DNS 查询服务地址(需要配置服务器使用 Consul DNS)
dig @consul-server-IP -p 8600 web-api.service.consul SRV
# 结果包含所有健康实例的 IP 和端口
将应用的 DNS 指向 Consul:
bash
# 在 /etc/resolv.conf 里添加
nameserver consul-server-IP
domain service.consul
HTTP API 方式
bash
# 查询服务的所有健康实例
curl http://consul:8500/v1/health/service/web-api?passing=true
# 在 Go 应用里
import "github.com/hashicorp/consul/api"
client, _ := api.NewClient(api.DefaultConfig())
services, _, _ := client.Health().Service("web-api", "", true, nil)
// services[0].Service.Address + services[0].Service.Port → 选一个健康实例
KV 配置存储
bash
# 写入配置
consul kv put myapp/database/host "db.example.com"
consul kv put myapp/database/port "5432"
consul kv put myapp/feature/dark-mode "true"
# 读取
consul kv get myapp/database/host
# 列出所有键
consul kv get -recurse myapp/
# 删除
consul kv delete myapp/feature/dark-mode
# 监听变化(配置热更新)
consul watch -type=key -key=myapp/database/host -shell true \
'echo "配置已更新:$(consul kv get myapp/database/host)"'
健康检查类型
json
{
"checks": [
{"http": "http://localhost:8080/health", "interval": "10s"},
{"tcp": "localhost:3306", "interval": "10s"},
{"script": "/usr/local/bin/health-check.sh", "interval": "30s"},
{"grpc": "localhost:50051", "interval": "10s"},
{"ttl": "30s"} // 应用主动上报健康状态
]
}
查看集群状态
bash
# 查看集群成员
consul members
# 查看 Leader
consul operator raft list-peers
# 查看服务列表
consul catalog services
# 查看特定服务的实例
consul catalog nodes -service web-api
Consul 是微服务架构里最成熟的服务注册中心之一,配置简单、文档完善、社区活跃。在雨云rainyun上部署 3 节点 Consul 集群,服务发现和健康检查一体化,多台服务器集群方案成本可控,是微服务基础设施的可靠选择。