MQTT 共享订阅$share/原理:怎么用它给消费端水平扩容做负载均衡

MQTT 共享订阅$share/原理:怎么用它给消费端水平扩容做负载均衡

单消费者处理不过来,你会怎么办?MQTT 里最"反直觉"的一个点是:同一个主题订阅多个客户端,默认不是"分摊",而是"广播" ------每个订阅者都收到完整的一份。想实现类似 Kafka 消费组那样的"一条消息只被组里一个消费者处理",就得靠共享订阅(Shared Subscription)。这篇讲透它的原理、语法、负载均衡策略和那些容易踩的坑。


一、先搞懂三种订阅模式

MQTT 的订阅,按"消息如何分发"分三种:
#mermaid-svg-cX7xe4cGCTkbcnS0{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-cX7xe4cGCTkbcnS0 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-cX7xe4cGCTkbcnS0 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-cX7xe4cGCTkbcnS0 .error-icon{fill:#552222;}#mermaid-svg-cX7xe4cGCTkbcnS0 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-cX7xe4cGCTkbcnS0 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-cX7xe4cGCTkbcnS0 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-cX7xe4cGCTkbcnS0 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-cX7xe4cGCTkbcnS0 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-cX7xe4cGCTkbcnS0 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-cX7xe4cGCTkbcnS0 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-cX7xe4cGCTkbcnS0 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-cX7xe4cGCTkbcnS0 .marker.cross{stroke:#333333;}#mermaid-svg-cX7xe4cGCTkbcnS0 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-cX7xe4cGCTkbcnS0 p{margin:0;}#mermaid-svg-cX7xe4cGCTkbcnS0 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-cX7xe4cGCTkbcnS0 .cluster-label text{fill:#333;}#mermaid-svg-cX7xe4cGCTkbcnS0 .cluster-label span{color:#333;}#mermaid-svg-cX7xe4cGCTkbcnS0 .cluster-label span p{background-color:transparent;}#mermaid-svg-cX7xe4cGCTkbcnS0 .label text,#mermaid-svg-cX7xe4cGCTkbcnS0 span{fill:#333;color:#333;}#mermaid-svg-cX7xe4cGCTkbcnS0 .node rect,#mermaid-svg-cX7xe4cGCTkbcnS0 .node circle,#mermaid-svg-cX7xe4cGCTkbcnS0 .node ellipse,#mermaid-svg-cX7xe4cGCTkbcnS0 .node polygon,#mermaid-svg-cX7xe4cGCTkbcnS0 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-cX7xe4cGCTkbcnS0 .rough-node .label text,#mermaid-svg-cX7xe4cGCTkbcnS0 .node .label text,#mermaid-svg-cX7xe4cGCTkbcnS0 .image-shape .label,#mermaid-svg-cX7xe4cGCTkbcnS0 .icon-shape .label{text-anchor:middle;}#mermaid-svg-cX7xe4cGCTkbcnS0 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-cX7xe4cGCTkbcnS0 .rough-node .label,#mermaid-svg-cX7xe4cGCTkbcnS0 .node .label,#mermaid-svg-cX7xe4cGCTkbcnS0 .image-shape .label,#mermaid-svg-cX7xe4cGCTkbcnS0 .icon-shape .label{text-align:center;}#mermaid-svg-cX7xe4cGCTkbcnS0 .node.clickable{cursor:pointer;}#mermaid-svg-cX7xe4cGCTkbcnS0 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-cX7xe4cGCTkbcnS0 .arrowheadPath{fill:#333333;}#mermaid-svg-cX7xe4cGCTkbcnS0 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-cX7xe4cGCTkbcnS0 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-cX7xe4cGCTkbcnS0 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-cX7xe4cGCTkbcnS0 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-cX7xe4cGCTkbcnS0 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-cX7xe4cGCTkbcnS0 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-cX7xe4cGCTkbcnS0 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-cX7xe4cGCTkbcnS0 .cluster text{fill:#333;}#mermaid-svg-cX7xe4cGCTkbcnS0 .cluster span{color:#333;}#mermaid-svg-cX7xe4cGCTkbcnS0 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-cX7xe4cGCTkbcnS0 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-cX7xe4cGCTkbcnS0 rect.text{fill:none;stroke-width:0;}#mermaid-svg-cX7xe4cGCTkbcnS0 .icon-shape,#mermaid-svg-cX7xe4cGCTkbcnS0 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-cX7xe4cGCTkbcnS0 .icon-shape p,#mermaid-svg-cX7xe4cGCTkbcnS0 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-cX7xe4cGCTkbcnS0 .icon-shape rect,#mermaid-svg-cX7xe4cGCTkbcnS0 .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-cX7xe4cGCTkbcnS0 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-cX7xe4cGCTkbcnS0 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-cX7xe4cGCTkbcnS0 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} ② 共享订阅(队列/分摊)
消息1
消息2
消息3
Broker
组内消费者1
组内消费者2
组内消费者3
① 普通订阅(广播)
同一条消息
同一条消息
同一条消息
Broker
订阅者1
订阅者2
订阅者3

  • 普通订阅:广播语义。适合"监控大屏、告警服务、手机 App 都要各收一份"的场景。
  • 共享订阅 :队列语义。同一组内的订阅者互斥,一条消息只发给其中一个,适合"多消费者分摊处理"。

