点击上方“芋道源码”,选择“设为星标”
做积极的人,而不是积极废人!
源码精品专栏
摘要: 原创出处 http://www.iocoder.cn/RocketMQ/spring-boot-integration/ 「芋道源码」欢迎转载,保留摘要,谢谢!
1. 概述
2. 调试环境搭建
3. 项目结构一览
5.
annotation包6.
autoconfigure包7.
config包8.
support包9.
core包666. 彩蛋
1. 概述
在开始分享 https://github.com/apache/rocketmq-spring 项目(RocketMQ 集成到 Spring Boot 中),我们先恶趣味的看一段历史:
2014-08 Spring Boot 1 正式发布。
2018-03 Spring Boot 2 正式发布。
2018-12 RocketMQ 团队发布 RocketMQ 集成到 Spring Boot 的解决方案,并且提供了中文文档。
在阅读本文之前,希望胖友能够先熟读 中文文档 。最好呢,当然不强制,可以操练下每个 Demo 。
2. 调试环境搭建
在读源码之前,我们当然是先把调试环境搭建起来。
2.1 依赖工具
JDK :1.8+
Maven
IntelliJ IDEA
2. 源码拉取
从官方仓库 https://github.com/apache/rocketmq-spring Fork 出属于自己的仓库。为什么要 Fork ?既然开始阅读、调试源码,我们可能会写一些注释,有了自己的仓库,可以进行自由的提交。????
使用 IntelliJ IDEA 从 Fork 出来的仓库拉取代码。拉取完成后,Maven 会下载依赖包,可能会花费一些时间,耐心等待下。
在等待的过程中,我来简单说下,搭建调试环境的过程:
启动 RocketMQ Namesrv
启动 RocketMQ Broker
启动 RocketMQ Spring Boot Producer
启动 RocketMQ Spring Boot Consumer
最小化的 RocketMQ 的环境,暂时不考虑 Namesrv 集群、Broker 集群、Consumer 集群。
???? 另外,本文使用的 rocketmq-spring 版本是 2.0.2-SNAPSHOT 。
2.3 启动 RocketMQ Namesrv
方式一,可以参考 《RocketMQ 源码解析 —— 调试环境搭建》 的 「3. 启动 RocketMQ Namesrv」 的方式,进行启动 RocketMQ Namesrv 。
方式一,可以方便调试 RocketMQ Namesrv 的代码。
方式二,也可以按照 《Apache RocketMQ —— Quick Start》 的 「Start Name Server」 的方式,进行启动 RocketMQ Namesrv 。
方式二,比较方便。
2.4 启动 RocketMQ Broker
方式一,可以参考 《RocketMQ 源码解析 —— 调试环境搭建》 的 「4. 启动 RocketMQ Broker」 的方式,进行启动 RocketMQ Broker 。
需要注意的是,要删除
org.apache.rocketmq.broker.transaction.AbstractTransactionalMessageCheckListener和org.apache.rocketmq.broker.transaction.TransactionalMessageService两个 SPI 配置文件,否则事务功能,无法正常使用。
方式二,也可以按照 《Apache RocketMQ —— Quick Start》 的 「Start Broker」 的方式,进行启动 RocketMQ Broker 。
2.5 启动 RocketMQ Spring Boot Producer
第一步,打开根目录的 pom.xml 文件,将 rocketmq-spring-boot-samples 示例项目的注释去掉。如下:
<!-- pom -->
<modules>
<module>rocketmq-spring-boot-parent</module>
<module>rocketmq-spring-boot</module>
<module>rocketmq-spring-boot-starter</module>
<!-- Note: The samples need to mvn compiple in its own directory
<module>rocketmq-spring-boot-samples</module>
-->
<module>rocketmq-spring-boot-samples</module>
</modules>
此时,Maven 又会下载依赖包,可能会花费一些时间,耐心等待下。
第二步,右键运行 rocketmq-produce-demo 的 ProducerApplication 的 #main(String[] args) 方法,Producer 就启动完成了。输出日志如下图:
此时,可能会报 Intellij IDEA 报错:Error : java 不支持发行版本5 。可以参考 《Intellij idea 报错:Error : java 不支持发行版本5》 文章,进行解决。
2.6 启动 RocketMQ Spring Boot Consumer
右键运行 rocketmq-consumer-demo 的 ConsumerApplication 的 #main(String[] args) 方法,Consumer 就启动完成了。输出日志如下图:
???? 后面,我们就可以愉快的各种调试玩耍了~
3. 项目结构一览
本文主要分享 rocketmq-spring 的 项目结构。
希望通过本文能让胖友对 rocketmq-spring 的整体项目有个简单的了解。
3.1 代码统计
这里先分享一个小技巧。笔者在开始源码学习时,会首先了解项目的代码量。
第一种方式,使用 IDEA Statistic 插件,统计整体代码量。
我们可以粗略的看到,总的代码量在 1700 行。这其中还包括单元测试,示例等等代码。
所以,不慌,一点不慌~
第二种方式,使用 Shell 脚本命令逐个 Maven 模块统计 。
一般情况下,笔者使用 find . -name "*.java"|xargs cat|grep -v -e ^$ -e ^\s*\/\/.*$|wc -l 。这个命令只过滤了部分注释,所以相比 IDEA Statistic 会偏多。
当然,考虑到准确性,胖友需要手动 cd 到每个 Maven 项目的 src/main/java 目录下,以达到排除单元测试的代码量。
统计完后,是不是更加不慌了。哈哈哈哈。
3.2 rocketmq-spring-boot-parent 模块
rocketmq-spring-boot-parent 模块,无具体代码,作为其它项目的 Maven Parent 项目,例如定义了依赖版本号。
3.3 rocketmq-spring-boot-starter 模块
rocketmq-spring-boot-starter 模块,无具体代码,作为 Spring Boot RocketMQ Starter 模块。其 pom.xml 的代码如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-parent</artifactId>
<version>2.0.2-SNAPSHOT</version>
<relativePath>../rocketmq-spring-boot-parent/pom.xml</relativePath>
</parent>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<packaging>jar</packaging>
<name>RocketMQ Spring Boot Starter</name>
<description>SRocketMQ Spring Boot Starter</description>
<url>https://github.com/apache/rocketmq-spring</url>
<dependencies>
<!-- Spring Boot RocketMQ 具体实现 -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot</artifactId>
</dependency>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 提供 Validation 功能 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
</project>
3.4 rocketmq-spring-boot 模块
rocketmq-spring-boot 模块,1979 行代码,提供了 Spring Boot RocketMQ 的具体实现。其每个 package 包的功能,分别如下:
annotation:注解和注解相关的枚举。autoconfigure:自动配置。config:配置类。有点难解释。等后面直接撸源码。
core:核心实现。support:提供支持,例如说工具类。有点难解释。等后面直接撸源码。
3.5 rocketmq-spring-boot-samples 模块
rocketmq-spring-boot-samples 模块,435 行代码,提供示例。* rocketmq-consume-demo 模块,提供消费者示例。* rocketmq-produce-demo 模块,提供生产者示例。
艿艿:后面的小节,我们开始看具体的源码。
5. annotation 包
5.1 @RocketMQMessageListener
org.apache.rocketmq.spring.annotation.@RocketMQMessageListener 注解,声明指定 Bean 是 RocketMQ 消费者的 MessageListener 。代码如下:
// RocketMQMessageListener.java
@Target(ElementType.TYPE) // 表名,注解在类上
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RocketMQMessageListener {
/**
* 消费分组
*
* Consumers of the same role is required to have exactly same subscriptions and consumerGroup to correctly achieve
* load balance. It's required and needs to be globally unique.
*
*
* See <a href="http://rocketmq.apache.org/docs/core-concept/">here</a> for further discussion.
*/
String consumerGroup();
/**
* 消费主体
*
* Topic name.
*/
String topic();
/**
* 选择消息的方式
*
* Control how to selector message.
*
* @see SelectorType
*/
SelectorType selectorType() default SelectorType.TAG;
/**
* 选择消息的表达式
*
* Control which message can be select. Grammar please see {@link SelectorType#TAG} and {@link SelectorType#SQL92}
*/
String selectorExpression() default "*";
/**
* 消费模式
*
* Control consume mode, you can choice receive message concurrently or orderly.
*/
ConsumeMode consumeMode() default ConsumeMode.CONCURRENTLY;
/**
* 消费模型
*
* Control message mode, if you want all subscribers receive message all message, broadcasting is a good choice.
*/
MessageModel messageModel() default MessageModel.CLUSTERING;
/**
* 消费线程数
*
* Max consumer thread number.
*/
int consumeThreadMax() default 64;
}
具体使用,见示例 OrderPaidEventConsumer 。
selectorType属性,org.apache.rocketmq.spring.annotation.SelectorType枚举,选择消息的方式。代码如下:// SelectorType.java public enum SelectorType { /** * @see ExpressionType#TAG * * 标签 */ TAG, /** * @see ExpressionType#SQL92 * * SQL */ SQL92 }consumeMode属性,org.apache.rocketmq.spring.annotation.ConsumeMode,消费模式。代码如下:// ConsumeMode.java public enum ConsumeMode { /** * Receive asynchronously delivered messages concurrently * * 并发消费 */ CONCURRENTLY, /** * Receive asynchronously delivered messages orderly. one queue, one thread * * 顺序消费 */ ORDERLY }messageModel属性,org.apache.rocketmq.spring.annotation.MessageModel,消费模型。代码如下:// MessageModel.java public enum MessageModel { /** * 广播消费 */ BROADCASTING("BROADCASTING"), /** * 集群消费 */ CLUSTERING("CLUSTERING"); private final String modeCN; MessageModel(String modeCN) { this.modeCN = modeCN; } public String getModeCN() { return this.modeCN; } }
5.2 @RocketMQTransactionListener
org.apache.rocketmq.spring.annotatio.@RocketMQTransactionListener 注解,声明指定 Bean 是 RocketMQ 生产者的 RocketMQLocalTransactionListener 。代码如下:
// RocketMQTransactionListener.java
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE}) // 表名,注解在类上
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component // 默认带了 @Component 注解,所以只要添加到了类上,就会注册成 Spring Bean 对象
public @interface RocketMQTransactionListener {
/**
* 生产者分组
*
* Declare the txProducerGroup that is used to relate callback event to the listener, rocketMQTemplate must send a
* transactional message with the declared txProducerGroup.
* <p>
* <p>It is suggested to use the default txProducerGroup if your system only needs to define a TransactionListener class.
*/
String txProducerGroup() default RocketMQConfigUtils.ROCKETMQ_TRANSACTION_DEFAULT_GLOBAL_NAME;
/**
* Set ExecutorService params -- corePoolSize
*/
int corePoolSize() default 1;
/**
* Set ExecutorService params -- maximumPoolSize
*/
int maximumPoolSize() default 1;
/**
* Set ExecutorService params -- keepAliveTime
*/
long keepAliveTime() default 1000 * 60; //60ms
/**
* Set ExecutorService params -- blockingQueueSize
*/
int blockingQueueSize() default 2000;
}
// RocketMQConfigUtils.java
public static final String ROCKETMQ_TRANSACTION_DEFAULT_GLOBAL_NAME = "rocketmq_transaction_default_global_name";
6. autoconfigure 包
6.1 RocketMQProperties
org.apache.rocketmq.spring.autoconfigure.RocketMQProperties ,RocketMQ 客户端的 Properties 对象。代码如下:
// RocketMQProperties.java
@ConfigurationProperties(prefix = "rocketmq") // 配置文件中 rocketmq 前缀
public class RocketMQProperties {
/**
* The name server for rocketMQ, formats: `host:port;host:port`.
*
* Namesrv 地址
*/
private String nameServer;
/**
* Producer 配置
*/
private Producer producer;
// ... 省略 setting/getting 方法
public static class Producer {
/**
* Name of producer.
*/
private String group;
/**
* Millis of send message timeout.
*/
private int sendMessageTimeout = 3000;
/**
* Compress message body threshold, namely, message body larger than 4k will be compressed on default.
*/
private int compressMessageBodyThreshold = 1024 * 4;
/**
* Maximum number of retry to perform internally before claiming sending failure in synchronous mode.
* This may potentially cause message duplication which is up to application developers to resolve.
*/
private int retryTimesWhenSendFailed = 2;
/**
* <p> Maximum number of retry to perform internally before claiming sending failure in asynchronous mode. </p>
* This may potentially cause message duplication which is up to application developers to resolve.
*/
private int retryTimesWhenSendAsyncFailed = 2;
/**
* Indicate whether to retry another broker on sending failure internally.
*/
private boolean retryNextServer = false;
/**
* Maximum allowed message size in bytes.
*/
private int maxMessageSize = 1024 * 1024 * 4;
&n

本文介绍了如何集成RocketMQ到Spring Boot项目中,包括调试环境的搭建、项目结构分析,以及关键类如RocketMQMessageListener、RocketMQTransactionListener等的讲解。详细探讨了RocketMQSpringBootStarter模块、RocketMQTemplate的使用,并提供了调试和配置RocketMQ Namesrv、Broker、Producer和Consumer的步骤。此外,还介绍了RocketMQListenerContainer的相关配置和功能。
最低0.47元/天 解锁文章
4163

被折叠的 条评论
为什么被折叠?



