RabbitMQ 订阅限制预取消息的数量

RabbitMQ subscription limit the number of messages to pre fetch

我正在使用 rabbitmq 在 rails 上用 ruby 编写的 microservices 之间进行通信。每个服务订阅一个 topic。所有服务都是 scaled 和 运行 作为基于需要的多个实例。

在订阅期间 bunnyall the messages 从队列移至 unacked 状态。这使得其他缩放实例只是空闲,因为在 ready 状态下没有消息。

有没有办法限制订阅可以获取的消息数量,以便其他实例可以从队列中获取剩余的消息。

根据您提供的信息,我假设您正在使用 rubybunny。如果这个假设不正确(还有其他 ruby 客户端可用于 rabbitmq)让我知道 and/or 检查与您的客户端相关的文档。

回到rubybunny,link提供了必要信息的要点,引用它:

For cases when multiple consumers share a queue, it is useful to be able to specify how many messages each consumer can be sent at once before sending the next acknowledgement.

In AMQP 0.9.1 parlance this is known as QoS or message prefetching. Prefetching is configured on a per-channel basis. To configure prefetching use the Bunny::Channel#prefetch method like so:

ch1 = connection1.create_channel
ch1.prefetch(10)