Consul 服务发现与配置中心
Consul 是 HashiCorp 推出的服务网络解决方案,提供服务发现、配置管理、健康检查和服务网格等功能。在微服务架构中,Consul 可以让各个服务自动注册和发现彼此,无需硬编码地址。本文将介绍如何在搬瓦工 VPS 上部署和使用 Consul。
一、Consul 核心功能
- 服务发现:服务自动注册,其他服务通过 DNS 或 HTTP API 发现。
- 健康检查:持续监控服务健康状态,自动剔除不健康的实例。
- KV 存储:分布式键值存储,用于配置管理和 Leader 选举。
- 服务网格:通过 Sidecar 代理实现服务间的安全通信。
二、安装 Consul
2.1 包管理器安装
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/hashicorp.list
apt update
apt install consul -y
2.2 验证安装
consul version
2.3 Docker 安装
docker run -d \
--name consul \
--restart=always \
-p 8500:8500 \
-p 8600:8600/udp \
-v consul_data:/consul/data \
hashicorp/consul:latest agent -server -bootstrap-expect=1 -ui -client=0.0.0.0
三、启动 Consul Server
3.1 创建配置文件
mkdir -p /etc/consul.d /opt/consul/data
cat > /etc/consul.d/server.hcl <<EOF
datacenter = "dc1"
data_dir = "/opt/consul/data"
node_name = "consul-server-1"
server = true
bootstrap_expect = 1
ui_config {
enabled = true
}
client_addr = "0.0.0.0"
bind_addr = "{{ GetInterfaceIP \"eth0\" }}"
connect {
enabled = true
}
ports {
grpc = 8502
}
EOF
3.2 启动服务
consul agent -config-dir=/etc/consul.d/
或配置为系统服务:
cat > /etc/systemd/system/consul.service <<EOF
[Unit]
Description=Consul Agent
After=network.target
[Service]
ExecStart=/usr/bin/consul agent -config-dir=/etc/consul.d/
ExecReload=/bin/kill -HUP \$MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl enable consul
systemctl start consul
3.3 访问 Web UI
浏览器访问 http://你的IP:8500 即可看到 Consul 管理界面。
四、服务注册
4.1 配置文件注册
cat > /etc/consul.d/web-service.hcl <<EOF
service {
name = "web"
port = 80
tags = ["nginx", "production"]
check {
http = "http://localhost:80/health"
interval = "10s"
timeout = "3s"
}
}
EOF
consul reload
4.2 HTTP API 注册
curl --request PUT \
--data '{
"ID": "api-1",
"Name": "api",
"Port": 8080,
"Tags": ["v1", "production"],
"Check": {
"HTTP": "http://localhost:8080/health",
"Interval": "10s"
}
}' \
http://localhost:8500/v1/agent/service/register
4.3 Docker 容器自动注册
使用 Registrator 实现 Docker 容器自动注册到 Consul:
docker run -d \
--name registrator \
--net=host \
--volume=/var/run/docker.sock:/tmp/docker.sock \
gliderlabs/registrator:latest \
consul://localhost:8500
五、服务发现
5.1 HTTP API 查询
# 查看所有服务
curl http://localhost:8500/v1/catalog/services
# 查询特定服务
curl http://localhost:8500/v1/catalog/service/web
# 只查询健康的实例
curl http://localhost:8500/v1/health/service/web?passing=true
5.2 DNS 查询
# 查询服务地址
dig @localhost -p 8600 web.service.consul
# 查询 SRV 记录(包含端口信息)
dig @localhost -p 8600 web.service.consul SRV
# 通过标签过滤
dig @localhost -p 8600 production.web.service.consul
六、KV 存储
# 写入配置
consul kv put config/database/host 192.168.1.20
consul kv put config/database/port 3306
consul kv put config/database/name myapp
# 读取配置
consul kv get config/database/host
# 列出所有键
consul kv get -recurse config/
# 删除键
consul kv delete config/database/host
# 导出配置
consul kv export config/ > config-backup.json
# 导入配置
consul kv import @config-backup.json
七、使用 Docker Compose 部署完整示例
version: '3.8'
services:
consul:
image: hashicorp/consul:latest
container_name: consul
restart: always
ports:
- "8500:8500"
- "8600:8600/udp"
command: agent -server -bootstrap-expect=1 -ui -client=0.0.0.0
volumes:
- consul_data:/consul/data
networks:
- consul-net
web:
image: nginx:alpine
networks:
- consul-net
deploy:
replicas: 2
registrator:
image: gliderlabs/registrator:latest
volumes:
- /var/run/docker.sock:/tmp/docker.sock
command: consul://consul:8500
depends_on:
- consul
networks:
- consul-net
volumes:
consul_data:
networks:
consul-net:
八、ACL 安全配置
# 启用 ACL(在 server.hcl 中添加)
acl {
enabled = true
default_policy = "deny"
enable_token_persistence = true
}
# 初始化 ACL 系统
consul acl bootstrap
使用生成的 Master Token 进行管理操作,为不同服务创建不同权限的 Token。
九、常见问题
服务健康检查失败
consul members
curl http://localhost:8500/v1/health/service/web
检查健康检查的 URL 和端口是否正确。
节点无法加入集群
consul join LEADER_IP
确保防火墙开放了 8301/tcp+udp(Serf LAN)和 8300/tcp(Server RPC)端口。
KV 数据丢失
确保 data_dir 目录配置正确且有持久化存储,定期使用 consul snapshot save 备份数据。
总结
Consul 为微服务架构提供了完整的服务发现和配置管理方案。搭配 Traefik 可以实现自动化的服务路由,搭配 Vault 可以实现安全的密钥管理。选购搬瓦工 VPS 请参考 全部方案,使用优惠码 NODESEEK2026 享受 6.77% 折扣。