第三章:核心概念与架构
深入了解 RocketMQ 的核心概念、架构设计和消息存储机制。
最后更新: 2024-01-15
页面目录
RocketMQ 核心概念与架构
本章深入介绍 RocketMQ 的核心概念和架构设计。
核心概念
Topic(主题)
Topic 是消息的逻辑分类单元,用于消息的组织和过滤。
┌─────────────────────────────────────────────────────────────────┐
│ Topic 概念 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Topic: Order Topic │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Producer ──► OrderCreated ──► Message ──► Consumer │ │
│ │ Producer ──► OrderPaid ──► Message ──► Consumer │ │
│ │ Producer ──► OrderShipped ──► Message ──► Consumer │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
Message Queue(消息队列)
Queue 是 Topic 的物理实现,一个 Topic 可以包含多个 Queue。
| 配置 | 说明 |
|---|---|
| readQueueNums | 读队列数量 |
| writeQueueNums | 写队列数量 |
| perm | 队列权限 |
Tag(消息标签)
Tag 是消息的二级分类,用于更细粒度的消息过滤。
# Topic: ORDER_TOPIC
# Tags:
# - order:create # 订单创建
# - order:pay # 订单支付
# - order:ship # 订单发货
# - order:complete # 订单完成
Message(消息)
消息是 RocketMQ 的基本传输单元。
┌─────────────────────────────────────────────────────────────────┐
│ Message 结构 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Message { │
│ topic: "ORDER_TOPIC", # 主题 │
│ tags: ["order:create"], # 标签 │
│ keys: "order_12345", # 业务标识 │
│ body: "订单内容...", # 消息体 │
│ flag: 0, # 标识位 │
│ properties: { # 扩展属性 │
│ property1: "value1" │
│ }, │
│ msgId: "A0B2C3...", # 消息唯一ID │
│ bornTimestamp: 1705312800, # 产生时间 │
│ storeTimestamp: 1705312801 # 存储时间 │
│ } │
│ │
└─────────────────────────────────────────────────────────────────┘
Producer(生产者)
生产者是消息的发送方,负责将消息发送到 RocketMQ。
Consumer(消费者)
消费者是消息的接收方,从 RocketMQ 拉取并消费消息。
架构设计
NameServer 架构
┌─────────────────────────────────────────────────────────────────┐
│ NameServer 架构 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ NameServer │ │
│ │ │ │
│ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │
│ │ │ Cluster1 │ │ Cluster2 │ │ Cluster3 │ │ │
│ │ │ Info Table │ │ Info Table │ │ Info Table │ │ │
│ │ └─────────────┘ └─────────────┘ └─────────────┘ │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────┐ │ │
│ │ │ TopicRouteTable │ │ │
│ │ │ OrderTopic ──► [Broker1:Queue0-3] │ │ │
│ │ │ ProductTopic ──► [Broker2:Queue0-3] │ │ │
│ │ └─────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────┐ │ │
│ │ │ BrokerLiveTable │ │ │
│ │ │ Broker1 ──► lastUpdate: 1705312800 │ │ │
│ │ │ Broker2 ──► lastUpdate: 1705312801 │ │ │
│ │ └─────────────────────────────────────────────────┘ │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
│ 特点: │
│ ✅ 轻量级,无状态 │
│ ✅ 节点间无通信 │
│ ✅ 快速故障恢复 │
│ ✅ 支持水平扩展 │
│ │
└─────────────────────────────────────────────────────────────────┘
Broker 架构
┌─────────────────────────────────────────────────────────────────┐
│ Broker 架构 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Broker │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────┐ │ │
│ │ │ 客户端接入层 │ │ │
│ │ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │ │
│ │ │ │ Remoting│ │ POP │ │ gRPC │ │ │ │
│ │ │ │ Server │ │ Client │ │ Server │ │ │ │
│ │ │ └────┬────┘ └────┬────┘ └────┬────┘ │ │ │
│ │ └────────┼─────────────┼─────────────┼─────────────┘ │ │
│ │ │ │ │ │ │
│ │ ┌────────▼─────────────▼─────────────▼─────────────┐ │ │
│ │ │ 处理层 │ │ │
│ │ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │ │
│ │ │ │ Producer│ │ Consumer│ │ Admin │ │ │ │
│ │ │ │ Process │ │ Process │ │ Process │ │ │ │
│ │ │ └────┬────┘ └────┬────┘ └────┬────┘ │ │ │
│ │ └────────┼─────────────┼─────────────┼─────────────┘ │ │
│ │ │ │ │ │ │
│ │ ┌────────▼─────────────▼─────────────▼─────────────┐ │ │
│ │ │ 存储层 │ │ │
│ │ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │ │
│ │ │ │CommitLog│ │IndexFile│ │Checkpoint│ │ │ │
│ │ │ └─────────┘ └─────────┘ └─────────┘ │ │ │
│ │ └─────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────┐ │ │
│ │ │ 高可用层 │ │ │
│ │ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │ │
│ │ │ │ Master │ │ Slave │ │ DLedger │ │ │ │
│ │ │ └─────────┘ └─────────┘ └─────────┘ │ │ │
│ │ └─────────────────────────────────────────────────┘ │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
消息存储机制
存储结构
┌─────────────────────────────────────────────────────────────────┐
│ 消息存储结构 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Store Path: /home/rocketmq/store │
│ │
│ ├── commitlog/ # 消息存储文件 │
│ │ ├── 00000000000000000000 # 单个文件 1GB │
│ │ ├── 00000000001073741824 │
│ │ └── ... │
│ │ │
│ ├── consumequeue/ # 消费队列 │
│ │ └── TopicName/ │
│ │ └── QueueId/ │
│ │ └── 00000000000000000001 │
│ │ │
│ ├── index/ # 消息索引 │
│ │ └── 20240115000000000 │
│ │ │
│ ├── config/ # 配置信息 │
│ │ ├── consumerOffset.json │
│ │ ├── subscriptionGroup.json │
│ │ └── topics.json │
│ │ │
│ └── checkpoint # 检查点 │
│ │
└─────────────────────────────────────────────────────────────────┘
CommitLog
CommitLog 是 RocketMQ 的核心存储文件,所有消息都顺序写入 CommitLog。
┌─────────────────────────────────────────────────────────────────┐
│ CommitLog │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ MSG1 │ MSG2 │ MSG3 │ MSG4 │ MSG5 │ ... │ │
│ └──────────────────────────────────────────────────────────┘ │
│ 0 1000 2500 4000 6000 │
│ │
│ 特点: │
│ ✅ 顺序写入,磁盘顺序IO性能高 │
│ ✅ 单文件固定大小(默认1GB) │
│ ✅ 消息索引通过 ConsumeQueue 定位 │
│ │
└─────────────────────────────────────────────────────────────────┘
ConsumeQueue
ConsumeQueue 是消息消费队列,用于加速消息定位。
┌─────────────────────────────────────────────────────────────────┐
│ ConsumeQueue │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Topic: ORDER_TOPIC, QueueId: 0 │
│ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ offset │ size │ tagsCode │ timestamp │ msgKey │ │
│ │ (8B) │ (4B) │ (8B) │ (8B) │ (8B) │ │
│ ├──────────────────────────────────────────────────────────┤ │
│ │ 0 │ 512 │ tagCode │ 1705312800│ key12345 │ │
│ │ 1000 │ 480 │ tagCode │ 1705312801│ key12346 │ │
│ │ 2500 │ 520 │ tagCode │ 1705312802│ key12347 │ │
│ └──────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
消息查找流程
┌─────────────────────────────────────────────────────────────────┐
│ 消息查找流程 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ 查找 Topic:ORDER_TOPIC, QueueId:0, Offset:100 │
│ │
│ 1. 定位 ConsumeQueue │
│ Topic:ORDER_TOPIC → Queue:0 → ConsumeQueue 文件 │
│ │
│ 2. 读取 ConsumeQueue[100] │
│ 得到 CommitLog Offset: 10000 │
│ │
│ 3. 读取 CommitLog │
│ CommitLog[10000] → 读取完整消息 │
│ │
└─────────────────────────────────────────────────────────────────┘
消息索引机制
基于时间的索引
# 消息索引结构
IndexHeader {
beginTimestamp: 1705312800000
endTimestamp: 1705399200000
beginPhyOffset: 0
endPhyOffset: 10737418240
hashSlotCount: 5000000
indexCount: 12345678
}
# Index 文件结构
┌─────────────────────────────────────────────────────────────────┐
│ Header (40B) │ HashSlot (4B * N) │ IndexData (20B * M) │
└─────────────────────────────────────────────────────────────────┘
高可用机制
主从复制
┌─────────────────────────────────────────────────────────────────┐
│ 主从复制机制 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Master Slave │
│ ┌─────────┐ ┌─────────┐ │
│ │ CommitLog│ ──── Sync ────► │ CommitLog│ │
│ └─────────┘ └─────────┘ │
│ │
│ 复制模式: │
│ ✅ SYNC_MASTER: 同步复制,等待从节点确认 │
│ ✅ ASYNC_MASTER: 异步复制,主节点写入即返回 │
│ │
└─────────────────────────────────────────────────────────────────┘
DLedger 自动故障转移
┌─────────────────────────────────────────────────────────────────┐
│ DLedger 故障转移 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Before: After: │
│ │
│ ┌─────────┐ ┌─────────┐ │
│ │ Node 1 │ ◄─── Leader ────► │ Node 2 │ │
│ │ Leader │ Raft │ Follower│ │
│ └─────────┘ └─────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────┐ ┌─────────┐ │
│ │ Node 3 │ │ Node 3 │ │
│ │ Follower│ │ Leader │ │
│ └─────────┘ └─────────┘ │
│ │
│ 故障转移: Node 1 宕机后,Node 3 自动升级为 Leader │
│ │
└─────────────────────────────────────────────────────────────────┘
下一步
接下来让我们学习 RocketMQ 生产者。
👉 生产者