rabbitMQ 可靠性消息投递

参考(16条消息) RabbitMQ消息100%可靠性投递的解决方案实现(一)_eluanshi12的博客-CSDN博客_rabbitmq可靠性投递

可靠性消息:

在使用RabbitMQ时,作为消息发送方希望杜绝消息丢失或者投递失败的场景。RabbitMQ为我们提供了两种方式用来控制消息的投递可靠性。

  • confirm模式:
    • 生产者发送消息到交换机的时机上使用
  • return模式
    • 交换机转发给queue的时机上使用

confirm模式代码实现

1加入起步依赖

<!--rabbitmq起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2,在配置文件中开启confirm模式

spring:
  rabbitmq:
    port: 5672
    host: localhost
    username: guest
    password: guest
    virtual-host: /
    #开启confirms这个模式
    #springboot2.2.0.RELEASE支持这个
    #publisher-confirm-type: correlated
    publisher-confirms: true

3,创建一个实现abbitTemplate.ConfirmCallback接口的实现类

package com.example.confirm;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
public class MyConfirmCallBac
posted @ 2021-09-06 23:04  在线电影制作人  阅读(7)  评论(0)    收藏  举报  来源