The author | accommodation, los night source | alibaba cloud native public number

RocketMQ is widely used in message and flow processing as the business message of choice. The Spring framework for microservices ecology is also the most popular framework for business development, and the perfect fit between the two makes RocketMQ the most popular Messaging implementation for Spring Messaging implementations. This article presents five ways to play RocketMQ in Spring Ecology Chinese, and describes the characteristics and usage scenarios of each project. The end of the article can be directly online experience.

preface

In the late 1990s, with the advent of Java EE(Enterprise Edition), and in particular Enterprise Java Beans, the use of complex descriptor configurations and inflexible code implementations increased the learning curve and development costs for developers. This led to Spring technology based on simple XML configuration and Plain Old Java Objects, Dependency Injection, Inversion of Control and AOP technologies provide a more agile solution to the shortcomings of traditional Java enterprises and versions. As Spring continues to evolve, annotation-based configuration gradually replaces XML file configuration. In addition to dependency injection, control flip, and AOP, Spring has spawned modules called AMQP, Transactional, Security, Batch, and Data Access that cover all areas of development.

Spring Boot 1.0.0 was released on April 1, 2014. It is based on the concept of “Convention over Configuration” to quickly develop, test, run, and deploy Spring applications, and can be easily combined with various initiators such as spring-boot-web-starter. Let the application run directly from the command line, without deploying to a separate container. The emergence of Spring Boot can be said to be the second Spring framework, it not only simplifies the development process, is now the de facto standard. The figure below shows a comparison of Spring’s code implementation with Spring Boot for the same function.

Apache RocketMQ is an industry-renowned distributed message and flow middleware. Its main functions include message distribution, asynchronous decoupling, peak clipping, and valley filling. RocketMQ is a financial grade messaging and streaming data platform. RocketMQ is widely used in transaction and payment links, mainly for scenarios requiring very high quality of message links and supporting trillion-level message flood peaks. RocketMQ is widely used in business messages, deriving sequential messages, transactional messages, delayed messages, and other special messages that match various business scenarios.

Spring and RocketMQ are the main characters in this article, so how does the Spring framework, which almost every Java programmer uses, clash with RocketMQ, which supports rich business scenarios?

RocketMQ collision with Spring

Before introducing the RocketMQ and Spring story, it is important to mention two frameworks for Messaging in Spring, Spring Messaging and Spring Cloud Stream. Both can be integrated with Spring Boot and provide some reference implementations. Like all implementation frameworks, messaging framework aims to achieve lightweight message-driven microservices, which can effectively simplify the complexity of using messaging middleware for developers and allow system developers to focus more on the processing of core business logic.

1. Spring Messaging

Spring Messaging is a module added in Spring Framework 4, which is an extended support for Spring and Messaging system integration. It implements a complete infrastructure from simple using JMS interfaces based on JmsTemplate to receiving messages asynchronously, and Spring AMQP provides a similar set of capabilities required by the protocol. After integration with Spring Boot, it has automatic configuration capabilities that integrate with appropriate messaging systems at test and run time.

For clients only, Spring Messaging provides a set of abstract APIS or agreed standards to specify the modes of message sender and message receiver. For example, the corresponding model of Messaging includes a message body Payload and message Header. Different messaging middleware providers can provide their own Spring implementations in this mode: on the message sending side, they need to implement a Java Bean in the form of XXXTemplate, which provides multiple different message sending methods in combination with Spring Boot’s automatic configuration options. On the consuming side of the message is an XXXMessageListener interface (typically implemented using an annotation to declare a message-driven POJO) that provides callback methods to listen for and consume messages, also using Spring Boot automation options and some custom properties.

Rocketmq-spring-boot-starter (rocketMQ-Spring) is a project that supports the Spring Messaging API standard in the Apache RocketMQ ecosystem. This project encapsulates the RocketMQ client using Spring Boot, allowing users to send and consume messages using simple annotations and standard Spring Messaging API code. Extensions to the RocketMQ native API are also supported to support richer message types. In the early days of rocketMQ-Spring graduation, students in the RocketMQ community asked students in the Spring community to review the RocketMQ-Spring code and lead to a good story about RocketMQ and Spring Boot. Famous Spring evangelist Josh Long explains how to use RocketMQ-Spring to send and receive messages. Rocketmq-spring has also surpassed Spring-Kafka and Spring-AMQP (both maintained by the Spring community) as the most active Messaging project in the Spring Messaging ecosystem in just two years.

2. Spring Cloud Stream

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

An independent application kernel is provided in the Spring Cloud Stream framework. It communicates with the outside world through the @input and @output channels. The message Source sends messages through the Input channel. The consumption target (Sink) listens on the output channel to get the consumption message. These channels are connected to external agents with dedicated binders. Developers’ code only needs to be programmed against the fixed interfaces and annotations provided by the application kernel, not the binder-bound messaging middleware of the runtime.

