Spring Boot集成RocketMQ

pom依赖

XML 复制代码
        <!--spring-boot-starter的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <!--rocketmq的依赖-->
        <dependency>
            <groupId>org.apache.rocketmq</groupId>
            <artifactId>rocketmq-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

配置文件

XML 复制代码
spring.application.name=rocketmq-demo

rocketmq.nameServer=192.168.21.110:9876
rocketmq.producer.group=rocketmq-test-producer-group
rocketmq.consumer.group=rocketmq-test-consumer-group

消息生产者

java 复制代码
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

/**
 * @author qushen
 * @date 2025/1/8 9:21
 * @description: 创建消息生产者
 * @Version:1.0
 */
@Service
public class RocketMQProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    private final String topic = "demo-topic";

    // 1.同步发送消息
    public void sendSyncMessage(String message){
        rocketMQTemplate.syncSend(topic, MessageBuilder.withPayload(message).build());
        System.out.printf("同步发送结果: %s\n", message);
    }

    // 2.异步发送消息
    public void sendAsyncMessage(String message){
        rocketMQTemplate.asyncSend(topic, MessageBuilder.withPayload(message).build(), new SendCallback() {

            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.printf("异步发送成功: %s\n", sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.printf("异步发送失败: %s\n", throwable.getMessage());
            }
        });
    }

    // 3.单向发送消息
    public void sendOneWayMessage(String message){
        rocketMQTemplate.sendOneWay(topic, MessageBuilder.withPayload(message).build());
        System.out.println("单向消息发送成功");
    }
}

消息消费者

java 复制代码
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;


/**
 * @author qushen
 * @date 2025/1/8 9:22
 * @description: 创建消息消费者
 * @Version:1.0
 */
@Service
@RocketMQMessageListener(topic = "demo-topic", consumerGroup = "rocketmq-test-consumer-group", messageModel = MessageModel.CLUSTERING)
public class RocketMQConsumer implements RocketMQListener<String> {

    @Override
    public void onMessage(String s) {
        System.out.printf("收到消息: %s\n", s);
    }
}

消息控制器

java 复制代码
import com.sws.rocketmq.services.RocketMQProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api")
public class RocketController {
    @Autowired
    private RocketMQProducer rocketMQProducer;

    /**
     * 发送消息后,发送方会等待 Broker 的响应,确保消息成功发送到 Broker。
     * 可靠性高,确保消息不会丢失。
     * @param message
     * @return
     */
    @GetMapping("/sendSync")
    public String sendSync(@RequestParam String message) {
        rocketMQProducer.sendSyncMessage(message);
        return "同步消息发送成功";
    }

    /**
     * 发送消息后,发送方不会等待 Broker 的响应,而是通过回调函数处理发送结果。
     * 性能较高,适合高并发场景。
     * @param message
     * @return
     */
    @GetMapping("/sendAsync")
    public String sendAsync(@RequestParam String message) {
        rocketMQProducer.sendAsyncMessage(message);
        return "异步消息发送中";
    }

    /**
     * 发送消息后,发送方不会等待 Broker 的响应,也不关心发送结果。
     * 性能最高,适合对可靠性要求不高的场景。
     * @param message
     * @return
     */
    @GetMapping("/sendOneWay")
    public String sendOneWay(@RequestParam String message) {
        rocketMQProducer.sendOneWayMessage(message);
        return "单向消息发送成功";
    }
}

测试:发送同步消息

bash 复制代码
http://localhost:8080/api/sendSync?message=HelloSync

项目地址:rocketmq: Spring Boot 与 RocketMQ 集成

相关推荐
招风的黑耳13 小时前
我用SpringBoot撸了一个智慧水务监控平台
java·spring boot·后端
大佐不会说日语~13 小时前
Spring AI Alibaba 的 ChatClient 工具注册与 Function Calling 实践
人工智能·spring boot·python·spring·封装·spring ai
Miss_Chenzr14 小时前
Springboot优卖电商系统s7zmj(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
程序员游老板14 小时前
基于SpringBoot3+vue3的爱心陪诊平台
java·spring boot·毕业设计·软件工程·课程设计·信息与通信
期待のcode14 小时前
Springboot核心构建插件
java·spring boot·后端
Miss_Chenzr14 小时前
Springboot旅游景区管理系统9fu3n(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·旅游
五阿哥永琪15 小时前
Spring Boot 中自定义线程池的正确使用姿势:定义、注入与最佳实践
spring boot·后端·python
canonical_entropy16 小时前
Nop入门:增加DSL模型解析器
spring boot·后端·架构
计算机毕设VX:Fegn089517 小时前
计算机毕业设计|基于springboot + vue图书借阅管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
undsky_18 小时前
【RuoYi-SpringBoot3-Pro】:接入 AI 对话能力
人工智能·spring boot·后端·ai·ruoyi