记录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

启动后测试

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

相关推荐
暮志未晚Webgl4 分钟前
109. UE5 GAS RPG 实现检查点的存档功能
android·java·ue5
灯雾️8 分钟前
Spring Boot、Spring MVC和Spring间的区别
spring boot
小叶lr30 分钟前
idea 配置 leetcode插件 代码模版
java·leetcode·intellij-idea
qq_4298565734 分钟前
idea启动服务报错Application run failed
java·ide·intellij-idea
瑞雨溪36 分钟前
java中的this关键字
java·开发语言
supercool739 分钟前
SpringBoot(9)-Dubbo+Zookeeper
spring boot·dubbo·java-zookeeper
J不A秃V头A44 分钟前
Redisson 中开启看门狗(watchdog)机制
java·分布式锁·看门狗
草字1 小时前
uniapp input限制输入负数,以及保留小数点两位.
java·前端·uni-app
李迟1 小时前
某Linux发行版本无法使用nodejs程序重命名文件问题的研究
java·linux·服务器
MapleLea1f1 小时前
26届JAVA 学习日记——Day14
java·开发语言·学习·tcp/ip·程序人生·学习方法