Appearance
Consul 详解:服务注册与配置中心
Spring Cloud Consul 是 HashiCorp Consul 的 Spring 集成,提供服务注册发现和配置管理两大核心能力。在 Spring Cloud 官方生态中,它是 Eureka 的替代方案,也是 Nacos 之外的另一选择。
一、Consul 是什么?
Consul 是 HashiCorp 出品的分布式服务网格解决方案,提供:
┌──────────────────────────────────────────────────────────────────┐
│ Consul 功能全景 │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ 服务发现 │ │ 配置管理 │ │ 健康检查 │ │
│ ├──────────────┤ ├──────────────┤ ├──────────────┤ │
│ │ 服务注册/注销 │ │ KV 存储 │ │ HTTP/TCP/gRPC│ │
│ │ DNS/HTTP API │ │ 动态配置 │ │ 自动注销 │ │
│ │ 多数据中心 │ │ Watch 机制 │ │ 熔断标记 │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ 安全通信 │ │ 多数据中心 │ │
│ ├──────────────┤ ├──────────────┤ │
│ │ mTLS │ │ WAN 联邦 │ │
│ │ ACL 权限 │ │ 跨机房同步 │ │
│ │ Gossip 加密 │ │ 故障隔离 │ │
│ └──────────────┘ └──────────────┘ │
└──────────────────────────────────────────────────────────────────┘二、核心架构
┌──────────────────────────────────────────────────────────────────┐
│ Consul 集群 │
│ │
│ ┌────────────┐ ┌────────────┐ ┌────────────┐ │
│ │ Server 1 │ │ Server 2 │ │ Server 3 │ ← 3-5 台 Server │
│ │ (Leader) │ │ (Follower) │ │ (Follower) │ Raft 共识 │
│ └─────┬──────┘ └─────┬──────┘ └─────┬──────┘ │
│ │ │ │ │
│ └───────────────┼───────────────┘ │
│ │ Gossip 协议(Serf) │
│ ▼ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Agent │ │ Agent │ │ Agent │ │ Agent │ │
│ │ (Client) │ │ (Client) │ │ (Client) │ │ (Client) │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
│ │ │ │ │ │
│ ▼ ▼ ▼ ▼ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ order- │ │ user- │ │ inventory│ │ payment- │ │
│ │ service │ │ service │ │ -service │ │ service │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
└──────────────────────────────────────────────────────────────────┘关键角色:
- Server:存储数据,处理查询,Raft 共识选举 Leader
- Agent:每个节点上运行的 Consul 进程,转发请求到 Server
- Client:运行在微服务同机,注册服务、健康检查
三、快速上手
3.1 启动 Consul
bash
# 开发模式
consul agent -dev
# 生产模式
consul agent -server -bootstrap-expect=3 -data-dir=/tmp/consul3.2 依赖
xml
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>3.3 服务注册配置
yaml
spring:
application:
name: order-service
cloud:
consul:
host: localhost
port: 8500
discovery:
enabled: true
register: true # 启动时注册
deregister: true # 关闭时注销
health-check-path: /actuator/health
health-check-interval: 10s
health-check-critical-timeout: 30s
instance-id: ${spring.application.name}:${random.value}
prefer-ip-address: true # 优先使用 IP 注册
tags:
- version=1.0
- env=dev3.4 配置管理
yaml
spring:
cloud:
consul:
config:
enabled: true
prefix: config # KV 路径前缀
default-context: application # 默认上下文
format: YAML # 配置格式
watch:
enabled: true # 自动监听配置变更
delay: 1000 # 轮询间隔配置读取路径:
Consul KV 存储:
config/application/data → 所有应用共享的配置
config/application,dev/data → 开发环境共享配置
config/order-service/data → 订单服务配置
config/order-service,dev/data → 订单服务开发环境配置3.5 动态刷新
java
@RestController
@RefreshScope // 配置变更时自动刷新
public class OrderController {
@Value("${order.limit:100}")
private int limit;
@GetMapping("/limit")
public String getLimit() {
return "当前限流阈值:" + limit;
}
}四、健康检查
Consul 支持多种健康检查方式:
yaml
# HTTP 健康检查(最常用)
health-check-path: /actuator/health
health-check-interval: 10s
# TCP 健康检查
health-check-tcp: localhost:8080
# 自定义脚本检查
health-check-script: /usr/bin/check.sh健康检查流程:
1. Consul Agent 定期请求 health-check-path
2. 连续失败 → 标记为 critical
3. 服务从可用列表中移除,不再分配流量
4. 恢复后自动重新加入五、Consul vs Nacos vs Eureka
| 维度 | Consul | Nacos | Eureka |
|---|---|---|---|
| CAP 模型 | CP(强一致性) | AP + CP(可切换) | AP(高可用) |
| 配置中心 | ✅(KV 存储) | ✅(内置) | ❌(需配合 Config) |
| 健康检查 | ✅(HTTP/TCP/gRPC/Script) | ✅(HTTP/TCP/MySQL) | ✅(HTTP 心跳) |
| 多数据中心 | ✅(原生支持) | ✅(支持) | ✅(多 Region) |
| 控制台 | ✅(Web UI) | ✅(Web UI) | ❌(Dashboard 简陋) |
| 一致性协议 | Raft | Raft + Distro | Peer to Peer |
| 动态配置刷新 | ✅(Watch 机制) | ✅(自动推送) | ❌ |
| 学习成本 | 中 | 低 | 低 |
| 适用场景 | 多数据中心、非阿里云 | 阿里云生态 | 停维,不推荐新项目 |
选型建议:
纯 Spring Cloud,需要多数据中心 → Consul
Spring Cloud Alibaba 生态 → Nacos
老项目维护 → Eureka(仅维护)
需要强一致性(CP) → Consul
需要高可用 + 配置中心 + 易用 → Nacos六、面试要点
Q1:Consul 的 CAP 模型是什么?
Consul 是 CP 模型(强一致性 + 分区容错)。使用 Raft 共识算法保证数据一致性,在网络分区时优先保证一致性,可能短暂牺牲可用性。Nacos 支持 AP/CP 切换,Eureka 是 AP。
Q2:Consul 的健康检查有哪几种?
HTTP(检查 HTTP 端点)、TCP(检查端口连通性)、gRPC(gRPC 健康检查协议)、Script(自定义脚本)、Docker(容器检查)。最常用的是 HTTP 健康检查。
Q3:Consul 和 Nacos 怎么选?
需要多数据中心选 Consul(原生支持 WAN 联邦);需要 Web 控制台、易用性、配置自动推送选 Nacos;两者功能高度重叠,在 Spring Cloud 生态中按团队熟悉度和基础设施选。
Q4:Consul 的 Watch 机制是什么?
Watch 机制允许客户端监听 Consul 中 KV 存储的变化,配置变更时自动通知应用刷新。实现了配置的热更新,无需手动触发。
