文档
Consul 入门教程:服务网格与多数据中心
1. Consul vs ZooKeeper vs etcd
| 维度 | Consul | ZooKeeper | etcd |
|---|---|---|---|
| 协议 | Raft + Gossip | ZAB | Raft |
| 服务发现 | 原生 DNS/HTTP API | 需框架封装 | 需框架封装 |
| 健康检查 | 丰富(TCP/HTTP/gRPC/Script) | 仅 Session 超时 | 租约(Lease) |
| KV Store | ✅ | ❌(弱支持) | ✅ |
| 多数据中心 | ✅ 原生 | ❌ | ❌ |
| 语言 | Go | Java | Go |
2. 健康检查深度
{
"check": {
"http": "http://localhost:8080/health", // HTTP
"tcp": "localhost:3306", // TCP
"grpc": "localhost:50051", // gRPC
"args": ["/usr/local/bin/check.sh"], // Script
"interval": "10s",
"timeout": "3s"
}
}
健康检查状态:passing → warning → critical。只有 passing 的服务被 DNS/HTTP 返回。
3. DNS 接口
# 查询服务地址
dig @localhost -p 8600 order-service.service.consul
# 按标签过滤
dig @localhost -p 8600 v1.order-service.service.consul
# SRV 记录(含端口)
dig @localhost -p 8600 order-service.service.consul SRV
4. Consul Connect(Service Mesh)
启用 mTLS 和鉴权的服务间通信:
# Envoy Sidecar 代理
service:
name: order-service
connect:
sidecar_service:
proxy:
upstreams:
- destination_name: payment-service
local_bind_port: 5000
# 意图(Intentions):访问控制
consul intention create order-service payment-service allow
5. 多数据中心架构
Datacenter "us-east" ←→ WAN Gossip ←→ Datacenter "eu-west"
│ │
[Service A] [Service A]
# 加入 WAN
consul join -wan <remote-server-ip>
# 查询远程服务
curl http://localhost:8500/v1/catalog/service/service-a?dc=eu-west
6. 思考题
- Consul 的 Raft 和 Gossip 协议分别负责什么?
- Consul Connect 和 Istio 的架构有何不同?各自优劣?
- 多数据中心环境下如何保证配置最终一致性?