[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>RabbitMQ重复消费如何解决
<>MQ消息为什么重复
网络不可达造成消息重复。只要通过网络交换数据,就无法避免这个问题。
<>AMQP 消费者确认机制
AMQP 定义了消费者确认机制(message ack),如果一个消费者应用B崩溃掉(此时连接会断掉),但是 broker 尚未获得
ACK(6.ACK确认),那么消息会被重新放入队列。所以 AMQP 提供的是“至少一次交付”(at-least-once
delivery),异常情况下,消息会被重复消费,此时业务要实现幂等性(重复消息处理)。
<>重复消息如何处理?
<>1.消费端处理消息的业务逻辑保持幂等性。 <>2.保证每条消息都有唯一编号且保证消息处理成功与去重表的日志同时出现。
第 1 条很好理解,只要保持幂等性,不管来多少条重复消息,最后处理的结果都一样。第 2 条原理就是利用一张日志表来记录已经处理成功的消息的
ID,如果新到的消息 ID 已经在日志表中,那么就不再处理这条消息。
第 1 条解决方案,很明显应该在消费端实现,不属于消息系统要实现的功能。第 2
条可以消息系统实现,也可以业务端实现。正常情况下出现重复消息的概率其实很小,如果由消息系统来实现的话,肯定会对消息系统的吞吐量和高可用有影响,所以最好还是由业务端自己处理消息重复的问题,这也是
RabbitMQ 不解决消息重复的问题的原因。
<>代码示例: if(redisTemplate.opsForValue().setIfAbsent(messageId, messageId,
expireTime, TimeUnit.SECONDS)){ exeBizCode(); }