SpringBoot3 集成 RocketMQ:生产者发送普通消息 - 详解
1. 需求
SpringBoot3项目,集成RocketMQ,作为生产者,发送普通消息。
2. 技术栈
| 技术栈 | 版本 |
|---|---|
| Java (JDK) | 21 |
| Spring Boot | 3.5.4 |
| rocketmq-spring(rocketmq-spring-boot-starter) | 2.3.4 |
| RocketMQ | 5.3.3 |
注意:RocketMQ 版本 5.3.3 ,指的是 RocketMQ 的服务器版本。
3. 实现步骤
3.1. 添加依赖
在 pom.xml 文件中添加 RocketMQ 的依赖:
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.3.4</version>
</dependency>
3.2. 配置 RocketMQ(YAML配置)
# RocketMQ配置:NameServer地址、生产者组
rocketmq:
name-server: 127.0.0.1:9876
producer:
group: hello-rocketmq-producer-group
3.3. 创建生产者类(发送消息代码)
创建一个生产者类,使用 RocketMQTemplate 发送普通消息:
package com.example.hello.rocketmq.producer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@RequiredArgsConstructor
public class HelloRockerMqProducer {
private final RocketMQTemplate rocketMQTemplate;
public void sendMessage() {
log.info("开始发送消息");
// 同步发送消息
rocketMQTemplate.convertAndSend("test-topic-1", "Hello, World!");
log.info("发送消息成功");
}
}
注意:应提前创建好
主题,可以通过 RocketMQ Dashboard 直接创建。
3.4. 发送消息(单元测试)
3.4.1. 单元测试代码
注意:发送消息前,请确保NameServer、Broker已启动且正常运行,主题(Topic)已经创建完成。
package com.example.hello.rocketmq.producer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class ProducerTest {
@Autowired
private HelloRockerMqProducer helloRockerMqProducer;
@Test
void testSendMessage() {
helloRockerMqProducer.sendMessage();
}
}
- 查看生产者控制台输出,确认消息是否正常发送。
- 通过 RocketMQ Dashboard 查看消息,确认消息是否已经发送到 RocketMQ 服务器。
3.4.2. 查看日志(生产者控制台)
23:26:03.712 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.example.hello.rocketmq.producer.ProducerTest]: ProducerTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
23:26:04.281 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration com.example.hello.rocketmq.producer.HelloRocketmqProducerApplication for test class com.example.hello.rocketmq.producer.ProducerTest
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.5.4)
2025-09-24T23:26:06.979+08:00 INFO 15576 --- [hello-rocketmq-producer] [ main] c.e.h.rocketmq.producer.ProducerTest : Starting ProducerTest using Java 21.0.1 with PID 15576 (started by SongGuanxun in E:\hello-world\hello-rocketmq-producer)
2025-09-24T23:26:06.992+08:00 INFO 15576 --- [hello-rocketmq-producer] [ main] c.e.h.rocketmq.producer.ProducerTest : No active profile set, falling back to 1 default profile: "default"
2025-09-24T23:26:13.175+08:00 INFO 15576 --- [hello-rocketmq-producer] [ main] o.a.r.s.a.RocketMQAutoConfiguration : a producer (hello-rocketmq-producer-group) init on namesrv 127.0.0.1:9876
2025-09-24T23:26:15.963+08:00 INFO 15576 --- [hello-rocketmq-producer] [ main] c.e.h.rocketmq.producer.ProducerTest : Started ProducerTest in 10.862 seconds (process running for 15.632)
Mockito is currently self-attaching to enable the inline-mock-maker. This will no longer work in future releases of the JDK. Please add Mockito as an agent to your build as described in Mockito's documentation: https://javadoc.io/doc/org.mockito/mockito-core/latest/org.mockito/org/mockito/Mockito.html#0.3
WARNING: A Java agent has been loaded dynamically (D:\maven\repository\net\bytebuddy\byte-buddy-agent\1.17.6\byte-buddy-agent-1.17.6.jar)
WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning
WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information
WARNING: Dynamic loading of agents will be disallowed by default in a future release
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
2025-09-24T23:26:18.227+08:00 INFO 15576 --- [hello-rocketmq-producer] [ main] c.e.h.r.producer.HelloRockerMqProducer : 开始发送消息
2025-09-24T23:26:18.956+08:00 INFO 15576 --- [hello-rocketmq-producer] [ main] c.e.h.r.producer.HelloRockerMqProducer : 发送消息成功
进程已结束,退出代码为 0
3.4.3. 查看消息( RocketMQ Dashboard)
通过 RocketMQ Dashboard 查看消息,已经发送到 RocketMQ 服务器。

浙公网安备 33010602011771号