SpringBoot集成RocketMQ(附带集成工具类)

1. pom文件引入rocketmq的client端依赖

        
        
            org.apache.rocketmq
            rocketmq-client
            4.3.0
        

2. 配置文件yml设置

rocketmq:
  namesrvAddr: localhost:9876
  sendMsgTimeoutMillis: 3000
  reconsumeTimes: 3

3. 配置文件以及常量类配置

package com.es.jd.config.mq;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Created by xiaoxudong on 2019/3/22
 */
@Data
@Component
@ConfigurationProperties(prefix = "rocketmq")
public class RocketMqProperties {
 

    private String namesrvAddr;
    private Integer sendMsgTimeoutMillis;

    /**
     * 失败重试次数
     */
    private Integer reconsumeTimes;

 
}
package com.es.jd.config.mq;


import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


/**
 * Created by xiaoxudong on 2019/3/22
 * @author zhouzhou
 */
@Component
public class RocketMQConfig {
    Logger log = LoggerFactory.getLogger(RocketMQConfig.class);

    @Autowired
    private RocketMqProperties mqProperties;

    /**
     * 初始化生产者
     *
     * @return
     */
    @Bean
    public DefaultMQProducer defaultProducer() throws Exception {
        // 实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer(MqConstant.ConsumeGroup.ES_USER_IMPORT);
        // 设置NameServer的地址
        producer.setNamesrvAddr(mqProperties.getNamesrvAddr());
        // 设置发送消息超时时间
        producer.setSendMsgTimeout(mqProperties.getSendMsgTimeoutMillis());
        // 设置重试次数
        producer.setRetryTimesWhenSendFailed(mqProperties.getReconsumeTimes());
        // 启动Producer实例
        producer.start();

        return producer;
    }

}
package com.es.jd.config.mq;

/**
 * 功能描述:
 *
 * @Author: zhouzhou
 * @Date: 2020/8/7$ 16:09$
 */
public class MqConstant {


    /**
     * top
     */
    public static class Topic {

        /**
         * 稿件录入
         */
        public static final String ES_USER_IMPORT = "ES_USER_IMPORT";

    }


    /**
     * TAG
     */
    public static class Tag {

        public static final String ES_USER_IMPORT_TAG_INSERT = "ES_USER_IMPORT_TAG_INSERT";
        public static final String ES_USER_IMPORT_TAG_UPDATE = "ES_USER_IMPORT_TAG_UPDATE";
        public static final String ES_USER_IMPORT_TAG_DELETE = "ES_USER_IMPORT_TAG_DELETE";
    }


    /**
     * consumeGroup 消费者
     */
    public static class ConsumeGroup {

        public static final String ES_USER_IMPORT = "GID_ES_USER_IMPORT";

    }

}

4. 核心工具类以及消费类

package com.es.jd.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Description:Mq 消息发送工具类
 *
 * @author: zhouzhou
 * Date: 2020-05-25
 * Time: 2:19 PM
 */
@Component
@Slf4j
public class MqMessageSendUtils {

    @Autowired(required = false)
    private DefaultMQProducer rocketMqProducer;


