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;
-
-
-
public class LazyConfig {
-
/**
-
* @description:惰性队列
-
* @author: jie
-
* @time: 2022/3/13 11:06
-
*/
-
-
public Queue lazyQueue(){
-
-
return QueueBuilder.durable("lazy.queue")
-
.lazy()
-
.build();
-
}
-
/**
-
* @description:普通队列
-
* @author: jie
-
* @time: 2022/3/13 11:06
-
*/
-
-
public Queue normalQueue(){
-
return QueueBuilder.durable("normal.queue")
-
.build();
-
}
-
-
}
运行,查看浏览器。
1.2 基于@RabbitListener声明LazyQueue
-
/**
-
* @description:声明惰性队列
-
* @author: jie
-
* @time: 2022/3/13 14:37
-
*/
-
-
-
-
-
-
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;
-
-
-
-
-
public class SpringAmqpTest {
-
-
private RabbitTemplate rabbitTemplate;
-
-
-
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);
-
}
-
}
-
-
-
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
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgbhbef
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01