Couchbase跨数据中心复制 #

一、XDCR概述 #

1.1 什么是XDCR #

XDCR(Cross Data Center Replication)是Couchbase的跨数据中心复制功能,用于:

text
用途:
- 灾难恢复:异地备份
- 数据就近访问:降低延迟
- 负载分担:分散读取压力
- 合规要求:数据本地化存储

1.2 XDCR架构 #

text
┌─────────────────────────────────────────────────────────────┐
│                                                             │
│   数据中心A(北京)              数据中心B(上海)            │
│   ┌─────────────┐               ┌─────────────┐            │
│   │ Cluster A   │               │ Cluster B   │            │
│   │             │               │             │            │
│   │ ┌─────────┐ │   XDCR复制    │ ┌─────────┐ │            │
│   │ │ Node 1  │ │──────────────►│ │ Node 1  │ │            │
│   │ │ Node 2  │ │               │ │ Node 2  │ │            │
│   │ │ Node 3  │ │◄─────────────│ │ Node 3  │ │            │
│   │ └─────────┘ │   双向复制    │ └─────────┘ │            │
│   └─────────────┘               └─────────────┘            │
│                                                             │
└─────────────────────────────────────────────────────────────┘

1.3 复制类型 #

类型 说明 使用场景
单向复制 A → B 灾备、只读副本
双向复制 A ↔ B 多活架构
链式复制 A → B → C 多级灾备
集中式复制 A → B, A → C 分发数据

二、配置XDCR #

2.1 准备工作 #

text
前提条件:
1. 两个独立的Couchbase集群
2. 网络互通(端口开放)
3. 相同的Bucket配置
4. 足够的带宽

2.2 Web控制台配置 #

  1. 登录源集群Web控制台
  2. 进入XDCR页面
  3. 点击"Add Remote Cluster"
  4. 配置远程集群信息:
参数 说明
Cluster Name 远程集群名称
IP/Hostname 远程集群地址
Username 远程集群用户名
Password 远程集群密码
  1. 创建复制:
参数 说明
Source Bucket 源Bucket
Destination Bucket 目标Bucket
Replication Type 复制类型

2.3 命令行配置 #

添加远程集群:

bash
/opt/couchbase/bin/couchbase-cli xdcr-setup \
    --cluster localhost:8091 \
    --username Administrator \
    --password your-password \
    --create \
    --xdcr-cluster-name cluster-shanghai \
    --xdcr-hostname 192.168.2.10 \
    --xdcr-username Administrator \
    --xdcr-password password

创建复制:

bash
/opt/couchbase/bin/couchbase-cli xdcr-replicate \
    --cluster localhost:8091 \
    --username Administrator \
    --password your-password \
    --create \
    --xdcr-cluster-name cluster-shanghai \
    --xdcr-from-bucket my-bucket \
    --xdcr-to-bucket my-bucket \
    --xdcr-replication-mode xmem

2.4 REST API配置 #

bash
curl -X POST http://localhost:8091/pools/default/remoteClusters \
    -u Administrator:password \
    -d name=cluster-shanghai \
    -d hostname=192.168.2.10 \
    -d username=Administrator \
    -d password=password

curl -X POST http://localhost:8091/controller/createReplication \
    -u Administrator:password \
    -d toCluster=cluster-shanghai \
    -d fromBucket=my-bucket \
    -d toBucket=my-bucket \
    -d replicationType=continuous

三、复制配置参数 #

3.1 基本参数 #

bash
/opt/couchbase/bin/couchbase-cli xdcr-replicate \
    --cluster localhost:8091 \
    --username Administrator \
    --password your-password \
    --create \
    --xdcr-cluster-name cluster-shanghai \
    --xdcr-from-bucket my-bucket \
    --xdcr-to-bucket my-bucket \
    --xdcr-replication-mode xmem \
    --xdcr-priority High \
    --xdcr-checkpoint-interval 1800

3.2 性能参数 #

