Kafka Topic 深度解析:消息世界的逻辑容器
-
- 一、Topic 概述
-
- 1.1 什么是 Topic?
- 1.2 Topic 的核心作用
- 二、Topic 的内部结构
-
- 2.1 Topic 与 Partition 的关系
- 2.2 Partition 的内部结构
- 三、Topic 的工作流程
-
- 3.1 生产者视角:消息发布
- 3.2 消费者视角:消息订阅
- 3.3 内部视图:Topic 的存储结构
- 四、Topic 的关键配置
-
- 4.1 创建 Topic 时的核心参数
- 4.2 Partition 数量估算公式
- 4.3 创建 Topic 的三种方式
- 五、Topic 的命名规范与最佳实践
-
- 5.1 命名规范建议
- 5.2 Topic 数量规划
- 5.3 Partition 数量规划原则
- 5.4 清理策略选择
- 六、Topic 的监控与管理
-
- 6.1 常用管理命令
- 6.2 关键监控指标
- 七、实战示例:订单系统 Topic 设计
-
- 7.1 业务场景
- 7.2 Topic 规划方案
- 八、总结
-
- 8.1 Topic 核心要点回顾
- 8.2 Topic 的完整生命周期
- 8.3 一句话总结
|
🌺The Begin🌺点点关注,收藏不迷路🌺 |
摘要:在 Kafka 的世界里,Topic 是最基础也最重要的概念。它是消息的逻辑分类单元,是生产者发布消息的目的地,也是消费者订阅的数据源。理解 Topic 的本质,是掌握 Kafka 整个架构的钥匙。本文将深入剖析 Topic 的定义、作用、内部结构以及在实际应用中的设计原则,通过流程图和实战示例,帮助读者全面理解这一核心概念。
一、Topic 概述
1.1 什么是 Topic?
在 Kafka 中,Topic(主题)是消息的逻辑分类单元。它类似于数据库中的表,或者是文件系统中的文件夹。生产者将消息发送到特定的 Topic,消费者从感兴趣的 Topic 读取消息。
#mermaid-svg-25zc8rmIcgtef8JO{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-25zc8rmIcgtef8JO .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-25zc8rmIcgtef8JO .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-25zc8rmIcgtef8JO .error-icon{fill:#552222;}#mermaid-svg-25zc8rmIcgtef8JO .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-25zc8rmIcgtef8JO .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-25zc8rmIcgtef8JO .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-25zc8rmIcgtef8JO .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-25zc8rmIcgtef8JO .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-25zc8rmIcgtef8JO .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-25zc8rmIcgtef8JO .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-25zc8rmIcgtef8JO .marker{fill:#333333;stroke:#333333;}#mermaid-svg-25zc8rmIcgtef8JO .marker.cross{stroke:#333333;}#mermaid-svg-25zc8rmIcgtef8JO svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-25zc8rmIcgtef8JO p{margin:0;}#mermaid-svg-25zc8rmIcgtef8JO .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-25zc8rmIcgtef8JO .cluster-label text{fill:#333;}#mermaid-svg-25zc8rmIcgtef8JO .cluster-label span{color:#333;}#mermaid-svg-25zc8rmIcgtef8JO .cluster-label span p{background-color:transparent;}#mermaid-svg-25zc8rmIcgtef8JO .label text,#mermaid-svg-25zc8rmIcgtef8JO span{fill:#333;color:#333;}#mermaid-svg-25zc8rmIcgtef8JO .node rect,#mermaid-svg-25zc8rmIcgtef8JO .node circle,#mermaid-svg-25zc8rmIcgtef8JO .node ellipse,#mermaid-svg-25zc8rmIcgtef8JO .node polygon,#mermaid-svg-25zc8rmIcgtef8JO .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-25zc8rmIcgtef8JO .rough-node .label text,#mermaid-svg-25zc8rmIcgtef8JO .node .label text,#mermaid-svg-25zc8rmIcgtef8JO .image-shape .label,#mermaid-svg-25zc8rmIcgtef8JO .icon-shape .label{text-anchor:middle;}#mermaid-svg-25zc8rmIcgtef8JO .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-25zc8rmIcgtef8JO .rough-node .label,#mermaid-svg-25zc8rmIcgtef8JO .node .label,#mermaid-svg-25zc8rmIcgtef8JO .image-shape .label,#mermaid-svg-25zc8rmIcgtef8JO .icon-shape .label{text-align:center;}#mermaid-svg-25zc8rmIcgtef8JO .node.clickable{cursor:pointer;}#mermaid-svg-25zc8rmIcgtef8JO .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-25zc8rmIcgtef8JO .arrowheadPath{fill:#333333;}#mermaid-svg-25zc8rmIcgtef8JO .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-25zc8rmIcgtef8JO .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-25zc8rmIcgtef8JO .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-25zc8rmIcgtef8JO .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-25zc8rmIcgtef8JO .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-25zc8rmIcgtef8JO .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-25zc8rmIcgtef8JO .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-25zc8rmIcgtef8JO .cluster text{fill:#333;}#mermaid-svg-25zc8rmIcgtef8JO .cluster span{color:#333;}#mermaid-svg-25zc8rmIcgtef8JO div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-25zc8rmIcgtef8JO .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-25zc8rmIcgtef8JO rect.text{fill:none;stroke-width:0;}#mermaid-svg-25zc8rmIcgtef8JO .icon-shape,#mermaid-svg-25zc8rmIcgtef8JO .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-25zc8rmIcgtef8JO .icon-shape p,#mermaid-svg-25zc8rmIcgtef8JO .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-25zc8rmIcgtef8JO .icon-shape rect,#mermaid-svg-25zc8rmIcgtef8JO .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-25zc8rmIcgtef8JO .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-25zc8rmIcgtef8JO .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-25zc8rmIcgtef8JO :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
Topic 概念类比
数据库
表
文件系统
文件夹
Kafka
Topic
1.2 Topic 的核心作用
Topic 在 Kafka 架构中扮演着至关重要的角色:
| 消息分类 | 将不同类型的消息进行逻辑隔离,如订单消息、用户行为消息分别存入不同 Topic |
| 数据订阅点 | 消费者通过订阅 Topic 来获取感兴趣的数据流 |
| 命名空间 | 为消息提供唯一的标识,便于管理和监控 |
| 存储单元 | 在物理层面,Topic 对应着磁盘上的一组日志文件 |
二、Topic 的内部结构
2.1 Topic 与 Partition 的关系
一个 Topic 在物理上可以被划分为多个 Partition(分区)。这是 Kafka 实现高吞吐和并行处理的关键设计。
#mermaid-svg-NkDDg2Oc0aQgGRov{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-NkDDg2Oc0aQgGRov .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-NkDDg2Oc0aQgGRov .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-NkDDg2Oc0aQgGRov .error-icon{fill:#552222;}#mermaid-svg-NkDDg2Oc0aQgGRov .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-NkDDg2Oc0aQgGRov .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-NkDDg2Oc0aQgGRov .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-NkDDg2Oc0aQgGRov .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-NkDDg2Oc0aQgGRov .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-NkDDg2Oc0aQgGRov .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-NkDDg2Oc0aQgGRov .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-NkDDg2Oc0aQgGRov .marker{fill:#333333;stroke:#333333;}#mermaid-svg-NkDDg2Oc0aQgGRov .marker.cross{stroke:#333333;}#mermaid-svg-NkDDg2Oc0aQgGRov svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-NkDDg2Oc0aQgGRov p{margin:0;}#mermaid-svg-NkDDg2Oc0aQgGRov .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-NkDDg2Oc0aQgGRov .cluster-label text{fill:#333;}#mermaid-svg-NkDDg2Oc0aQgGRov .cluster-label span{color:#333;}#mermaid-svg-NkDDg2Oc0aQgGRov .cluster-label span p{background-color:transparent;}#mermaid-svg-NkDDg2Oc0aQgGRov .label text,#mermaid-svg-NkDDg2Oc0aQgGRov span{fill:#333;color:#333;}#mermaid-svg-NkDDg2Oc0aQgGRov .node rect,#mermaid-svg-NkDDg2Oc0aQgGRov .node circle,#mermaid-svg-NkDDg2Oc0aQgGRov .node ellipse,#mermaid-svg-NkDDg2Oc0aQgGRov .node polygon,#mermaid-svg-NkDDg2Oc0aQgGRov .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-NkDDg2Oc0aQgGRov .rough-node .label text,#mermaid-svg-NkDDg2Oc0aQgGRov .node .label text,#mermaid-svg-NkDDg2Oc0aQgGRov .image-shape .label,#mermaid-svg-NkDDg2Oc0aQgGRov .icon-shape .label{text-anchor:middle;}#mermaid-svg-NkDDg2Oc0aQgGRov .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-NkDDg2Oc0aQgGRov .rough-node .label,#mermaid-svg-NkDDg2Oc0aQgGRov .node .label,#mermaid-svg-NkDDg2Oc0aQgGRov .image-shape .label,#mermaid-svg-NkDDg2Oc0aQgGRov .icon-shape .label{text-align:center;}#mermaid-svg-NkDDg2Oc0aQgGRov .node.clickable{cursor:pointer;}#mermaid-svg-NkDDg2Oc0aQgGRov .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-NkDDg2Oc0aQgGRov .arrowheadPath{fill:#333333;}#mermaid-svg-NkDDg2Oc0aQgGRov .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-NkDDg2Oc0aQgGRov .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-NkDDg2Oc0aQgGRov .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-NkDDg2Oc0aQgGRov .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-NkDDg2Oc0aQgGRov .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-NkDDg2Oc0aQgGRov .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-NkDDg2Oc0aQgGRov .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-NkDDg2Oc0aQgGRov .cluster text{fill:#333;}#mermaid-svg-NkDDg2Oc0aQgGRov .cluster span{color:#333;}#mermaid-svg-NkDDg2Oc0aQgGRov div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-NkDDg2Oc0aQgGRov .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-NkDDg2Oc0aQgGRov rect.text{fill:none;stroke-width:0;}#mermaid-svg-NkDDg2Oc0aQgGRov .icon-shape,#mermaid-svg-NkDDg2Oc0aQgGRov .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-NkDDg2Oc0aQgGRov .icon-shape p,#mermaid-svg-NkDDg2Oc0aQgGRov .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-NkDDg2Oc0aQgGRov .icon-shape rect,#mermaid-svg-NkDDg2Oc0aQgGRov .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-NkDDg2Oc0aQgGRov .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-NkDDg2Oc0aQgGRov .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-NkDDg2Oc0aQgGRov :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
Topic-B
Topic-A
Partition 0消息0,1,2…
Partition 1消息0,1,2…
Partition 2消息0,1,2…
Partition 0消息0,1,2…
Partition 1消息0,1,2…
Broker 1
Broker 2
Broker 3
核心关系:
- 1 个 Topic 包含 1 到多个 Partition
- 每个 Partition 是 Topic 的物理存储单元
- Partition 分布在不同的 Broker 上,实现水平扩展
2.2 Partition 的内部结构
每个 Partition 在磁盘上对应一个有序的、不可变的消息序列,并以日志文件的形式存储。
#mermaid-svg-hYEPvL7PiisNTdKU{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-hYEPvL7PiisNTdKU .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-hYEPvL7PiisNTdKU .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-hYEPvL7PiisNTdKU .error-icon{fill:#552222;}#mermaid-svg-hYEPvL7PiisNTdKU .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-hYEPvL7PiisNTdKU .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-hYEPvL7PiisNTdKU .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-hYEPvL7PiisNTdKU .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-hYEPvL7PiisNTdKU .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-hYEPvL7PiisNTdKU .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-hYEPvL7PiisNTdKU .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-hYEPvL7PiisNTdKU .marker{fill:#333333;stroke:#333333;}#mermaid-svg-hYEPvL7PiisNTdKU .marker.cross{stroke:#333333;}#mermaid-svg-hYEPvL7PiisNTdKU svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-hYEPvL7PiisNTdKU p{margin:0;}#mermaid-svg-hYEPvL7PiisNTdKU .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-hYEPvL7PiisNTdKU .cluster-label text{fill:#333;}#mermaid-svg-hYEPvL7PiisNTdKU .cluster-label span{color:#333;}#mermaid-svg-hYEPvL7PiisNTdKU .cluster-label span p{background-color:transparent;}#mermaid-svg-hYEPvL7PiisNTdKU .label text,#mermaid-svg-hYEPvL7PiisNTdKU span{fill:#333;color:#333;}#mermaid-svg-hYEPvL7PiisNTdKU .node rect,#mermaid-svg-hYEPvL7PiisNTdKU .node circle,#mermaid-svg-hYEPvL7PiisNTdKU .node ellipse,#mermaid-svg-hYEPvL7PiisNTdKU .node polygon,#mermaid-svg-hYEPvL7PiisNTdKU .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-hYEPvL7PiisNTdKU .rough-node .label text,#mermaid-svg-hYEPvL7PiisNTdKU .node .label text,#mermaid-svg-hYEPvL7PiisNTdKU .image-shape .label,#mermaid-svg-hYEPvL7PiisNTdKU .icon-shape .label{text-anchor:middle;}#mermaid-svg-hYEPvL7PiisNTdKU .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-hYEPvL7PiisNTdKU .rough-node .label,#mermaid-svg-hYEPvL7PiisNTdKU .node .label,#mermaid-svg-hYEPvL7PiisNTdKU .image-shape .label,#mermaid-svg-hYEPvL7PiisNTdKU .icon-shape .label{text-align:center;}#mermaid-svg-hYEPvL7PiisNTdKU .node.clickable{cursor:pointer;}#mermaid-svg-hYEPvL7PiisNTdKU .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-hYEPvL7PiisNTdKU .arrowheadPath{fill:#333333;}#mermaid-svg-hYEPvL7PiisNTdKU .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-hYEPvL7PiisNTdKU .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-hYEPvL7PiisNTdKU .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-hYEPvL7PiisNTdKU .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-hYEPvL7PiisNTdKU .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-hYEPvL7PiisNTdKU .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-hYEPvL7PiisNTdKU .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-hYEPvL7PiisNTdKU .cluster text{fill:#333;}#mermaid-svg-hYEPvL7PiisNTdKU .cluster span{color:#333;}#mermaid-svg-hYEPvL7PiisNTdKU div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-hYEPvL7PiisNTdKU .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-hYEPvL7PiisNTdKU rect.text{fill:none;stroke-width:0;}#mermaid-svg-hYEPvL7PiisNTdKU .icon-shape,#mermaid-svg-hYEPvL7PiisNTdKU .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-hYEPvL7PiisNTdKU .icon-shape p,#mermaid-svg-hYEPvL7PiisNTdKU .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-hYEPvL7PiisNTdKU .icon-shape rect,#mermaid-svg-hYEPvL7PiisNTdKU .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-hYEPvL7PiisNTdKU .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-hYEPvL7PiisNTdKU .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-hYEPvL7PiisNTdKU :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
Partition 0 日志文件
消息0Offset 0
消息1Offset 1
消息2Offset 2
…
Partition 内部特性:
| 有序性 | 每个 Partition 内的消息严格按照写入顺序存储 | ⭐⭐⭐⭐⭐ |
| 不可变性 | 消息一旦写入就不能修改,只能追加 | ⭐⭐⭐⭐ |
| Offset | 每个消息都有唯一的偏移量标识其位置 | ⭐⭐⭐⭐⭐ |
| 持久化 | 消息会持久化到磁盘,可配置保留策略 | ⭐⭐⭐⭐ |
三、Topic 的工作流程
3.1 生产者视角:消息发布
生产者将消息发送到指定的 Topic,Kafka 根据分区规则决定消息写入哪个 Partition。
#mermaid-svg-Pln9RwcZrvXu2enn{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Pln9RwcZrvXu2enn .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Pln9RwcZrvXu2enn .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Pln9RwcZrvXu2enn .error-icon{fill:#552222;}#mermaid-svg-Pln9RwcZrvXu2enn .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Pln9RwcZrvXu2enn .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Pln9RwcZrvXu2enn .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Pln9RwcZrvXu2enn .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Pln9RwcZrvXu2enn .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Pln9RwcZrvXu2enn .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Pln9RwcZrvXu2enn .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Pln9RwcZrvXu2enn .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Pln9RwcZrvXu2enn .marker.cross{stroke:#333333;}#mermaid-svg-Pln9RwcZrvXu2enn svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Pln9RwcZrvXu2enn p{margin:0;}#mermaid-svg-Pln9RwcZrvXu2enn .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-Pln9RwcZrvXu2enn .cluster-label text{fill:#333;}#mermaid-svg-Pln9RwcZrvXu2enn .cluster-label span{color:#333;}#mermaid-svg-Pln9RwcZrvXu2enn .cluster-label span p{background-color:transparent;}#mermaid-svg-Pln9RwcZrvXu2enn .label text,#mermaid-svg-Pln9RwcZrvXu2enn span{fill:#333;color:#333;}#mermaid-svg-Pln9RwcZrvXu2enn .node rect,#mermaid-svg-Pln9RwcZrvXu2enn .node circle,#mermaid-svg-Pln9RwcZrvXu2enn .node ellipse,#mermaid-svg-Pln9RwcZrvXu2enn .node polygon,#mermaid-svg-Pln9RwcZrvXu2enn .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Pln9RwcZrvXu2enn .rough-node .label text,#mermaid-svg-Pln9RwcZrvXu2enn .node .label text,#mermaid-svg-Pln9RwcZrvXu2enn .image-shape .label,#mermaid-svg-Pln9RwcZrvXu2enn .icon-shape .label{text-anchor:middle;}#mermaid-svg-Pln9RwcZrvXu2enn .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Pln9RwcZrvXu2enn .rough-node .label,#mermaid-svg-Pln9RwcZrvXu2enn .node .label,#mermaid-svg-Pln9RwcZrvXu2enn .image-shape .label,#mermaid-svg-Pln9RwcZrvXu2enn .icon-shape .label{text-align:center;}#mermaid-svg-Pln9RwcZrvXu2enn .node.clickable{cursor:pointer;}#mermaid-svg-Pln9RwcZrvXu2enn .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Pln9RwcZrvXu2enn .arrowheadPath{fill:#333333;}#mermaid-svg-Pln9RwcZrvXu2enn .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Pln9RwcZrvXu2enn .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Pln9RwcZrvXu2enn .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Pln9RwcZrvXu2enn .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Pln9RwcZrvXu2enn .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Pln9RwcZrvXu2enn .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Pln9RwcZrvXu2enn .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Pln9RwcZrvXu2enn .cluster text{fill:#333;}#mermaid-svg-Pln9RwcZrvXu2enn .cluster span{color:#333;}#mermaid-svg-Pln9RwcZrvXu2enn div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Pln9RwcZrvXu2enn .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Pln9RwcZrvXu2enn rect.text{fill:none;stroke-width:0;}#mermaid-svg-Pln9RwcZrvXu2enn .icon-shape,#mermaid-svg-Pln9RwcZrvXu2enn .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Pln9RwcZrvXu2enn .icon-shape p,#mermaid-svg-Pln9RwcZrvXu2enn .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Pln9RwcZrvXu2enn .icon-shape rect,#mermaid-svg-Pln9RwcZrvXu2enn .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Pln9RwcZrvXu2enn .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Pln9RwcZrvXu2enn .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Pln9RwcZrvXu2enn :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
是
否
是
否
生产者创建消息
指定 Topic
指定了 Partition?
直接使用指定 Partition
消息有 Key?
对 Key 哈希取模
轮询选择 Partition
写入选定 Partition
// 生产者示例:发送消息到 Topic
ProducerRecord<String, String> record = new ProducerRecord<>(
"order-events", // Topic 名称
"order-123", // 消息 Key(用于分区)
"Order created" // 消息 Value
);
producer.send(record);
3.2 消费者视角:消息订阅
消费者订阅 Topic 后,Kafka 会为消费者组分配要消费的 Partition。
Topic
消费者组协调器
消费者
Topic
消费者组协调器
消费者
#mermaid-svg-zyBLJPsdIgEoQQkd{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-zyBLJPsdIgEoQQkd .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-zyBLJPsdIgEoQQkd .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-zyBLJPsdIgEoQQkd .error-icon{fill:#552222;}#mermaid-svg-zyBLJPsdIgEoQQkd .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-zyBLJPsdIgEoQQkd .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-zyBLJPsdIgEoQQkd .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-zyBLJPsdIgEoQQkd .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-zyBLJPsdIgEoQQkd .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-zyBLJPsdIgEoQQkd .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-zyBLJPsdIgEoQQkd .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-zyBLJPsdIgEoQQkd .marker{fill:#333333;stroke:#333333;}#mermaid-svg-zyBLJPsdIgEoQQkd .marker.cross{stroke:#333333;}#mermaid-svg-zyBLJPsdIgEoQQkd svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-zyBLJPsdIgEoQQkd p{margin:0;}#mermaid-svg-zyBLJPsdIgEoQQkd .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-zyBLJPsdIgEoQQkd text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-zyBLJPsdIgEoQQkd .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-zyBLJPsdIgEoQQkd .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-zyBLJPsdIgEoQQkd .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-zyBLJPsdIgEoQQkd .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-zyBLJPsdIgEoQQkd #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-zyBLJPsdIgEoQQkd .sequenceNumber{fill:white;}#mermaid-svg-zyBLJPsdIgEoQQkd #sequencenumber{fill:#333;}#mermaid-svg-zyBLJPsdIgEoQQkd #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-zyBLJPsdIgEoQQkd .messageText{fill:#333;stroke:none;}#mermaid-svg-zyBLJPsdIgEoQQkd .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-zyBLJPsdIgEoQQkd .labelText,#mermaid-svg-zyBLJPsdIgEoQQkd .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-zyBLJPsdIgEoQQkd .loopText,#mermaid-svg-zyBLJPsdIgEoQQkd .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-zyBLJPsdIgEoQQkd .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-zyBLJPsdIgEoQQkd .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-zyBLJPsdIgEoQQkd .noteText,#mermaid-svg-zyBLJPsdIgEoQQkd .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-zyBLJPsdIgEoQQkd .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-zyBLJPsdIgEoQQkd .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-zyBLJPsdIgEoQQkd .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-zyBLJPsdIgEoQQkd .actorPopupMenu{position:absolute;}#mermaid-svg-zyBLJPsdIgEoQQkd .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-zyBLJPsdIgEoQQkd .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-zyBLJPsdIgEoQQkd .actor-man circle,#mermaid-svg-zyBLJPsdIgEoQQkd line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-zyBLJPsdIgEoQQkd :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
1. 订阅 Topic "order-events"
2. 重新平衡,分配 Partition
3. 分配 Partition 0,1
4. 从 Partition 0 拉取消息
5. 返回消息
6. 从 Partition 1 拉取消息
7. 返回消息
3.3 内部视图:Topic 的存储结构
#mermaid-svg-vo2JCTei3TVo3mJJ{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-vo2JCTei3TVo3mJJ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-vo2JCTei3TVo3mJJ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-vo2JCTei3TVo3mJJ .error-icon{fill:#552222;}#mermaid-svg-vo2JCTei3TVo3mJJ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-vo2JCTei3TVo3mJJ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-vo2JCTei3TVo3mJJ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-vo2JCTei3TVo3mJJ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-vo2JCTei3TVo3mJJ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-vo2JCTei3TVo3mJJ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-vo2JCTei3TVo3mJJ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-vo2JCTei3TVo3mJJ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-vo2JCTei3TVo3mJJ .marker.cross{stroke:#333333;}#mermaid-svg-vo2JCTei3TVo3mJJ svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-vo2JCTei3TVo3mJJ p{margin:0;}#mermaid-svg-vo2JCTei3TVo3mJJ .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-vo2JCTei3TVo3mJJ .cluster-label text{fill:#333;}#mermaid-svg-vo2JCTei3TVo3mJJ .cluster-label span{color:#333;}#mermaid-svg-vo2JCTei3TVo3mJJ .cluster-label span p{background-color:transparent;}#mermaid-svg-vo2JCTei3TVo3mJJ .label text,#mermaid-svg-vo2JCTei3TVo3mJJ span{fill:#333;color:#333;}#mermaid-svg-vo2JCTei3TVo3mJJ .node rect,#mermaid-svg-vo2JCTei3TVo3mJJ .node circle,#mermaid-svg-vo2JCTei3TVo3mJJ .node ellipse,#mermaid-svg-vo2JCTei3TVo3mJJ .node polygon,#mermaid-svg-vo2JCTei3TVo3mJJ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-vo2JCTei3TVo3mJJ .rough-node .label text,#mermaid-svg-vo2JCTei3TVo3mJJ .node .label text,#mermaid-svg-vo2JCTei3TVo3mJJ .image-shape .label,#mermaid-svg-vo2JCTei3TVo3mJJ .icon-shape .label{text-anchor:middle;}#mermaid-svg-vo2JCTei3TVo3mJJ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-vo2JCTei3TVo3mJJ .rough-node .label,#mermaid-svg-vo2JCTei3TVo3mJJ .node .label,#mermaid-svg-vo2JCTei3TVo3mJJ .image-shape .label,#mermaid-svg-vo2JCTei3TVo3mJJ .icon-shape .label{text-align:center;}#mermaid-svg-vo2JCTei3TVo3mJJ .node.clickable{cursor:pointer;}#mermaid-svg-vo2JCTei3TVo3mJJ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-vo2JCTei3TVo3mJJ .arrowheadPath{fill:#333333;}#mermaid-svg-vo2JCTei3TVo3mJJ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-vo2JCTei3TVo3mJJ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-vo2JCTei3TVo3mJJ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-vo2JCTei3TVo3mJJ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-vo2JCTei3TVo3mJJ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-vo2JCTei3TVo3mJJ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-vo2JCTei3TVo3mJJ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-vo2JCTei3TVo3mJJ .cluster text{fill:#333;}#mermaid-svg-vo2JCTei3TVo3mJJ .cluster span{color:#333;}#mermaid-svg-vo2JCTei3TVo3mJJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-vo2JCTei3TVo3mJJ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-vo2JCTei3TVo3mJJ rect.text{fill:none;stroke-width:0;}#mermaid-svg-vo2JCTei3TVo3mJJ .icon-shape,#mermaid-svg-vo2JCTei3TVo3mJJ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-vo2JCTei3TVo3mJJ .icon-shape p,#mermaid-svg-vo2JCTei3TVo3mJJ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-vo2JCTei3TVo3mJJ .icon-shape rect,#mermaid-svg-vo2JCTei3TVo3mJJ .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-vo2JCTei3TVo3mJJ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-vo2JCTei3TVo3mJJ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-vo2JCTei3TVo3mJJ :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
Topic: order-events
Partition 1 (Broker 2)
log文件消息序列
索引文件Offset→位置
时间索引时间→Offset
Partition 0 (Broker 1)
log文件消息序列
索引文件Offset→位置
时间索引时间→Offset
每个 Partition 在磁盘上对应一组文件:
- 00000000000000000000.log:存储实际消息数据的日志文件
- 00000000000000000000.index:偏移量索引,用于快速定位消息
- 00000000000000000000.timeindex:时间戳索引,用于按时间查询消息
四、Topic 的关键配置
4.1 创建 Topic 时的核心参数
| –partitions | Partition 数量 | 1 | 根据吞吐量估算(见下文) |
| –replication-factor | 副本因子 | 1 | 生产环境≥2,推荐 3 |
| –config cleanup.policy | 清理策略 | delete | delete/compact |
| –config retention.ms | 数据保留时间 | 7 天 | 根据业务需求 |
| –config retention.bytes | 数据保留大小 | -1(无限) | 根据磁盘容量 |
4.2 Partition 数量估算公式
// Partition 数量估算
int estimatedPartitions = max(
expectedThroughput / singlePartitionThroughput,
maxConsumerCountInGroup
);
// 例如:
// 预期吞吐量:100 MB/s
// 单 Partition 吞吐量:20 MB/s
// 消费者组最大并发数:10
int partitions = max(100/20, 10) = 5;
4.3 创建 Topic 的三种方式
# 1. 手动创建(生产环境推荐)
bin/kafka-topics.sh –create \\
–bootstrap-server localhost:9092 \\
–replication-factor 3 \\
–partitions 10 \\
–topic order-events
# 2. 自动创建(需配置 auto.create.topics.enable=true)
# 当生产者发送消息到不存在的 Topic 时自动创建
# 3. 通过代码创建(不常用)
AdminClient admin = AdminClient.create(props);
NewTopic newTopic = new NewTopic("order-events", 10, (short) 3);
admin.createTopics(Collections.singleton(newTopic));
五、Topic 的命名规范与最佳实践
5.1 命名规范建议
良好的 Topic 命名应遵循以下原则:
✅ 使用小写字母、数字、点和下划线
✅ 采用"点分"风格:<业务域>.<数据源>.<事件类型>
✅ 命名应具有自解释性
# 示例
– order-service.order-created.v1
– user-behavior.page-view
– inventory.stock-updated
– metrics.cpu.usage
5.2 Topic 数量规划
| 中小型应用 | 10-50 个 | 按业务模块划分 |
| 大型系统 | 50-200 个 | 细粒度划分,便于管理和权限控制 |
| 超大规模 | 200-1000 个 | 需配合自动化管理工具 |
5.3 Partition 数量规划原则
#mermaid-svg-RKQ0btbupMJyQm7u{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-RKQ0btbupMJyQm7u .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-RKQ0btbupMJyQm7u .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-RKQ0btbupMJyQm7u .error-icon{fill:#552222;}#mermaid-svg-RKQ0btbupMJyQm7u .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-RKQ0btbupMJyQm7u .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-RKQ0btbupMJyQm7u .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-RKQ0btbupMJyQm7u .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-RKQ0btbupMJyQm7u .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-RKQ0btbupMJyQm7u .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-RKQ0btbupMJyQm7u .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-RKQ0btbupMJyQm7u .marker{fill:#333333;stroke:#333333;}#mermaid-svg-RKQ0btbupMJyQm7u .marker.cross{stroke:#333333;}#mermaid-svg-RKQ0btbupMJyQm7u svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-RKQ0btbupMJyQm7u p{margin:0;}#mermaid-svg-RKQ0btbupMJyQm7u .edge{stroke-width:3;}#mermaid-svg-RKQ0btbupMJyQm7u .section–1 rect,#mermaid-svg-RKQ0btbupMJyQm7u .section–1 path,#mermaid-svg-RKQ0btbupMJyQm7u .section–1 circle,#mermaid-svg-RKQ0btbupMJyQm7u .section–1 polygon,#mermaid-svg-RKQ0btbupMJyQm7u .section–1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .section–1 text{fill:#ffffff;}#mermaid-svg-RKQ0btbupMJyQm7u .node-icon–1{font-size:40px;color:#ffffff;}#mermaid-svg-RKQ0btbupMJyQm7u .section-edge–1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .edge-depth–1{stroke-width:17;}#mermaid-svg-RKQ0btbupMJyQm7u .section–1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled,#mermaid-svg-RKQ0btbupMJyQm7u .disabled circle,#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:lightgray;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:#efefef;}#mermaid-svg-RKQ0btbupMJyQm7u .section-0 rect,#mermaid-svg-RKQ0btbupMJyQm7u .section-0 path,#mermaid-svg-RKQ0btbupMJyQm7u .section-0 circle,#mermaid-svg-RKQ0btbupMJyQm7u .section-0 polygon,#mermaid-svg-RKQ0btbupMJyQm7u .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-RKQ0btbupMJyQm7u .section-0 text{fill:black;}#mermaid-svg-RKQ0btbupMJyQm7u .node-icon-0{font-size:40px;color:black;}#mermaid-svg-RKQ0btbupMJyQm7u .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-RKQ0btbupMJyQm7u .edge-depth-0{stroke-width:14;}#mermaid-svg-RKQ0btbupMJyQm7u .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled,#mermaid-svg-RKQ0btbupMJyQm7u .disabled circle,#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:lightgray;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:#efefef;}#mermaid-svg-RKQ0btbupMJyQm7u .section-1 rect,#mermaid-svg-RKQ0btbupMJyQm7u .section-1 path,#mermaid-svg-RKQ0btbupMJyQm7u .section-1 circle,#mermaid-svg-RKQ0btbupMJyQm7u .section-1 polygon,#mermaid-svg-RKQ0btbupMJyQm7u .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .section-1 text{fill:black;}#mermaid-svg-RKQ0btbupMJyQm7u .node-icon-1{font-size:40px;color:black;}#mermaid-svg-RKQ0btbupMJyQm7u .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .edge-depth-1{stroke-width:11;}#mermaid-svg-RKQ0btbupMJyQm7u .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled,#mermaid-svg-RKQ0btbupMJyQm7u .disabled circle,#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:lightgray;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:#efefef;}#mermaid-svg-RKQ0btbupMJyQm7u .section-2 rect,#mermaid-svg-RKQ0btbupMJyQm7u .section-2 path,#mermaid-svg-RKQ0btbupMJyQm7u .section-2 circle,#mermaid-svg-RKQ0btbupMJyQm7u .section-2 polygon,#mermaid-svg-RKQ0btbupMJyQm7u .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .section-2 text{fill:#ffffff;}#mermaid-svg-RKQ0btbupMJyQm7u .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-RKQ0btbupMJyQm7u .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .edge-depth-2{stroke-width:8;}#mermaid-svg-RKQ0btbupMJyQm7u .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled,#mermaid-svg-RKQ0btbupMJyQm7u .disabled circle,#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:lightgray;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:#efefef;}#mermaid-svg-RKQ0btbupMJyQm7u .section-3 rect,#mermaid-svg-RKQ0btbupMJyQm7u .section-3 path,#mermaid-svg-RKQ0btbupMJyQm7u .section-3 circle,#mermaid-svg-RKQ0btbupMJyQm7u .section-3 polygon,#mermaid-svg-RKQ0btbupMJyQm7u .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .section-3 text{fill:black;}#mermaid-svg-RKQ0btbupMJyQm7u .node-icon-3{font-size:40px;color:black;}#mermaid-svg-RKQ0btbupMJyQm7u .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .edge-depth-3{stroke-width:5;}#mermaid-svg-RKQ0btbupMJyQm7u .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled,#mermaid-svg-RKQ0btbupMJyQm7u .disabled circle,#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:lightgray;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:#efefef;}#mermaid-svg-RKQ0btbupMJyQm7u .section-4 rect,#mermaid-svg-RKQ0btbupMJyQm7u .section-4 path,#mermaid-svg-RKQ0btbupMJyQm7u .section-4 circle,#mermaid-svg-RKQ0btbupMJyQm7u .section-4 polygon,#mermaid-svg-RKQ0btbupMJyQm7u .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .section-4 text{fill:black;}#mermaid-svg-RKQ0btbupMJyQm7u .node-icon-4{font-size:40px;color:black;}#mermaid-svg-RKQ0btbupMJyQm7u .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .edge-depth-4{stroke-width:2;}#mermaid-svg-RKQ0btbupMJyQm7u .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled,#mermaid-svg-RKQ0btbupMJyQm7u .disabled circle,#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:lightgray;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:#efefef;}#mermaid-svg-RKQ0btbupMJyQm7u .section-5 rect,#mermaid-svg-RKQ0btbupMJyQm7u .section-5 path,#mermaid-svg-RKQ0btbupMJyQm7u .section-5 circle,#mermaid-svg-RKQ0btbupMJyQm7u .section-5 polygon,#mermaid-svg-RKQ0btbupMJyQm7u .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .section-5 text{fill:black;}#mermaid-svg-RKQ0btbupMJyQm7u .node-icon-5{font-size:40px;color:black;}#mermaid-svg-RKQ0btbupMJyQm7u .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .edge-depth-5{stroke-width:-1;}#mermaid-svg-RKQ0btbupMJyQm7u .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled,#mermaid-svg-RKQ0btbupMJyQm7u .disabled circle,#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:lightgray;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:#efefef;}#mermaid-svg-RKQ0btbupMJyQm7u .section-6 rect,#mermaid-svg-RKQ0btbupMJyQm7u .section-6 path,#mermaid-svg-RKQ0btbupMJyQm7u .section-6 circle,#mermaid-svg-RKQ0btbupMJyQm7u .section-6 polygon,#mermaid-svg-RKQ0btbupMJyQm7u .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .section-6 text{fill:black;}#mermaid-svg-RKQ0btbupMJyQm7u .node-icon-6{font-size:40px;color:black;}#mermaid-svg-RKQ0btbupMJyQm7u .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .edge-depth-6{stroke-width:-4;}#mermaid-svg-RKQ0btbupMJyQm7u .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled,#mermaid-svg-RKQ0btbupMJyQm7u .disabled circle,#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:lightgray;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:#efefef;}#mermaid-svg-RKQ0btbupMJyQm7u .section-7 rect,#mermaid-svg-RKQ0btbupMJyQm7u .section-7 path,#mermaid-svg-RKQ0btbupMJyQm7u .section-7 circle,#mermaid-svg-RKQ0btbupMJyQm7u .section-7 polygon,#mermaid-svg-RKQ0btbupMJyQm7u .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .section-7 text{fill:black;}#mermaid-svg-RKQ0btbupMJyQm7u .node-icon-7{font-size:40px;color:black;}#mermaid-svg-RKQ0btbupMJyQm7u .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .edge-depth-7{stroke-width:-7;}#mermaid-svg-RKQ0btbupMJyQm7u .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled,#mermaid-svg-RKQ0btbupMJyQm7u .disabled circle,#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:lightgray;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:#efefef;}#mermaid-svg-RKQ0btbupMJyQm7u .section-8 rect,#mermaid-svg-RKQ0btbupMJyQm7u .section-8 path,#mermaid-svg-RKQ0btbupMJyQm7u .section-8 circle,#mermaid-svg-RKQ0btbupMJyQm7u .section-8 polygon,#mermaid-svg-RKQ0btbupMJyQm7u .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .section-8 text{fill:black;}#mermaid-svg-RKQ0btbupMJyQm7u .node-icon-8{font-size:40px;color:black;}#mermaid-svg-RKQ0btbupMJyQm7u .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .edge-depth-8{stroke-width:-10;}#mermaid-svg-RKQ0btbupMJyQm7u .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled,#mermaid-svg-RKQ0btbupMJyQm7u .disabled circle,#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:lightgray;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:#efefef;}#mermaid-svg-RKQ0btbupMJyQm7u .section-9 rect,#mermaid-svg-RKQ0btbupMJyQm7u .section-9 path,#mermaid-svg-RKQ0btbupMJyQm7u .section-9 circle,#mermaid-svg-RKQ0btbupMJyQm7u .section-9 polygon,#mermaid-svg-RKQ0btbupMJyQm7u .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .section-9 text{fill:black;}#mermaid-svg-RKQ0btbupMJyQm7u .node-icon-9{font-size:40px;color:black;}#mermaid-svg-RKQ0btbupMJyQm7u .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .edge-depth-9{stroke-width:-13;}#mermaid-svg-RKQ0btbupMJyQm7u .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled,#mermaid-svg-RKQ0btbupMJyQm7u .disabled circle,#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:lightgray;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:#efefef;}#mermaid-svg-RKQ0btbupMJyQm7u .section-10 rect,#mermaid-svg-RKQ0btbupMJyQm7u .section-10 path,#mermaid-svg-RKQ0btbupMJyQm7u .section-10 circle,#mermaid-svg-RKQ0btbupMJyQm7u .section-10 polygon,#mermaid-svg-RKQ0btbupMJyQm7u .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .section-10 text{fill:black;}#mermaid-svg-RKQ0btbupMJyQm7u .node-icon-10{font-size:40px;color:black;}#mermaid-svg-RKQ0btbupMJyQm7u .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .edge-depth-10{stroke-width:-16;}#mermaid-svg-RKQ0btbupMJyQm7u .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled,#mermaid-svg-RKQ0btbupMJyQm7u .disabled circle,#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:lightgray;}#mermaid-svg-RKQ0btbupMJyQm7u .disabled text{fill:#efefef;}#mermaid-svg-RKQ0btbupMJyQm7u .section-root rect,#mermaid-svg-RKQ0btbupMJyQm7u .section-root path,#mermaid-svg-RKQ0btbupMJyQm7u .section-root circle,#mermaid-svg-RKQ0btbupMJyQm7u .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-RKQ0btbupMJyQm7u .section-root text{fill:#ffffff;}#mermaid-svg-RKQ0btbupMJyQm7u .section-root span{color:#ffffff;}#mermaid-svg-RKQ0btbupMJyQm7u .section-2 span{color:#ffffff;}#mermaid-svg-RKQ0btbupMJyQm7u .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-RKQ0btbupMJyQm7u .edge{fill:none;}#mermaid-svg-RKQ0btbupMJyQm7u .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-RKQ0btbupMJyQm7u :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
Partition规划原则
吞吐量需求
单 Partition 约 10-20 MB/s
按预期峰值计算
消费者并发
不超过 Partition 数
考虑未来扩展
顺序性要求
需保证顺序的业务放在同一 Partition
使用相同 Key
端到端延迟
Partition 过多增加延迟
平衡吞吐与延迟
5.4 清理策略选择
Kafka 支持两种清理策略:
| delete | 基于时间或大小删除旧数据 | 日志数据、临时事件 |
| compact | 基于 Key 保留最新数据 | 数据库变更日志、配置信息 |
# 配置 compact 策略
bin/kafka-topics.sh –alter \\
–topic user-profile \\
–config cleanup.policy=compact
六、Topic 的监控与管理
6.1 常用管理命令
# 查看所有 Topic
bin/kafka-topics.sh –list –bootstrap-server localhost:9092
# 查看 Topic 详情
bin/kafka-topics.sh –describe –topic order-events –bootstrap-server localhost:9092
# 输出示例:
# Topic: order-events PartitionCount: 10 ReplicationFactor: 3
# Topic: order-events Partition: 0 Leader: 1 Replicas: 1,2,3 Isr: 1,2,3
# Topic: order-events Partition: 1 Leader: 2 Replicas: 2,3,1 Isr: 2,3,1
# 修改 Topic(增加分区数)
bin/kafka-topics.sh –alter \\
–topic order-events \\
–partitions 20 \\
–bootstrap-server localhost:9092
6.2 关键监控指标
| Messages in per sec | Topic 写入流量 | 超过预期峰值 |
| Bytes in/out per sec | 网络带宽使用 | 接近网卡上限 |
| Total time (ms) | 消息处理延迟 | > 100ms |
| Under replicated partitions | 分区副本同步延迟 | > 0 |
七、实战示例:订单系统 Topic 设计
7.1 业务场景
假设我们设计一个电商订单系统,需要处理以下事件流:
#mermaid-svg-DSwKDtggRHcpEDcS{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-DSwKDtggRHcpEDcS .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-DSwKDtggRHcpEDcS .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-DSwKDtggRHcpEDcS .error-icon{fill:#552222;}#mermaid-svg-DSwKDtggRHcpEDcS .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-DSwKDtggRHcpEDcS .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-DSwKDtggRHcpEDcS .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-DSwKDtggRHcpEDcS .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-DSwKDtggRHcpEDcS .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-DSwKDtggRHcpEDcS .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-DSwKDtggRHcpEDcS .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-DSwKDtggRHcpEDcS .marker{fill:#333333;stroke:#333333;}#mermaid-svg-DSwKDtggRHcpEDcS .marker.cross{stroke:#333333;}#mermaid-svg-DSwKDtggRHcpEDcS svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-DSwKDtggRHcpEDcS p{margin:0;}#mermaid-svg-DSwKDtggRHcpEDcS .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-DSwKDtggRHcpEDcS .cluster-label text{fill:#333;}#mermaid-svg-DSwKDtggRHcpEDcS .cluster-label span{color:#333;}#mermaid-svg-DSwKDtggRHcpEDcS .cluster-label span p{background-color:transparent;}#mermaid-svg-DSwKDtggRHcpEDcS .label text,#mermaid-svg-DSwKDtggRHcpEDcS span{fill:#333;color:#333;}#mermaid-svg-DSwKDtggRHcpEDcS .node rect,#mermaid-svg-DSwKDtggRHcpEDcS .node circle,#mermaid-svg-DSwKDtggRHcpEDcS .node ellipse,#mermaid-svg-DSwKDtggRHcpEDcS .node polygon,#mermaid-svg-DSwKDtggRHcpEDcS .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-DSwKDtggRHcpEDcS .rough-node .label text,#mermaid-svg-DSwKDtggRHcpEDcS .node .label text,#mermaid-svg-DSwKDtggRHcpEDcS .image-shape .label,#mermaid-svg-DSwKDtggRHcpEDcS .icon-shape .label{text-anchor:middle;}#mermaid-svg-DSwKDtggRHcpEDcS .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-DSwKDtggRHcpEDcS .rough-node .label,#mermaid-svg-DSwKDtggRHcpEDcS .node .label,#mermaid-svg-DSwKDtggRHcpEDcS .image-shape .label,#mermaid-svg-DSwKDtggRHcpEDcS .icon-shape .label{text-align:center;}#mermaid-svg-DSwKDtggRHcpEDcS .node.clickable{cursor:pointer;}#mermaid-svg-DSwKDtggRHcpEDcS .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-DSwKDtggRHcpEDcS .arrowheadPath{fill:#333333;}#mermaid-svg-DSwKDtggRHcpEDcS .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-DSwKDtggRHcpEDcS .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-DSwKDtggRHcpEDcS .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-DSwKDtggRHcpEDcS .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-DSwKDtggRHcpEDcS .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-DSwKDtggRHcpEDcS .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-DSwKDtggRHcpEDcS .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-DSwKDtggRHcpEDcS .cluster text{fill:#333;}#mermaid-svg-DSwKDtggRHcpEDcS .cluster span{color:#333;}#mermaid-svg-DSwKDtggRHcpEDcS div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-DSwKDtggRHcpEDcS .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-DSwKDtggRHcpEDcS rect.text{fill:none;stroke-width:0;}#mermaid-svg-DSwKDtggRHcpEDcS .icon-shape,#mermaid-svg-DSwKDtggRHcpEDcS .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-DSwKDtggRHcpEDcS .icon-shape p,#mermaid-svg-DSwKDtggRHcpEDcS .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-DSwKDtggRHcpEDcS .icon-shape rect,#mermaid-svg-DSwKDtggRHcpEDcS .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-DSwKDtggRHcpEDcS .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-DSwKDtggRHcpEDcS .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-DSwKDtggRHcpEDcS :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
消费者
生产者
Kafka Topic
订单服务
order-events
库存服务
inventory-events
支付服务
payment-events
用户行为
user-behavior
数据分析
实时报表
风控系统
推荐引擎
7.2 Topic 规划方案
| order-events | 12 | 3 | 7 天 | 订单状态变更事件 |
| inventory-events | 6 | 3 | 3 天 | 库存变更事件 |
| payment-events | 8 | 3 | 30 天 | 支付事件(审计需要) |
| user-behavior | 20 | 3 | 2 天 | 用户浏览/点击事件 |
八、总结
8.1 Topic 核心要点回顾
| 定义 | 消息的逻辑分类单元,类似于数据库中的表 |
| 作用 | 消息分类、订阅点、命名空间、存储单元 |
| 内部结构 | 由 1 到多个 Partition 组成,每个 Partition 是有序的日志文件 |
| 关键参数 | Partition 数、副本因子、清理策略、保留时间 |
| 命名规范 | 采用"业务域.数据源.事件类型"格式 |
8.2 Topic 的完整生命周期
#mermaid-svg-dOEtDAMB4g8U0EVJ{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-dOEtDAMB4g8U0EVJ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-dOEtDAMB4g8U0EVJ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-dOEtDAMB4g8U0EVJ .error-icon{fill:#552222;}#mermaid-svg-dOEtDAMB4g8U0EVJ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-dOEtDAMB4g8U0EVJ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-dOEtDAMB4g8U0EVJ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-dOEtDAMB4g8U0EVJ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-dOEtDAMB4g8U0EVJ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-dOEtDAMB4g8U0EVJ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-dOEtDAMB4g8U0EVJ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-dOEtDAMB4g8U0EVJ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-dOEtDAMB4g8U0EVJ .marker.cross{stroke:#333333;}#mermaid-svg-dOEtDAMB4g8U0EVJ svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-dOEtDAMB4g8U0EVJ p{margin:0;}#mermaid-svg-dOEtDAMB4g8U0EVJ .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-dOEtDAMB4g8U0EVJ .cluster-label text{fill:#333;}#mermaid-svg-dOEtDAMB4g8U0EVJ .cluster-label span{color:#333;}#mermaid-svg-dOEtDAMB4g8U0EVJ .cluster-label span p{background-color:transparent;}#mermaid-svg-dOEtDAMB4g8U0EVJ .label text,#mermaid-svg-dOEtDAMB4g8U0EVJ span{fill:#333;color:#333;}#mermaid-svg-dOEtDAMB4g8U0EVJ .node rect,#mermaid-svg-dOEtDAMB4g8U0EVJ .node circle,#mermaid-svg-dOEtDAMB4g8U0EVJ .node ellipse,#mermaid-svg-dOEtDAMB4g8U0EVJ .node polygon,#mermaid-svg-dOEtDAMB4g8U0EVJ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-dOEtDAMB4g8U0EVJ .rough-node .label text,#mermaid-svg-dOEtDAMB4g8U0EVJ .node .label text,#mermaid-svg-dOEtDAMB4g8U0EVJ .image-shape .label,#mermaid-svg-dOEtDAMB4g8U0EVJ .icon-shape .label{text-anchor:middle;}#mermaid-svg-dOEtDAMB4g8U0EVJ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-dOEtDAMB4g8U0EVJ .rough-node .label,#mermaid-svg-dOEtDAMB4g8U0EVJ .node .label,#mermaid-svg-dOEtDAMB4g8U0EVJ .image-shape .label,#mermaid-svg-dOEtDAMB4g8U0EVJ .icon-shape .label{text-align:center;}#mermaid-svg-dOEtDAMB4g8U0EVJ .node.clickable{cursor:pointer;}#mermaid-svg-dOEtDAMB4g8U0EVJ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-dOEtDAMB4g8U0EVJ .arrowheadPath{fill:#333333;}#mermaid-svg-dOEtDAMB4g8U0EVJ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-dOEtDAMB4g8U0EVJ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-dOEtDAMB4g8U0EVJ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-dOEtDAMB4g8U0EVJ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-dOEtDAMB4g8U0EVJ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-dOEtDAMB4g8U0EVJ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-dOEtDAMB4g8U0EVJ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-dOEtDAMB4g8U0EVJ .cluster text{fill:#333;}#mermaid-svg-dOEtDAMB4g8U0EVJ .cluster span{color:#333;}#mermaid-svg-dOEtDAMB4g8U0EVJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-dOEtDAMB4g8U0EVJ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-dOEtDAMB4g8U0EVJ rect.text{fill:none;stroke-width:0;}#mermaid-svg-dOEtDAMB4g8U0EVJ .icon-shape,#mermaid-svg-dOEtDAMB4g8U0EVJ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-dOEtDAMB4g8U0EVJ .icon-shape p,#mermaid-svg-dOEtDAMB4g8U0EVJ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-dOEtDAMB4g8U0EVJ .icon-shape rect,#mermaid-svg-dOEtDAMB4g8U0EVJ .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-dOEtDAMB4g8U0EVJ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-dOEtDAMB4g8U0EVJ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-dOEtDAMB4g8U0EVJ :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
规划阶段确定分区数、副本数
创建 Topic
生产阶段消息写入
消费阶段消息读取
清理阶段根据策略删除旧数据
8.3 一句话总结
Kafka 的 Topic 是消息的逻辑容器,通过 Partition 实现物理存储和并行处理,是连接生产者和消费者的桥梁,也是 Kafka 实现高吞吐、可扩展、持久化存储的基石。正确设计和规划 Topic,是构建高性能 Kafka 应用的第一步。

|
🌺The End🌺点点关注,收藏不迷路🌺 |






