0
点赞
收藏
分享

微信扫一扫

RabbitMQ:惰性队列


4.惰性队列

4.1.消息堆积问题

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

RabbitMQ:惰性队列_System

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

  • 增加更多消费者,提高消费速度。也就是我们之前说的work queue模式
  • 扩大队列容积,提高堆积上限

要提升队列容积,把消息保存在内存中显然是不行的。

4.2 惰性队列

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

  • 接收到消息后直接存入磁盘而非内存
  • 消费者要消费消息时才会从磁盘中读取并加载到内存
  • 支持数百万条的消息存储

4.2.1.基于命令行设置lazy-queue

而要设置一个队列为惰性队列,只需要在声明队列时,指定x-queue-mode属性为lazy即可。可以通过命令行将一个运行中的队列修改为惰性队列:

rabbitmqctl set_policy Lazy "^lazy-queue$" '{"queue-mode":"lazy"}' --apply-to queues

命令解读:

  • ​rabbitmqctl​​ :RabbitMQ的命令行工具
  • ​set_policy​​ :添加一个策略
  • ​Lazy​​ :策略名称,可以自定义
  • ​"^lazy-queue$"​​ :用正则表达式匹配队列的名字
  • ​'{"queue-mode":"lazy"}'​​ :设置队列模式为lazy模式
  • ​--apply-to queues ​​:策略的作用对象,是所有的队列

4.2.2.基于@Bean声明lazy-queue

RabbitMQ:惰性队列_java-rabbitmq_02

4.2.3.基于@RabbitListener声明LazyQueue

RabbitMQ:惰性队列_rabbitmq_03

package cn.itcast.mq.config;

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

@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();
}
}

@Test
public void testLazyQueue() throws InterruptedException {
long b = System.nanoTime();
for (int i = 0; i < 1000000; i++) {
// 1.准备消息
Message message = MessageBuilder
.withBody("hello, Spring".getBytes(StandardCharsets.UTF_8))
.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT)
.build();
// 2.发送消息
rabbitTemplate.convertAndSend("lazy.queue", message);
}
long e = System.nanoTime();
System.out.println(e - b);
}
@Test
public void testNormalQueue() throws InterruptedException {
long b = System.nanoTime();
for (int i = 0; i < 1000000; i++) {
// 1.准备消息
Message message = MessageBuilder
.withBody("hello, Spring".getBytes(StandardCharsets.UTF_8))
.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT)
.build();
// 2.发送消息
rabbitTemplate.convertAndSend("normal.queue", message);
}
long e = System.nanoTime();
System.out.println(e - b);
}

4.3.总结

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

  • 队列上绑定多个消费者,提高消费速度
  • 使用惰性队列,可以再mq中保存更多消息

惰性队列的优点有哪些?

  • 基于磁盘存储,消息上限高
  • 没有间歇性的page-out,性能比较稳定

惰性队列的缺点有哪些?

  • 基于磁盘存储,消息时效性会降低
  • 性能受限于磁盘的IO

Message message = MessageBuilder
.withBody(“hello, Spring”.getBytes(StandardCharsets.UTF_8))
.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT)
.build();
// 2.发送消息
rabbitTemplate.convertAndSend(“lazy.queue”, message);
}
long e = System.nanoTime();
System.out.println(e - b);
}
@Test
public void testNormalQueue() throws InterruptedException {
long b = System.nanoTime();
for (int i = 0; i < 1000000; i++) {
// 1.准备消息
Message message = MessageBuilder
.withBody(“hello, Spring”.getBytes(StandardCharsets.UTF_8))
.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT)
.build();
// 2.发送消息
rabbitTemplate.convertAndSend(“normal.queue”, message);
}
long e = System.nanoTime();
System.out.println(e - b);
}##

 4.3.总结

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

- 队列上绑定多个消费者,提高消费速度
- 使用惰性队列,可以再mq中保存更多消息

惰性队列的优点有哪些?

- 基于磁盘存储,消息上限高
- 没有间歇性的page-out,性能比较稳定

惰性队列的缺点有哪些?

- 基于磁盘存储,消息时效性会降低
- 性能受限于磁盘的IO

举报

相关推荐

0 条评论