SpringBoot整合RocketMq

createTime:4/23 version:0.0.1

updateTime:5/27 version:0.1.1(新增集群消费广播消费的概念)

RocketMq介绍说明(选型、对比)

https://www.jianshu.com/p/2838890f3284

个人对rocketMQ的简单认识

1.rocketMQ topic需要手动创建后才可以使用

2.rocketMQ有个组的概念,根据groupName接收消息,同一个groupName的消息处理完成之后被消费,不同的组名可同步消费同一个消息(集群消费模式)

3.rocketMQ可以在接收消息的同时判断消息是否被重复消费,可根据消息重试次数来进行相应处理

4.rocketMQ在防止消息丢失方面通过ConsumeConcurrentlyStatus.CONSUME_SUCCESS枚举确当被消费,只有返回此枚举值才确定消息被消费,否则会重新发送。

(补充知识点:集群消费模式、广播消费模式)在配置类中配置

集群消费:相当于点对点,groupName 一致时消费端只有一个会收到消息

广播消费:即使groupname一致,也会都收到消息

整合步骤(一步到位,支持可扩展的配置)

1.引入依赖

        <dependency>
            <groupId>com.alibaba.rocketmq</groupId>
            <artifactId>rocketmq-client</artifactId>
            <version>3.2.6</version>
        </dependency>

2.编写rocketMQ配置类

package com.jmall.iot.config;

import com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer;

import com.alibaba.rocketmq.common.consumer.ConsumeFromWhere;
import com.jmall.iot.rocketMQ.MessageListenerImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author liujichang
 * @description: RocketMQ消费端配置类
 * @date 2019/3/18
 */
@Configuration
public class MQConsumerConfiguration {
    public static final Logger logger = LoggerFactory.getLogger(MQConsumerConfiguration.class);

    @Value("${rocketmq.consumer.namesrvAddr}")
    private String namesrvAddr;
    @Value("${rocketmq.consumer.groupName}")
    private String groupName;
    @Value("${rocketmq.consumer.consumeThreadMin}")
    private int consumeThreadMin;
    @Value("${rocketmq.consumer.consumeThreadMax}")
    private int consumeThreadMax;
    @Value("${rocketmq.consumer.topics}")
    private String topics;
    @Value("${rocketmq.consumer.consumeMessageBatchMaxSize}")
    private int consumeMessageBatchMaxSize;

    @Autowired
    private MessageListenerImpl mqMessageListenerProcessor;


    @Bean
    public DefaultMQPushConsumer getRocketMQConsumer() {
        if(StringUtils.isEmpty(groupName)){
            throw new RuntimeException("groupName 为空");
        }

        if(StringUtils.isEmpty(namesrvAddr)){
            throw new RuntimeException("namesrvAddr 为空");
        }

        if(StringUtils.isEmpty(topics)){
            throw new RuntimeException("topics 为空");
        }

        if(StringUtils.isEmpty(consumeThreadMin)){
            throw new RuntimeException("consumeThreadMin 为空");
        }

        if(StringUtils.isEmpty(consumeThreadMax)){
            throw new RuntimeException("consumeThreadMax 为空");
        }

        if(StringUtils.isEmpty(consumeMessageBatchMaxSize)){
            throw new RuntimeException("consumeMessageBatchMaxSize 为空");
        }
        //TODO 测试
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值