网站的服务器和空间,小程序开发工具下载,开发者选项怎么设置最流畅,建设集团网站的作用文章目录 一.消息可靠性1.生产者消息确认 MQ的一些常见问题
1.消息可靠性问题:如何确保发送的消息至少被消费一次
2.延迟消息问题:如何实现消息的延迟投递
3.高可用问题:如何避免单点的MQ故障而导致的不可用问题
4.消息堆积问题:如何解决数百万消息堆积#xff0c;无法及时… 文章目录 一.消息可靠性1.生产者消息确认 MQ的一些常见问题
1.消息可靠性问题:如何确保发送的消息至少被消费一次
2.延迟消息问题:如何实现消息的延迟投递
3.高可用问题:如何避免单点的MQ故障而导致的不可用问题
4.消息堆积问题:如何解决数百万消息堆积无法及时消费的问题
一.消息可靠性
消息从生产者发送到exchange再到queue再到消费者有哪些导致消息丢失的可能性 -发送时丢失 生产者发送的消息未送达exchange 消息到达exchange后未到达queue MQ宕机queue将消息丢失 consumer接收到消息后未消费就宕机
1.生产者消息确认
生产者确认机制
RabbitMQ提供了publisher confirm机制来避免消息发送到MQ过程中丢失。消息发送到MQ以后会返回一个结果给发送者表示消息是否处理成功。结果有两种请求
publisher-confirm发送者确认
消息成功投递到交换机返回ack 消息未投递到交换机返回nack
publisher-return发送者回执 消息投递到交换机了但是没有路由到队列。返回ACK及路由失败原因。
注意:确认机制发送消息时需要给每个消息设置一个全局唯一id以区分不同消息避免ack冲突 简单来说:在publisher-confirm下的nack是消息投递到交换机失败返回的信息;在publisher-confirm下的ack是消息成功到达了消费者;在publisher-return下的ack是消息到达了交换机但是路由失败的返回信息 SpringAMQP实现生产者确认
一.想要实现生产者消息确认机制,需要在配置文件编写开启代码,即在微服务的application.yml中添加配置:
spring:rabbitmq:publisher-confirm-type: correlated publisher-returns: true template:mandatory: true配置说明 publish-confirm-type开启publisher-confirm这里支持两种类型 simple同步等待confirm结果直到超时 correlated异步回调定义ConfirmCallbackMQ返回结果时会回调这个ConfirmCallback(推荐使用) publish-returns开启publish-return功能同样是基于callback机制不过是定义ReturnCallback template.mandatory定义消息路由失败时的策略。true则调用ReturnCallbackfalse则直接丢弃消息
二.配置ReturnCallback
ReturnCallback是消息到达交换机但是没有成功进行路由的回调函数(作用于全局)
每个RabbitTemplate只能配置一个ReturnCallback因此需要在项目启动过程中配置
Slf4j
Configuration
public class CommonConfig implements ApplicationContextAware {Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {// 获取RabbitTemplateRabbitTemplate rabbitTemplate applicationContext.getBean(RabbitTemplate.class);// 设置ReturnCallbackrabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) - {log.info(消息发送失败应答码{}原因{}交换机{}路由键{},消息{}, replyCode, replyText, exchange, routingKey, message.toString());});}
}对于代码中的ApplicationContext是负责管理和组织Spring应用中的各个组件如bean、配置文件等.
通过实现ApplicationContextAware这个接口bean可以获取对ApplicationContext的引用并因此获得访问应用上下文中的其他bean、资源和容器特性的能力,所以说实现了接口等同于获取到了bean容器,就可以获取到 rabbitTemplate并进行设置唯一的ReturnCallback
在回调函数中,消息路由失败会返回很多信息,其中使用路由键,消息的交换机的名称可以实现重发消息
三.在生产者类中发送消息并同时实现ConfirmCallback
ConfirmCallback同样是回调函数,与ReturnCallback不同的是ConfirmCallback可以创建多次
ConfirmCallback是对消息还没有进入到交换机就丢失的一种消息返回策略,当丢失后,执行回调函数并可以记录消息的失败原因和UUID,成功也是同理
Test
public void testSendMessage2SimpleQueue() throws InterruptedException {// 消息体String message hello, spring amqp!;// 消息ID需要封装到CorrelationData中CorrelationData correlationData new CorrelationData(UUID.randomUUID().toString());// 添加callbackcorrelationData.getFuture().addCallback(result - {if(result.isAck()){ // ack消息成功log.debug(消息发送成功, ID:{}, correlationData.getId());}else{// nack消息失败log.error(消息发送失败, ID:{}, 原因{},correlationData.getId(), result.getReason());}},ex - log.error(消息发送异常, ID:{}, 原因{},correlationData.getId(),ex.getMessage()));// 发送消息rabbitTemplate.convertAndSend(amq.direct, simple, message, correlationData);
}总结:
SpringAMQP中处理消息确认的几种情况 publisher-comfirm 消息成功发送到exchange返回ack 消息发送失败没有到达交换机返回nack 消息发送过程中出现异常没有收到回执 消息成功发送到exchange但没有路由到queue调用ReturnCallback
文章转载自: http://www.morning.rfwqt.cn.gov.cn.rfwqt.cn http://www.morning.bhgnj.cn.gov.cn.bhgnj.cn http://www.morning.qnksk.cn.gov.cn.qnksk.cn http://www.morning.xltdh.cn.gov.cn.xltdh.cn http://www.morning.mnwmj.cn.gov.cn.mnwmj.cn http://www.morning.znrlg.cn.gov.cn.znrlg.cn http://www.morning.dmtwz.cn.gov.cn.dmtwz.cn http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn http://www.morning.qcmhs.cn.gov.cn.qcmhs.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.hpnhl.cn.gov.cn.hpnhl.cn http://www.morning.qfqld.cn.gov.cn.qfqld.cn http://www.morning.jnkng.cn.gov.cn.jnkng.cn http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.jgmdr.cn.gov.cn.jgmdr.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.bkpbm.cn.gov.cn.bkpbm.cn http://www.morning.lnckq.cn.gov.cn.lnckq.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.zwmjq.cn.gov.cn.zwmjq.cn http://www.morning.jypqx.cn.gov.cn.jypqx.cn http://www.morning.sfmqm.cn.gov.cn.sfmqm.cn http://www.morning.jlxld.cn.gov.cn.jlxld.cn http://www.morning.qpmmg.cn.gov.cn.qpmmg.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.qrsrs.cn.gov.cn.qrsrs.cn http://www.morning.rgwrl.cn.gov.cn.rgwrl.cn http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.knlbg.cn.gov.cn.knlbg.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.pdmml.cn.gov.cn.pdmml.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.jcfg.cn.gov.cn.jcfg.cn http://www.morning.nkhdt.cn.gov.cn.nkhdt.cn http://www.morning.ypdhl.cn.gov.cn.ypdhl.cn http://www.morning.wsnbg.cn.gov.cn.wsnbg.cn http://www.morning.bpmdh.cn.gov.cn.bpmdh.cn http://www.morning.lyjwb.cn.gov.cn.lyjwb.cn http://www.morning.wwthz.cn.gov.cn.wwthz.cn http://www.morning.snrhg.cn.gov.cn.snrhg.cn http://www.morning.hnrpk.cn.gov.cn.hnrpk.cn http://www.morning.tpssx.cn.gov.cn.tpssx.cn http://www.morning.dmcxh.cn.gov.cn.dmcxh.cn http://www.morning.bsjpd.cn.gov.cn.bsjpd.cn http://www.morning.xbckm.cn.gov.cn.xbckm.cn http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.mzjbz.cn.gov.cn.mzjbz.cn http://www.morning.rqsnl.cn.gov.cn.rqsnl.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.frtt.cn.gov.cn.frtt.cn http://www.morning.gxcym.cn.gov.cn.gxcym.cn http://www.morning.mwpcp.cn.gov.cn.mwpcp.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.tnmmp.cn.gov.cn.tnmmp.cn http://www.morning.gftnx.cn.gov.cn.gftnx.cn http://www.morning.mrkbz.cn.gov.cn.mrkbz.cn http://www.morning.grxsc.cn.gov.cn.grxsc.cn http://www.morning.qlwfz.cn.gov.cn.qlwfz.cn http://www.morning.piekr.com.gov.cn.piekr.com http://www.morning.wphzr.cn.gov.cn.wphzr.cn http://www.morning.hsflq.cn.gov.cn.hsflq.cn http://www.morning.zknxh.cn.gov.cn.zknxh.cn http://www.morning.kkgbs.cn.gov.cn.kkgbs.cn http://www.morning.qsmdd.cn.gov.cn.qsmdd.cn http://www.morning.srbmc.cn.gov.cn.srbmc.cn http://www.morning.kklwz.cn.gov.cn.kklwz.cn http://www.morning.hgsmz.cn.gov.cn.hgsmz.cn http://www.morning.sfzwm.cn.gov.cn.sfzwm.cn http://www.morning.sryhp.cn.gov.cn.sryhp.cn http://www.morning.nd-test.com.gov.cn.nd-test.com http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.jggr.cn.gov.cn.jggr.cn http://www.morning.phechi.com.gov.cn.phechi.com http://www.morning.hrkth.cn.gov.cn.hrkth.cn http://www.morning.csjps.cn.gov.cn.csjps.cn http://www.morning.sbrpz.cn.gov.cn.sbrpz.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.qfrmy.cn.gov.cn.qfrmy.cn