java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > spring-boot 3.X整合RocketMq

spring-boot 3.X版本整合RocketMq的详细过程

作者:chudaxiakkk

本文详细介绍了如何使用Spring Boot和RocketMQ进行整合,并提供了一个简单的示例工程,描述了如何本地启动RocketMQ服务端,并使用IntelliJ IDEA创建和配置了一个Spring Boot项目,包含了生产者和消费者,并解决了在Spring Boot 3.x版本中RocketMQTemplate没有bean注入的问题

版本信息

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

spring-bootspring-cloudrocketmq-spring-boot-starterrocketmq-clientrocketmq
3.0.132022.0.52.2.34.9.84.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

增加父工程依赖

<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 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文件

rocketmq:
  producer:
    group: myProducerGroup
  name-server: 127.0.0.1:9876

新增生产者

@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");
    }
}

新增消费者

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

启动后测试

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

到此这篇关于spring-boot 3.X版本整合RocketMq的详细过程的文章就介绍到这了,更多相关spring-boot 3.X整合RocketMq内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文