springboot3 要使用RocketTemplate 和 @RocketMQMessageListener
需要在配置类上加@Import(RocketMQAutoConfiguration.class)
切换rocketmq目录
cd d:\\rocketmq-all-5.1.4-bin-release\rocketmq-all-5.1.4-bin-release\bin
启动nameserver
mqnamesrv
启动brocker
mqbroker.cmd -n localhost:9876 autoCreateTopic=true
启动mq控制台 (可省略此步骤)
git clone https://github.com/apache/rocketmq-dashboard.git
mvn spring-boot:run
搭建springboot项目
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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>rocketmq-producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rocketmq-producer</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>
<!-- https://mvnrepository.com/artifact/org.apache.rocketmq/rocketmq-spring-boot-starter -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
发送消息
package com.example.rocketmq_producer;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.spring.autoconfigure.RocketMQAutoConfiguration;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
@Import(RocketMQAutoConfiguration.class)
@SpringBootApplication
public class RocketmqProducerApplication {
@Autowired
RocketMQTemplate mqTemplate;
@Bean
ApplicationRunner applicationRunner() {
return args -> {
// TODO Auto-generated method stub
System.out.println("Hello RocketMQ");
for (int i = 0; i < 10; i++) {
var res = mqTemplate.syncSend("test-topic", "Hello RocketMQ" + i);
System.out.printf("sync send result = %s %n", res);
}
};
}
public static void main(String[] args) {
SpringApplication.run(RocketmqProducerApplication.class, args);
}
}
接收消息
package com.example.rocketmq_listener.listener;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;
import java.time.Instant;
@Service
@RocketMQMessageListener(consumerGroup = "${rocketmq.producer.group:producer-group}", topic = "test-topic")
public class SimpleMesssageListener implements RocketMQListener<String> {
static {
System.out.println("listener start...");
}
@Override
public void onMessage(String s) {
System.out.println(Instant.now() +":收到消息:" + s);
}
}