RocketMQ代码实战(四):RocketMQ消息顺序

maven依赖和配置参考RocketMQ代码实战(一):使用rocketmq-spring-boot-starter发送和消费消息

随机消费

我们先来看这样一个程序:

发送消息的代码:

@RestController
public class RocketMqController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @GetMapping("sendMqRandom")
    public Object sendMqRandom() {
        List<SendResult> results = new ArrayList<>();
        for (int i = 0; i <= 3; i++) {
            MqMessage message = MqMessage.builder().name("无序消息" + i).msg("这是无序消息" + i).build();
            SendResult sendResult = rocketMQTemplate.syncSend(MqUtil.random_topic, message);
            results.add(sendResult);
        }
        return results;
    }

}

这里顺序的发了4条消息

消费者程序:

@Slf4j
@Component
@RocketMQMessageListener(
        topic = MqUtil.random_topic,
        consumerGroup = "rundown_consumer_group")
public class RandomListener implements RocketMQListener<MqMessage> {
    @Override
    public void onMessage(MqMessage message) {
        //发送消息是顺序发送的0,1,2,3,消费的顺序不一定是顺序的
        log.info("{}收到消息:{}", this.getClass().getSimpleName(), message);
    }
}

常量如下:

public class MqUtil {

    public static final String  random_topic = "random_topic";
}

启动程序后,访问:http://127.0.0.1:8080/sendMqRandom 控制台打印的日志信息中的消息数字并不一定是按照0,1,2,3的

由此可见,我们顺序发送的消息,消费顺序不一定是按照我们发送的顺序来消费的。

那么如果我们的业务场景中需要消费顺序与发送顺序一致,那么该如何做呢?

 

首先,为什么我们顺序发的消息并不是顺序消费呢?

这是因为发送消息的时候,消息发送默认是会采用轮询的方式发送到不同的queue,而消费端消费的时候,也是会分配到多个queue的,多个queue是同时拉取消息消费。同一条queue里面,RocketMQ的确是能保证FIFO(先进先出)的。但是由于顺序产生的消息不一定是发送到同一个queue,所以消费端自然也不能保证是顺序消费了。

看上个程序返回的SendResult的结果如下,每个消息的queueId都不同


[

    {
        "sendStatus": "SEND_OK",
        "msgId": "C0A808D119F418B4AAC223AC1B1B0029",
        "messageQueue": {
            "topic": "random_topic",
            "brokerName": "brocker",
            "queueId": 0
        },
        "queueOffset": 19,
        "transactionId": null,
        "offsetMsgId": "AC17900100002A9F00000000000346CE",
        "regionId": "DefaultRegion",
        "traceOn": true
    },
    {
        "sendStatus": "SEND_OK",
        "msgId": "C0A808D119F418B4AAC223AC1B1D002B",
        "messageQueue": {
            "topic": "random_topic",
            "brokerName": "brocker",
            "queueId": 1
        },
        "queueOffset": 19,
        "transactionId": null,
        "offsetMsgId": "AC17900100002A9F00000000000347FB",
        "regionId": "DefaultRegion",
        "traceOn": true
    },
    {
        "sendStatus": "SEND_OK",
        "msgId": "C0A808D119F418B4AAC223AC1B1E002D",
        "messageQueue": {
            "topic": "random_topic",
            "brokerName": "brocker",
            "queueId": 2
        },
        "queueOffset": 19,
        "transactionId": null,
        "offsetMsgId": "AC17900100002A9F0000000000034928",
        "regionId": "DefaultRegion",
        "traceOn": true
    },
    {
        "sendStatus": "SEND_OK",
        "msgId": "C0A808D119F418B4AAC223AC1B200031",
        "messageQueue": {
            "topic": "random_topic",
            "brokerName": "brocker",
            "queueId": 3
        },
        "queueOffset": 19,
        "transactionId": null,
        "offsetMsgId": "AC17900100002A9F0000000000034A55",
        "regionId": "DefaultRegion",
        "traceOn": true
    }

]

 

如果要做到顺序消费,就要把想要顺序消费的消息确保投递到同一条queue。

顺序消费

上代码:

@RestController
public class RocketMqController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @GetMapping("sendMqOrder")
    public Object sendMqOrder() {
        List<SendResult> results = new ArrayList<>();
        for (int i = 0; i <= 3; i++) {
            MqMessage message = MqMessage.builder().name("有序消息" + i).msg("这是有序消息" + i).build();
            SendResult sendResult = rocketMQTemplate.syncSendOrderly(MqUtil.order_topic, message, "hashkey");
            results.add(sendResult);
        }

        return results;
    }

}

其中常量信息

public class MqUtil {

    public static final String  order_topic = "order_topic";
}

我们通过:org.apache.rocketmq.spring.core.RocketMQTemplate#syncSendOrderly(String destination, Object payload, String hashKey)方法发送顺序消息,其中hashkey的解释如下:

use this key to select queue. for example: orderId, productId ...

消费者程序如下:

@Slf4j
@Component
@RocketMQMessageListener(
        topic = MqUtil.order_topic,
        consumeMode = ConsumeMode.ORDERLY,//指定为顺序消费
        consumerGroup = "order_consumer_group")
public class OrderListener implements RocketMQListener<MqMessage> {
    @Override
    public void onMessage(MqMessage message) {
        //发送消息是顺序发送的0,1,2,3,消费的顺序也是顺序的
        log.info("{}收到消息:{}", this.getClass().getSimpleName(), message);
    }
}

我们通过设置属性consumeMode = ConsumeMode.ORDERLY,指定消费模式为顺序消费。

启动程序后访问:http://127.0.0.1:8080/sendMqOrder 后,返回的SendResult的queueId也都是同一个,消费的顺序也和发送顺序一致了。

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值