差别
这里会显示出您选择的修订版和当前版本之间的差别。
| 后一修订版 | 前一修订版 | ||
| rocketmq [2026/10/16 12:56] – 新建 rocketmq 页面并写入思维导图 张叶安 | rocketmq [2026/10/16 14:11] (当前版本) – 参考小节补充 proto 与 nodejs 客户端链接 张叶安 | ||
|---|---|---|---|
| 行 24: | 行 24: | ||
| ### Java 5.x gRPC | ### Java 5.x gRPC | ||
| ### Spring Boot(rocketmq-spring-boot-starter) | ### Spring Boot(rocketmq-spring-boot-starter) | ||
| - | ### Go / Python / C++ 客户端 | + | ### Go / Python / C++ / Node.js |
| ## 消息类型 | ## 消息类型 | ||
| - 普通消息 | - 普通消息 | ||
| 行 44: | 行 44: | ||
| - 内存不足导致启动失败 | - 内存不足导致启动失败 | ||
| ``` | ``` | ||
| + | |||
| + | ===== 一、RocketMQ 是什么 ===== | ||
| + | |||
| + | RocketMQ 最早由阿里内部研发(前身 MetaQ),2016 年捐给 Apache 基金会,现为 Apache 顶级项目。纯 Java 实现,特点是**高吞吐 + 低延迟 + 功能丰富**:事务消息、延时/ | ||
| + | |||
| + | ^ 组件 ^ 作用 ^ | ||
| + | | NameServer | 无状态注册中心,Broker 向它注册、客户端向它拉路由;生产环境一般部署 2~3 台 | | ||
| + | | Broker | 真正存消息、转发消息的节点,启动时向所有 NameServer 注册 | | ||
| + | | Topic | 逻辑上的消息分类,内部再切成多个 Queue(默认 4 个) | | ||
| + | | Producer / Consumer Group | 生产者组 / 消费者组;组内负载均衡,组间各得一份 | | ||
| + | |||
| + | 四个招牌能力(也是它相对 Kafka 的差异点): | ||
| + | |||
| + | * **事务消息**:半消息 + 本地事务回查,解决「发消息」和「写数据库」的一致性问题。 | ||
| + | * **延时 / 定时消息**:4.x 支持 18 个固定延时级别,5.x 支持任意时间戳。 | ||
| + | * **消息回溯**:按时间戳重置消费位点('' | ||
| + | * **丰富的过滤**:Tag 过滤、SQL92 属性过滤,在 Broker 端就把不需要的消息筛掉。 | ||
| + | |||
| + | ===== 二、4.x 与 5.x 的核心区别 ===== | ||
| + | |||
| + | 一句话:**5.x = 4.x 的存储内核 + 新增 Proxy 统一入口 + gRPC 协议**。服务端换了架构,老客户端不改代码也能用。 | ||
| + | |||
| + | ^ 对比项 ^ 4.x ^ 5.x ^ | ||
| + | | 通信协议 | Remoting(自定义 TCP 协议) | gRPC(新客户端)+ Remoting(兼容老客户端) | | ||
| + | | 客户端连接目标 | 先连 NameServer: | ||
| + | | 官方 Java 客户端 | '' | ||
| + | | 多语言客户端 | C++ / Go / Python,各基于 remoting 或 C++ 内核 | 官方统一 gRPC 客户端:Java / C++ / Go / Rust / Python | | ||
| + | | 延时消息 | 固定 18 级(1s 5s 10s 30s 1m … 2h) | 任意时间戳,精度秒级 | | ||
| + | | 消费模式 | Push('' | ||
| + | | 扩缩容表现 | 消费者按队列重平衡,扩容时可能短暂重复消费 | POP 模式把重平衡放到 Broker 侧,扩缩容更平滑 | | ||
| + | | 运维入口 | mqadmin 命令行 + rocketmq-console | mqadmin + Proxy 管理接口 + rocketmq-dashboard | | ||
| + | | 兼容性 | — | 5.x 服务端同时开 remoting(10911)与 gRPC(8081),4.x 客户端可继续接 | | ||
| + | |||
| + | **怎么选**: | ||
| + | |||
| + | * 老项目、已有 4.x 客户端、Spring Boot 老版本 starter —— 继续走 remoting,服务端升到 5.x 也能跑。 | ||
| + | * 新项目、多语言团队(Go / Rust / Python)、需要任意时刻定时消息 —— 直接上 5.x Proxy + gRPC 客户端。 | ||
| + | * 4.x 客户端要连 5.x 集群时,Proxy 需要保留 remoting 兼容(配置项在 '' | ||
| + | |||
| + | == 端口速记 == | ||
| + | |||
| + | ^ 端口 ^ 用途 ^ | ||
| + | | 9876 | NameServer(4.x 客户端连这个) | | ||
| + | | 10911 | Broker 主服务端口(客户端收发消息) | | ||
| + | | 10909 | Broker VIP / fast-remoting 通道 | | ||
| + | | 8080 | 5.x Proxy 的 HTTP 接口 / 管理端口 | | ||
| + | | 8081 | 5.x Proxy 的 gRPC 端口(新客户端连这个) | | ||
| + | | 8082 | rocketmq-dashboard 示例端口 | | ||
| + | |||
| + | ===== 三、部署 ===== | ||
| + | |||
| + | == 3.1 老版本:4.x 二进制包(最经典) == | ||
| + | |||
| + | <code bash> | ||
| + | wget https:// | ||
| + | unzip rocketmq-all-4.9.7-bin-release.zip && cd rocketmq-all-4.9.7-bin-release | ||
| + | |||
| + | # 1) 改内存:默认要 4G/ | ||
| + | vi bin/ | ||
| + | vi bin/ | ||
| + | </ | ||
| + | |||
| + | <code ini> | ||
| + | # 2) Broker 配置 conf/ | ||
| + | brokerClusterName = DefaultCluster | ||
| + | brokerName = broker-a | ||
| + | brokerId = 0 | ||
| + | namesrvAddr = 127.0.0.1: | ||
| + | brokerIP1 = 192.168.1.10 | ||
| + | listenPort = 10911 | ||
| + | deleteWhen = 04 | ||
| + | fileReservedTime = 48 | ||
| + | brokerRole = ASYNC_MASTER | ||
| + | flushDiskType = ASYNC_FLUSH | ||
| + | autoCreateTopicEnable = true # 生产环境建议 false,改为手工建 Topic | ||
| + | </ | ||
| + | |||
| + | <code bash> | ||
| + | # 3) 启动 | ||
| + | nohup sh bin/ | ||
| + | nohup sh bin/ | ||
| + | |||
| + | # 4) 验证 | ||
| + | sh bin/mqadmin clusterList -n 127.0.0.1: | ||
| + | </ | ||
| + | |||
| + | 控制台(可选): | ||
| + | |||
| + | <code bash> | ||
| + | docker run -d --name rocketmq-dashboard -p 8082:8080 \ | ||
| + | -e " | ||
| + | apacherocketmq/ | ||
| + | </ | ||
| + | |||
| + | == 3.2 新版本:5.x(namesrv + broker + proxy 三角色) == | ||
| + | |||
| + | 生产建议三个角色分开部署;开发环境可以让 Broker 内嵌 Proxy(Local 模式)。 | ||
| + | |||
| + | <code bash> | ||
| + | # NameServer | ||
| + | docker run -d --name rmqnamesrv -p 9876:9876 \ | ||
| + | -e " | ||
| + | apache/ | ||
| + | |||
| + | # Broker(指向 NameServer) | ||
| + | docker run -d --name rmqbroker --link rmqnamesrv: | ||
| + | -p 10911:10911 -p 10909:10909 \ | ||
| + | -e " | ||
| + | -e " | ||
| + | apache/ | ||
| + | |||
| + | # Proxy(客户端连它的 8081) | ||
| + | docker run -d --name rmqproxy --link rmqnamesrv: | ||
| + | -p 8080:8080 -p 8081:8081 \ | ||
| + | -e " | ||
| + | apache/ | ||
| + | </ | ||
| + | |||
| + | 要点: | ||
| + | |||
| + | * 新客户端连的是 **Proxy 的 8081**,不是 NameServer 的 9876,这是新老接入最大的差别。 | ||
| + | * '' | ||
| + | * Proxy 的配置在 '' | ||
| + | * 用云上托管版时,控制台给出的「接入点」就是 Proxy 的 endpoints,直接交给第 5 节的 gRPC 客户端。 | ||
| + | |||
| + | ===== 四、接入方式一:Java 4.x(remoting,连 9876) ===== | ||
| + | |||
| + | 依赖: | ||
| + | |||
| + | <code xml> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | == 生产者 == | ||
| + | |||
| + | <code java> | ||
| + | DefaultMQProducer producer = new DefaultMQProducer(" | ||
| + | producer.setNamesrvAddr(" | ||
| + | producer.setSendMsgTimeout(3000); | ||
| + | producer.setRetryTimesWhenSendFailed(2); | ||
| + | producer.start(); | ||
| + | |||
| + | Message msg = new Message( | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | |||
| + | SendResult sr = producer.send(msg); | ||
| + | System.out.println(sr.getSendStatus() + " / " + sr.getMsgId()); | ||
| + | |||
| + | // 异步:producer.send(msg, | ||
| + | // 单向:producer.sendOneway(msg); | ||
| + | // 批量:producer.send(Collection< | ||
| + | // 指定队列:producer.send(msg, | ||
| + | |||
| + | producer.shutdown(); | ||
| + | </ | ||
| + | |||
| + | == 消费者(Push 模式,最常用) == | ||
| + | |||
| + | <code java> | ||
| + | DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(" | ||
| + | consumer.setNamesrvAddr(" | ||
| + | consumer.subscribe(" | ||
| + | consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET); | ||
| + | consumer.setConsumeThreadMin(10); | ||
| + | consumer.setConsumeThreadMax(20); | ||
| + | consumer.setMaxReconsumeTimes(16); | ||
| + | |||
| + | consumer.registerMessageListener((MessageListenerConcurrently) (msgs, ctx) -> { | ||
| + | for (MessageExt m : msgs) { | ||
| + | try { | ||
| + | handle(new String(m.getBody(), | ||
| + | } catch (Exception e) { | ||
| + | return ConsumeConcurrentlyStatus.RECONSUME_LATER; | ||
| + | } | ||
| + | } | ||
| + | return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; | ||
| + | }); | ||
| + | consumer.start(); | ||
| + | </ | ||
| + | |||
| + | == 顺序消息 == | ||
| + | |||
| + | 同一订单的消息必须落进同一个队列,消费端用 '' | ||
| + | |||
| + | <code java> | ||
| + | // 生产端:按业务主键选队列 | ||
| + | producer.send(msg, | ||
| + | mqs.get(Math.abs(arg.hashCode()) % mqs.size()), | ||
| + | |||
| + | // 消费端:顺序监听器 | ||
| + | consumer.registerMessageListener((MessageListenerOrderly) (msgs, ctx) -> { | ||
| + | // 暂时无法处理时返回这个状态,而不是无限阻塞 | ||
| + | // return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT; | ||
| + | return ConsumeOrderlyStatus.SUCCESS; | ||
| + | }); | ||
| + | </ | ||
| + | |||
| + | == 延时消息(4.x 固定 18 级) == | ||
| + | |||
| + | <code java> | ||
| + | msg.setDelayTimeLevel(5); | ||
| + | // level=5 → 1m,level=17 → 2h | ||
| + | </ | ||
| + | |||
| + | == 事务消息 == | ||
| + | |||
| + | <code java> | ||
| + | TransactionMQProducer producer = new TransactionMQProducer(" | ||
| + | producer.setNamesrvAddr(" | ||
| + | producer.setExecutorService(Executors.newFixedThreadPool(8)); | ||
| + | producer.setTransactionListener(new TransactionListener() { | ||
| + | @Override | ||
| + | public LocalTransactionState executeLocalTransaction(Message msg, Object arg) { | ||
| + | try { | ||
| + | orderService.create((String) arg); // 本地事务:写库 | ||
| + | return LocalTransactionState.COMMIT_MESSAGE; | ||
| + | } catch (Exception e) { | ||
| + | return LocalTransactionState.ROLLBACK_MESSAGE; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public LocalTransactionState checkLocalTransaction(MessageExt msg) { | ||
| + | // Broker 回查本地事务状态,必须能查到真实结果 | ||
| + | return orderService.exists(msg.getKeys()) | ||
| + | ? LocalTransactionState.COMMIT_MESSAGE | ||
| + | : LocalTransactionState.ROLLBACK_MESSAGE; | ||
| + | } | ||
| + | }); | ||
| + | producer.start(); | ||
| + | |||
| + | producer.sendMessageInTransaction(msg, | ||
| + | </ | ||
| + | |||
| + | ===== 五、接入方式二:Java 5.x(gRPC,连 8081) ===== | ||
| + | |||
| + | 依赖(JDK 8+): | ||
| + | |||
| + | <code xml> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | == 客户端配置与生产者 == | ||
| + | |||
| + | <code java> | ||
| + | ClientServiceProvider provider = ClientServiceProvider.loadService(); | ||
| + | |||
| + | ClientConfiguration cfg = ClientConfiguration.newBuilder() | ||
| + | .setEndpoints(" | ||
| + | // 开启 ACL 时: | ||
| + | // .setCredentialProvider(SessionCredentialsProvider.newBuilder() | ||
| + | // | ||
| + | .build(); | ||
| + | |||
| + | Producer producer = provider.newProducerBuilder() | ||
| + | .setClientConfiguration(cfg) | ||
| + | .setTopics(" | ||
| + | .build(); | ||
| + | |||
| + | Message msg = provider.newMessageBuilder() | ||
| + | .setTopic(" | ||
| + | .setTag(" | ||
| + | .setKeys(" | ||
| + | .setBody(" | ||
| + | // 定时消息(5.x 新能力,任意时刻,精度秒级) | ||
| + | // .setDeliveryTimestamp(System.currentTimeMillis() + 60_000L) | ||
| + | .build(); | ||
| + | |||
| + | SendReceipt receipt = producer.send(msg); | ||
| + | System.out.println(receipt.getMessageId()); | ||
| + | |||
| + | // 异步:producer.sendAsync(msg) → CompletableFuture< | ||
| + | producer.close(); | ||
| + | </ | ||
| + | |||
| + | == PushConsumer(推荐,框架自动 ack) == | ||
| + | |||
| + | <code java> | ||
| + | PushConsumer consumer = provider.newPushConsumerBuilder() | ||
| + | .setClientConfiguration(cfg) | ||
| + | .setConsumerGroup(" | ||
| + | .setSubscriptionExpressions(Collections.singletonMap(" | ||
| + | .setMessageListener(messageView -> { | ||
| + | try { | ||
| + | handle(messageView.getBody().toString(StandardCharsets.UTF_8)); | ||
| + | return ConsumeResult.SUCCESS; | ||
| + | } catch (Exception e) { | ||
| + | return ConsumeResult.FAILURE; | ||
| + | } | ||
| + | }) | ||
| + | .build(); | ||
| + | </ | ||
| + | |||
| + | == SimpleConsumer(拉模式,显式 ack,适合批量与自定义流控) == | ||
| + | |||
| + | <code java> | ||
| + | SimpleConsumer consumer = provider.newSimpleConsumerBuilder() | ||
| + | .setClientConfiguration(cfg) | ||
| + | .setConsumerGroup(" | ||
| + | .setAwaitDuration(java.time.Duration.ofSeconds(15)) | ||
| + | .setSubscriptionExpressions(Collections.singletonMap(" | ||
| + | .build(); | ||
| + | |||
| + | List< | ||
| + | for (MessageView m : msgs) { | ||
| + | process(m); | ||
| + | consumer.ack(m); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | == 事务消息(5.x 写法更简洁) == | ||
| + | |||
| + | <code java> | ||
| + | Producer producer = provider.newProducerBuilder() | ||
| + | .setClientConfiguration(cfg) | ||
| + | .setTopics(" | ||
| + | .setTransactionChecker(messageView -> TransactionResolution.COMMIT) | ||
| + | .build(); | ||
| + | |||
| + | Transaction transaction = producer.beginTransaction(); | ||
| + | producer.send(transaction, | ||
| + | transaction.commit(); | ||
| + | </ | ||
| + | |||
| + | == 4.x → 5.x API 映射速查 == | ||
| + | |||
| + | ^ 能力 ^ 4.x remoting ^ 5.x gRPC ^ | ||
| + | | 入口配置 | '' | ||
| + | | 生产者 | '' | ||
| + | | 推消费 | '' | ||
| + | | 拉消费 | '' | ||
| + | | 发送结果 | '' | ||
| + | | 延时消息 | '' | ||
| + | | 事务消息 | '' | ||
| + | | 日志 | 自带 logback 配置 | 需自行引入 slf4j 实现,否则启动报错 | | ||
| + | | 资源释放 | '' | ||
| + | |||
| + | ===== 六、接入方式三:Spring Boot ===== | ||
| + | |||
| + | <code xml> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <!-- 2.2.x 面向 4.x;2.3.x 基于 5.x 客户端 jar(仍是 remoting 协议连 9876),4.x/ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | <code ini> | ||
| + | rocketmq.name-server=192.168.1.10: | ||
| + | rocketmq.producer.group=pg_order | ||
| + | rocketmq.producer.send-message-timeout=3000 | ||
| + | # 开启 ACL 时再填 | ||
| + | # rocketmq.producer.access-key= | ||
| + | # rocketmq.producer.secret-key= | ||
| + | </ | ||
| + | |||
| + | 发送('' | ||
| + | |||
| + | <code java> | ||
| + | @Resource | ||
| + | private RocketMQTemplate rocketMQTemplate; | ||
| + | |||
| + | public void pay(String payload) { | ||
| + | // 同步 | ||
| + | SendResult r = rocketMQTemplate.syncSend(" | ||
| + | |||
| + | // 异步 | ||
| + | rocketMQTemplate.asyncSend(" | ||
| + | @Override public void onSuccess(SendResult sendResult) { } | ||
| + | @Override public void onException(Throwable e) { } | ||
| + | }); | ||
| + | |||
| + | // 单向 | ||
| + | rocketMQTemplate.sendOneWay(" | ||
| + | |||
| + | // 顺序消息:第三个参数是 hashKey | ||
| + | rocketMQTemplate.syncSendOrderly(" | ||
| + | |||
| + | // 延时消息:最后一个参数是 4.x 的延时级别 | ||
| + | rocketMQTemplate.syncSend(" | ||
| + | MessageBuilder.withPayload(payload).build(), | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 消费: | ||
| + | |||
| + | <code java> | ||
| + | @Service | ||
| + | @RocketMQMessageListener( | ||
| + | topic = " | ||
| + | consumerGroup = " | ||
| + | selectorExpression = " | ||
| + | consumeMode = ConsumeMode.ORDERLY) | ||
| + | public class OrderConsumer implements RocketMQListener< | ||
| + | @Override | ||
| + | public void onMessage(String body) { | ||
| + | // 正常返回即消费成功;抛异常会触发重试 | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== 七、接入方式四:Go / Python / C++ / Node.js ===== | ||
| + | |||
| + | ^ 语言 ^ 4.x(remoting) ^ 5.x(gRPC) ^ | ||
| + | | Go | '' | ||
| + | | Python | '' | ||
| + | | C++ | '' | ||
| + | | Rust | '' | ||
| + | | Node.js | '' | ||
| + | |||
| + | Go(4.x 风格): | ||
| + | |||
| + | <code go> | ||
| + | p, _ := rocketmq.NewProducer( | ||
| + | producer.WithNameServer([]string{" | ||
| + | producer.WithGroupName(" | ||
| + | producer.WithRetry(2)) | ||
| + | _ = p.Start() | ||
| + | |||
| + | res, err := p.SendSync(context.Background(), | ||
| + | Topic: " | ||
| + | Body: []byte(" | ||
| + | }) | ||
| + | _ = res | ||
| + | _ = err | ||
| + | |||
| + | defer p.Shutdown() | ||
| + | </ | ||
| + | |||
| + | Go(5.x gRPC 风格): | ||
| + | |||
| + | <code go> | ||
| + | producer, _ := golang.NewProducer(& | ||
| + | Endpoint: | ||
| + | Credentials: | ||
| + | }, golang.WithTopics(" | ||
| + | _ = producer.Start() | ||
| + | |||
| + | resp, err := producer.Send(context.Background(), | ||
| + | Topic: " | ||
| + | Body: []byte(" | ||
| + | }) | ||
| + | _ = resp | ||
| + | _ = err | ||
| + | // defer producer.GracefulStop() | ||
| + | </ | ||
| + | |||
| + | Python(4.x 风格): | ||
| + | |||
| + | <code python> | ||
| + | from rocketmq.client import Producer, Message | ||
| + | |||
| + | producer = Producer(' | ||
| + | producer.set_name_server_address(' | ||
| + | producer.start() | ||
| + | |||
| + | msg = Message(' | ||
| + | msg.set_keys(' | ||
| + | msg.set_tags(' | ||
| + | msg.set_body(' | ||
| + | producer.send_sync(msg) | ||
| + | |||
| + | producer.shutdown() | ||
| + | </ | ||
| + | |||
| + | == Node.js 接 5.x gRPC:官方没客户端,两条路 == | ||
| + | |||
| + | 先把现状说清楚:官方 '' | ||
| + | |||
| + | === 路径 A:用 @grpc/ | ||
| + | |||
| + | 原理:Proxy 对外暴露的就是标准 gRPC 服务 '' | ||
| + | |||
| + | <code bash> | ||
| + | npm i @grpc/ | ||
| + | |||
| + | # 从 apache/ | ||
| + | # | ||
| + | # | ||
| + | </ | ||
| + | |||
| + | <code javascript> | ||
| + | const grpc = require(' | ||
| + | const protoLoader = require(' | ||
| + | |||
| + | const def = protoLoader.loadSync(' | ||
| + | includeDirs: | ||
| + | keepCase: false, | ||
| + | longs: String, | ||
| + | enums: String, | ||
| + | defaults: true, | ||
| + | oneofs: true, | ||
| + | }); | ||
| + | const { apache } = grpc.loadPackageDefinition(def); | ||
| + | |||
| + | // 关键:这里填 Proxy 的 8081,不是 NameServer 的 9876 | ||
| + | const client = new apache.rocketmq.v2.MessagingService( | ||
| + | ' | ||
| + | grpc.credentials.createInsecure(), | ||
| + | { ' | ||
| + | ); | ||
| + | |||
| + | function send(body, tag, keys) { | ||
| + | return new Promise((resolve, | ||
| + | client.sendMessage({ | ||
| + | messages: [{ | ||
| + | topic: ' | ||
| + | systemProperties: | ||
| + | body: Buffer.from(body, | ||
| + | }], | ||
| + | }, (err, resp) => (err ? reject(err) : resolve(resp.entries))); | ||
| + | }); | ||
| + | } | ||
| + | |||
| + | send(' | ||
| + | .then(entries => console.log(JSON.stringify(entries))) | ||
| + | .catch(console.error); | ||
| + | </ | ||
| + | |||
| + | 动手前必须知道的几点: | ||
| + | |||
| + | * **Message 结构动过**:'' | ||
| + | * **心跳不能省**:官方 SDK 会周期性发 '' | ||
| + | * **消费端远比发送麻烦**:POP 消费要自己维持 '' | ||
| + | * **ACL**:开启鉴权后必须在 gRPC metadata 里带签名头(AK/ | ||
| + | * **升级代价**:官方 proto 不承诺向后兼容,Proxy 升级后自维护的 stub 可能要跟着改 —— 这正是路径 B 存在的理由。 | ||
| + | |||
| + | === 路径 B:Go / Python sidecar(生产推荐) === | ||
| + | |||
| + | 用官方 Go('' | ||
| + | |||
| + | < | ||
| + | Node 业务进程 | ||
| + | </ | ||
| + | |||
| + | * 好处:协议、心跳、重试、ACK、死信全交给官方 SDK,Node 只写业务;SDK 升级只动 sidecar,不用维护 proto stub。 | ||
| + | * 代价:多一个常驻进程,部署、监控、超时与错误透传要一起设计。 | ||
| + | * 什么时候值得:只要这条链路要进生产,就值得。团队里 Go / Java 更熟的话,投入产出比比在 Node 里手写协议高得多。 | ||
| + | |||
| + | 选型建议:多语言团队优先用 5.x 的 gRPC 客户端,接口在各语言间基本一致;**Node.js 没有官方客户端**,生产链路建议用 Go/Python sidecar 包住协议细节,而不是长期自己维护 gRPC stub;4.x 时代的 Python/Node 客户端维护活跃度较低,新项目不建议依赖。 | ||
| + | |||
| + | ===== 八、消息类型对照 ===== | ||
| + | |||
| + | ^ 类型 ^ 4.x 写法 ^ 5.x 写法 ^ 注意点 ^ | ||
| + | | 普通消息 | '' | ||
| + | | 顺序消息 | 手动选队列 + Orderly 监听器 | PushConsumer 分区顺序消费 | 只在同一 Queue 内有序,选队列的 key 必须是业务主键 | | ||
| + | | 延时消息 | '' | ||
| + | | 事务消息 | 半消息 + 本地事务回查 | '' | ||
| + | | 批量消息 | '' | ||
| + | | 过滤 | Tag 过滤(多 Tag 用两条竖线连接)、SQL92 | 同左 | 过滤表达式写在订阅方法里,注意转义 | | ||
| + | |||
| + | ===== 九、运维与排错 ===== | ||
| + | |||
| + | '' | ||
| + | |||
| + | <code bash> | ||
| + | # 集群与 Topic | ||
| + | sh bin/mqadmin clusterList | ||
| + | sh bin/mqadmin topicList | ||
| + | sh bin/mqadmin topicRoute | ||
| + | sh bin/mqadmin updateTopic | ||
| + | sh bin/mqadmin deleteTopic | ||
| + | |||
| + | # 消费情况与位点 | ||
| + | sh bin/mqadmin consumerProgress | ||
| + | sh bin/mqadmin consumerStatus | ||
| + | sh bin/mqadmin resetOffsetByTime -n 127.0.0.1: | ||
| + | |||
| + | # 手工发一条测试消息 | ||
| + | sh bin/mqadmin sendMessage | ||
| + | </ | ||
| + | |||
| + | 5.x 还可以配合 Proxy 的管理接口和 rocketmq-dashboard 使用。 | ||
| + | |||
| + | 排查清单(按出现频率排序): | ||
| + | |||
| + | * '' | ||
| + | * '' | ||
| + | * 4.x 客户端连 5.x 连不上:Proxy 未保留 remoting 兼容,或客户端仍指向已关闭的 9876。 | ||
| + | * 5.x 新客户端连不上:'' | ||
| + | * Broker 启动即 OOM:默认 '' | ||
| + | * 消费堆积:看 '' | ||
| + | * 消息重复:默认至少一次语义,别指望 MQ 去重,消费端必须幂等。 | ||
| + | |||
| + | ===== 十、可靠性要点 ===== | ||
| + | |||
| + | * **刷盘**:'' | ||
| + | * **主从**:'' | ||
| + | * **消费重试**:并发消费失败会重试(默认 16 次),消息进入 '' | ||
| + | * **幂等**:用业务唯一 ID(订单号 + 事件类型)做 Redis 或数据库唯一键去重,这是应对「至少一次」的标准做法。 | ||
| + | * **不丢消息的三道关**:生产端同步发送 + 失败重试;Broker 端同步刷盘 / 主从可靠;消费端处理成功才 ack。 | ||
| + | * **顺序**:顺序消费要求消息进同一队列且消费端单线程('' | ||
| + | |||
| + | ===== 参考 ===== | ||
| + | |||
| + | * 官方文档:https:// | ||
| + | * 官方示例代码:https:// | ||
| + | * 5.x 多语言客户端:https:// | ||
| + | * gRPC 协议定义(proto,Node 自建客户端要取这里的文件):https:// | ||
| + | * Node.js 4.x 社区绑定(仅 remoting,连 9876,长期未更新):https:// | ||
| + | * Spring 集成:https:// | ||
| + | |||