RabbitMQ 轮换单一活跃消费者

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

RabbitMQ rotate Single Active Consumer

问题

以下是翻译好的部分:

RabbitMQ文档对此问题没有明确定义(因为这可能是不可能的),因此我在这里提出我的问题:

我有大量队列,所有队列都启用了“Single Active Consumer”。现在,我有多个运行中的消费者应用程序实例。我原本希望“Single Active Consumer”会在我的多个消费者应用程序实例之间轮换,但不幸的是,第一个启动的实例成为了所有队列的活动消费者。其他实例只是空闲的。

是否有可能以某种方式将启用SAC的所有队列的负载分布到所有实例之间?

英文:

The RabbitMQ documentation is not clear about this (because it's probably not possible), hence my question here:

I have a large number of queues, all with Single Active Consumer enabled. Now, I have multiple instances of my consumer application running. I hoped that the Single Active Consumers would be rotated over my consumer applications, but unfortunately the first instance to go up is the active consumer for ALL my queues. The other instances are simply idle.

Is it somehow possible to distribute the load of all my queues with SAC enabled over all my instances?

答案1

得分: 0

抱歉,RabbitMQ似乎没有内置的功能来实现这一点。我通过向每个应用程序实例传递两个环境变量来解决了这个问题。假设我的应用程序有4个并行运行的实例,每个实例都会获得环境变量INSTANCE_CNT = 4和INSTANCE_IDX = 0到INSTANCE_IDX = 3。

在初始化队列时,我确保队列是有序的。如果这些队列不存在,每个实例都会初始化所有队列,但只会将监听器绑定到满足queue_idx % INSTANCE_CNT == INSTANCE_IDX条件的队列。在这里,queue_idx是我的所有队列列表中队列的索引。这样,实例0将监听队列0、4、8、12、16等,实例1将监听队列1、5、9、13、15等,依此类推。

这不是最漂亮的解决方案,但它有效。

英文:

Unfortunately RabbitMQ doesn't seem to have something built-in to achieve this. I've solved it by passing two environment variables to each instance of my application. Say my application has 4 instances running in parallel, each instance gets the environment variables INSTANCE_CNT = 4 and INSTANCE_IDX = 0 to INSTANCE_IDX = 3.

When initialising the queues I make sure that the queues are ordered. Every instance initialises all queues if these don't exist, but only bind listeners to every queue where queue_idx % INSTANCE_CNT == INSTANCE_IDX. Here, queue_idx is the idx of the queue in my list of all queues. This way, instance 0 will listen to queues 0, 4, 8, 12, 16, ..., instance 1 to 1, 5, 9, 13, 15, ... etc.

Not the prettiest solution, but it works.

huangapple
  • 本文由 发表于 2023年5月26日 00:47:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334576.html
匿名

发表评论

匿名网友

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

确定