rabbitmq 学习 之Consumer prefetch count(34)

本文详细介绍了RabbitMQ中的预取机制,解释了如何使用basic.qos方法限制未确认消息数量,以及RabbitMQ如何重新定义了全局标志的含义,使预取计数能独立应用于每个消费者。通过实例展示了不同场景下预取限制的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://www.rabbitmq.com/consumer-prefetch.html

Consumer prefetch is an extension to the consumer prefetch mechanism.

AMQP 0-9-1 specifies the basic.qos method to make it possible to limit the number of unacknowledged messages on a channel (or connection) when consuming (aka "prefetch count"). Unfortunately the channel is not the ideal scope for this - since a single channel may consume from multiple queues, the channel and the queue(s) need to coordinate with each other for every message sent to ensure they don't go over the limit. This is slow on a single machine, and very slow when consuming across a cluster.

AMQP 0-9-1指定basic.qos方法,限制了通道(或连接)上未确认消息的数量(也称为“预取计数”)。不幸的是,通道不是理想的范围 - 因为单个通道可能消耗多个队列,所以通道和队列需要为发送的每个消息相互协调,以确保它们不超过限制。这在单台机器上很慢,而在跨群集消耗时非常慢。

Furthermore for many uses it is simply more natural to specify a prefetch count that applies to each consumer.

Therefore RabbitMQ redefines the meaning of the global flag in the basic.qos method:

因此,RabbitMQ 在basic.qos方法中重新定义了 全局标志的含义:

`global`Meaning of `prefetch_count` in AMQP 0-9-1Meaning of `prefetch_count` in RabbitMQ
falseshared across all consumers on the channelapplied separately to each new consumer on the channel
trueshared across all consumers on the connectionshared across all consumers on the channel

Note that the default value for the global flag is false in most APIs.

`global`AMQP 0-9-1中`prefetch_count`的含义RabbitMQ中`prefetch_count`的含义
在渠道上的所有消费者之间共享这个count单独应用于channel的每个新消费者
在连接上所有消费者共享这个count在渠道上的所有消费者之间共享这个count

请注意,在大多数API中,global标志 的默认值为false。

 

Single Consumer

The following basic example in Java will receive a maximum of 10 unacknowledged messages at once:

Channel channel = ...;
Consumer consumer = ...;
channel.basicQos(10); // Per consumer limit
channel.basicConsume("my-queue", false, consumer);

Independent Consumers

This example starts two consumers on the same channel, each of which will independently receive a maximum of 10 unacknowledged messages at once:

Channel channel = ...;
Consumer consumer1 = ...;
Consumer consumer2 = ...;
channel.basicQos(10); // Per consumer limit
channel.basicConsume("my-queue1", false, consumer1);
channel.basicConsume("my-queue2", false, consumer2);

Multiple Consumers Sharing the Limit

The AMQP 0-9-1 specification does not explain what happens if you invoke basic.qos multiple times with different global values. RabbitMQ interprets this as meaning that the two prefetch limits should be enforced independently of each other; consumers will only receive new messages when neither limit on unacknowledged messages has been reached.

For example:

Channel channel = ...;
Consumer consumer1 = ...;
Consumer consumer2 = ...;
channel.basicQos(10, false); // Per consumer limit
channel.basicQos(15, true);  // Per channel limit
channel.basicConsume("my-queue1", false, consumer1);
channel.basicConsume("my-queue2", false, consumer2);

These two consumers will only ever have 15 unacknowledged messages between them, with a maximum of 10 messages for each consumer. This will be slower than the above examples, due to the additional overhead of coordinating between the channel and the queues to enforce the global limit.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值