RabbitMQ(高级)笔记

一、生产者可靠性

(1)生产者重连(不建议使用)

复制代码
logging:
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS

spring:
  rabbitmq:
    virtual-host: /hamll
    port: 5672
    host: 192.168.92.136
    username: hmall
    password: 123
    listener:
      simple:
        prefetch: 1
    connection-timeout: 1s
    template:
      retry:
        enabled: true
        initial-interval: 1000ms
        multiplier: 1
        max-attempts: 3

(2)生产者确认

引入日志依赖

复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>

二、MQ持久化

(1)持久化介绍

发送100万条消息给队列,需要先在application.yml中取消生产者确认

复制代码
    publisher-confirm-type: none
    publisher-returns: false

没有持久化的测试用例,会出行阻塞的状态:

改为持久化后的测试用例:

(2)最好的持久化方式Lazy Queue

三、消息可靠性

(1)消费者确认

使用auto自动模式

(2)失败重试机制

(3)业务幂等性

四、延迟消息

(1)死信交换机

注意:创建的simple.queue需要Add Dead letter exchange,其他都跟之前创建的操作差不多

监听

复制代码
    @RabbitListener(queues = "dlx.queue")
    public void listenDlxQueue(String msg){
        log.info("dlx.queue消费者收到消息:"+msg);
    }

测试用例

复制代码
    @Test
    void testSendTTLMessage(){

        rabbitTemplate.convertAndSend("simple.direct", "hi", "hello", new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("10000");
                return message;
            }
        });
        log.info("消息发送成功!");
    }

(2)延迟消息插件

(3)取消订单

相关推荐
CodeWithMe20 分钟前
【Note】《Kafka: The Definitive Guide》 第5章:深入 Kafka 内部结构,理解分布式日志系统的核心奥秘
分布式·kafka
UQI-LIUWJ24 分钟前
李宏毅LLM笔记: AI Agent
人工智能·笔记
CodeWithMe36 分钟前
【Note】《Kafka: The Definitive Guide》第一章:Meet Kafka
分布式·kafka
CodeWithMe38 分钟前
【Note】《Kafka: The Definitive Guide》 第二章 Installing Kafka:Kafka 安装与运行
分布式·kafka
ouliten40 分钟前
cuda编程笔记(6)--流
笔记
Love__Tay1 小时前
笔记/云计算基础
笔记·学习·云计算
李元豪1 小时前
【行云流水ai笔记】粗粒度控制:推荐CTRL、GeDi 细粒度/多属性控制:推荐TOLE、GPT-4RL
人工智能·笔记
特种加菲猫3 小时前
指尖上的魔法:优雅高效的Linux命令手册
linux·笔记
wuxuanok4 小时前
Web后端开发-分层解耦
java·笔记·后端·学习
wuxuanok4 小时前
Web后端开发-请求响应
java·开发语言·笔记·学习