RabbitMQ消息堆积问题

当生产者发送消息的速度超过了消费者处理消息的速度,就会导致队列中的消息堆积,直到队列存储消息达到上限。最早接收到的消息,可能就会成为死信,会被丢弃,这就是消息堆积问题。

解决消息堆积有三种思路:

  1. 增加更多消费者,提高消费速度

  2. 在消费者内开启线程池加快消息处理速度

  3. 扩大队列容积,提高堆积上限

1、惰性队列

上面呢,我们已经 知道解决消息队列的常见三种解决方案,其中一种方案就是想办法去提高一个队列它能存储一个消息量的上限。

但是RabbitMQ呢是内存存储的,如果说在高并发的情况下消息量非常的大,这些消息我们如果都给它丢到内存当中,显然是不合适的,所以我们就要学习一个惰性队列来解决这个问题!

RabbitMQ3.6.0版本开始,就增加了Lazy Queues的概念,也就是惰性队列。惰性队列的特征如下:

  1. 接收到消息后直接存入磁盘而非内存

  2. 消费者要消费消息时才会从磁盘中读取并加载到内存

  3. 支持数百万条的消息存储

1.1 基于@Bean声明lazy-queue

package com.jie.mq.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LazyConfig {
   /**
    * @description:惰性队列
    * @author: jie
    * @time: 2022/3/13 11:06
    */
    @Bean
    public Queue lazyQueue(){

        return QueueBuilder.durable("lazy.queue")
                .lazy()
                .build();
    }
    /**
     * @description:普通队列
     * @author: jie
     * @time: 2022/3/13 11:06
     */
    @Bean
    public Queue normalQueue(){
        return QueueBuilder.durable("normal.queue")
                .build();
    }

}

运行,查看浏览器。

1.2 基于@RabbitListener声明LazyQueue

 /**
     * @description:声明惰性队列
     * @author: jie
     * @time: 2022/3/13 14:37
     */
    @RabbitListener(queuesToDeclare = @Queue(
            name = "lazy.queue",
            durable = "true",
            arguments = @Argument(name = "x-queue-mode",value = "lazy")
    ))
    public void listenLazyQueue(String msg){
        System.out.println("消费者接收到simple.queue的消息:【" + msg + "】");
    }

1.3 发送消息

package com.jie.mq.spring;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.nio.charset.StandardCharsets;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAmqpTest {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testLazyQueue() throws InterruptedException {
        for (int i = 0; i < 1000000; i++) {
            String routingKey = "delay";
            //1、准备消息
            Message message = MessageBuilder
                    .withBody("hell,Spring".getBytes(StandardCharsets.UTF_8))
                    .setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT)
                    .build();
            //2、发送消息
            rabbitTemplate.convertAndSend("lazy.queue", message);
        }
    }

    @Test
    public void testNormaQueue() throws InterruptedException {
        for (int i = 0; i < 1000000; i++) {
            String routingKey = "delay";
            //1、准备消息
            Message message = MessageBuilder
                    .withBody("hell,Spring".getBytes(StandardCharsets.UTF_8))
                    .setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT)
                    .build();
            //2、发送消息
            rabbitTemplate.convertAndSend("normal.queue", message);
        }
    }

}

2、总结

消息堆积问题的解决方案?

  1. 队列上绑定多个消费者,提高消费速度

  2. 使用惰性队列,可以再mq中保存更多消息

惰性队列的优点有哪些?

  1. 基于磁盘存储,消息上限高

  2. 没有间歇性的page-out,性能比较稳定

惰性队列的缺点有哪些?

  1. 基于磁盘存储,消息时效性会降低

  2. 性能受限于磁盘的IO

  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叫我阿杰好了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值