SpringBoot2.x+rocketmq5.x整合服务

1 坑, 主要是各版本的依赖maven不一样,导致连接异常,找不到topic

2 JDK maven依赖和springboot start 依赖是不一样  下面是pom.xml

可以参考此处 文档, 还是官方文档靠谱消息队列 RocketMQ 版 Spring Boot Starter 使用-RocketMQ 5.x-文档中心-腾讯云


    org.apache.rocketmq
    rocketmq-spring-boot-starter
    2.2.2
         
                     
                 org.apache.rocketmq        
                 rocketmq-client    
                 
                     
                 org.apache.rocketmq        
                 rocketmq-acl    
              
      

     
            org.apache.rocketmq    
            rocketmq-client    
            4.9.7
        
            
            org.apache.rocketmq    
            rocketmq-acl    
            4.9.7
 

2 编写一个工具类发送消息

package cn.djrj.common.config;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.springframework.messaging.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * @author lmc
 * @Classname RocketMqHelper
 * @Description TODO
 * @Date 2023/12/4 13:42
 * @Created by lmc
 */
@Component
public class RocketMqHelper {

    /**
     * 日志
     */
    private static final Logger LOG = LoggerFactory.getLogger(RocketMqHelper.class);

    /**
     * rocketmq模板注入
     */
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @PostConstruct
    public void init() {
        LOG.info("---RocketMq助手初始化---");
    }

    /**
     * 发送异步消息
     *
     * @param topic   消息Topic
     * @param message 消息实体
     */
    public void asyncSend(Enum topic, Message message) {
        asyncSend(topic.name(), message, getDefaultSendCallBack());
    }

    /**
     * 发送异步消息
     *
     * @param topic        消息Topic
     * @param message      消息实体
     * @param sendCallback 回调函数
     */
    public void asyncSend(Enum topic, Message message, SendCallback sendCallback) {
        asyncSend(topic.name(), message, sendCallback);
    }

    /**
     * 发送异步消息
     *
     * @param topic   消息Topic
     * @param message 消息实体
     */
    public void asyncSend(String topic, Message message) {
        rocketMQTemplate.asyncSend(topic, message, getDefaultSendCallBack());
    }

    /**
     * 发送异步消息
     *
     * @param topic        消息Topic
     * @param message      消息实体
     * @param sendCallback 回调函数
     */
    public void asyncSend(String topic, Message message, SendCallback sendCallback) {
        rocketMQTemplate.asyncSend(topic, message, sendCallback);
    }

    /**
     * 发送异步消息
     *
     * @param topic        消息Topic
     * @param message      消息实体
     * @param sendCallback 回调函数
     * @param timeout      超时时间
     */
    public void asyncSend(String topic, Message message, SendCallback sendCallback, long timeout) {
        rocketMQTemplate.asyncSend(topic, message, sendCallback, timeout);
    }

    /**
     * 发送异步消息
     *
     * @param topic        消息Topic
     * @param message      消息实体
     * @param sendCallback 回调函数
     * @param timeout      超时时间
     * @param delayLevel   延迟消息的级别
     */
    public void asyncSend(String topic, Message message, SendCallback sendCallback, long timeout, int delayLevel) {
        rocketMQTemplate.asyncSend(topic, message, sendCallback, timeout, delayLevel);
    }

    /**
     * 发送顺序消息
     *
     * @param message
     * @param topic
     * @param hashKey
     */
    public void syncSendOrderly(Enum topic, Message message, String hashKey) {
        syncSendOrderly(topic.name(), message, hashKey);
    }

    /**
     * 发送顺序消息
     *
     * @param message
     * @param topic
     * @param hashKey
     */
    public void syncSendOrderly(String topic, Message message, String hashKey) {
        LOG.info("发送顺序消息,topic:" + topic + ",hashKey:" + hashKey);
        rocketMQTemplate.syncSendOrderly(topic, message, hashKey);
    }

    /**
     * 发送顺序消息
     *
     * @param message
     * @param topic
     * @param hashKey
     * @param timeout
     */
    public void syncSendOrderly(String topic, Message message, String hashKey, long timeout) {
        LOG.info("发送顺序消息,topic:" + topic + ",hashKey:" + hashKey + ",timeout:" + timeout);
        rocketMQTemplate.syncSendOrderly(topic, message, hashKey, timeout);
    }

    /**
     * 默认CallBack函数
     *
     * @return
     */
    private SendCallback getDefaultSendCallBack() {
        return new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                LOG.info("---发送MQ成功---");
            }
            @Override
            public void onException(Throwable throwable) {
                LOG.error("---发送MQ失败---" + throwable.getMessage(), throwable.getMessage());
            }
        };
    }
    @PreDestroy
    public void destroy() {
        LOG.info("---RocketMq助手注销---");
    }


}

3  消费者类

package cn.djrj.common.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**  消费者
 * @author lmc
 * @Classname ConsumerMqListener
 * @Description TODO
 * @Date 2023/12/4 13:49
 * @Created by lmc
 */

@Slf4j
@Component
@RocketMQMessageListener(topic = "TestTopic", consumerGroup = "${rocketmq.consumer.group}")
public class ConsumerMqListener implements RocketMQListener {

    @Override
    public void onMessage(String msg) {
        // System.out.println("接收到消息,开始消费..msg: " + person.getName() + ",age:" + person.getAge());
        log.info("接收到消息,开始消费...msg: " + msg);
    }

}

4 配置文件, 消费者和生产者都需要加账号

#rocketmq配置
rocketmq:
    name-server: rmq-cn.cn-hangzhou.rmq.aliyuncs.com:80
    consumer:
        group: Test_Group1
        access-key: xxxxxx
        secret-key: xxxxxx
    # 生产者配置
    producer:
        access-key: xxxxx
        secret-key: xxxxx
        is-on-off: on
        group: Test_Group1
        # 发送同一类消息的设置为同一个group,保证唯一
        # 服务地址
        #namesrvAddr: rmq-cn.cn-hangzhou.rmq.aliyuncs.com:80
        # 消息最大长度 默认1024*4(4M)
        maxMessageSize: 4096
        # 发送消息超时时间,默认3000
        sendMsgTimeout: 3000
        # 发送消息失败重试次数,默认2
        retryTimesWhenSendFailed: 2

测试方法, 测试类可以自定义发送

    @Resource
    RocketMqHelper rocketMqHelper;

    @GetMapping("/send")
    public AjaxResult sendMq() {
        SysVersion version = new SysVersion();
        version.setCode("2005");
        version.setProgrammeName("产品名称");
        version.setName("测试MQ");
        rocketMqHelper.asyncSend("TestTopic", MessageBuilder.withPayload(version).build());
        return AjaxResult.success(true);
    }

主要的坑, 版本必须对应,并且topic在消停队列存在, 分组必须要有

你可能感兴趣的:(java,spring,boot,spring)