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)取消订单

相关推荐
逑之1 天前
C语言笔记10:sizeof和strlen,指针与数组
c语言·笔记·算法
saoys1 天前
Opencv 学习笔记:创建与原图等尺寸的空白图像
笔记·opencv·学习
晓幂1 天前
【2025】HECTF
笔记·学习·web安全
做cv的小昊1 天前
【TJU】信息检索与分析课程笔记和练习(8)(9)发现系统和全文获取、专利与知识产权基本知识
大数据·笔记·学习·全文检索·信息检索
hkNaruto1 天前
【AI】AI学习笔记:MCP协议与gRPC、OpenAPI的差异
人工智能·笔记·学习
九成宫1 天前
计算机网络期末复习——第2章:应用层 Part One
笔记·计算机网络·软件工程
半夏知半秋1 天前
rust学习-闭包
开发语言·笔记·后端·学习·rust
你喜欢喝可乐吗?1 天前
FastAPI 入门笔记
笔记·fastapi
hkNaruto1 天前
【AI】AI学习笔记:A2A(智能体协作)入门指南:从概念到实践
人工智能·笔记·学习
yj_sharing1 天前
动手学深度学习softmax回归的笔记
笔记·深度学习·回归