最容易搞混的对比:普通订阅 = "人人有份";共享订阅 = "组内一人一份"。

附赠一个知识点:共享订阅是 MQTT 5.0 的标准特性 ,但 EMQX 在 3.1.1 上也做了扩展支持 ,所以国内 EMQX 用户即使还在用 3.1.1,也能用 $share/。


二、语法:$share/{组名}/{主题过滤器}

共享订阅的过滤器格式是固定的三段:

text 复制代码
$share / 组名 / 实际主题过滤器

示例:

普通订阅 共享订阅 含义
iot/dev/# $share/data-pool/iot/dev/# data-pool 组内分摊 iot/dev/# 的消息
alerts/critical $share/alarm-workers/alerts/critical alarm-workers 组分摊告警消息

规则(重点,容易踩坑):

  1. $share 必须打头,是保留前缀(还记得第 4 篇讲的 $ 开头主题吗------通配符 #/+ 匹配不到它)。
  2. 组名(中间段)不能包含 /、+、#。
  3. 第三段才是真正的主题过滤器,可以正常用通配符。

三、负载均衡策略:不是只有"轮询"

Broker 在"组内多个消费者之间怎么挑一个"这件事上,策略是可配的。以 EMQX 为例(broker.shared_subscription_strategy),常见策略:

策略 分发逻辑 适用场景
round_robin 轮流分发给组内订阅者(默认) 通用,各消费者能力均衡
random 随机挑一个 简单场景
sticky 尽量固定发给同一个消费者 需要"同一来源消息串行处理"时
hash_clientid 按发布者 Client ID 哈希到固定消费者 保证"某设备的消息总被同一消费者处理",利于幂等/时序
hash_topic 按主题哈希到固定消费者 保证"某主题的消息总被同一消费者处理"

#mermaid-svg-fCv27NSbOarwV58t{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-fCv27NSbOarwV58t .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-fCv27NSbOarwV58t .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-fCv27NSbOarwV58t .error-icon{fill:#552222;}#mermaid-svg-fCv27NSbOarwV58t .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-fCv27NSbOarwV58t .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-fCv27NSbOarwV58t .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-fCv27NSbOarwV58t .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-fCv27NSbOarwV58t .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-fCv27NSbOarwV58t .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-fCv27NSbOarwV58t .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-fCv27NSbOarwV58t .marker{fill:#333333;stroke:#333333;}#mermaid-svg-fCv27NSbOarwV58t .marker.cross{stroke:#333333;}#mermaid-svg-fCv27NSbOarwV58t svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-fCv27NSbOarwV58t p{margin:0;}#mermaid-svg-fCv27NSbOarwV58t .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-fCv27NSbOarwV58t .cluster-label text{fill:#333;}#mermaid-svg-fCv27NSbOarwV58t .cluster-label span{color:#333;}#mermaid-svg-fCv27NSbOarwV58t .cluster-label span p{background-color:transparent;}#mermaid-svg-fCv27NSbOarwV58t .label text,#mermaid-svg-fCv27NSbOarwV58t span{fill:#333;color:#333;}#mermaid-svg-fCv27NSbOarwV58t .node rect,#mermaid-svg-fCv27NSbOarwV58t .node circle,#mermaid-svg-fCv27NSbOarwV58t .node ellipse,#mermaid-svg-fCv27NSbOarwV58t .node polygon,#mermaid-svg-fCv27NSbOarwV58t .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-fCv27NSbOarwV58t .rough-node .label text,#mermaid-svg-fCv27NSbOarwV58t .node .label text,#mermaid-svg-fCv27NSbOarwV58t .image-shape .label,#mermaid-svg-fCv27NSbOarwV58t .icon-shape .label{text-anchor:middle;}#mermaid-svg-fCv27NSbOarwV58t .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-fCv27NSbOarwV58t .rough-node .label,#mermaid-svg-fCv27NSbOarwV58t .node .label,#mermaid-svg-fCv27NSbOarwV58t .image-shape .label,#mermaid-svg-fCv27NSbOarwV58t .icon-shape .label{text-align:center;}#mermaid-svg-fCv27NSbOarwV58t .node.clickable{cursor:pointer;}#mermaid-svg-fCv27NSbOarwV58t .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-fCv27NSbOarwV58t .arrowheadPath{fill:#333333;}#mermaid-svg-fCv27NSbOarwV58t .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-fCv27NSbOarwV58t .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-fCv27NSbOarwV58t .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-fCv27NSbOarwV58t .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-fCv27NSbOarwV58t .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-fCv27NSbOarwV58t .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-fCv27NSbOarwV58t .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-fCv27NSbOarwV58t .cluster text{fill:#333;}#mermaid-svg-fCv27NSbOarwV58t .cluster span{color:#333;}#mermaid-svg-fCv27NSbOarwV58t 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-fCv27NSbOarwV58t .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-fCv27NSbOarwV58t rect.text{fill:none;stroke-width:0;}#mermaid-svg-fCv27NSbOarwV58t .icon-shape,#mermaid-svg-fCv27NSbOarwV58t .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-fCv27NSbOarwV58t .icon-shape p,#mermaid-svg-fCv27NSbOarwV58t .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-fCv27NSbOarwV58t .icon-shape rect,#mermaid-svg-fCv27NSbOarwV58t .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-fCv27NSbOarwV58t .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-fCv27NSbOarwV58t .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-fCv27NSbOarwV58t :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 分发策略
hash_clientid
hash_clientid
hash_clientid
消息 来源A
固定→消费者1
消息 来源B
固定→消费者2
消息 来源C

选型要点 :如果下游处理要求时序 (同一台设备的消息必须按序处理),就用 hash_clientid 或 hash_topic,把"同源消息"钉在同一个消费者上;否则 round_robin 的负载最均匀。


四、典型场景:消费端水平扩容

设想你有 100 万台设备,每秒上报几万条消息,需要写库、做实时分析、触发告警。单个消费服务处理不过来,怎么办?
#mermaid-svg-KMPIBWdTVBQMX084{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-KMPIBWdTVBQMX084 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-KMPIBWdTVBQMX084 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-KMPIBWdTVBQMX084 .error-icon{fill:#552222;}#mermaid-svg-KMPIBWdTVBQMX084 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-KMPIBWdTVBQMX084 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-KMPIBWdTVBQMX084 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-KMPIBWdTVBQMX084 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-KMPIBWdTVBQMX084 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-KMPIBWdTVBQMX084 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-KMPIBWdTVBQMX084 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-KMPIBWdTVBQMX084 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-KMPIBWdTVBQMX084 .marker.cross{stroke:#333333;}#mermaid-svg-KMPIBWdTVBQMX084 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-KMPIBWdTVBQMX084 p{margin:0;}#mermaid-svg-KMPIBWdTVBQMX084 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-KMPIBWdTVBQMX084 .cluster-label text{fill:#333;}#mermaid-svg-KMPIBWdTVBQMX084 .cluster-label span{color:#333;}#mermaid-svg-KMPIBWdTVBQMX084 .cluster-label span p{background-color:transparent;}#mermaid-svg-KMPIBWdTVBQMX084 .label text,#mermaid-svg-KMPIBWdTVBQMX084 span{fill:#333;color:#333;}#mermaid-svg-KMPIBWdTVBQMX084 .node rect,#mermaid-svg-KMPIBWdTVBQMX084 .node circle,#mermaid-svg-KMPIBWdTVBQMX084 .node ellipse,#mermaid-svg-KMPIBWdTVBQMX084 .node polygon,#mermaid-svg-KMPIBWdTVBQMX084 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-KMPIBWdTVBQMX084 .rough-node .label text,#mermaid-svg-KMPIBWdTVBQMX084 .node .label text,#mermaid-svg-KMPIBWdTVBQMX084 .image-shape .label,#mermaid-svg-KMPIBWdTVBQMX084 .icon-shape .label{text-anchor:middle;}#mermaid-svg-KMPIBWdTVBQMX084 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-KMPIBWdTVBQMX084 .rough-node .label,#mermaid-svg-KMPIBWdTVBQMX084 .node .label,#mermaid-svg-KMPIBWdTVBQMX084 .image-shape .label,#mermaid-svg-KMPIBWdTVBQMX084 .icon-shape .label{text-align:center;}#mermaid-svg-KMPIBWdTVBQMX084 .node.clickable{cursor:pointer;}#mermaid-svg-KMPIBWdTVBQMX084 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-KMPIBWdTVBQMX084 .arrowheadPath{fill:#333333;}#mermaid-svg-KMPIBWdTVBQMX084 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-KMPIBWdTVBQMX084 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-KMPIBWdTVBQMX084 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-KMPIBWdTVBQMX084 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-KMPIBWdTVBQMX084 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-KMPIBWdTVBQMX084 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-KMPIBWdTVBQMX084 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-KMPIBWdTVBQMX084 .cluster text{fill:#333;}#mermaid-svg-KMPIBWdTVBQMX084 .cluster span{color:#333;}#mermaid-svg-KMPIBWdTVBQMX084 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-KMPIBWdTVBQMX084 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-KMPIBWdTVBQMX084 rect.text{fill:none;stroke-width:0;}#mermaid-svg-KMPIBWdTVBQMX084 .icon-shape,#mermaid-svg-KMPIBWdTVBQMX084 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-KMPIBWdTVBQMX084 .icon-shape p,#mermaid-svg-KMPIBWdTVBQMX084 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-KMPIBWdTVBQMX084 .icon-shape rect,#mermaid-svg-KMPIBWdTVBQMX084 .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-KMPIBWdTVBQMX084 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-KMPIBWdTVBQMX084 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-KMPIBWdTVBQMX084 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 发布
share/processors/iot/dev/# share/processors/iot/dev/#
$share/processors/iot/dev/#
100万台设备
Broker
消费者1

处理消息1/4/7...
消费者2

处理消息2/5/8...
消费者3

处理消息3/6/9...
数据库

扩容就一句话:加一个消费者实例,订阅同一个 $share 组,Broker 自动把负载重新分摊过去。 无需改设备端、无需改发布端,这是消费端水平扩容最优雅的方式。


五、📌 可拿走资产:三种订阅模式对比 + 负载均衡选型

模式 分发语义 订阅写法 典型用途
普通订阅 广播,人人有份 iot/dev/# 大屏、告警、多副本
共享订阅 队列,组内互斥 $share/group/iot/dev/# 多消费者分摊处理
(对照)Kafka 消费组 组内互斥、组间广播 --- 与共享订阅同构,便于理解

负载均衡选型速查:

你的需求 推荐策略
各消费者能力一致,只求负载均匀 round_robin
同一条消息流需要串行保序处理 hash_clientid / hash_topic
无所谓,越简单越好 random
尽量让会话粘着,减少状态重建 sticky

共享订阅避坑清单:

  1. 别把普通订阅和共享订阅混用:同一个客户端既普通订阅又共享订阅同一主题,会收到"广播的一份 + 分摊的一份"。
  2. 组名别带 /、+、#。
  3. $share 是保留前缀 ,用 #/+ 订阅不到 $share 相关主题。
  4. 扩容不等于无限快:组内消费者数量超过消息处理瓶颈后,收益递减;真正的瓶颈往往在下游(DB/下游服务)。

六、写在最后

共享订阅解决了"消费端扩容",但它只是"消息流转"的一个环节。你有没有好奇过:一条消息从设备发出来,在 EMQX 内部到底经历了什么,才到达那个消费者手里的? 订阅树是怎么匹配的?离线队列在哪?集群下跨节点的消息又是怎么路由的?

下一篇 一条MQTT消息在EMQX内部怎么流转------订阅树、主题匹配、会话队列(架构视角),我带你钻进 Broker 的"内脏",把整条链路看清------这是"会用"和"真懂 Broker"的分水岭。


🔔 评论区提问: 你在做消费端扩容时,是选择"多个消费者共享订阅"还是"引入 Kafka 这类消息队列做缓冲"?两种方案的边界你是怎么判断的?欢迎聊聊你的实践。

相关推荐
海宇AI1 小时前
零信任架构实战:基于海宇行驶OCR证识别构建自动化租车车队行驶证电子档案网关
运维·人工智能·架构·自动化·1024程序员节
北京中科新远科技2 小时前
高速接口兼容排查实战:OSFP、QSFP112与QSFP56如何逐层验证
linux·运维·网络
Ruiery2 小时前
Linux 6.6内核 CPU 深度解析(零):x86 内核引导协议 — 从 bootloader 到 main()
linux·运维·服务器
Ruiery2 小时前
Linux 6.6内核 CPU 深度解析(十六):x86 保护机制 — 从 ring 到页表的权限防线
linux·运维·服务器
liubinkaixin2 小时前
一炉硬质合金的温度曲线,卡在几十根补偿导线上
物联网·1024程序员节
不老刘2 小时前
异构双 Agent 协同:构建无人值守的长任务自动化研发流水线
运维·人工智能·自动化·ai编程·1024程序员节
织码weavecodes3 小时前
医疗、零售、物业培训怎么落地?织码在线教育系统的三类行业案例
运维·服务器·零售
智能运维指南3 小时前
企业运维监控平台哪家好?2026年五大全栈可观测方案对比与选型推荐
大数据·运维·人工智能
乔旭伟3 小时前
部署形态选型笔记:多租户 SaaS 与单机本地部署的运维模型对比
运维·服务器·1024程序员节