参数 说明 默认值
xdcr-priority 复制优先级 High/Medium/Low
xdcr-checkpoint-interval 检查点间隔(秒) 1800
xdcr-worker-batch-size 批量大小 500
xdcr-doc-batch-size 文档批量大小(KB) 2048
xdcr-failure-restart-interval 失败重启间隔(秒) 30
xdcr-optimistic-replication-threshold 乐观复制阈值 256

3.3 冲突解决参数 #

bash
/opt/couchbase/bin/couchbase-cli xdcr-replicate \
    --cluster localhost:8091 \
    --username Administrator \
    --password your-password \
    --create \
    --xdcr-cluster-name cluster-shanghai \
    --xdcr-from-bucket my-bucket \
    --xdcr-to-bucket my-bucket \
    --xdcr-conflict-resolution-type seqno

冲突解决类型:

类型 说明
seqno 基于序列号(默认)
lww 基于时间戳

3.4 过滤规则 #

bash
/opt/couchbase/bin/couchbase-cli xdcr-replicate \
    --cluster localhost:8091 \
    --username Administrator \
    --password your-password \
    --create \
    --xdcr-cluster-name cluster-shanghai \
    --xdcr-from-bucket my-bucket \
    --xdcr-to-bucket my-bucket \
    --filter-expression 'type = "user"'

四、双向复制 #

4.1 配置双向复制 #

bash
/opt/couchbase/bin/couchbase-cli xdcr-setup \
    --cluster beijing-cluster:8091 \
    --username Administrator \
    --password password \
    --create \
    --xdcr-cluster-name shanghai \
    --xdcr-hostname shanghai-cluster \
    --xdcr-username Administrator \
    --xdcr-password password

/opt/couchbase/bin/couchbase-cli xdcr-setup \
    --cluster shanghai-cluster:8091 \
    --username Administrator \
    --password password \
    --create \
    --xdcr-cluster-name beijing \
    --xdcr-hostname beijing-cluster \
    --xdcr-username Administrator \
    --xdcr-password password

/opt/couchbase/bin/couchbase-cli xdcr-replicate \
    --cluster beijing-cluster:8091 \
    --username Administrator \
    --password password \
    --create \
    --xdcr-cluster-name shanghai \
    --xdcr-from-bucket my-bucket \
    --xdcr-to-bucket my-bucket

/opt/couchbase/bin/couchbase-cli xdcr-replicate \
    --cluster shanghai-cluster:8091 \
    --username Administrator \
    --password password \
    --create \
    --xdcr-cluster-name beijing \
    --xdcr-from-bucket my-bucket \
    --xdcr-to-bucket my-bucket

4.2 双向复制注意事项 #

text
注意事项:
1. 确保初始数据一致
2. 配置相同的冲突解决策略
3. 监控复制延迟
4. 处理好冲突场景

五、冲突解决 #

5.1 冲突场景 #

text
场景:同一文档在两个数据中心同时被修改

时间线:
T1: 北京集群修改文档A,版本=2
T2: 上海集群修改文档A,版本=2
T3: XDCR同步,发现冲突

5.2 基于序列号解决 #

text
规则:
- 比较文档的CAS值
- CAS值大的获胜
- 自动解决,无需人工干预

5.3 基于时间戳解决(LWW) #

text
规则:
- 比较文档的修改时间戳
- 时间戳新的获胜
- 需要时钟同步

5.4 自定义冲突解决 #

python
def custom_conflict_resolution(doc1, doc2):
    if doc1.get('version', 0) > doc2.get('version', 0):
        return doc1
    elif doc1.get('version', 0) < doc2.get('version', 0):
        return doc2
    else:
        return doc1 if doc1.get('updated_at', '') > doc2.get('updated_at', '') else doc2

六、监控XDCR #

6.1 查看复制状态 #

bash
/opt/couchbase/bin/couchbase-cli xdcr-replicate \
    --cluster localhost:8091 \
    --username Administrator \
    --password your-password \
    --list

