RabbitMQ 与 RocketMQ
→ 返回 Spring Cloud
除 Kafka 外,国内业务常见 RabbitMQ(路由灵活、延迟低)与 RocketMQ(阿里生态、事务消息、定时)。Spring 侧均可通过 Spring Cloud Stream Binder 或 Spring Boot AMQP / RocketMQ Starter 接入。
对比速览
| 维度 | RabbitMQ | RocketMQ |
|---|
| 协议 | AMQP | 自定义 |
| 典型场景 | 订单通知、RPC 式队列 | 电商、金融、与阿里云对齐 |
| Spring 集成 | Stream Rabbit / spring-rabbit | Stream Rocket / rocketmq-spring-boot-starter |
| 中间件文档 | RabbitMQ | RocketMQ |
架构选型:消息队列。
RabbitMQ + Spring Cloud Stream
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
spring:
rabbitmq:
host: localhost
port: 5672
cloud:
stream:
bindings:
notify-out-0:
destination: order.notify
notify-in-0:
destination: order.notify
group: sms-service
@Bean
public Consumer<NotifyEvent> notifySink() {
return event -> smsService.send(event);
}
Spring AMQP(直连)
@RabbitListener(queues = "order.notify")
public void handle(NotifyEvent event) { ... }
@RabbitTemplate
public void send(NotifyEvent event) {
rabbitTemplate.convertAndSend("order.exchange", "notify", event);
}
| 模式 | 说明 |
|---|
| Work Queue | 多消费者竞争 |
| Fanout | 广播 |
| Topic | 按 routing key 订阅 |
| DLX | 死信队列处理失败消息 |
RocketMQ + Spring Cloud Alibaba
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
</dependency>
spring:
cloud:
stream:
rocketmq:
binder:
name-server: 127.0.0.1:9876
bindings:
order-out-0:
destination: order-topic
order-in-0:
destination: order-topic
group: order-consumer
rocketmq-spring(常用)
@RocketMQMessageListener(
topic = "order-topic",
consumerGroup = "order-consumer"
)
public class OrderConsumer implements RocketMQListener<OrderEvent> {
@Override
public void onMessage(OrderEvent message) { ... }
}
@Resource
private RocketMQTemplate rocketMQTemplate;
public void publish(OrderEvent event) {
rocketMQTemplate.convertAndSend("order-topic", event);
}
| 能力 | 场景 |
|---|
| 顺序消息 | 同一业务键进同一队列 |
| 事务消息 | 本地事务与发消息一致性 |
| 延迟消息 | 超时关单、定时提醒 |
与 Kafka 如何选
| 优先 Kafka | 优先 Rabbit / Rocket |
|---|
| 日志、埋点、大数据管道 | 复杂路由、低延迟任务队列 |
| 超高吞吐、可回放 | 事务消息(Rocket)、运维已有 MQ |
| 流处理(Flink) | 团队已有 Rabbit 运维经验 |
详见 Kafka 集成。
相关链接