At runtime, Spring Cloud Stream automatically probes and uses binders found under the CLASspath. This allows developers to easily use different types of middleware in the same code: they just need to include different binders at build time. In more complex usage scenarios, you can also package multiple binders in an application and let it choose its own, or even use different binders for different channels at run time.

The Binder abstraction enables Spring Cloud Stream applications to be flexibly connected to middleware. In addition, Spring Cloud Stream uses the flexible configuration capabilities of Spring Boot. This configuration can be provided through externally configured properties and any form supported by Spring Boot (including application startup parameters, environment variables, and application.yml or application.properties files), The deploayer can dynamically select channel connection destinations (for example, RocketMQ’s topic or RabbitMQ’s Exchange) at runtime.

Spring Cloud Stream shields the implementation details of the underlying message-oriented middleware, hoping to use a unified set of APIS to send/consume messages. The implementation details of the underlying message-oriented middleware are completed by the binders of each message-oriented middleware. Spring officially implements Rabbit Binder and Kafka Binder. Spring Cloud Alibaba implements the RocketMQ Binder, which ultimately brokers sending messages to RocketMQ-Spring’s RocketMQTemplate, On the Consumer side, the RocketMQ-Spring Consumer Container is internally started to receive messages. On this basis, Spring Cloud Alibaba also implements Spring Cloud Bus RocketMQ. Users can use RocketMQ as a message Bus within the Spring Cloud system to connect all nodes of the distributed system. With the Spring Cloud Stream RocketMQ Binder, RocketMQ can better integrate with the Spring Cloud ecosystem. For example, in conjunction with Spring Cloud Data Flow and Spring Cloud Funtion, RocketMQ can be used in Spring Flow Computing Ecology and Serverless(FaaS) projects.

Now Spring Cloud Stream RocketMQ Binder and Spring Cloud Bus RocketMQ as the implementation of Spring Cloud Alibaba have landed on the official website of Spring. Spring Cloud Alibaba has also become the most active implementation of Spring Cloud.

How do I choose a RocketMQ implementation in the Spring ecosystem?

By introducing the messaging framework in Spring, I introduced several projects based on RocketMQ that integrate with the Spring messaging framework, The main products are RocketMQ-Spring, Spring Cloud Stream RocketMQ Binder, Spring Cloud Bus RocketMQ, Spring Data Flow and Spring Cloud The Function. The relationship between them can be illustrated as follows.

How to select corresponding projects to use in actual business development? The features and usage scenarios of each project are listed below.

1. RocketMQ-Spring

Features:

  • As a starting dependency, all the functionality of the RocketMQ client can be used in the Spring ecosystem by simply introducing a package.

  • Simplifies the programming model with extensive automatic configuration and annotations, and supports the Spring Messaging API.

  • Fully aligned with the RocketMQ native Java SDK functionality.

Usage Scenarios:

  • Suitable for users using RocketMQ in Spring Boot who want all the features of the RocketMQ native Java client and simplify the programming model with Spring annotations and automatic configuration.

2. Spring Cloud Stream RocketMQ Binder

Features:

  • Shielding the low-level MQ implementation details, the API for the upper Spring Cloud Stream is uniform. If you want to cut from Kafka to RocketMQ, just change the configuration.

  • More convenient integration with Spring Cloud ecology. For example, Spring Cloud Data Flow, where the Flow calculation is based on Spring Cloud Stream; A Spring Cloud Stream is also used inside the message Bus.

  • Spring Cloud Stream provides excellent annotations and programming experience.

Usage Scenarios:

  • At the code level, users of the underlying message-oriented middleware can be completely shielded, and the project is expected to have better access to the Spring Cloud ecosystem (Spring Cloud Data Flow, Spring Cloud Funtcion, etc.).

3. Spring Cloud Bus RocketMQ

Features:

  • Use RocketMQ as an event “transmitter” for event distribution and notification by sending an event (message) to a message queue, which is then broadcast to all nodes that subscribe to the event (message).

Usage Scenarios:

  • Users who want to use RocketMQ as a message bus in the Spring ecosystem can use it for inter-application event communication, configuration center client refresh, and other scenarios.

4. Spring Cloud Data Flow

Features:

  • The Source/Processor/Sink component is used for streaming task processing. RocketMQ acts as an intermediate storage component in the flow process.

Usage Scenarios:

  • Flow processing and big data processing scenarios.

5. Spring Cloud Function

Features:

  • The consumption/production/processing of the message is a Function call, incorporating the Function model of the Java ecosystem.

Usage Scenarios:

  • Serverless scenario.

This article provides an overview of five ways to integrate RockeMQ into the Spring ecosystem to give developers a macro view of several classic scenarios. There will be a follow-up column detailing how each of these projects is used and applied to truly leverage RocketMQ in the Spring ecosystem!

Log in to start.aliyun.com for immersive online interactive tutorials.