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-mq abstract interface; switching the underlying implementation only requires changing one line of configuration.


Suitable Users

User ProfileRecommended Solution
Development/Debugging PhaseSpring Event (default, zero dependencies)
Small & Medium ProjectsRedis Stream (persistent, supports consumer groups)
Enterprise ProjectsRabbitMQ (stable & reliable, mature operations)
High-Throughput ScenariosRocketMQ (Alibaba ecosystem, distributed transactions)
Big Data ScenariosKafka (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

Five Implementation Comparison

ImplementationPersistenceConsumption ModeUse CaseSwitch Config
Spring Event❌ In-memoryBroadcastDevelopment/DebuggingDefault
Redis StreamCluster/BroadcastSmall & Medium projectsyudao.mq.type=redis
RabbitMQMultiple modesEnterprise projectsyudao.mq.type=rabbitmq
RocketMQCluster/Broadcast/OrderedHigh throughput, transactional messagesyudao.mq.type=rocketmq
KafkaPartition-basedBig data, logsyudao.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.

docs