Skip to content

分区与副本 ​

分区作用 ​

  1. 水平扩展:多分区并行处理
  2. 高吞吐:多 Broker 分布式存储
  3. 并行消费:消费者组并行处理

副本机制 ​

每个分区有多个副本,保证高可用。

Partition 0:
├── Leader (Broker 1) ← 读写
├── Follower (Broker 2) ← 同步
└── Follower (Broker 3) ← 同步

ISR(In-Sync Replicas) ​

与 Leader 保持同步的副本集合。

bash
# 查看ISR
kafka-topics.sh --describe --topic my-topic \
  --bootstrap-server localhost:9092

副本配置 ​

bash
# 创建主题时指定副本数
kafka-topics.sh --create --topic my-topic \
  --partitions 3 --replication-factor 3 \
  --bootstrap-server localhost:9092

Leader 选举 ​

Leader 故障时,从 ISR 中选举新 Leader。

bash
# 选举配置
unclean.leader.election.enable=false  # 只从ISR选举

分区重分配 ​

bash
# 生成重分配计划
kafka-reassign-partitions.sh --generate \
  --topics-to-move-json-file topics.json \
  --broker-list "1,2,3" \
  --bootstrap-server localhost:9092

# 执行重分配
kafka-reassign-partitions.sh --execute \
  --reassignment-json-file move.json \
  --bootstrap-server localhost:9092

参考:https://kafka.apache.org/documentation/