How to play RocketMQ in the Spring ecosystem?

Introduction:  RocketMQ is the first choice for business messaging and is widely used in the field of messaging and stream processing. The Spring framework of microservice ecology is also the most popular framework in business development. The perfect fit of the two makes RocketMQ the most popular message implementation in Spring Messaging implementation. This article shows 5 ways to play RocketMQ in Spring Ecological Chinese, and describes the characteristics and usage scenarios of each project. At the end of the article you can go directly to the online experience.

 

image.png

 

RocketMQ, as the first choice for business messaging, is widely used in the field of messaging and stream processing. The Spring framework of microservice ecology is also the most popular framework in business development. The perfect fit of the two makes RocketMQ the most popular message implementation in Spring Messaging implementation. This article shows 5 ways to play RocketMQ in Spring Ecological Chinese, and describes the characteristics and usage scenarios of each project. At the end of the article you can go directly to the online experience.

I. Introduction

In the late 1990s, with the emergence of Java EE (Enterprise Edition), especially the use of Enterprise Java Beans required complex descriptor configuration and rigid and complex code implementation, which increased the learning curve and development costs for developers. This Spring technology based on simple XML configuration and Plain Old Java Objects came into being. Dependency Injection, Inversion of Control and Aspect-Oriented Programming (AOP) technologies are more agile Solve the shortcomings of traditional Java enterprises and versions. With the continuous evolution of Spring, Annotation-based configuration gradually replaced XML file configuration. In addition to technologies such as dependency injection, control rollover, and AOP, Spring subsequently derives modules such as AMQP, Transactional, Security, Batch, and Data Access, which involve various fields of development.

 

image.png

 

On April 1, 2014, Spring Boot 1.0.0 was officially released. It is based on the concept of "Convention over configuration" (Convention over configuration) to quickly develop, test, run and deploy Spring applications, and can be easily combined with various starters (such as spring-boot-web-starter), Let the application run directly in the command line mode, no need to deploy to a separate container. The emergence of Spring Boot can be said to be the second spring of the Spring framework. It not only simplifies the development process, but is currently the de facto standard. The following figure shows the comparison of the code implementation of Spring and Spring Boot with the same function.

 

image.png

 

Apache RocketMQ is a well-known distributed message and stream processing middleware in the industry. Its main functions are message distribution, asynchronous decoupling, peak cutting and valley filling, etc. RocketMQ is a financial-level messaging and streaming data platform. RocketMQ is used a lot in transaction and payment links, mainly in scenarios where the quality of message links is very high, and it can support trillions of message floods. RocketMQ is widely used in business messages and derives special messages that match various business scenarios such as sequential messages, transaction messages, and delayed messages.

The protagonists of this article are Spring and RocketMQ, so what kind of spark will almost every Java programmer use Spring framework to collide with RocketMQ that supports rich business scenarios?

Two collision between RocketMQ and Spring

Before introducing the RocketMQ and Spring story, I have to mention two messaging frameworks in Spring, Spring Messaging and Spring Cloud Stream. They can be integrated with Spring Boot and provide some reference implementations. Like all implementation frameworks, the purpose of the message framework is to implement lightweight message-driven microservices, which can effectively simplify the complexity of developers’ use of message middleware, and allow system developers to have more energy to focus on Processing of core business logic.

1 Spring Messaging

Spring Messaging is a module added in Spring Framework 4, which is an extensible support for the integration of Spring and the messaging system. It implements a complete infrastructure from a simple JMS interface based on JmsTemplate to asynchronous message reception. Spring AMQP provides a similar set of functions required by the protocol. After the integration with Spring Boot, it has automatic configuration capabilities and can be integrated with the corresponding messaging system during testing and runtime.

Purely for the client, Spring Messaging provides a set of abstract APIs or agreed standards, which specify the mode of the message sender and the message receiver. For example, the model corresponding to the message Messaging includes a message body Payload and a message header. Header. Different messaging middleware providers can provide their own Spring implementations in this mode: What needs to be implemented at the message sending end is a Java Bean in the form of XXXTemplate, combined with Spring Boot's automatic configuration options to provide multiple different methods of sending messages; The consumer end of the message is a XXXMessageListener interface (implementation usually uses an annotation to declare a message-driven POJO), which provides callback methods to monitor and consume messages. This interface can also use Spring Boot's automation options and some customized properties .

 

image.png

 

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. This project encapsulates the RocketMQ client using Spring Boot, which allows users to send and consume messages through simple annotations and standard Spring Messaging API to write code, and it also supports the expansion of RocketMQ native API to support more abundant The message type. In the early days of RocketMQ-Spring graduation, students from the RocketMQ community asked the students from the Spring community to review the RocketMQ-Spring code, leading to a story about RocketMQ and Spring Boot [1], the famous Spring evangelist Josh Long Introduce to foreign students how to use RocketMQ-Spring to send and receive messages [2]. RocketMQ-Spring has also surpassed Spring-Kafka and Spring-AMQP (note: both are maintained by the Spring community) in just two years, becoming the most active messaging project in the Spring Messaging ecosystem.

 

image.png

 

2 Spring Cloud Stream

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

 

image.png

 

