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)06/01 Report--
It is believed that many inexperienced people are at a loss about how to send and consume messages in an elegant Spring. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
The editor will give a brief introduction to the design and implementation of rocktmq-spring-boot. Readers can understand the development details of integrating RocketMQ Client into a spring-boot-starter framework, and then use a simple example to explain step by step how to use this spring-boot-starter toolkit to configure, send and consume RocketMQ messages.
About the author: Liaotian, Alibaba technical expert, Apache RocketMQ kernel control, has many years of distributed system research and development experience, has a deep understanding of Microservice, Messaging and Storage and other fields, currently focuses on RocketMQ kernel optimization and Messaging ecological construction.
Through this article, you will learn:
Introduction to the message Framework of Spring
The concrete realization of rocketmq-spring-boot
Use the example
Insert an advertisement: this Saturday afternoon, Apache RocketMQ developer Sharon will come to Hangzhou. Welcome to the scene. For more information, please click "read the original text".
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. The techniques of dependency injection (Dependency Injection), inversion of Control (Inversion of Control) and aspect-oriented programming (AOP) solve the shortcomings of traditional Java enterprises and versions more agile.
With the continuous evolution of Spring, Annotation-based configuration has gradually replaced XML file configuration. Spring Boot 1.0.0 was officially released on April 1, 2014. 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 allows applications to run directly on the command line by simply combining with various initiators (such as spring-boot-web-starter). You no longer need to deploy to a separate container. This simple, direct and rapid process of building and developing applications, which can use the agreed configuration and simplify deployment, is welcomed by more and more developers.
Apache RocketMQ is a well-known distributed message and flow processing middleware in the industry. To put it simply, it consists of two parts: Broker server and client:
One of the clients is the message publisher client (Producer), which is responsible for sending messages to the Broker server
The other is the message consumer client (Consumer), where multiple consumers can form a consumption group to subscribe to and pull messages stored on the consumer Broker server.
In order to take advantage of the rapid development of Spring Boot and to make users more flexible in using RocketMQ messaging clients, the Apache RocketMQ community has introduced the spring-boot-starter implementation. With the release of the distributed transaction message function in RocketMQ version 4.3.0, the relevant spring-boot code has been upgraded recently to support the lookback of distributed transactions and the sending of transaction messages through annotations.
This article will give a brief introduction to the current design and implementation, through which readers can understand the development details of integrating RocketMQ Client into a spring-boot-starter framework, and then use a simple example to explain step by step how to use this spring-boot-starter toolkit to configure, send and consume RocketMQ messages.
Message Framework in Spring
By the way, let's discuss the two main frameworks for messaging in Spring, namely 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.
2.1 Spring Messaging
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.
As far as the client is concerned, Spring Messaging provides a set of abstract API or agreed standards to specify the mode of message sender and message receiver. Different message middleware providers can provide their own Spring implementation under this mode: what needs to be implemented on the message sender is a Java Bean in the form of XXXTemplate, and several different methods of sending messages are provided combined with the automatic configuration option 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.
If you are interested in learning more about Spring Messaging and the use of different messaging products, it is recommended to read this document. Referring to the existing implementation of Spring Messaging, the spring-boot-starter of RocketMQ follows the relevant design patterns and provides the corresponding API (such as sequential, asynchronous and transactional semi-message, etc.) according to the functional characteristics of RocketMQ.
2.2 Spring Cloud Stream
Spring Cloud Stream combines the annotations and functions of Spring Integration, and its application model is as follows:
Cdn.xitu.io/2018/11/28/167584efb430aa68?w=511&h=416&f=webp&s=9250 ">
The picture is quoted from spring cloud stream.
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 Boo (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 Kafka or exchange for RabbitMQ).
Binder SPI approach to enable message middleware products to use extensible API to write the corresponding Binder, and integrated into the Spring Cloud Steam environment, RocketMQ does not provide related Binder, we plan to improve this function in the next step, and hope that students with experience in this field in the community will actively try to contribute PR or suggestions.
The realization of spring-boot-starter
At the beginning, we already know that the initiator constructed by spring boot starter is very convenient for users, as long as the user introduces the dependency definition of starter in pom.xml, and the corresponding compilation, run and deployment functions are all automatically introduced. Therefore, the commonly used open source components will provide Spring users with a spring-boot-starter package for developers, so it is very convenient for developers to integrate and use. Here we introduce the starter implementation process of RocketMQ (client) in detail.
3.1. Implementation steps of spring-boot-starter
For a spring-boot-starter implementation, you need to include the following parts:
In the definition of pom.xml
Define the starter component information that will eventually be generated
Org.apache.rocketmqspring-boot-starter-rocketmq1.0.0-SNAPSHOT
Define dependency packages
It is divided into two parts: a, Spring's own dependency package; B, RocketMQ's dependency package
Org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.apache.rocketmq rocketmq-client ${rocketmq-version} org.springframework.boot spring-boot-starter-parent ${spring.boot.version} pom import
Profile class
Defines the application property profile class RocketMQProperties, which defines a default set of property values. When using the final starter, users can modify the value according to the properties defined by the class, of course, not directly modify the configuration of the class, but the corresponding configuration file in the spring-boot application: src/main/resources/application.properties.
Define autoload classes
The purpose of defining the autoload class in the src/resources/META-INF/spring.factories file is to let spring boot initialize the relevant Bean,Component or Service automatically with the automation configuration class specified in this article, which is as follows:
Org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.apache.rocketmq.spring.starter.RocketMQAutoConfiguration
In the concrete implementation of the RocketMQAutoConfiguration class, define Bean objects that are open to users for direct use. These include:
RocketMQProperties loads the processing class of the application properties profile
Sending template class for sending messages sent by RocketMQTemplate sender
The ListenerContainerConfiguration container Bean is responsible for discovering and registering the consumer implementation interface class, which requires that it be annotated by the @ RocketMQMessageListener annotation and implement the RocketMQListener generalization interface.
Finally, the specific RocketMQ-related packaging
It is encapsulated on the sending side (producer) and the consumer side (consumer) client respectively, and provides a compatible way for Spring Messaging interface in the current implementation version.
3.2. Message sender implementation
Ordinary sender
The code of the sender is encapsulated in RocketMQTemplate POJO. The following figure is the call diagram of the relevant code of the sender:
In order to be compatible with Spring Messaging's sending template, the AbstractMessageSendingTemplate abstract class is integrated in RocketMQTemplate to support related message conversion and sending methods, which will eventually proxy to the doSend () method; doSend () and some RocoketMQ-specific methods such as asynchronous, one-way and sequential methods are directly added to RoketMQTempalte, these methods directly proxy calls to RocketMQ's Producer API to send messages.
Transaction message sender
A method sendMessageInTransaction () is added to RocketMQTemplate to send transaction messages, and eventually this method will proxy to RocketMQ's TransactionProducer to make calls, and its associated TransactionListener implementation class will be registered on this Producer so that the method implementation in TransactionListener can be called after sending the message.
3.3. Message consumer implementation
After the consumer Spring-Boot application is launched, all classes containing @ RocketMQMessageListener annotations (these classes need to integrate RocketMQListener interfaces and implement the onMessage () method) will be scanned. The Listener will be placed in the DefaultRocketMQListenerContainer container object one-to-one, and the container object will encapsulate the RocketMQListener into the concurrent or sequential interface implementation within the specific RocketMQ according to the consumption mode (concurrency or order). Create a RocketMQ Consumer object in the container, start and listen for custom Topic messages, and if there is a consumer message, call back to the onMessage () method of Listener.
Use the example
The above chapter introduces the implementation of RocketMQ in spring-boot-starter mode, and here we show how to make this rocketmq-spring-boot-starter through the simplest example of message sending and consumption.
4.1 preparation of RocketMQ server
Start NameServer and Broker
To verify the Spring-Boot client for RocketMQ, first make sure that the RocketMQ service is downloaded and started correctly. You can refer to the quick start of the RocketMQ master station for operation. Make sure that startup NameServer and Broker are started correctly.
Create the Topics required in the instance
Perform the following command line operation in the directory where the startup command was executed
Bash bin/mqadmin updateTopic-c DefaultCluster-t string-topic4.2. Compile rocketmq-spring-boot-starter
The current spring-boot-starter relies on the central library of Maven that has not yet been submitted. Users need to download the git source code before using it, and then execute mvn clean install to install it to the local repository.
Git clone https://github.com/apache/rocketmq-externals.gitcd rocketmq-spring-boot-startermvn clean install4.3. Write client code
If you use it, you need to add the following dependencies to the maven profile pom.xml of the message publishing and consuming client:
1.0.0-SNAPSHOT org.apache.rocketmq spring-boot-starter-rocketmq ${spring-boot-starter-rocketmq-version}
The value of the property spring-boot-starter-rocketmq-version is: 1.0.0-SNAPSHOT, which is consistent with the version installed to the local repository performed in the previous step.
The code of the message sender
The configuration file application.properties of the sender
# define name-server address spring.rocketmq.name-server=localhost:9876# define publisher group name spring.rocketmq.producer.group=my-group1# define the topicspring.rocketmq.topic=string-topic to be sent
Java code of the sender
Import org.apache.rocketmq.spring.starter.core.RocketMQTemplate;...@SpringBootApplicationpublic class ProducerApplication implements CommandLineRunner {/ / declares and references RocketMQTemplate @ Resource private RocketMQTemplate rocketMQTemplate; / / uses the topic attribute @ Value ("${spring.rocketmq.springTopic}") private String springTopic; public static void main (String [] args) {SpringApplication.run (ProducerApplication.class, args);} public void run (String...) defined in application.properties Args) throws Exception {/ / sends a string message to the specified topic SendResult sendResult= rocketMQTemplate.syncSend (springTopic, "Hello, World!") synchronously; / / prints the result information System.out.printf ("string-topic syncSend1 sendResult=%s% n", sendResult);}}
Message consumer code
Consumer-side configuration file application.properties
# define name-server address spring.rocketmq.name-server=localhost:9876# define publisher group name spring.rocketmq.consumer.group=my-customer-group1# define the topicspring.rocketmq.topic=string-topic to be sent
Java code on the consumer side
@ SpringBootApplicationpublic class ConsumerApplication {public static void main (String [] args) {SpringApplication.run (ConsumerApplication.class, args) }} / / declare the class of the consumption message and specify in the note that the relevant consumption information @ Service@RocketMQMessageListener (topic = "${spring.rocketmq.topic}", consumerGroup = "${spring.rocketmq.consumer.group}") class StringConsumer implements RocketMQListener {@ Override public void onMessage (String message) {System.out.printf ("- StringConsumer received:% s% f", message);}}
Here is only a brief introduction to the use of spring-boot to write the most basic message sending and receiving code, if you need to know more about the calling methods, such as: asynchronous sending, object message body, specifying tag tags and specifying transaction messages, please refer to the github documentation and detailed code. We will introduce these advanced features one after another.
After reading the above, have you mastered how to send and consume messages in an elegant Spring? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.