多数据中心
2026/1/15大约 4 分钟Consul多数据中心WAN
多数据中心
一、多数据中心概述
Consul 原生支持多数据中心部署,实现跨地域的服务发现和配置管理。
1.1 架构设计
1.2 核心概念
| 概念 | 说明 |
|---|---|
| LAN Gossip | 数据中心内部通信 |
| WAN Gossip | 跨数据中心通信 |
| WAN Federation | 数据中心联邦 |
| Mesh Gateway | 网格网关 |
二、配置多数据中心
2.1 数据中心 1 配置
{
"datacenter": "dc1",
"data_dir": "/opt/consul/data",
"server": true,
"bootstrap_expect": 3,
"bind_addr": "10.0.1.10",
"advertise_addr_wan": "公网IP1",
"retry_join": ["10.0.1.11", "10.0.1.12"],
"retry_join_wan": ["公网IP2"]
}2.2 数据中心 2 配置
{
"datacenter": "dc2",
"data_dir": "/opt/consul/data",
"server": true,
"bootstrap_expect": 3,
"bind_addr": "10.0.2.10",
"advertise_addr_wan": "公网IP2",
"retry_join": ["10.0.2.11", "10.0.2.12"],
"retry_join_wan": ["公网IP1"]
}2.3 HCL 配置
# dc1-server.hcl
datacenter = "dc1"
data_dir = "/opt/consul/data"
server = true
bootstrap_expect = 3
bind_addr = "10.0.1.10"
advertise_addr_wan = "公网IP1"
retry_join = ["10.0.1.11", "10.0.1.12"]
retry_join_wan = ["公网IP2"]
ports {
serf_wan = 8302
}三、跨数据中心服务发现
3.1 DNS 查询
# 查询本地数据中心服务
dig @127.0.0.1 -p 8600 user-service.service.consul
# 查询指定数据中心服务
dig @127.0.0.1 -p 8600 user-service.service.dc2.consul
# 查询所有数据中心
dig @127.0.0.1 -p 8600 user-service.service.consul SRV3.2 HTTP API 查询
# 查询本地数据中心
curl http://localhost:8500/v1/catalog/service/user-service
# 查询指定数据中心
curl "http://localhost:8500/v1/catalog/service/user-service?dc=dc2"
# 列出所有数据中心
curl http://localhost:8500/v1/catalog/datacenters3.3 服务发现流程
四、Prepared Query
4.1 创建跨数据中心查询
curl -X POST http://localhost:8500/v1/query \
-d '{
"Name": "user-service-geo",
"Service": {
"Service": "user-service",
"Failover": {
"NearestN": 2,
"Datacenters": ["dc2", "dc3"]
}
}
}'4.2 执行查询
# 使用查询名称
curl http://localhost:8500/v1/query/user-service-geo/execute
# DNS 查询
dig @127.0.0.1 -p 8600 user-service-geo.query.consul4.3 故障转移策略
{
"Name": "user-service-failover",
"Service": {
"Service": "user-service",
"OnlyPassing": true,
"Failover": {
"NearestN": 3,
"Datacenters": ["dc2", "dc3"]
}
}
}五、WAN Federation
5.1 传统 WAN Federation
5.2 Mesh Gateway Federation
5.3 配置 Mesh Gateway
# mesh-gateway.hcl
connect {
enabled = true
}
ports {
grpc = 8502
}
config_entries {
bootstrap {
kind = "mesh"
config {
peering {
peer_through_mesh_gateways = true
}
}
}
}六、数据复制
6.1 KV 复制
Consul 的 KV 存储不会自动跨数据中心复制,需要手动同步或使用 consul-replicate。
# 安装 consul-replicate
go install github.com/hashicorp/consul-replicate@latest
# 运行复制
consul-replicate \
-consul localhost:8500 \
-prefix "config/" \
-destination "dc2"6.2 服务目录复制
服务注册信息会通过 WAN Gossip 自动同步到其他数据中心。
七、Spring Cloud 多数据中心
7.1 配置
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
datacenter: dc1
query-passing: true7.2 跨数据中心调用
@Service
public class CrossDCService {
@Autowired
private ConsulClient consulClient;
public List<ServiceInstance> getServiceFromDC(String serviceName, String dc) {
CatalogServicesRequest request = CatalogServicesRequest.newBuilder()
.setDatacenter(dc)
.build();
Response<List<CatalogService>> response =
consulClient.getCatalogService(serviceName, request);
return response.getValue().stream()
.map(this::toServiceInstance)
.collect(Collectors.toList());
}
}7.3 自定义负载均衡
@Configuration
public class MultiDCLoadBalancerConfig {
@Bean
public ServiceInstanceListSupplier serviceInstanceListSupplier(
ConfigurableApplicationContext context) {
return ServiceInstanceListSupplier.builder()
.withDiscoveryClient()
.withCaching()
.build(context);
}
}八、网络配置
8.1 端口要求
| 端口 | 协议 | 用途 |
|---|---|---|
| 8300 | TCP | Server RPC |
| 8301 | TCP/UDP | LAN Gossip |
| 8302 | TCP/UDP | WAN Gossip |
| 8500 | TCP | HTTP API |
| 8600 | TCP/UDP | DNS |
8.2 防火墙配置
# WAN Gossip
iptables -A INPUT -p tcp --dport 8302 -j ACCEPT
iptables -A INPUT -p udp --dport 8302 -j ACCEPT
# Server RPC (跨数据中心)
iptables -A INPUT -p tcp --dport 8300 -j ACCEPT九、监控与运维
9.1 查看数据中心状态
# 列出所有数据中心
consul catalog datacenters
# 查看 WAN 成员
consul members -wan
# 查看数据中心服务数量
consul catalog services -datacenter=dc29.2 健康检查
# 检查跨数据中心连接
consul operator raft list-peers
# 查看 WAN 连接状态
consul info | grep -A 10 serf_wan十、最佳实践
10.1 数据中心命名
推荐格式:{地区}-{环境}
示例:
- beijing-prod
- shanghai-prod
- singapore-dr10.2 故障转移策略
10.3 延迟优化
- 使用 Prepared Query 的 NearestN 功能
- 配置合理的 WAN Gossip 间隔
- 考虑使用 Mesh Gateway 减少直接连接
