问题 RabbitMQ等待多个队列完成


好的,这是对正在发生的事情的概述:

    M <-- Message with unique id of 1234
    |
    +-Start Queue
    |
    |
    | <-- Exchange
   /|\
  / | \
 /  |  \ <-- bind to multiple queues
Q1  Q2  Q3
\   |   / <-- start of the problem is here
 \  |  / 
  \ | /
   \|/
    |
    Q4 <-- Queues 1,2 and 3 must finish first before Queue 4 can start
    |
    C <-- Consumer 

所以我有一个推送到多个队列的交换,每个队列都有一个任务,一旦所有任务完成,只有队列4才能启动。

因此,具有唯一ID 1234的消息被发送到交换机,交换机将其路由到所有任务队列(Q1,Q2,Q3等...),当消息id 1234的所有任务都已完成时,运行Q4以获取消息id 1234。

我该如何实现呢?

使用Symfony2,RabbitMQBundle和RabbitMQ 3.x

资源:

更新#1

好的我觉得这就是我要找的东西:

RPC with Parallel Processing,但是如何将Correlation Id设置为我的唯一ID来对消息进行分组并识别哪个队列?


2653
2017-12-13 14:03


起源

所以,如果我理解正确,你有一个第四个队列只能在其他3个队列为空时开始处理?如果你并行处理很多事情,你的3个队列不会总是传递信息吗? - afuzzyllama
是的,因为队列将始终有新消息,忘了提到每个队列中的所有数据都由唯一的ID相关联。所以交换机将唯一ID 1234发送到Q1,Q2和Q3。每个队列执行不同的任务。在Q4中,我需要知道在Q4中处理消息之前,Q1,Q2和Q3中具有唯一ID 1234的消息何时完成。更新了我的问题 - Phill Pafford


答案:


在里面 RPC 在RabbitMQ网站上的教程中,有一种方法可以传递一个“关联ID”,可以识别队列中用户的消息。

我建议在前三个队列中使用某种带有消息的id,然后再使用另一个进程将消息从3中排队到某种类型的桶中。当那些桶收到我假设完成的3个任务时,将最后的消息发送到第4个队列进行处理。

如果要为一个用户向每个队列发送多个工作项,则可能需要进行一些预处理以找出特定用户放入队列的项目数,以便在4之前出列的进程知道在排队之前预期的数量向上。


我在C#中做了我的rabbitmq,很抱歉我的伪代码不是php风格

// Client
byte[] body = new byte[size];
body[0] = uniqueUserId;
body[1] = howManyWorkItems;
body[2] = command;

// Setup your body here

Queue(body)

// Server
// Process queue 1, 2, 3
Dequeue(message)

switch(message.body[2])
{
    // process however you see fit
}

processedMessages[message.body[0]]++;

if(processedMessages[message.body[0]] == message.body[1])
{
    // Send to queue 4
    Queue(newMessage)
}

对更新#1的响应

您可以将客户端视为服务器上的进程,而不是将客户端视为终端。因此,如果您在服务器上设置RPC客户端 这个,那么您需要做的就是让服务器处理用户唯一ID的生成并将消息发送到适当的队列:

    public function call($uniqueUserId, $workItem) {
        $this->response = null;
        $this->corr_id = uniqid();

        $msg = new AMQPMessage(
            serialize(array($uniqueUserId, $workItem)),
            array('correlation_id' => $this->corr_id,
            'reply_to' => $this->callback_queue)
        );

        $this->channel->basic_publish($msg, '', 'rpc_queue');
        while(!$this->response) {
            $this->channel->wait();
        }

        // We assume that in the response we will get our id back
        return deserialize($this->response);
    }


$rpc = new Rpc();

// Get unique user information and work items here

// Pass even more information in here, like what queue to use or you could even loop over this to send all the work items to the queues they need.
$response = rpc->call($uniqueUserId, $workItem);

$responseBuckets[array[0]]++;

// Just like above code that sees if a bucket is full or not

4
2017-12-13 14:36



你能解释一下吗?我理解RPC部分,但你是说将RPC添加到Q1,Q2和Q3? - Phill Pafford
而不是使用RPC的id,而是使用它来为位于第4个队列前面的进程分组消息。您可能甚至不需要使用该ID。您可以将用户ID嵌入到邮件正文中。 - afuzzyllama
是的,每个队列的每个唯一ID是一个任务 - Phill Pafford
你是什​​么意思'群发消息'?我想我已经掌握了这个概念但需要更多细节 - Phill Pafford
我试着在我的问题中写一个简单的例子。当然还有很多东西需要填写,但我认为它显示了我会尝试的路线? - afuzzyllama


你需要实现这个: http://www.eaipatterns.com/Aggregator.html 但是针对Symfony的RabbitMQBundle不支持,所以你必须使用底层的php-amqplib。

来自捆绑包的普通消费者回调将获得AMQPMessage。从那里,您可以访问该频道并手动发布到“管道和过滤器”实现中接下来的任何交换


6
2017-12-13 21:50



有没有更新RabbitMQBundle的努力? - Phill Pafford


我对你在这里想要达到的目标有点不清楚。但我可能会稍微改变一下设计,以便一旦从队列中清除所有消息,就会发布到发布到队列4的单独交换。


2
2017-12-13 14:30



当然,你能指点我一些文件吗?如何从所有队列中清除所有消息(对于给定的唯一ID)? - Phill Pafford
你的独特身份是什么意思?话题?您可以使用通道接口检查队列是否为空 - robthewolf
我已经更新了我的问题,我想我正在寻找带并行处理的RPC,+1为你的努力。你有机会再看一遍我的问题吗? - Phill Pafford
@PhillPafford我没有看到你的努力+1;) - Vitalii Zurian
@thecatontheflat谢谢! - Phill Pafford