An independent application kernel is provided in the Spring Cloud Stream framework, which communicates with the outside world through input (@Input) and output (@Output) channels. The message source (Source) sends messages through the input channel, and the consumption target (Sink) Obtain consumption messages by monitoring the output channel. These channels are connected to external agents through a dedicated Binder. The developer's code only needs to program for the fixed interface and annotation mode provided by the application kernel, and does not need to care about the specific Binder-bound message middleware at runtime.

At runtime, Spring Cloud Stream can automatically detect and use the Binder found under the classpath. In this way, developers can easily use different types of middleware in the same code: only need to include different Binders in the build. In more complex usage scenarios, you can also package multiple binders in the application and let it choose the binder itself, or even use different binders for different channels at runtime.

The Binder abstraction allows Spring Cloud Stream applications to be flexibly connected to middleware. In addition, Spring Cloud Stream uses Spring Boot's flexible configuration configuration capabilities. Such configuration can be provided through external configuration properties and any form supported by Spring Boot (including Application startup parameters, environment variables and application.yml or application.properties files), the deployer can dynamically select the channel connection destination at runtime (for example, RocketMQ topic or RabbitMQ exchange).

Spring Cloud Stream shields the implementation details of the underlying message middleware, and hopes to use a unified set of APIs for message sending/consumption. The implementation details of the underlying message middleware are completed by the Binder of each message middleware. Spring officially implements Rabbit binder and Kafka Binder. Spring Cloud Alibaba implements RocketMQ Binder[3], and its main implementation principle is to finally proxy the sent message to RocketMQ-Spring's RocketMQTemplate, and on the consumer side, RocketMQ-Spring Consumer Container will be started to receive messages. Based on this, 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 of the distributed system. Through Spring Cloud Stream RocketMQ Binder, RocketMQ can better integrate with the Spring Cloud ecosystem. For example, in combination with Spring Cloud Data Flow and Spring Cloud Funtion, RocketMQ can be used in the Spring stream computing ecosystem and Serverless (FaaS) projects.

Now Spring Cloud Stream RocketMQ Binder and Spring Cloud Bus RocketMQ as implementations of Spring Cloud Alibaba have been listed on Spring's official website [4], and Spring Cloud Alibaba has also become the most active implementation of Spring Cloud.

3. How to choose RocketMQ implementation in Spring ecology?

By introducing the message framework in Spring, several projects based on RocketMQ combined with the Spring message framework are introduced, mainly RocketMQ-Spring, Spring Cloud Stream RocketMQ Binder, Spring Cloud Bus RocketMQ, Spring Data Flow and Spring Cloud Function. The relationship between them can be shown in the following figure.

 

image.png

 

How to choose the corresponding project for use in actual business development? The characteristics and usage scenarios of each project are listed below.

RocketMQ-Spring

Features:

  • As a starting dependency, simply introduce a package to use all the functions of RocketMQ client in the Spring ecosystem.
  • Utilizing a large number of automatic configuration and annotation to simplify the programming model, and support Spring Messaging API.
  • It is fully aligned with the functions of RocketMQ native Java SDK.

scenes to be used:

  • Suitable for users who use RocketMQ in Spring Boot, hoping to use all the functions of RocketMQ native java client, and simplify the programming model through Spring annotations and automatic configuration.

Spring Cloud Stream RocketMQ Binder

Features:

  • Shield the implementation details of the underlying MQ, and the API of the upper Spring Cloud Stream is unified. If you want to switch from Kafka to RocketMQ, just change the configuration directly.
  • It is more convenient to integrate with the Spring Cloud ecosystem. For example, Spring Cloud Data Flow, the above stream calculations are all based on Spring Cloud Stream; Spring Cloud Bus is also used internally in the Spring Cloud Bus message bus.
  • The annotations provided by Spring Cloud Stream and the programming experience are very good.

scenes to be used:

  • At the code level, users of the underlying message middleware can be completely shielded, and it is hoped that the project can better integrate into the Spring Cloud ecosystem (Spring Cloud Data Flow, Spring Cloud Funtcion, etc.).

Spring Cloud Bus RocketMQ

Features:

  • Use RocketMQ as the "transmitter" of the event, by sending the event (message) to the message queue, so as to broadcast to all nodes that subscribe to the event (message) to complete the distribution and notification of the event.

scenes to be used:

  • Users who want to use RocketMQ as a message bus in the Spring ecosystem can use it in the communication of events between applications, configuration center client refresh and other scenarios.

Spring Cloud Data Flow

Features:

  • Stream task processing with Source/Processor/Sink components. RocketMQ acts as an intermediate storage component in the stream processing process.

scenes to be used:

  • Stream processing, big data processing scenarios.

Spring Cloud Function

Features:

  • The consumption/production/processing of messages are all one-time function calls, integrating the Function model of the Java ecosystem.

scenes to be used:

  • Serverless scenario.

This article introduces 5 ways to access RockeMQ in the Spring ecosystem as a whole, so that developers have a macro understanding of several classic scenarios. There will be a column to introduce the specific usage and application scenarios of the above projects in detail, and really play RocketMQ in the Spring ecology! Click "Read the original text" to go directly to the Action Lab, and directly experience the 3 most commonly used ways to access RocketMQ in the Spring ecosystem.

Guess you like

Origin blog.csdn.net/weixin_43970890/article/details/115011669