记录spring-boot 3.X版本整合RocketMq

版本信息

先把该次整合的版本信息列如下:

spring-boot spring-cloud rocketmq-spring-boot-starter rocketmq-client rocketmq
3.0.13 2022.0.5 2.2.3 4.9.8 4.9.8

版本信息是如何选择的呢?看rocketMq官网springcloud alibaba版本声明

rocketMq我用的是最新的4.X版本4.9.8

本地启动rocketMq服务端

这块你们找文章看。我是从github上fork后下载到本地。使用idea启动nameSrv和broker

构建测试工程

先创建父工程,用于pom的依赖继承

配置属性

    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <revision>0.0.1</revision>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.test.skip>false</maven.test.skip>
    <maven.javadoc.skip>true</maven.javadoc.skip>
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>

    <spring-boot-dependencies.version>3.0.13</spring-boot-dependencies.version>
    <spring.cloud-version>2022.0.5</spring.cloud-version>
    <com.alibaba.cloud.version>2022.0.0.0</com.alibaba.cloud.version>
    <knife4j.version>2.0.5</knife4j.version>

注意使用spring-boot 3.x版本后,jdk必须要17以上。所以本地需要安装jdk17

增加父工程依赖

xml 复制代码
<dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot-dependencies.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${com.alibaba.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

创建子工程

使用spring-boot的初始化initializr创建工程并下载放到父工程子目录下

修改pom文件

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.hjq</groupId>
		<artifactId>sunshine-all</artifactId>
		<version>${revision}</version>
		<relativePath>../pom.xml</relativePath>
	</parent>
<!--	<parent>-->
<!--		<groupId>org.springframework.boot</groupId>-->
<!--		<artifactId>spring-boot-starter-parent</artifactId>-->
<!--		<version>3.2.11</version>-->
<!--		<relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
<!--	</parent>-->
	<groupId>com.hjq.sunshine</groupId>
	<artifactId>demo</artifactId>

	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.rocketmq</groupId>
			<artifactId>rocketmq-spring-boot-starter</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.apache.rocketmq</groupId>
					<artifactId>rocketmq-client</artifactId>
				</exclusion>
			</exclusions>
			<version>2.2.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.rocketmq</groupId>
			<artifactId>rocketmq-client</artifactId>
			<version>4.9.8</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

增加生产者消费者启动配置

新增application.yml文件

java 复制代码
rocketmq:
  producer:
    group: myProducerGroup
  name-server: 127.0.0.1:9876

新增生产者

java 复制代码
@Component
@Log
public class FirstProduce implements CommandLineRunner {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void run(String... args) throws Exception {
        log.info("1111");
        SendResult result = rocketMQTemplate.syncSend("TestTopic", "1111");
        log.info(JSONObject.toJSONString(result));
        log.info("2222");


    }
}

新增消费者

java 复制代码
package com.hjq.sunshine.demo.rocketmq;

import lombok.extern.java.Log;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;

import java.util.function.Consumer;

/**
 * @Description
 * @Author hjq
 * @Date 2024/11/21
 */
@Service
@Log
@RocketMQMessageListener(consumerGroup = "MyConsumer", topic = "TestTopic",
        consumeMode = ConsumeMode.CONCURRENTLY, messageModel = MessageModel.BROADCASTING)
public class FirstConsumer implements RocketMQListener<String> {


    @Override
    public void onMessage(String message) {
        log.info("rece msg: " + message);
    }
}

注意事项。使用spring-boot 3.x版本,rocketmq-spring-boot-starter的自动配置会有bug,提示rocketMQTemplate没有bean注入。解决方案参考未注入bean

启动后测试

启动就可以看到如下,正常的生产和消费。

相关推荐
小张认为的测试5 分钟前
Liunx上Jenkins 持续集成 Java + Maven + TestNG + Allure + Rest-Assured 接口自动化项目
java·ci/cd·jenkins·maven·接口·testng
蘑菇丁34 分钟前
ansible批量生产kerberos票据,并批量分发到所有其他主机脚本
java·ide·eclipse
呼啦啦啦啦啦啦啦啦2 小时前
【Redis】持久化机制
java·redis·mybatis
我想学LINUX2 小时前
【2024年华为OD机试】 (A卷,100分)- 微服务的集成测试(JavaScript&Java & Python&C/C++)
java·c语言·javascript·python·华为od·微服务·集成测试
空の鱼7 小时前
java开发,IDEA转战VSCODE配置(mac)
java·vscode
!!!5258 小时前
日志技术-LogBack入门程序&Log配置文件&日志级别
spring boot
P7进阶路8 小时前
Tomcat异常日志中文乱码怎么解决
java·tomcat·firefox
小丁爱养花9 小时前
Spring MVC:HTTP 请求的参数传递2.0
java·后端·spring
CodeClimb9 小时前
【华为OD-E卷 - 第k个排列 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od