RabbitMQ 轮询

huangapple go评论83阅读模式
英文:

RabbitMQ Round Robin

问题

我正在尝试构建一个系统,其中一个主服务创建一系列消息,并根据路由键将它们路由到正确的队列。我已经实现了这个功能,并且工作/从属方面正在消费这些消息。然而,我希望工作方只从队列中取出一条消息,而不是所有放入其中的15条消息。

我看了一下这个链接:https://github.com/streadway/amqp/blob/master/channel.go#L616,但我不确定如何设置只取一条事件。

有什么想法吗?

英文:

I am trying to build a system where a master service creates a bunch of messages and routes them to the correct queue based on the routing key. I have that working and the messages are being consumed on the worker/slave side. However I want the worker to only take one message from the queue and not all 15 that get put into it.

I was looking at this https://github.com/streadway/amqp/blob/master/channel.go#L616 however I am not sure how to set it up so only one event is taken.

Any ideas?

答案1

得分: 1

免责声明:我没有使用过Go AMQP客户端,但AMQP协议的语义应该在不同的客户端实现中是通用的,所以我会尝试回答。

你已经正确地确定了prefetch-sizeprefetch-count参数作为要调整的配置变量。根据RabbitMQ文档在这方面的说明如下:1

AMQP指定了basic.qos方法,允许你在消费时(也称为“预取计数”)限制通道(或连接)上未确认消息的数量。

如果你希望每个消费者一次只接收一条消息,你应该将prefetch-count设置为1,将prefetch-size未定义(即0):

err := channel.Qos(1, 0, false)
if err != nil {
    // ...
}

1
RabbitMQ的AMQP参考文档中也有关于这个问题的更详细描述:

long prefetch-size

客户端可以请求提前发送消息,这样当客户端完成处理一条消息时,后续的消息已经在本地保留,而不需要发送到通道中。[...] 如果消息的大小等于或小于可用的预取大小(并且还符合其他预取限制),服务器将提前发送消息。可以将其设置为零,表示“没有特定限制”,尽管其他预取限制仍然适用。 如果设置了no-ack选项,则忽略预取大小。

[...]

short prefetch-count

以整个消息为单位指定预取窗口。该字段可以与预取大小字段结合使用;只有在预取窗口(以及通道和连接级别的窗口)都允许的情况下,才会提前发送消息。[...]

英文:

Disclaimer: I haven't worked with the Go AMQP client, but the AMQP protocol semantics should be universal across client implementations, so I'll take a shot.

<hr>

You've already correctly identified the prefetch-size and prefetch-count parameters as the configuration variables to adjust. The RabbitMQ documentation says the following in this regard:<sup>1</sup>

>AMQP specifies the basic.qos method to allow you to limit the number of unacknowledged messages on a channel (or connection) when consuming (aka "prefetch count").

If you want each consumer to be sent only one message at a time, you should set prefetch-count to 1, and leave prefetch-size undefined (i.e. 0):

err := channel.Qos(1, 0, false)
if err != nil {
    // ...
}

<hr>

<sup>1</sup>
There's also a longer description in RabbitMQ's AMQP reference on this one:

>long prefetch-size
>
>The client can request that messages be sent in advance so that when the client finishes processing a message, the following message is already held locally, rather than needing to be sent down the channel. [...] The server will send a message in advance if it is equal to or smaller in size than the available prefetch size (and also falls into other prefetch limits). May be set to zero, meaning "no specific limit", although other prefetch limits may still apply. The prefetch-size is ignored if the no-ack option is set.
>
> [...]
>
>short prefetch-count
>
Specifies a prefetch window in terms of whole messages. This field may be used in combination with the prefetch-size field; a message will only be sent in advance if both prefetch windows (and those at the channel and connection level) allow it. [...]

huangapple
  • 本文由 发表于 2016年1月19日 05:04:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/34864046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定