Py学习  »  MQ

如何限制在rabbitmq中使用的消息数?

itaied • 4 年前 • 729 次点击  

我正在和 rabbitmq 具有 amqplib 在我的 node 项目。

我使用队列在服务之间传递作业消息。
其中一个服务使用这些消息,进行一些处理并将结果返回到另一个队列。

有时排队的人 许多 消息,服务尝试同时使用所有消息,从而导致进程(服务)崩溃。

如何限制通道/服务可以处理的消息数?
我想到了一个定制的解决方案,有一个全球性的限制,但我宁愿用它作为最后的手段…

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38199
 
729 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Akashdeep Singh
Reply   •   1 楼
Akashdeep Singh    5 年前

您的问题还不完全清楚,但您似乎希望限制服务如何使用消息,而不是队列包含多少消息。 如果这种理解是正确的,那么你需要的是 prefetch_count . 更多文档: https://www.rabbitmq.com/consumer-prefetch.html

var amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost', function(err, conn) {
conn.createChannel(function(err, ch) {
    var q = 'task_queue';

    ch.assertQueue(q, {durable: true});
    ch.prefetch(1);                // THIS SHOULD SOLVE YOUR PROBLEM
    console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
    ch.consume(q, function(msg) {
      var secs = msg.content.toString().split('.').length - 1;

      console.log(" [x] Received %s", msg.content.toString());
      setTimeout(function() {
        console.log(" [x] Done");
        ch.ack(msg);
      }, secs * 1000);
    });
  });
});

代码源: https://www.rabbitmq.com/tutorials/tutorial-two-javascript.html