使用Spring-boot-starter標准改造項目內的RocketMQ客戶端組件


一、背景介紹

我們在使用Spring Cloud全家桶構建微服務應用時,經常能看到spring-boot-xxx-starter的依賴,像spring-boot-starter-web、spring-cloud-starter-feign、spring-boot-starter-test、mybatis-spring-boot-starter,仿佛只要帶上starter的東西,你就擁有了這個組件的一切,包括所有的配置,引用類都搞定了,這樣一個神奇的拿來就用的東西,是怎么實現的呢?我們自己能不能把自己的工具包做成一個starter?

二、Spring Boot Starter組件規范

  • 命名規范

groupId:這個標簽的命名沒做太多要求,基本上使用公司域名+項目名方式,如官方一般使用org.springframework.cloud,第三方一般用自己公司域名,如org.mybatis.spring.boot。
artifactId:這個標簽的命名Spring官方給了建議命名方式,Spring官方自己發布的組件,命名方式是spring-boot-starter-xxx,xxx表示組件名稱,像上文提及的spring-boot-starter-web和spring-cloud-starter-feign;第三方開發的組件,命名方式是xxx-spring-boot-starter,如mybatis-spring-boot-starter。

  • 工程規范

以maven工程為例,Spring Boot Starter用多模塊方式建立工程,工程內有autoconfigure模塊和starter模塊。
autoconfigure模塊為自動配置模塊,里面包含配置加載,全部的功能代碼實現及需要引用的jar包,負責對內功能實現,所有的代碼開發都在這個模塊中完成。
starter模塊提供自動配置模塊的依賴,里面沒有代碼,是個空jar包,只有對autoconfigure模塊的所有引用,是一個依賴集,它的目的是簡化使用該組件時的依賴,只要添加starter模塊,就能使用整個starter組件。

三、案例實戰

我們以常用的RocketMQ客戶端組件為例,搭建一個自己定義的starter,RocketMQ是由阿里巴巴團隊開發並捐贈給apache團隊的優秀消息中間件,承受過歷年雙十一大促的考驗。

  1. 創建一個Maven工程,增加兩個模塊rocketmq-spring-boot-autoconfigure和rocketmq-spring-boot-starter,這里使用的RocketMQ版本為4.5.2,主pom.xml節選如下:
<groupId>com.hy.demo</groupId>
<artifactId>rocketmq</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<modules>
	<module>rocketmq-spring-boot-autoconfigure</module>
	<module>rocketmq-spring-boot-starter</module>
</modules>

<dependencies>
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-lang3</artifactId>
		<version>3.3.2</version>
	</dependency>
	<dependency>
		<groupId>org.apache.rocketmq</groupId>
		<artifactId>rocketmq-client</artifactId>
		<version>4.5.2</version>
	</dependency>
	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>5.1.8.RELEASE</version>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-api</artifactId>
	</dependency>
</dependencies>
  1. autoconfigure模塊開發
    src目錄下添加相應的工具類,如注解,配置類,接口等,略
    添加定位配置侯選類,在META-INF/目錄下新建spring.factories文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.hy.demo.rocketmq.MQConfig

Spring Boot會檢查你發布的jar中是否存在META-INF/spring.factories文件,自動配置類只能通過這種方式加載

  1. starter模塊開發
    只需要修改pom.xml文件即可:
<parent>
	<artifactId>rocketmq</artifactId>
	<groupId>com.hy.demo</groupId>
	<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>rocketmq-spring-boot-starter</artifactId>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>com.hy.demo</groupId>
			<artifactId>rocketmq</artifactId>
			<version>1.0-SNAPSHOT</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

<dependencies>
	<dependency>
		<groupId>com.hy.demo</groupId>
		<artifactId>rocketmq-spring-boot-autoconfigure</artifactId>
		<version>1.0-SNAPSHOT</version>
	</dependency>
</dependencies>

在IDEA上對該工程進行編譯,打包,命令:

clean install -DskipTests=true

4、打包部署完成后,在應用模塊里添加該starter的依賴即可

<dependency>
   <groupId>com.hy.demo</groupId>
   <artifactId>rocketmq-spring-boot-starter</artifactId>
   <version>1.0-SNAPSHOT</version>
</dependency>

注:因為RocketMQ組件較為通用,目前提供基本的幾種發送和接收消息的方式,支持事務消息,文章內就不一一解釋代碼功能,附上此次源碼地址:

rocketmq-spring-boot-starter源碼示例

四、知識點梳理

  1. Spring的幾個注解:
    @Import用來整合所有在@Configuration注解中定義的Bean配置;
    @EventListener 事件監聽,里面寫的ContextStartedEvent,表示監聽Spring上下文啟動完成后的事件;
    @Configuration相當於xml的beans標簽;
    @Bean標注在方法上,等同於xml的bean;

  2. 自定義注解@MQConsumer和注解@MQTransactionProducer是如何起作用的?
    工程里定義了com.hy.demo.rocketmq.config.RocketMQAnnotationScan類對這兩個注解進行掃描,利用注解@EventListener(ContextStartedEvent.class),監聽Spring上下文初始化事件,然后從Spring容器內讀取所有帶這兩個注解的類,把RocketMQ相關的配置信息加載進去,由於事務消息生產者類org.apache.rocketmq.client.producer.TransactionMQProducer的特殊性(它需要在初始化時注入TransactionListener監聽類,與應用模塊有一定耦合性),所以增加了一個Map集合存儲應用模塊內所有使用了@MQTransactionProducer注解的實例。
    專注Java高並發、分布式架構,更多技術干貨分享與心得,請關注公眾號:Java架構社區
    Java架構社區


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM