保证消息不丢失,可靠抵达,可以使用事务消息,性能下降250倍,为此引入确认机制。

在springboot的application配置文件中添加开启配置
publisher-confirm-type: correlated
在配置类中添加初始化RabbitTemplate的方法
@Autowired
private RabbitTemplate rabbitTemplate;
@PostConstruct
public void initRabbitTemplate() {
/**
* 设置ConfirmCallBack回调 produce -> broker
* correlationData 当前消息的唯一关联数据(这个是消息的唯一id)
* ack 消息是否成功收到 只要消息抵达 ack就为true
* cause 失败的原因
*/
this.rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
log.info("ConfirmCallBack...correlationData[{}]==>ack[{}]==>cause[{}]",correlationData,ack,cause);
});
}

在springboot的application配置文件中添加开启配置
publisher-returns: true
# 优先回调returns
template:
mandatory: true
在配置类中添加初始化RabbitTemplate的方法
@Autowired
private RabbitTemplate rabbitTemplate;
@PostConstruct
public void initRabbitTemplate() {
this.rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
/**
* message 投递失败的消息详细信息
* replayCode 回复的状态码
* replayText 回复的文本内容
* exchange 当时这个消息发给哪个交换机
* routingKey 当时这个消息用哪个路由键
*/
@Override
public void returnedMessage(ReturnedMessage returned) {
log.info("ReturnsCallback...message[{}]==>replayCode[{}]==>replayText[{}]==>exchange[{}]==>routingKey[{}]",
returned.getMessage(),returned.getReplyCode(),returned.getReplyText(),returned.getExchange(),returned.getRoutingKey());
}
});
}

问题:
解决方法:配置手动ack确认
在springboot的application配置文件中添加开启配置
listener:
direct:
acknowledge-mode: manual