×
Community Blog How to Implement RocketMQ in the Spring Ecosystem?

How to Implement RocketMQ in the Spring Ecosystem?

This article describes five ways to implement RocketMQ in the Spring ecosystem along with the key features and application scenarios.

By Tongrong and Luoye

1

As the preferred choice for business messages, RocketMQ is widely used in message and stream processing. Meanwhile, the microservice ecosystem Spring Framework is also the most popular framework in business development. The perfect combination of the two makes RocketMQ the most popular among all the Spring Messaging implementations. This article describes five ways to implement RocketMQ in the Spring ecosystem, together with the features and application scenarios of each. Go to the address at the end of this article for an online experience.

Preface

In the late 1990s, with the emergence of Java Enterprise Edition (Java EE), especially with the use of Enterprise Java Beans which required complex descriptor configuration and inflexible code implementation, it has been harder and costlier for the developers to learn and develop. Therefore, the Spring technology based on simple XML configuration and Plain Old Java Objects came into being. It has solved the shortcomings of traditional Java EE in a more agile way by means of Dependency Injection, Inversion of Control, and Aspect Oriented Programming (AOP) technologies. As Spring continues to evolve, XML file configuration has gradually been replaced by configuration based on annotations. In addition to technologies such as Dependency Injection, Inversion of Control, and AOP, Spring has later developed modules such as AMQP, Transactional, Security, Batch, and Data Access, covering all fields of development.

2

On April 1, 2014, Spring Boot 1.0.0 was officially released. Based on the concept of "convention over configuration", it quickly develops, tests, runs and deploys Spring applications. What's more, with the combination of various initiators, such as spring-boot-web-starter, applications can run directly in command lines without being deployed in a separate container. The Spring framework thrives with the emergence of Spring Boot which not only simplifies the development process but is also a de facto standard now. The following figure compares the code implementation of Spring and Spring Boot with the same functions.

3

Apache RocketMQ is a well-known distributed message and stream processing middleware featuring message delivery, asynchronous decoupling, load shifting, and so on. Being able to deal with trillion-level message peaks, RocketMQ is a financial message and stream data platform widely used in scenarios that demand high-quality message links such as transaction and payment links. Besides, its widespread use in business messages also breeds the special messages suit for various business scenarios, including ordered messages, transactional messages, and delayed messages.

The article mainly focuses on Spring and RocketMQ and the impact of using them together.

When RocketMQ Meets Spring

Before diving into the story of RocketMQ and Spring, there are two message frameworks in Spring that should be noted: Spring Messaging and Spring Cloud Stream. Both of them can be integrated with Spring Boot and provide some implementations for reference. Like all implementation frameworks, the messaging framework is designed to implement lightweight message-driven microservices, which can effectively simplify the use of message-oriented middleware for developers. As a result, system developers can focus more on the processing of core business logic.

1) Spring Messaging

Spring Messaging is a newly added module in Spring Framework 4 and extensible support for the integration of Spring with the message system. It implements a complete set of infrastructure from simple usage of the JMS interface based on JmsTemplate to the asynchronous message receiving. Spring AMQP provides a similar set of functions required by the protocol. After integration with Spring Boot, it is equipped with automatic configuration so that it integrates with the corresponding messaging system during testing and at runtime.

For the client, Spring Messaging provides a set of abstract APIs or agreed standards to regulate the modes of message producers and receivers. For example, the model corresponding to Messaging includes a payload and a header. Different message-oriented middleware providers can provide their own Spring implementations under this model. On the message producer, a Java Bean in the form of XXXTemplate needs to be implemented, which provides multiple messages sending methods with the help of the automatic configuration options of Spring Boot; on the message consumer, there is an XXXMessageListener interface that provides callback methods for message listening and consumption. The interface is usually implemented by declaring a message-driven POJO through an annotation. It also supports the automation options and some custom attributes of Spring Boot.

4

In the Apache RocketMQ ecosystem, RocketMQ-Spring-Boot-Starter (hereinafter referred to as RocketMQ-Spring) is a project that supports the Spring Messaging API standard. It encapsulates the way the RocketMQ client uses the Spring Boot, allowing users to write code for sending and consuming messages through simple annotations and the standard Spring Messaging API. It also extends native RocketMQ APIs to support more various message types. When RocketMQ-Spring was initially launched, the RocketMQ community members invited the Spring community members to review the RocketMQ-Spring code, bringing about the story of RocketMQ and Spring Boot. The famous Spring evangelist Josh Long introduced to foreign community members about how to use RocketMQ-Spring to send and receive messages. In two years, RocketMQ-Spring has surpassed Spring-Kafka and Spring-AMQP (both are maintained by the Spring community) and become the most active message project in the Spring Messaging ecosystem.

5

2) Spring Cloud Stream

Spring Cloud Stream combines the annotations and features of Spring Integration. Its application model is as follows:

6

The Spring Cloud Stream Framework provides an independent application kernel, which communicates with the outside through the input (@Input) and output (@Output) channels. The source sends messages through the input channel and the sink obtains the consumed messages by listening to the output channel. These channels connect to external proxies through a dedicated binder. Developers only need to write programs based on the fixed interfaces and annotations provided by the application kernel without considering the message-oriented middleware that the specific binder is bound to at runtime.

At runtime, Spring Cloud Stream can automatically detect and use the binder found under classpath. As such, developers only need to include different types of middleware in different binders so that they can use them in the same code at ease. In more complex scenarios, users can also pack multiple binders in the application for the channels to choose, or even choose different binders for different channels at runtime.

Binder abstraction allows Spring Cloud Stream applications to flexibly connect to the middleware. In addition, Spring Cloud Stream uses the flexible configuration capability of Spring Boot which can be obtained through the attributes of external configurations and any forms supported by Spring Boot (including application startup parameters, environment variables, and application.yml or application.properties file). The deployment personnel can dynamically select the channel to connect to the destination at runtime, such as the topic of RocketMQ or the exchange of RabbitMQ.

Spring Cloud Stream shields the implementation details of the underlying message middleware in the hope that a unified set of APIs can be used to send and consume messages. The implementation details of the underlying message middleware are completed by the binder of each message middleware. Spring has officially implemented the Rabbit binder and Kafka Binder. Spring Cloud Alibaba has implemented RocketMQ Binder by virtue of the RocketMQTemplate as the proxy to send messages. As for the consumer, it starts RocketMQ-Spring Consumer Container to receive the messages. On this basis, Spring Cloud Alibaba also implements Spring Cloud Bus RocketMQ. Users can use RocketMQ as the message bus in the Spring Cloud system to connect all nodes in the distributed system. With Spring Cloud Stream RocketMQ Binder, RocketMQ can better integrate with the Spring Cloud ecosystem. For example, its combination with Spring Cloud Data Flow and Spring Cloud function allows it to be used in the Spring stream computing ecosystem and Serverless (FaaS) projects.

Now, Spring Cloud Stream RocketMQ Binder and Spring Cloud Bus RocketMQ are available on the Spring official website as the implementation of Spring Cloud Alibaba which has also become the most active implementation of Spring Cloud.

How to Choose RocketMQ Implementation in the Spring Ecosystem?

Through the introduction to the Spring messaging frameworks, this article also describes several RocketMQ-based projects integrated with the Spring messaging framework, including RocketMQ-Spring, Spring Cloud Stream RocketMQ Binder, Spring Cloud Bus RocketMQ, Spring Data Flow, and Spring Cloud Function. The relationship between them is shown in the following figure.

7

How to choose the projects accordingly for actual business development?

Outlined below are the features and scenarios of each project.

1) RocketMQ-Spring

Characteristics:

  • As the starting dependency, RocketMQ-Spring can use all the functions of the RocketMQ client in the Spring ecosystem by introducing a package.
  • It simplifies the programming model with a lot of automatic configurations and annotations with the Spring Messaging API supported.
  • It is fully compatible with the native Java SDK of RocketMQ.

Scenarios:

  • It is suitable for users who use RocketMQ in Spring Boot in the hope of using all functions of the RocketMQ native Java client and simplifying the programming model through Spring annotations and automatic configurations.

2) Spring Cloud Stream RocketMQ Binder

Characteristics:

  • It shields the underlying MQ implementation details with unified Spring Cloud Stream upper APIs. For switching to RocketMQ from Kafka, it is only needed to change the configuration.
  • It is more convenient to integrate with the Spring Cloud ecosystem, for example, Spring Cloud Data Flow. The stream computing of the Spring Cloud Data Flow is based on the Spring Cloud Stream which is also used by the Spring Cloud Bus message.
  • The programming experience using annotations provided by Spring Cloud Stream is excellent.

Scenarios:

  • It fits users who can completely shield the underlying message-oriented middleware in terms of code and hope to better connect the project to the Spring Cloud ecosystem, such as Spring Cloud Data Flow and Spring Cloud Function.

3) Spring Cloud Bus RocketMQ

Characteristics:

  • Using RocketMQ as an event transmitter, it sends an event (message) to the message queue and broadcasts the message to all nodes that subscribe to the event (message), thus completing event distribution and notification.

Scenarios:

  • It fits users who want to use RocketMQ as a message bus in the Spring ecosystem. It can also be used in scenarios such as the event communications between applications and the client refreshing at the configuration center.

4) Spring Cloud Data Flow

Characteristics:

  • It performs streaming job processing with the source, processor, and sink components. RocketMQ serves as an intermediate storage component during stream processing.

Scenarios:

  • Stream processing and big data processing.

5) Spring Cloud Function

Characteristics:

  • All the message consumption, production, and processing involve function calls. It integrates with the Function model in the Java ecosystem.

Scenarios:

  • Serverless scenarios.

This article provides five methods of integrating RocketMQ in the Spring ecosystem to help developers gain a macroscopic understanding of several classic scenarios. Later on, there will be an article describing in detail the usage and scenarios of each project, so that readers can become proficient in using RocketMQ in the Spring ecosystem.

0 0 0
Share on

You may also like

Comments

Related Products

  • ApsaraMQ for RocketMQ

    ApsaraMQ for RocketMQ is a distributed message queue service that supports reliable message-based asynchronous communication among microservices, distributed systems, and serverless applications.

    Learn More
  • Serverless Workflow

    Visualization, O&M-free orchestration, and Coordination of Stateful Application Scenarios

    Learn More
  • Serverless Application Engine

    Serverless Application Engine (SAE) is the world's first application-oriented serverless PaaS, providing a cost-effective and highly efficient one-stop application hosting solution.

    Learn More
  • Function Compute

    Alibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.

    Learn More