    /**
     * private RocketMqProperties mqProperties;
     * 发送普通消息
     *
     * @param topic
     * @param tag
     * @param body
     * @param 
     * @return
     */
    public  boolean sendNormalMessage(String topic, String tag, T body) {

        Message message = new Message(topic, tag, JSON.toJSONBytes(body));

        try {
            SendResult sendResult = rocketMqProducer.send(message);
            if (sendResult != null) {
                log.info("mq 消息发送成功" + sendResult);
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            log.warn("mq 消息发送失败", e);
            return false;
        }

    }

    /**
     * 发送普通延时消息
     *
     * @param topic
     * @param tag
     * @param body
     * @param 
     * @param delay 毫秒
     * @return
     */
    public  boolean sendNormalDelayMessage(String topic, String tag, T body, Long delay) {

        Message message = new Message(topic, tag, JSON.toJSONBytes(body));
        // 默认延时 10s
        // 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
        message.setDelayTimeLevel(3);
        try {
            SendResult sendResult = rocketMqProducer.send(message);
            if (sendResult != null) {
                log.info("mq 延时消息发送成功" + sendResult);
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            log.warn("mq 延时消息发送失败", e);
            return false;
        }
    }


}

5. 调用,以及消费端

如何调用:

boolean result = mqMessageSendUtils.sendNormalMessage(MqConstant.Topic.ES_USER_IMPORT, MqConstant.Tag.ES_USER_IMPORT_TAG_INSERT, entity);

如何消费:

package com.es.jd.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.es.jd.bean.ESUser;
import com.es.jd.config.mq.MqConstant;
import com.es.jd.config.mq.RocketMqProperties;
import com.es.jd.repository.EsUserRepository;
import com.es.jd.temp.entity.EsUserEntity;
import com.es.jd.utils.BeanUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 功能描述: Mq 消费者
 *
 * @Author: zhouzhou
 * @Date: 2020/8/7$ 17:15$
 */
@Slf4j
@Service
public class MqConsumerService {

    @Autowired
    private RocketMqProperties mqProperties;

    @Autowired
    private EsUserRepository esUserRepository;


    @PostConstruct
    public void consumerTest() throws Exception {
        DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer(MqConstant.ConsumeGroup.ES_USER_IMPORT);
        defaultMQPushConsumer.setNamesrvAddr(mqProperties.getNamesrvAddr());
        // * 代表不过滤
        defaultMQPushConsumer.subscribe(MqConstant.Topic.ES_USER_IMPORT, "*");
        defaultMQPushConsumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
            for (MessageExt msg : msgs) {
                try {
                    byte[] body = msg.getBody();
                    String tags = msg.getTags();
                    String msgId = msg.getMsgId();


                    // 根据标签tag来决定什么操作
                    executeByTags(tags, body);

                } catch (Exception e) {
                    // 对次数进行冲正并且落库 todo 发送告警信息
                    log.warn(String.format("即将导入ES库失败, 失败原因为{%s}", e.getMessage()), e);
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }

            }

            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;

        });
        defaultMQPushConsumer.start();
        log.info(String.format("消费者{%s}启动了", MqConstant.Topic.ES_USER_IMPORT));
    }

    private void executeByTags(String tags, byte[] body) {
        switch (tags) {
            case MqConstant.Tag.ES_USER_IMPORT_TAG_INSERT:
                JSONObject insertObject = (JSONObject) JSON.parse(body);
                EsUserEntity insertEsUserRequest = insertObject.toJavaObject(EsUserEntity.class);

                if (insertEsUserRequest != null) {
                    ESUser esUser = BeanUtils.copy(insertEsUserRequest, ESUser.class);
                    List tagList = Lists.newArrayList(insertEsUserRequest.getTags().split("\\|"));
                    esUser.setTags(tagList);
                    esUser.setDesc(insertEsUserRequest.getUserDesc());
                    ESUser save = esUserRepository.save(esUser);
                    log.info("导入es库成功" + save);
                }
                break;
            case MqConstant.Tag.ES_USER_IMPORT_TAG_UPDATE:
                JSONObject updateObject = (JSONObject) JSON.parse(body);
                EsUserEntity updateEsUser = updateObject.toJavaObject(EsUserEntity.class);
                if (updateEsUser != null) {
                    ESUser esUser = BeanUtils.copy(updateEsUser, ESUser.class);
                    List tagList = Lists.newArrayList(updateEsUser.getTags().split("\\|"));
                    esUser.setTags(tagList);
                    esUser.setDesc(updateEsUser.getUserDesc());
                    ESUser save = esUserRepository.save(esUser);
                    log.info("更新es库成功" + save);
                }
                break;
            case MqConstant.Tag.ES_USER_IMPORT_TAG_DELETE:
                Integer id = (Integer) JSON.parse(body);
                esUserRepository.deleteById(new Long(id));
                log.info("删除es库成功id=" + id);

                break;

        }
    }


}

结束, 有不懂的就直接留言吧

官方文档地址: 

https://github.com/apache/rocketmq/blob/master/docs/cn/RocketMQ_Example.md

你可能感兴趣的:(RocketMQ)