栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

RabbitMQ消息堆积问题

RabbitMQ消息堆积问题

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

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

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

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

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

1、惰性队列

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

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

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

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

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

    支持数百万条的消息存储

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 {
   
    @Bean
    public Queue lazyQueue(){

        return QueueBuilder.durable("lazy.queue")
                .lazy()
                .build();
    }
    
    @Bean
    public Queue normalQueue(){
        return QueueBuilder.durable("normal.queue")
                .build();
    }

}

运行,查看浏览器。

1.2 基于@RabbitListener声明LazyQueue
 
    @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、总结

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

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

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

惰性队列的优点有哪些?

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

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

惰性队列的缺点有哪些?

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

    性能受限于磁盘的IO

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/761944.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号