简体   繁体   English

如何使用 RabbitMQ 平衡发布者的请求?

[英]How to balance publishers' requests with RabbitMQ?

Suppose you have multiple producers and one consumer which wants to receive persistent messages from all publishers available.假设您有多个生产者和一个想要从所有可用发布者接收持久消息的消费者。

Producers work with different speed.生产者以不同的速度工作。 Let's say that system A produces 10 requests/sec and system B 1 request/sec.假设系统 A 产生 10 个请求/秒,系统 B 产生 1 个请求/秒。 So if you use the only queue you will process 10 messages from A then 1 message from B.因此,如果您使用唯一的队列,您将处理来自 A 的 10 条消息,然后处理来自 B 的 1 条消息。

But what if you want to balance load and process one message from A then one message from B etc.?但是,如果您想平衡负载并处理来自 A 的一条消息然后来自 B 的一条消息,该怎么办? Consuming from multiple queues is not a good option because we can't use wildcard binding in this case.从多个队列中使用不是一个好的选择,因为在这种情况下我们不能使用通配符绑定。

Update:更新:
Queue per producer seems as the best approach.每个生产者的队列似乎是最好的方法。 Producers don't know their speed which changes constantly.生产者不知道他们不断变化的速度。 Having one queue per consumer I can subscribe to one topic and receive messages from all publishers available.每个消费者有一个队列,我可以订阅一个主题并接收来自所有可用发布者的消息。 But having a queue per producer I need to code the logic by myself:但是每个生产者都有一个队列,我需要自己编写逻辑:

  1. Get all available queues through management plugin (AMQP doesn't allow to list queues).通过管理插件获取所有可用队列(AMQP 不允许列出队列)。
  2. Filter by queue name.按队列名称过滤。
  3. Implement round robin strategy.实施循环策略。
  4. Implement notification mechanism to subscribe to new publishers that can appear at any moment.实现通知机制订阅随时可能出现的新发布者。
  5. Remove unnecessary queue when publisher had disappeared and client read all messages.当发布者消失并且客户端读取所有消息时删除不必要的队列。

Well, it seems pretty easy but I thought that broker could provide all of this functionality without any coding.嗯,这看起来很简单,但我认为经纪人可以提供所有这些功能而无需任何编码。 In case with one queue I just create one persistent queue, bind it to a topic exchange then start any number of publishers that send messages to the topic.如果只有一个队列,我只需创建一个持久队列,将其绑定到主题交换,然后启动任意数量的发布者向该主题发送消息。 This option works almost out of the box.此选项几乎开箱即用。

I know I'm late for the party, but still.我知道我参加聚会迟到了,但仍然如此。

In Azure Service Bus terms it's called " partitioning " and it's based on the partition key.在 Azure 服务总线术语中,它称为“ 分区”,它基于分区键。 The best part is in Azure SB the receiving client is not aware of the partitioning, it simply subscribes to the single queue.最好的部分是在 Azure SB 中,接收客户端不知道分区,它只是订阅单个队列。

In RabbitMQ there is a X-Consistent-Hashing plugin (" rabbitmq_consistent_hash_exchange ") but unfortunately it's not that convenient.在 RabbitMQ 中有一个 X-Consistent-Hashing 插件(“ rabbitmq_consistent_hash_exchange ”)但不幸的是它不是那么方便。 The consumers must be explicitly configured to consume from specific queues.消费者必须明确配置为从特定队列消费。 If you have ten queues then you need to setup your consumers so that all ten are covered.如果您有 10 个队列,那么您需要设置您的使用者,以便涵盖所有 10 个队列。

Another two options:另外两个选项:

  1. Random Exchange Type 随机交换类型
  2. Sharding Plugin 分片插件

Bear in mind that with the Sharding Plugin even though it creates "one logical queue to consume" you'll have to have as many subscribers as there are virtual queues, otherwise some of the queues will be left unconsumed.请记住,使用 Sharding Plugin,即使它创建了“一个要使用的逻辑队列”,您也必须拥有与虚拟队列一样多的订阅者,否则某些队列将未被使用。

You can use the Priority Queue Support and associate a priority according to the producer speed.您可以使用优先队列支持并根据生产者速度关联优先级。 With the caveat that the priority must be set with caution (for example, if the consumer speed is below the system B, the consumer will only consume messages from B) and producers must be aware of their producing speed.需要注意的是必须谨慎设置优先级(例如,如果消费者速度低于系统 B,则消费者只会消费来自 B 的消息)并且生产者必须了解他们的生产速度。

Another option to consider is creating 3 types of queues according to the producing speed: HIGH , MEDIUM , LOW .另一个要考虑的选择是根据生产速度创建 3 种类型的队列: HIGHMEDIUMLOW The three queues are binded to the exchange with the binding key set according to the producing speed.三个队列根据生产速度设置绑定密钥绑定到交换机。 It could be done using.它可以完成使用。

Consumer will consume messages from these 3 queues using a round robin strategy.消费者将使用循环策略从这 3 个队列中消费消息。 With the caveat that producers must be aware of their producing speed.需要注意的是,生产者必须注意他们的生产速度。

But the best option may be a queue per producer especially if producers speed is not stable and cannot be categorized.但是最好的选择可能是每个生产者的队列,尤其是在生产者速度不稳定且无法分类的情况下。 Thus, producers do not need to know their producing speed.因此,生产者不需要知道他们的生产速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM