Consul
Consul 是 HashiCorp 开源的 CP 型服务网格与注册中心,使用 Raft 保证强一致性,原生支持多数据中心、健康检查、KV 存储和服务网格(mTLS),适合多语言、多云环境。
核心功能
| 功能 | 说明 |
|---|
| 服务注册与发现 | DNS / HTTP API 双接口 |
| 健康检查 | TCP / HTTP / Script / gRPC / TTL |
| KV 存储 | 分布式配置、特性开关、Leader 选举 |
| 多数据中心 | WAN Gossip 联邦多 DC |
| Service Mesh | 内置 Envoy Sidecar 代理,mTLS 加密 |
| ACL | Token 访问控制 |
整体架构
DataCenter A DataCenter B
┌──────────────────────┐ ┌──────────────────────┐
│ Consul Server(3/5) │◄──────►│ Consul Server(3/5) │
│ Raft Leader + 2Peer │ WAN │ Raft Leader + 2Peer │
└──────────┬───────────┘ Gossip └──────────────────────┘
│ LAN Gossip
┌────────┴────────────────┐
│ Consul Agent(每个节点)│
│ ├── 本地健康检查 │
│ ├── 转发请求到 Server │
│ └── DNS :8600 │
└─────────────────────────┘
| 节点角色 | 职责 |
|---|
| Server | 存储注册表,参与 Raft 选举,处理查询 |
| Client(Agent) | 部署在每台服务器,本地健康检查,转发 RPC |
一致性模型(CAP = CP)
Consul 默认使用 Raft,写操作需 Leader 确认并复制到多数节点才返回:
| 一致性级别 | 说明 | 参数 |
|---|
| default | 可能读到旧数据(Leader 租约) | 默认 |
| consistent | 强一致,每次读都走 Leader | ?consistent |
| stale | 任意节点响应,性能最高,数据最旧 | ?stale |
服务注册
配置文件注册
# /etc/consul.d/web.hcl
service {
name = "order-service"
id = "order-service-1"
port = 8080
tags = ["v1", "production"]
check {
http = "http://localhost:8080/actuator/health"
interval = "10s"
timeout = "3s"
}
}
HTTP API 注册
curl -X PUT http://localhost:8500/v1/agent/service/register \
-H "Content-Type: application/json" \
-d '{
"Name": "order-service",
"Port": 8080,
"Check": {
"HTTP": "http://localhost:8080/health",
"Interval": "10s"
}
}'
Spring Cloud 集成
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
spring:
application:
name: order-service
cloud:
consul:
host: 127.0.0.1
port: 8500
discovery:
instance-id: ${spring.application.name}-${server.port}
health-check-path: /actuator/health
health-check-interval: 10s
prefer-ip-address: true
config:
enabled: true
prefix: config # KV 路径前缀
default-context: application
data-key: data # KV 中存 YAML 的 key
KV 存储
# 写入配置
consul kv put config/order-service/data '{"timeout": 5000}'
# 读取
consul kv get config/order-service/data
# Watch 变更(长轮询)
consul watch -type=key -key=config/order-service/data cat
DNS 服务发现
# 查询 order-service 的地址(默认端口 8600)
dig @127.0.0.1 -p 8600 order-service.service.consul
# 指定数据中心
dig @127.0.0.1 -p 8600 order-service.service.dc1.consul
# 带 tag 过滤
dig @127.0.0.1 -p 8600 v1.order-service.service.consul
ACL 访问控制
# 创建策略
consul acl policy create -name "order-policy" \
-rules 'service "order-service" { policy = "write" }'
# 创建 Token
consul acl token create -policy-name "order-policy"
高可用部署
生产环境推荐 3 或 5 个 Server 节点(奇数):
3 节点:容忍 1 节点故障
5 节点:容忍 2 节点故障
consul agent -server \
-bootstrap-expect=3 \
-bind=192.168.1.1 \
-data-dir=/data/consul \
-ui \
-retry-join=192.168.1.2 \
-retry-join=192.168.1.3
与 Nacos / Eureka 对比
| 特性 | Consul | Nacos | Eureka |
|---|
| CAP | CP | AP/CP | AP |
| 配置中心 | KV 存储 | 专用配置中心 | 无 |
| 多数据中心 | 原生 | 支持 | 弱 |
| 健康检查 | 丰富(多种探针) | 心跳 + 主动 | 心跳 |
| 服务网格 | 内置(Connect) | 无 | 无 |
| 语言无关 | 是 | 主要 Java | 主要 Java |
相关链接