Kafka 整体架构¶
Kafka 整体架构图¶
flowchart LR
subgraph 生产者集群
P1[Producer 1]
P2[Producer 2]
end
subgraph Kafka Cluster
subgraph Broker 1
T1P0["Topic-A Partition 0\n(Leader)"]
T1P1["Topic-A Partition 1\n(Follower)"]
end
subgraph Broker 2
T1P0R["Topic-A Partition 0\n(Follower)"]
T1P1L["Topic-A Partition 1\n(Leader)"]
end
subgraph Broker 3
T1P2L["Topic-A Partition 2\n(Leader)"]
end
ZK["ZooKeeper\n存储元数据\n选举 Controller"]
end
subgraph 消费者组 G1
C1["Consumer 1\n消费 P0"]
C2["Consumer 2\n消费 P1"]
C3["Consumer 3\n消费 P2"]
end
P1 -->|写入 P0| T1P0
P2 -->|写入 P1| T1P1L
T1P0 -->|副本同步| T1P0R
T1P1L -->|副本同步| T1P1
T1P0 -->|拉取| C1
T1P1L -->|拉取| C2
T1P2L -->|拉取| C3
注意:Kafka 2.8+ 引入 KRaft 模式,用内置的 Raft 协议替代 ZooKeeper,Kafka 3.x 已完全支持无 ZooKeeper 部署。
架构核心要点¶
| 组件 | 职责 | 说明 |
|---|---|---|
| Producer | 消息生产者 | 将消息写入指定 Topic 的 Partition |
| Broker | Kafka 服务节点 | 存储分区数据,处理读写请求 |
| Topic | 消息主题 | 消息的逻辑分类,由多个 Partition 组成 |
| Partition | 分区 | Topic 的物理分片,是并行度的基本单位 |
| Leader/Follower | 主副本/从副本 | Leader 处理读写,Follower 同步数据 |
| Consumer Group | 消费者组 | 组内消费者共同消费 Topic,每个分区只被一个消费者消费 |
| ZooKeeper/KRaft | 元数据管理 | 存储集群元数据,负责 Controller 选举 |
数据流向¶
- 写入:Producer 只写 Leader Partition,Follower 异步同步
- 消费:Consumer 从 Leader Partition 拉取数据(Pull 模式)
- 副本:Follower 持续从 Leader 同步,保证高可用