Differences and choices between Spring Cloud Stream and SpringBoot integrating RocketMq

Concept overview

Spring Boot integrating RocketMQ and Spring Cloud Stream integrating RocketMQ are two different methods. They have some key differences when using RocketMQ message queue:

  1. Spring Boot integrates RocketMQ:

    • Spring Boot’s integration of RocketMQ usually directly integrates the RocketMQ client SDK to use RocketMQ in Spring Boot applications.
    • You need to configure RocketMQ’s producers and consumers, set up topics and tags, and write code to send and receive messages.
    • This method is more direct to use RocketMQ and is suitable for scenarios that require more custom control, such as specific message processing logic, exception handling, etc.
    • Maintenance and configuration of RocketMQ needs to be done manually.
  2. Spring Cloud Stream integrates RocketMQ:

    • Spring Cloud Stream is part of the Spring Cloud project and provides a more abstract way of handling messages to simplify the use of messaging systems.
    • Spring Cloud Stream integrates RocketMQ to abstract the details of message producers and consumers, and manages the connection and configuration of message queues through Binder.
    • You can define inputs and outputs declaratively and don’t need to worry about the details of underlying message communication, such as topics and tags.
    • Spring Cloud Stream provides a higher-level abstraction that allows developers to focus more on business logic without having to deal with the details of the underlying message queue.
    • In terms of configuration and maintenance, Spring Cloud Stream is handled through the Binder layer.

To sum up, Spring Boot integrating RocketMQ is more suitable for scenarios that require more custom control and lower-level operations of RocketMQ, while Spring Cloud Stream integrating RocketMQ is more suitable for those who want to focus more on business logic without caring too much about message queue details. scene. Which approach you choose depends on the needs of your project and the preferences of your development team.

Example description

Scenario: Suppose you are building an e-commerce platform and need to handle message notifications for orders.

Spring Boot integrates RocketMQ:

  1. Configuration: In a Spring Boot application, you need to configure RocketMQ’s connection information, producers and consumers, and define topics and tags, as follows:
# application.properties
rocketmq.producer.group=my-producer-group
rocketmq.consumer.group=my-consumer-group
rocketmq.namesrv-addr=localhost:9876

# RocketMQ Producer configuration
rocketmq.producer.topic=order-topic
rocketmq.producer.tag=order-tag

# RocketMQ Consumer configuration
rocketmq.consumer.topic=order-topic
rocketmq.consumer.tag=order-tag
  1. Producer: Write an order service and use RocketMQ’s producer to send order notifications to the “order-topic” topic.

  2. Consumer: Write an order processing service, use RocketMQ’s consumer to listen to the “order-topic” topic, and execute corresponding processing logic when receiving a message.

  3. Maintenance: Configure RocketMQ connections, topics and tags, and handle errors and exceptions in message consumption.

Spring Cloud Stream integrates RocketMQ:

  1. Dependencies: Add dependencies on Spring Cloud Stream and RocketMQ.
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
</dependency>
  1. Declarative configuration: In the application’s configuration file, define the input and output channels:
spring:
  cloud:
    stream:
      bindings:
        order-out:
          destination: order-topic
        order-in:
          destination: order-topic
  1. Producer: In the order service, just inject MessageChannel and send the order notification to the order-out channel.
@Autowired
@Output("order-out")
private MessageChannel output;

public void sendOrderNotification(Order order) {<!-- -->
    output.send(MessageBuilder.withPayload(order).build());
}
  1. Consumer: In the order processing service, just inject MessageChannel, and then listen to the order-in channel. When a new order message arrives, Execute the corresponding processing logic.
@Autowired
@Input("order-in")
private SubscribableChannel input;

@StreamListener("order-in")
public void handleOrderMessage(Order order) {<!-- -->
    // Process order notification logic
}
  1. Maintenance: Spring Cloud Stream manages RocketMQ connections and configurations through the Binder layer, simplifying configuration and maintenance work.

Summarize:

  • Integrating RocketMQ with Spring Boot requires explicit configuration of RocketMQ details, which is suitable for situations where more custom control is required.
  • Spring Cloud Stream integrates RocketMQ to provide a higher-level abstraction, simplifying the use of message queues, and is suitable for scenarios that focus on business logic without dealing with underlying details.

How to choose

Choosing Spring Boot to integrate RocketMQ or Spring Cloud Stream to integrate RocketMQ depends on the needs and complexity of the project.

Choose the scenario where Spring Boot integrates RocketMQ:

  1. Need more custom control: If you need full control over the configuration and details of RocketMQ, or your application needs more complex interactions with RocketMQ, such as custom message processing logic, transactional messages, etc., Then Spring Boot integrating RocketMQ may be more suitable for you.

  2. Existing project integration: If you already have a project using Spring Boot and want to integrate RocketMQ into the existing project, Spring Boot integration is more straightforward as it allows you to easily add RocketMQ dependencies and configuration.

  3. Greater team expertise: If your team has deep expertise in the use and management of RocketMQ and is willing to configure and maintain RocketMQ connections and configurations themselves, Spring Boot integration can provide you with More customization options.

Choose the scenario where Spring Cloud Stream integrates RocketMQ:

  1. Quick integration: If you need to quickly integrate a message queue and don’t want to deal with tedious configuration details, Spring Cloud Stream provides a more streamlined declarative configuration to make integration easier.

  2. Simplified abstraction: If you are more concerned with business logic and are unwilling to deal with the low-level details of message queues, Spring Cloud Stream provides higher-level abstractions, allowing you to only focus on message channels and processing logic.

  3. Microservice architecture: If your application is built based on a microservice architecture, the features of Spring Cloud Stream make it easier to pass messages between different microservices.

  4. Event-driven architecture: If your application adopts an event-driven architecture, Spring Cloud Stream provides a more convenient way to define and process event messages.

In summary, which method is more appropriate depends on your project needs and the technology stack and experience of your team. If you require more customization and control, or already have extensive RocketMQ experience, Spring Boot integration may be more suitable. If you want faster integration and higher level abstraction, Spring Cloud Stream may be a better choice.