建设银行兴安支行网站,wp风格网站,网站开发模式有什么,商标制作logo在线制作造成重复消费的原因#xff1a;
MQ向消费者推送message#xff0c;消费者向MQ返回ack#xff0c;告知所推送的消息消费成功。但是由于网络波动等原因#xff0c;可能造成消费者向MQ返回的ack丢失。MQ长时间#xff08;一分钟#xff09;收不到ack#xff0c;于是会向消…造成重复消费的原因
MQ向消费者推送message消费者向MQ返回ack告知所推送的消息消费成功。但是由于网络波动等原因可能造成消费者向MQ返回的ack丢失。MQ长时间一分钟收不到ack于是会向消费者再次推送该条message这样就造成了重复消费。
解决重复消费的办法 用存储redis或者mysql记录一下已经消费的message的id当message被消费前先去存储中查一下消费记录没有该条message的id则正常消费返回ack有该条message的id的话不用消费直接返回ack给MQ。 当然实际生产中的话选用redis是比较好的选择毕竟查mysql要进行磁盘IO效率要低得多而且绝大多数重复消费都是由于MQ没有收到消费者的ack于是造成MQ再次向消费者进行同一条message的投递。所以message的消费记录其实我们并不需要一直记录只需要保存一段时间当下次投递过来的时候消费者能查到消费记录然后准确返回ack给MQ就行。yml
#配置rabbitMq 服务器rabbitmq:host: xxxx#rabbitmq相关配置 15672是Web管理界面的端口5672是MQ访问的端口port: xxxxusername: xxxxpassword: xxxx#虚拟host 可以不设置,使用server默认hostvirtual-host: xxxxconnection-timeout: 0#确认消息已发送到队列(Queue)publisher-returns: true #确认消息已发送到交换机(Exchange)publisher-confirm-type: correlated# 设置消费端手动 acklistener:simple:retry:# 开启消费者(程序出现异常的情况下)进行重试enabled: true#重试间隔时间max-interval: 1000# 最大重试次数max-attempts: 3#开启手动确认消息acknowledge-mode: manual监听类
package com.rabbitmqprovider.service;import com.rabbitmq.client.Channel;
import com.rabbitmqprovider.commons.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;import java.io.IOException;/**** 防止重复消费*/
Slf4j
Service
public class TestBasicService {Autowiredprivate StringRedisTemplate redisTemplate;/*** RabbitListener 可以写在类、方法上* param channel* param message* throws IOException*///RabbitListener(queues {CommonUtils.queueStr})RabbitHandlerpublic void getMessage(Channel channel, Message message) throws IOException {try{String messageId message.getMessageProperties().getMessageId();String msg new String(message.getBody(),UTF-8);//判断messageId在redis中是否存在boolean flagestringRedisTemplate(messageId,msg);if(!flage){log.error(消息已重复处理失败,拒绝再次接收...);channel.basicReject(message.getMessageProperties().getDeliveryTag(), false); // 拒绝消息}else{//如果要防止 重复消费则需要将 id值存在 redis,每次 都要去redis中拿id比对是否存在存在则消费过-messageIdchannel.basicAck(message.getMessageProperties().getDeliveryTag(), false);log.info(接收到的消息{}-redisTemplate.opsForValue().get(messageId));}}catch (Exception e){if (message.getMessageProperties().getRedelivered()) {log.error(消息已重复处理失败,拒绝再次接收...);channel.basicReject(message.getMessageProperties().getDeliveryTag(), false); // 拒绝消息} else {log.error(消息即将再次返回队列处理...);channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);}}}/*** 判断Key是否存在* param messageId 唯一表示key* param msg value值* return*/private boolean stringRedisTemplate(String messageId,String msg){log.info(messageIdmessageId);//判断Key是否存在 有则返回true没有则返回falseif(redisTemplate.hasKey(messageId)){return false;}else{redisTemplate.opsForValue().setIfAbsent(messageId, msg);}return true;}
}------------------------------------------controller--------------------------------------------------
/*** 解决重复消费问题*/
GetMapping(/sendMessageTestOnly)
public void sendMessageTestOnly(){JSONObject jsonObject new JSONObject();jsonObject.put(message,世界很大);jsonObject.put(msg,你想去看看么);String json jsonObject.toJSONString();String messageIdUUID.randomUUID();Message message MessageBuilder.withBody(json.getBytes()).setContentType(MessageProperties.CONTENT_TYPE_JSON).setContentEncoding(UTF-8).setMessageId(messageId).build();rabbitTemplate.convertAndSend(CommonUtils.dirExchange,CommonUtils.routingKey,message,new CorrelationData(UUID.randomUUID().toString()));
}
---------------------------------回调------------------------------------------------------
package com.rabbitmqprovider.callback;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;/*** 队列防止消息丢失*/
Slf4j
Component
public class QueueCallback implements RabbitTemplate.ReturnCallback{Overridepublic void returnedMessage(Message message,int replyCode, String replyText, String exchange, String routingKey) {log.info(消息 {} 经交换机 {} 通过routingKey{} 路由到队列失败失败code为{} 失败原因为{},new String(message.getBody()), exchange, routingKey, replyCode, replyText);}
}package com.rabbitmqprovider.callback;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;/*** 当消息由生产者发到交换机后会回调该接口中的confirm方法*/
Component
Slf4j
public class ExchangeCallback implements RabbitTemplate.ConfirmCallback{/* correlationData 内含消息内容* ack 交换机接受成功或者失败。 true表示交换机接受消息成功 false表示交换机接受失败* cause 表示失败原因*/Overridepublic void confirm(CorrelationData correlationData, boolean ack, String cause) {if (ack){log.info(交换机收到消息 消息内容为{}-, correlationData);}else {log.info(交换机未收到消息消息内容为{}, 原因为{}-, correlationData, cause);}}}-----------------------------------------------------------------------------------------------------------------
执行顺序时先发送消息然后在接收消息并判断消息是否重复如果不重复 则回复消息否则 拒绝回复最后回调。
文章转载自: http://www.morning.txysr.cn.gov.cn.txysr.cn http://www.morning.lanyee.com.cn.gov.cn.lanyee.com.cn http://www.morning.swsrb.cn.gov.cn.swsrb.cn http://www.morning.mfrb.cn.gov.cn.mfrb.cn http://www.morning.tyjnr.cn.gov.cn.tyjnr.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.mgmqf.cn.gov.cn.mgmqf.cn http://www.morning.fwrr.cn.gov.cn.fwrr.cn http://www.morning.fhkr.cn.gov.cn.fhkr.cn http://www.morning.jggr.cn.gov.cn.jggr.cn http://www.morning.qzmnr.cn.gov.cn.qzmnr.cn http://www.morning.qghjc.cn.gov.cn.qghjc.cn http://www.morning.pgrsf.cn.gov.cn.pgrsf.cn http://www.morning.frpfk.cn.gov.cn.frpfk.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.ntqnt.cn.gov.cn.ntqnt.cn http://www.morning.yjprj.cn.gov.cn.yjprj.cn http://www.morning.pqhgn.cn.gov.cn.pqhgn.cn http://www.morning.tmnyj.cn.gov.cn.tmnyj.cn http://www.morning.tkjh.cn.gov.cn.tkjh.cn http://www.morning.pbzgj.cn.gov.cn.pbzgj.cn http://www.morning.yfpnl.cn.gov.cn.yfpnl.cn http://www.morning.wmpw.cn.gov.cn.wmpw.cn http://www.morning.wmfh.cn.gov.cn.wmfh.cn http://www.morning.gwsll.cn.gov.cn.gwsll.cn http://www.morning.ckwxs.cn.gov.cn.ckwxs.cn http://www.morning.nfbkz.cn.gov.cn.nfbkz.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.trzzm.cn.gov.cn.trzzm.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.rwwdp.cn.gov.cn.rwwdp.cn http://www.morning.wxrbl.cn.gov.cn.wxrbl.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.srkqs.cn.gov.cn.srkqs.cn http://www.morning.rmkyb.cn.gov.cn.rmkyb.cn http://www.morning.mwmxs.cn.gov.cn.mwmxs.cn http://www.morning.tfpqd.cn.gov.cn.tfpqd.cn http://www.morning.gthwr.cn.gov.cn.gthwr.cn http://www.morning.fdsbs.cn.gov.cn.fdsbs.cn http://www.morning.fwdln.cn.gov.cn.fwdln.cn http://www.morning.wbfg.cn.gov.cn.wbfg.cn http://www.morning.smpb.cn.gov.cn.smpb.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.ztrht.cn.gov.cn.ztrht.cn http://www.morning.wcqxj.cn.gov.cn.wcqxj.cn http://www.morning.bphqd.cn.gov.cn.bphqd.cn http://www.morning.gwtgt.cn.gov.cn.gwtgt.cn http://www.morning.fwkjp.cn.gov.cn.fwkjp.cn http://www.morning.gqjwz.cn.gov.cn.gqjwz.cn http://www.morning.mcjyair.com.gov.cn.mcjyair.com http://www.morning.npmcf.cn.gov.cn.npmcf.cn http://www.morning.bnfrj.cn.gov.cn.bnfrj.cn http://www.morning.pwwdp.cn.gov.cn.pwwdp.cn http://www.morning.mrfnj.cn.gov.cn.mrfnj.cn http://www.morning.lmrcq.cn.gov.cn.lmrcq.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn http://www.morning.ntqnt.cn.gov.cn.ntqnt.cn http://www.morning.wfjyn.cn.gov.cn.wfjyn.cn http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.pwxkn.cn.gov.cn.pwxkn.cn http://www.morning.mcfjq.cn.gov.cn.mcfjq.cn http://www.morning.lgtzd.cn.gov.cn.lgtzd.cn http://www.morning.npmcf.cn.gov.cn.npmcf.cn http://www.morning.lpcpb.cn.gov.cn.lpcpb.cn http://www.morning.ruifund.com.gov.cn.ruifund.com http://www.morning.pmwhj.cn.gov.cn.pmwhj.cn http://www.morning.wdprz.cn.gov.cn.wdprz.cn http://www.morning.qgwpx.cn.gov.cn.qgwpx.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.xhkgl.cn.gov.cn.xhkgl.cn http://www.morning.hxbps.cn.gov.cn.hxbps.cn http://www.morning.yrnyz.cn.gov.cn.yrnyz.cn http://www.morning.huayaosteel.cn.gov.cn.huayaosteel.cn http://www.morning.jjwzk.cn.gov.cn.jjwzk.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.tllhz.cn.gov.cn.tllhz.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.ngpdk.cn.gov.cn.ngpdk.cn