6.2 复制统计 #

bash
curl -X GET http://localhost:8091/pools/default/tasks \
    -u Administrator:password | jq '.[] | select(.type == "xdcr")'

6.3 关键指标 #

指标 说明 告警阈值
changes_left 待复制变更数 > 10000
docs_checked 已检查文档数 -
docs_written 已写入文档数 -
replication_rate 复制速率 < 预期
latency 复制延迟 > 60s

6.4 Web控制台监控 #

  1. 登录Web控制台
  2. 进入XDCR页面
  3. 查看复制状态和统计信息

七、暂停和恢复 #

7.1 暂停复制 #

bash
/opt/couchbase/bin/couchbase-cli xdcr-replicate \
    --cluster localhost:8091 \
    --username Administrator \
    --password your-password \
    --pause \
    --xdcr-replication-id replication-id

7.2 恢复复制 #

bash
/opt/couchbase/bin/couchbase-cli xdcr-replicate \
    --cluster localhost:8091 \
    --username Administrator \
    --password your-password \
    --resume \
    --xdcr-replication-id replication-id

7.3 删除复制 #

bash
/opt/couchbase/bin/couchbase-cli xdcr-replicate \
    --cluster localhost:8091 \
    --username Administrator \
    --password your-password \
    --delete \
    --xdcr-replication-id replication-id

八、最佳实践 #

8.1 网络规划 #

text
建议:
1. 使用专线或VPN连接
2. 确保足够带宽
3. 配置QoS优先级
4. 监控网络延迟

8.2 数据一致性 #

text
初始同步:
1. 先停止写入
2. 全量同步数据
3. 验证数据一致
4. 启动XDCR
5. 恢复写入

8.3 性能优化 #

bash
/opt/couchbase/bin/couchbase-cli xdcr-replicate \
    --cluster localhost:8091 \
    --username Administrator \
    --password your-password \
    --create \
    --xdcr-cluster-name cluster-shanghai \
    --xdcr-from-bucket my-bucket \
    --xdcr-to-bucket my-bucket \
    --xdcr-worker-batch-size 1000 \
    --xdcr-doc-batch-size 4096 \
    --xdcr-priority High

8.4 监控告警 #

text
监控项:
1. 复制延迟
2. 待复制队列
3. 错误率
4. 网络带宽使用

告警规则:
- 延迟 > 5分钟:警告
- 延迟 > 30分钟:严重
- 队列 > 100000:警告
- 错误率 > 1%:警告

九、故障处理 #

9.1 复制中断 #

bash
/opt/couchbase/bin/couchbase-cli xdcr-replicate \
    --cluster localhost:8091 \
    --username Administrator \
    --password your-password \
    --resume \
    --xdcr-replication-id replication-id

9.2 数据不一致 #

bash
/opt/couchbase/bin/couchbase-cli xdcr-replicate \
    --cluster localhost:8091 \
    --username Administrator \
    --password your-password \
    --delete \
    --xdcr-replication-id replication-id

/opt/couchbase/bin/couchbase-cli xdcr-replicate \
    --cluster localhost:8091 \
    --username Administrator \
    --password your-password \
    --create \
    --xdcr-cluster-name cluster-shanghai \
    --xdcr-from-bucket my-bucket \
    --xdcr-to-bucket my-bucket

9.3 性能问题 #

text
排查步骤:
1. 检查网络带宽
2. 检查目标集群负载
3. 调整复制参数
4. 增加复制线程

十、总结 #

XDCR要点:

功能 说明
单向复制 A → B
双向复制 A ↔ B
冲突解决 自动或手动
过滤规则 选择性复制

最佳实践:

  1. 规划好网络带宽
  2. 配置合适的冲突解决策略
  3. 监控复制状态
  4. 定期演练故障恢复
  5. 优化复制参数

下一步,让我们学习用户权限管理!

最后更新:2026-03-27