在 RabbitMQ 中设置消息优先级 PHP

Setting message priority in RabbitMQ PHP

我发现了很多在 RabbitMQ 中为 Java、Spring 等设置消息优先级的示例,但到目前为止我还没有找到如何在 [=20= 中实现它].

事实上,$channel->basic_publish() 函数似乎不支持提供附加参数 (https://github.com/videlalvaro/php-amqplib/blob/master/PhpAmqpLib/Channel/AMQPChannel.php),即使您可以在 RabbitMQ gui 中执行此操作。

在 PHP 中是否有人使用 RabbitMQ 获得消息优先级?

好吧,它一直盯着我看。您在实际 message 对象中设置优先级,而不是在将其推入队列时:

$msg = new AMQPMessage("Hello World!", array(
    'delivery_mode' => 2,
    'priority' => 1,
    'timestamp' => time(),
    'expiration' => strval(1000 * (strtotime('+1 day midnight') - time() - 1))
));

这是 AMQP Interop 的示例。请注意,在声明 queue 时,您不仅应该设置优先级 header,还应该设置一个特殊参数。

安装 AMQP Interop 兼容传输,例如

composer require enqueue/amqp-bunny

然后做下一步:

<?php
use Enqueue\AmqpBunny\AmqpConnectionFactory;
use Interop\Amqp\AmqpQueue;

$context = (new AmqpConnectionFactory())->createContext(); // connects to localhost with defaults

$queue = $context->createQueue("transcode2");
$queue->addFlag(AmqpQueue::FLAG_PASSIVE);
$queue->setArgument('x-max-priority', 10);
$context->declareQueue($queue);

$message = $context->createMessage(json_encode($msg));
$message->setPriority(5);

$producer = $context->createProducer($queue, $message);

$producer->send($queue, $message);