RabbitMQ(高级)笔记

发布于:2024-04-30 ⋅ 阅读:(26) ⋅ 点赞:(0)

一、生产者可靠性

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

 


网站公告

今日签到

点亮在社区的每一天
去签到