RocketMQ整合SpringBoot普通消息

复制代码
<!--匹配服务器的RocketMQ5.3.0-->
<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-spring-boot-starter</artifactId>
    <version>2.3.1</version>
</dependency>

application.properties

复制代码
spring.application.name=springboot-rocketmq
server.port=8999



rocketmq.name-server=xxx.xxx.xxx:9876
rocketmq.producer.group=mq_producer_group_test

控制器

复制代码
package com.example.springbootrocketmq.controller;

import com.example.springbootrocketmq.pojo.User;
import com.example.springbootrocketmq.producer.RocketMQProducerService;
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.RestController;

/**
 * @author hrui
 * @date 2024/8/2 11:40
 */
@RestController
@RequestMapping("/api/test")
public class TestController {

    @Autowired
    private RocketMQProducerService producerService;

    @GetMapping("/send")
    public String sendMessage() {
        User user = new User("Hrui", 18, "China");
        producerService.sendSimpleMessage("mq_test-topic", user);
        return "消息发送成功";
    }
}

生产者

复制代码
package com.example.springbootrocketmq.producer;

import com.example.springbootrocketmq.pojo.User;
import jakarta.annotation.Resource;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author hrui
 * @date 2024/8/2 11:36
 */
@Service
public class RocketMQProducerService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    /**
     * 发送普通消息
     * @param topic
     * @param message
     */
    public void sendSimpleMessage(String topic, User message) {
        rocketMQTemplate.convertAndSend(topic, message);
    }
}

消费者

复制代码
package com.example.springbootrocketmq.consumer;

import com.example.springbootrocketmq.pojo.User;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

/**
 * @author hrui
 * @date 2024/8/2 11:44
 */
@Service
@RocketMQMessageListener(topic = "mq_test-topic", consumerGroup = "mq_consumer_group_test")
public class RocketMQConsumerService implements RocketMQListener<User> {
    @Override
    public void onMessage(User user) {
        System.out.println("消费者接收到消息: " + user);
    }
}
相关推荐
阿里云云原生7 小时前
乌镇大赛丨5 万奖金已备好!RocketMQ 等你一起打造全新 AI-Native 管控平台
apache·rocketmq
爱敲代码的小鱼8 小时前
springboot原理篇:
java·spring boot·spring
默默敲代码的徐哥儿9 小时前
八股文整理——后端
java·开发语言·spring boot·后端·学习
牧小七9 小时前
宠物管理系统2026
spring boot
名字还没想好☜9 小时前
Spring Boot 全局异常处理:@ControllerAdvice 实战
java·spring boot·后端·spring·异常处理
宠友信息11 小时前
Redis 内容社区源码架构优化与即时通讯数据一致性处理
spring boot·后端·websocket·mysql·uni-app
hdsoft_huge11 小时前
SpringBoot系列06:RESTful接口开发,请求参数接收、全局跨域CORS完整配置
java·spring boot
小明bishe181 天前
计算机毕业设计之基于JAVA的植物科普网站
java·spring boot·spring·架构·课程设计
万亿少女的梦1681 天前
基于Python的高考志愿填报辅助系统设计与实现
java·spring boot·python·mysql·vue
万亿少女的梦1681 天前
基于SpringBoot与Vue的历史博物馆展品展示与游客管理系统设计
spring boot·mysql·vue·系统设计·博物馆管理系统