除了我的 基于RPC的答案 我想添加另一个基于的 EIP聚合器模式

接下来的想法是:一切都是异步的,没有RPC或其他同步的东西。每个任务在完成时发送一个偶数,聚合器订阅该事件。它基本上计算任务并在计数器达到预期数量时发送task4消息(在我们的例子中为3)。我选择一个文件系统作为计数器的存储空间来简化。你可以在那里使用数据库。

制作人看起来更简单。它只是火上浇油和忘记

<?php
use Enqueue\Client\Message;
use Enqueue\Client\ProducerInterface;
use Enqueue\Util\UUID;
use Symfony\Component\DependencyInjection\ContainerInterface;

/** @var ContainerInterface $container */

/** @var ProducerInterface $producer */
$producer = $container->get('enqueue.client.producer');

$message = new Message('the task data');
$message->setCorrelationId(UUID::generate());

$producer->sendCommand('task1', clone $message);
$producer->sendCommand('task2', clone $message);
$producer->sendCommand('task3', clone $message);

任务处理器必须在其工作完成后发送事件:

<?php
use Enqueue\Client\CommandSubscriberInterface;
use Enqueue\Client\Message;
use Enqueue\Client\ProducerInterface;
use Enqueue\Psr\PsrContext;
use Enqueue\Psr\PsrMessage;
use Enqueue\Psr\PsrProcessor;

class Task1Processor implements PsrProcessor, CommandSubscriberInterface
{
    private $producer;

    public function __construct(ProducerInterface $producer)
    {
        $this->producer = $producer;
    }

    public function process(PsrMessage $message, PsrContext $context)
    {
        // do the job

        // same for other
        $eventMessage = new Message('the event data');
        $eventMessage->setCorrelationId($message->getCorrelationId());

        $this->producer->sendEvent('task_is_done', $eventMessage);

        return self::ACK;
    }

    public static function getSubscribedCommand()
    {
        return 'task1';
    }
}

和聚合器处理器:

<?php

use Enqueue\Client\TopicSubscriberInterface;
use Enqueue\Psr\PsrContext;
use Enqueue\Psr\PsrMessage;
use Enqueue\Psr\PsrProcessor;
use Symfony\Component\Filesystem\LockHandler;

class AggregatorProcessor implements PsrProcessor, TopicSubscriberInterface
{
    private $producer;
    private $rootDir;

    /**
     * @param ProducerInterface $producer
     * @param string $rootDir
     */
    public function __construct(ProducerInterface $producer, $rootDir)
    {
        $this->producer = $producer;
        $this->rootDir = $rootDir;
    }

    public function process(PsrMessage $message, PsrContext $context)
    {
        $expectedNumberOfTasks = 3;

        if (false == $cId = $message->getCorrelationId()) {
            return self::REJECT;
        }

        try {
            $lockHandler = new LockHandler($cId, $this->rootDir.'/var/tasks');
            $lockHandler->lock(true);

            $currentNumberOfProcessedTasks = 0;
            if (file_exists($this->rootDir.'/var/tasks/'.$cId)) {
                $currentNumberOfProcessedTasks = file_get_contents($this->rootDir.'/var/tasks/'.$cId);

                if ($currentNumberOfProcessedTasks +1 == $expectedNumberOfTasks) {
                    unlink($this->rootDir.'/var/tasks/'.$cId);

                    $this->producer->sendCommand('task4', 'the task data');

                    return self::ACK;
                }
            }

            file_put_contents($this->rootDir.'/var/tasks/'.$cId, ++$currentNumberOfProcessedTasks);

            return self::ACK;
        } finally {
            $lockHandler->release();
        }
    }

    public static function getSubscribedTopics()
    {
        return 'task_is_done';
    }
}

1
2018-06-22 12:26





我可以告诉你如何做到这一点 排队束

所以用composer安装它并注册为任何其他bundle。然后配置:

// app/config/config.yml

enqueue:
  transport:
    default: 'amnqp://'
  client: ~

这种方法基于RPC。这是你如何做到的:

<?php
use Enqueue\Client\ProducerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/** @var ContainerInterface $container */

/** @var ProducerInterface $producer */
$producer = $container->get('enqueue.client.producer');

$promises = new SplObjectStorage();

$promises->attach($producer->sendCommand('task1', 'the task data', true));
$promises->attach($producer->sendCommand('task2', 'the task data', true));
$promises->attach($producer->sendCommand('task3', 'the task data', true));

while (count($promises)) {
    foreach ($promises as $promise) {
        if ($replyMessage = $promise->receiveNoWait()) {
            // you may want to check the response here
            $promises->detach($promise);
        }
    }
}

$producer->sendCommand('task4', 'the task data');

消费者处理器如下所示:

use Enqueue\Client\CommandSubscriberInterface;
use Enqueue\Consumption\Result;
use Enqueue\Psr\PsrContext;
use Enqueue\Psr\PsrMessage;
use Enqueue\Psr\PsrProcessor;

class Task1Processor implements PsrProcessor, CommandSubscriberInterface
{
    public function process(PsrMessage $message, PsrContext $context)
    {
        // do task job

        return Result::reply($context->createMessage('the reply data'));
    }

    public static function getSubscribedCommand()
    {
        // you can simply return 'task1'; if you do not need a custom queue, and you are fine to use what enqueue chooses. 

        return [
          'processorName' => 'task1',
          'queueName' => 'Q1',
          'queueNameHardcoded' => true,
          'exclusive' => true,
        ];
    }
}

将其作为带有标记的服务添加到容器中 enqueue.client.processor 并运行命令 bin/console enqueue:consume --setup-broker -vvv

这是普通的PHP版本


0
2018-06-22 11:51