In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article is about how to play RocketMQ in Spring ecology. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.
As the first choice of business message, RocketMQ is widely used in the field of message and flow processing. The micro-service ecological Spring framework is also the most popular framework in business development, and the perfect combination of the two makes RocketMQ the most popular message implementation in Spring Messaging implementation. The following shows five 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.
Preface
In the late 1990s, with the emergence of Java EE (Enterprise Edition), especially the use of Enterprise Java Beans requires complex descriptor configuration and rigid and complex code implementation, which increases the learning curve and development cost of developers. As a result, Spring technology based on simple XML configuration and common Java objects (Plain Old Java Objects) arises at the historic moment, dependency injection (Dependency Injection) The techniques of inversion of control (Inversion of Control) and aspect-oriented programming (AOP) are more agile to solve the shortcomings of traditional Java enterprises and versions. With the continuous evolution of Spring, Annotation-based configuration has gradually replaced the XML file configuration. In addition to dependency injection, control flip, AOP and other technologies, Spring subsequently derived AMQP, Transactional, Security, Batch, Data Access and other modules, involving various areas of development.
On April 1, 2014, Spring Boot 1.0.0 was officially released. It is based on the concept of "convention is greater than configuration" (Convention over configuration) to quickly develop, test, run and deploy Spring applications, and by simply combining with a variety of initiators (such as spring-boot-web-starter), let the application run directly on the command line without having to deploy to a separate container. The emergence of Spring Boot can be said to be the second spring of the Spring framework, which not only simplifies the development process, but also is the de facto standard. The following figure shows the code implementation comparison between Spring and Spring Boot with the same functionality.
Apache RocketMQ is a well-known distributed message and flow processing middleware in the industry. Its main functions are message distribution, asynchronous decoupling, peak cutting and valley filling. RocketMQ is a financial-level messaging and streaming data platform. RocketMQ is widely used in transaction and payment links, mainly in scenarios where the quality of message links is very high, and can support trillion-level message peaks. RocketMQ is widely used in business messages, and derives special messages that match all kinds of business scenarios, such as sequential messages, transaction messages, delayed messages and so on.
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?
The collision between RocketMQ and Spring
Before introducing the story of RocketMQ and Spring, I have to mention two messaging frameworks in Spring, Spring Messaging and Spring Cloud Stream. All of them can be integrated with Spring Boot and provide some reference implementations. Like all implementation frameworks, the message framework aims to implement lightweight message-driven micro-services, which can effectively simplify the complexity of developers' use of message middleware and enable system developers to pay more attention to the processing of core business logic.
1. Spring Messaging
Spring Messaging is a module added in Spring Framework 4 and is an extensible support for the integration of Spring and messaging systems. It implements a complete infrastructure from a simple JmsTemplate-based JMS interface to asynchronously receiving messages, and Spring AMQP provides a similar set of features required by the protocol. After integration with Spring Boot, it has the ability to automatically configure and integrate with the corresponding messaging system at test and run time.
As far as the client is concerned, Spring Messaging provides a set of abstract API or contractual standards to specify the patterns 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 message middleware providers can provide their own Spring implementation under this mode: what needs to be implemented on the message sending side is a Java Bean in the form of XXXTemplate, and several different methods of sending messages are provided combined with the automatic configuration options of Spring Boot. On the consumer side of the message is a XXXMessageListener interface (the implementation usually uses an annotation to declare a message-driven POJO), which provides callback methods to listen for and consume messages. This interface can also use the automation options of Spring Boot and some custom properties.
In the Apache RocketMQ ecology, RocketMQ-Spring-Boot-Starter (hereinafter referred to as RocketMQ-Spring) is a project that supports Spring Messaging API standards. The project encapsulates the client of RocketMQ using Spring Boot, which allows users to send and consume messages through simple annotation and standard Spring Messaging API coding. It also supports the extension of RocketMQ native API to support richer message types. In the early days of RocketMQ-Spring graduation, students from the RocketMQ community asked students from the Spring community to review the RocketMQ-Spring code, leading to a story about RocketMQ and Spring Boot. Josh Long, a famous Spring evangelist, introduced to foreign students how to use RocketMQ-Spring to send and receive messages. RocketMQ-Spring also surpassed Spring-Kafka and Spring-AMQP in just two years to become the most active news project in the Spring Messaging ecosystem.
2. Spring Cloud Stream
Spring Cloud Stream combines the annotations and functions of Spring Integration, and its application model is as follows:
An independent application kernel is provided in the Spring Cloud Stream framework, which communicates with the outside world through the input (@ Input) and output (@ Output) channels, the message source side (Source) sends messages through the input channel, and the consumer target side (Sink) listens to the output channel to obtain the consumed messages. These channels connect to external proxies through a dedicated Binder implementation. The developer's code only needs to be programmed against the fixed interfaces and annotations provided by the application kernel, and does not need to care about the runtime specific Binder binding message middleware.
At run time, Spring Cloud Stream can automatically detect and use the Binder found under classpath. This makes it easy for developers to use different types of middleware in the same code: just include different Binder at build time. In more complex usage scenarios, you can also package multiple Binder in the application and let it choose its own Binder, or even use different Binder for different channels at run time.
Binder abstraction allows Spring Cloud Stream applications to connect flexibly to middleware, and Spring Cloud Stream uses the flexible configuration capabilities of Spring Boot, which 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), and deployers can dynamically select channel connection destination at run time (for example, Topic for RocketMQ or exchange for RabbitMQ).
Spring Cloud Stream shields the implementation details of the underlying message middleware and hopes to send / consume messages with a unified set of API. 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, and its main implementation principle is that the sending message is finally proxied to the RocketMQTemplate of RocketMQ-Spring, and the RocketMQ-Spring Consumer Container is started internally at the consumer side to receive the message. 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 of the distributed system. Through Spring Cloud Stream RocketMQ Binder,RocketMQ, the ecology of Spring Cloud can be better integrated. For example, in combination with Spring Cloud Data Flow and Spring Cloud Funtion, RocketMQ can be used in Spring streaming 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 been logged on the official website of Spring, and Spring Cloud Alibaba has also become the most active implementation of Spring Cloud.
How to choose RocketMQ implementation in Spring ecology?
By introducing the message framework in Spring, this paper introduces several projects based on RocketMQ and Spring message framework, 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.
How to select the corresponding project to use in the actual business development? The characteristics and usage scenarios of each project are listed below.
1. RocketMQ-Spring
Features:
As a start-up dependency, simply introducing a package can use all the functions of the RocketMQ client in the Spring ecosystem.
Take advantage of a large number of automatic configuration and annotations to simplify the programming model and support Spring Messaging API.
Fully aligned with the functionality of RocketMQ's native Java SDK.
Use the scene:
Suitable for users using RocketMQ in Spring Boot, they want to take advantage of all the features of the RocketMQ native java client and simplify the programming model through Spring annotations and automatic configuration.
2. Spring Cloud Stream RocketMQ Binder
Features:
Shielding the implementation details of the underlying MQ, the API of the upper Spring Cloud Stream is unified. If you want to cut from Kafka to RocketMQ, just change the configuration.
Ecological integration with Spring Cloud is more convenient. For example, Spring Cloud Data Flow, the above stream computing is based on Spring Cloud Stream, which is also used inside the Spring Cloud Stream;Spring Cloud Bus message bus.
The annotations provided by Spring Cloud Stream and the programming experience are great.
Use the scene:
At the code level, it can completely block the users of the underlying message middleware, and hope that the project can better access the Spring Cloud ecology (Spring Cloud Data Flow, Spring Cloud Funtcion, etc.).
3. Spring Cloud Bus RocketMQ
Features:
The RocketMQ is used as the "transporter" of the event to complete the distribution and notification of the event by sending the event (message) to the message queue and broadcasting to all nodes that subscribe to the event (message).
Use the scene:
In Spring ecology, users who want to use RocketMQ as message bus can be used to communicate events between applications, configure central client refresh and other scenarios.
4. Spring Cloud Data Flow
Features:
Streaming tasks are processed with Source/Processor/Sink components. RocketMQ acts as an intermediate storage component in stream processing.
Use the scene:
Stream processing, big data handles the scene.
5. Spring Cloud Function
Features:
The consumption / production / processing of messages is a function call, which integrates the Function model of Java ecology.
Use the scene:
Serverless scene.
The above is how to play RocketMQ in the Spring ecology. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.