SpringBoot整合RocketMQ 不多说直接上代码
发布人:shili8
发布时间:2025-02-27 08:48
阅读次数:0
**Spring Boot 整合 RocketMQ**
本文将指导您如何在 Spring Boot项目中整合 RocketMQ,一个高性能的分布式消息队列系统。
###依赖配置首先,我们需要在 `pom.xml` 文件中添加必要的依赖:
xml<dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- RocketMQ --> <dependency> <groupId>com.alibaba.rocketmq</groupId> <artifactId>rocketmq-client</artifactId> <version>4.9.2</version> </dependency> <!-- Spring Boot Starter for RocketMQ --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-rocketmq</artifactId> <version>1.7.3</version> </dependency> </dependencies>
### 配置文件接下来,我们需要配置 `application.properties` 文件:
properties# RocketMQ 配置rocketmq: name-server:127.0.0.1:9876 # RocketMQ 名称服务器地址 producer-group: spring-boot-rocketmq-producer # 生产者组名 consumer-group: spring-boot-rocketmq-consumer # 消费者组名# Spring Boot 配置spring: application: name: spring-boot-rocketmq-example # 应用名称
### 生产者配置生产者配置位于 `application.properties` 文件中,我们需要配置以下内容:
properties# RocketMQ 生产者配置rocketmq.producer.group= spring-boot-rocketmq-producerrocketmq.producer.name-server=127.0.0.1:9876
### 消费者配置消费者配置位于 `application.properties` 文件中,我们需要配置以下内容:
properties# RocketMQ 消费者配置rocketmq.consumer.group= spring-boot-rocketmq-consumerrocketmq.consumer.name-server=127.0.0.1:9876
### 生产者代码生产者代码位于 `com.example.springbootrocketmqexample.ProducerApplication` 类中:
javapackage com.example.springbootrocketmqexample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.rocketmq.core.RocketMQTemplate;
import org.springframework.rocketmq.core.RocketMQTemplateConfig;
@SpringBootApplicationpublic class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
@Bean public RocketMQTemplate rocketMQTemplate() {
return new RocketMQTemplate(new RocketMQTemplateConfig());
}
@Bean public MessageChannel messageChannel() {
return new PollableChannel();
}
@Bean public Producer producer(RocketMQTemplate rocketMQTemplate) {
return new Producer(rocketMQTemplate);
}
}
class Producer {
private final RocketMQTemplate rocketMQTemplate;
public Producer(RocketMQTemplate rocketMQTemplate) {
this.rocketMQTemplate = rocketMQTemplate;
}
public void sendMessage(String message) {
Message msg = MessageBuilder.withPayload(message).build();
rocketMQTemplate.convertAndSend(msg);
}
}
### 消费者代码消费者代码位于 `com.example.springbootrocketmqexample.ConsumerApplication` 类中:
javapackage com.example.springbootrocketmqexample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.rocketmq.core.RocketMQTemplate;
import org.springframework.rocketmq.core.RocketMQTemplateConfig;
@SpringBootApplicationpublic class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@Bean public RocketMQTemplate rocketMQTemplate() {
return new RocketMQTemplate(new RocketMQTemplateConfig());
}
@Bean public MessageChannel messageChannel() {
return new PollableChannel();
}
@Bean public Consumer consumer(RocketMQTemplate rocketMQTemplate) {
return new Consumer(rocketMQTemplate);
}
}
class Consumer {
private final RocketMQTemplate rocketMQTemplate;
public Consumer(RocketMQTemplate rocketMQTemplate) {
this.rocketMQTemplate = rocketMQTemplate;
}
public void receiveMessage(Message message) {
System.out.println("Received message: " + message.getPayload());
}
}
### 测试测试代码位于 `com.example.springbootrocketmqexample.TestApplication` 类中:
javapackage com.example.springbootrocketmqexample;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTestpublic class TestApplication {
@Test public void testSendMessage() {
Producer producer = new Producer(new RocketMQTemplate(new RocketMQTemplateConfig()));
producer.sendMessage("Hello, World!");
}
@Test public void testReceiveMessage() {
Consumer consumer = new Consumer(new RocketMQTemplate(new RocketMQTemplateConfig()));
Message message = MessageBuilder.withPayload("Hello, World!").build();
consumer.receiveMessage(message);
}
}
### 总结本文介绍了如何在 Spring Boot项目中整合 RocketMQ,包括依赖配置、生产者和消费者代码,以及测试代码。通过阅读本文,您应该能够轻松地将 RocketMQ 集成到您的 Spring Boot项目中。

