Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Spring-boot-starter Standard to transform RocketMQ client components in a Project

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use the Spring-boot-starter standard to transform the RocketMQ client components in the project", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "how to use the Spring-boot-starter standard to transform the RocketMQ client components in the project"!

First, background introduction

When we use Spring Cloud family buckets to build micro-service applications, we can often see spring-boot-xxx-starter dependencies, such as spring-boot-starter-web, spring-cloud-starter-feign, spring-boot-starter-test, mybatis-spring-boot-starter, as long as you bring starter things, you have everything of this component, including all the configurations and reference classes are done. How is such a magic thing realized? Can we make our own toolkit into a starter?

II. Spring Boot Starter component specification

Naming convention

GroupId: the naming of this tag does not make too many requirements, basically using the company domain name + project name, as the official use of org.springframework.cloud, third parties generally use their own company domain name, such as org.mybatis.spring.boot. ArtifactId: the naming of this tag is officially recommended by Spring. Spring officially publishes its own components, which are named by spring-boot-starter-xxx,xxx to represent the component name, such as the components developed by spring-boot-starter-web and spring-cloud-starter-feign; mentioned above, and named by xxx-spring-boot-starter, such as mybatis-spring-boot-starter.

Engineering specification

Take the maven project as an example, Spring Boot Starter establishes the project in a multi-module way, and there are autoconfigure modules and starter modules in the project. The autoconfigure module is an automatic configuration module, which contains configuration loading, all functional code implementation and jar packages that need to be referenced. It is responsible for the implementation of internal functions. All code development is completed in this module. Starter module provides automatic configuration module dependency, which has no code, is an empty jar package, only all references to the autoconfigure module, is a dependency set, its purpose is to simplify the dependency when using this component, as long as you add the starter module, you can use the entire starter component.

III. Case study

We take the commonly used RocketMQ client components as an example to build a self-defined starter,RocketMQ is an excellent news middleware developed by Alibaba team and donated to apache team, which has withstood the test of Singles Day over the years.

Create a Maven project and add two modules, rocketmq-spring-boot-autoconfigure and rocketmq-spring-boot-starter. The version of RocketMQ used here is 4.5.2. The main pom.xml is excerpted as follows:

Com.hy.demorocketmqpom1.0-SNAPSHOT rocketmq-spring-boot-autoconfigure rocketmq-spring-boot-starter org.apache.commons commons-lang3 3.3.2 org.apache.rocketmq rocketmq-client 4.5.2 Org.projectlombok lombok provided org.springframework spring-context 5.1.8.RELEASE compile org.slf4j slf4j-api

Add corresponding tool classes, such as annotations, configuration classes, interfaces, etc., in the src directory for autoconfigure module development, slightly add location configuration candidate classes, and create a new spring.factories file in the META-INF/ directory:

Org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.hy.demo.rocketmq.MQConfig

Spring Boot will check whether the META-INF/spring.factories file exists in your published jar, and the autoconfiguration class can only be loaded in this way.

Starter module development only needs to modify the pom.xml file:

Rocketmq com.hy.demo 1.0-SNAPSHOT4.0.0rocketmq-spring-boot-starter com.hy.demo rocketmq 1.0-SNAPSHOT pom import Com.hy.demo rocketmq-spring-boot-autoconfigure 1.0-SNAPSHOT

Compile, package, and command the project on IDEA:

Clean install-DskipTests=true

4. After the package and deployment is completed, add the dependency of the starter to the application module.

Com.hy.demo rocketmq-spring-boot-starter 1.0-SNAPSHOT

Note: because the RocketMQ component is more general, it currently provides several basic ways to send and receive messages to support transaction messages, so the article does not explain the code functions one by one, and attach the secondary source code address:

Rocketmq-spring-boot-starter source code example

IV. Carding of knowledge points

Several annotations of Spring: @ Import is used to integrate all the Bean configurations defined in the @ Configuration annotation; @ EventListener event listener, which contains ContextStartedEvent, indicates listening for events after the Spring context is started; @ Configuration is equivalent to xml's beans tag; @ Bean is marked in method, which is equivalent to xml's bean

How do custom annotations @ MQConsumer and @ MQTransactionProducer work? The com.hy.demo.rocketmq.config.RocketMQAnnotationScan class is defined in the project to scan these two annotations, use the annotation @ EventListener (ContextStartedEvent.class) to listen for Spring context initialization events, and then read all classes with these two annotations from the Spring container to load RocketMQ-related configuration information. Due to the particularity of the transaction message producer class org.apache.rocketmq.client.producer.TransactionMQProducer (it needs to inject the TransactionListener listener class during initialization) There is some coupling with the application module), so add a Map collection to store all the instances in the application module that use the @ MQTransactionProducer annotation.

Thank you for reading, the above is the content of "how to use the Spring-boot-starter standard to transform the RocketMQ client components in the project". After the study of this article, I believe you have a deeper understanding of how to use the Spring-boot-starter standard to transform the RocketMQ client components in the project, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report