使用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架構社區

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章