By Tongrong and Luoye
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.
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.
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.
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.
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.
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.
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.
Spring Cloud Stream combines the annotations and features of Spring Integration. Its application model is as follows:
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.
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.
How to choose the projects accordingly for actual business development?
Outlined below are the features and scenarios of each project.
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.
Optimization on Alibaba Cloud-Native Etcd, Cluster Management, and Control
376 posts | 41 followersFollow
Alibaba Cloud Native Community - April 24, 2023
Alibaba Developer - July 8, 2021
Alibaba Developer - December 1, 2021
Alibaba Developer - October 22, 2021
Alibaba Cloud Native Community - January 5, 2023
Alibaba Developer - March 5, 2020
376 posts | 41 followersFollow
Visualization, O&M-free orchestration, and Coordination of Stateful Application ScenariosLearn More
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
AlibabaMQ for Apache RocketMQ is a distributed message queue service that supports reliable message-based asynchronous communication among microservices, distributed systems, and serverless applications.Learn More
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
More Posts by Alibaba Cloud Native Community