rabbitmq direct绑定多个路由键 php-amqplib

生产者

require_once './vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;


$connection = new AMQPStreamConnection('127.0.0.1', 5672, 'mq', 'mq123', '/');

$channel = $connection->channel();

$channel->exchange_declare('test_exchange_direct', 'direct', false, false, false);

//推送成功
$channel->set_ack_handler(
    function (AMQPMessage $message) {
        echo "成功: " . $message->body . PHP_EOL;
    }
);

//推送失败
$channel->set_nack_handler(
    function (AMQPMessage $message) {
        echo "失败: " . $message->body . PHP_EOL;
    }
);
$channel->confirm_select();

$channel->basic_publish(new AMQPMessage('info日志'), 'test_exchange_direct', 'info');
$channel->basic_publish(new AMQPMessage('error日志'), 'test_exchange_direct', 'error');

$channel->wait_for_pending_acks();

$channel->close();
$connection->close();

消费者

require_once './vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;

$connection = new AMQPStreamConnection('127.0.0.1', 5672, 'mq', 'mq123', '/');

$channel = $connection->channel();

$channel->exchange_declare('test_exchange_direct', 'direct', false, false, false);

list($queue_name, ,) = $channel->queue_declare("", false, false, true, false);

$severities = array_slice($argv, 1);


foreach($severities as $severity) {
    $channel->queue_bind($queue_name, 'test_exchange_direct', $severity);
}

echo ' [*] Waiting for logs. To exit press CTRL+C', "\n";

$callback = function($msg){
  echo ' [x] ',$msg->delivery_info['routing_key'], ':', $msg->body, "\n";
};

$channel->basic_consume($queue_name, '', false, true, false, false, $callback);

while(count($channel->callbacks)) {
    $channel->wait();
}

$channel->close();
$connection->close();

结果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值