Message Queue
Core Positioning
Message queue is the “central nervous system” for asynchronous communication between subsystems. When ERP outbound needs to notify WMS to update inventory, when mall payment success needs to award points to members — all these cross-module asynchronous collaborations are completed through the message queue.
Design Philosophy: Business code only depends on the
yudao-spring-boot-starter-mqabstract interface; switching the underlying implementation only requires changing one line of configuration.
Suitable Users
| User Profile | Recommended Solution |
|---|---|
| Development/Debugging Phase | Spring Event (default, zero dependencies) |
| Small & Medium Projects | Redis Stream (persistent, supports consumer groups) |
| Enterprise Projects | RabbitMQ (stable & reliable, mature operations) |
| High-Throughput Scenarios | RocketMQ (Alibaba ecosystem, distributed transactions) |
| Big Data Scenarios | Kafka (logs, event tracking, data pipelines) |
Architecture Design
graph TB
subgraph BusinessLayer["Business Code Layer (depends only on abstraction)"]
P["Producer
MessageProducer.send()"] C["Consumer
MessageConsumer.onMessage()"] end subgraph AbstractionLayer["Abstraction Layer yudao-spring-boot-starter-mq"] API["Unified API
Message / MessageProducer / MessageConsumer"] end subgraph ImplementationLayer["Implementation Layer (Choose One of Five)"] SE["yudao-spring-boot-starter-mq-spring-event
Spring Event (Default)"] REDIS["yudao-spring-boot-starter-mq-redis
Redis Stream"] RABBIT["yudao-spring-boot-starter-mq-rabbitmq
RabbitMQ"] ROCKET["yudao-spring-boot-starter-mq-rocketmq
RocketMQ"] KAFKA["yudao-spring-boot-starter-mq-kafka
Kafka"] end P --> API C --> API API --> SE API --> REDIS API --> RABBIT API --> ROCKET API --> KAFKA
MessageProducer.send()"] C["Consumer
MessageConsumer.onMessage()"] end subgraph AbstractionLayer["Abstraction Layer yudao-spring-boot-starter-mq"] API["Unified API
Message / MessageProducer / MessageConsumer"] end subgraph ImplementationLayer["Implementation Layer (Choose One of Five)"] SE["yudao-spring-boot-starter-mq-spring-event
Spring Event (Default)"] REDIS["yudao-spring-boot-starter-mq-redis
Redis Stream"] RABBIT["yudao-spring-boot-starter-mq-rabbitmq
RabbitMQ"] ROCKET["yudao-spring-boot-starter-mq-rocketmq
RocketMQ"] KAFKA["yudao-spring-boot-starter-mq-kafka
Kafka"] end P --> API C --> API API --> SE API --> REDIS API --> RABBIT API --> ROCKET API --> KAFKA
Five Implementation Comparison
| Implementation | Persistence | Consumption Mode | Use Case | Switch Config |
|---|---|---|---|---|
| Spring Event | ❌ In-memory | Broadcast | Development/Debugging | Default |
| Redis Stream | ✅ | Cluster/Broadcast | Small & Medium projects | yudao.mq.type=redis |
| RabbitMQ | ✅ | Multiple modes | Enterprise projects | yudao.mq.type=rabbitmq |
| RocketMQ | ✅ | Cluster/Broadcast/Ordered | High throughput, transactional messages | yudao.mq.type=rocketmq |
| Kafka | ✅ | Partition-based | Big data, logs | yudao.mq.type=kafka |
Usage Examples
Define Message
// 1. Define message class, implement Message interface
@Data
public class OrderPaidMessage implements Message {
private Long orderId;
private Long userId;
private Integer amount;
}
Send Message
@Resource
private MessageProducer messageProducer;
public void payOrder(Long orderId) {
// Business processing...
// Send message
OrderPaidMessage message = new OrderPaidMessage();
message.setOrderId(orderId);
message.setUserId(getCurrentUserId());
message.setAmount(order.getAmount());
messageProducer.send(message);
}
Consume Message
@Component
public class OrderPaidConsumer implements MessageConsumer<OrderPaidMessage> {
@Override
public void onMessage(OrderPaidMessage message) {
// Award points
memberService.addPoints(message.getUserId(), message.getAmount());
// Update inventory
stockService.decreaseStock(message.getOrderId());
}
}
Typical Message Flow Between Subsystems
sequenceDiagram
participant MALL as Mall Module
participant MQ as Message Queue
participant WMS as Warehouse Module
participant MEMBER as Member Module
MALL->>MALL: User payment successful
MALL->>MQ: Send "Order Paid" message
MQ->>WMS: Consume message → Deduct inventory
MQ->>MEMBER: Consume message → Award points
Note over WMS,MEMBER: Two consumers execute independently, no interference
Switching Method
Just modify the configuration file:
# Switch from Spring Event to Redis Stream
yudao:
mq:
type: redis # Change to redis / rabbitmq / rocketmq / kafka
No business code changes needed.