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

相关推荐
初次攀爬者8 小时前
RabbitMQ的消息模式和高级特性
后端·消息队列·rabbitmq
初次攀爬者2 天前
ZooKeeper 实现分布式锁的两种方式
分布式·后端·zookeeper
西岸行者3 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
starlaky3 天前
Django入门笔记
笔记·django
勇气要爆发3 天前
吴恩达《LangChain LLM 应用开发精读笔记》1-Introduction_介绍
笔记·langchain·吴恩达
悠哉悠哉愿意3 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
勇气要爆发3 天前
吴恩达《LangChain LLM 应用开发精读笔记》2-Models, Prompts and Parsers 模型、提示和解析器
android·笔记·langchain
qianshanxue113 天前
计算机操作的一些笔记标题
笔记
土拨鼠烧电路3 天前
笔记11:数据中台:不是数据仓库,是业务能力复用的引擎
数据仓库·笔记
土拨鼠烧电路3 天前
笔记14:集成与架构:连接孤岛,构建敏捷响应能力
笔记·架构