[PHP] Rabbit MQ 扇形交换机

扇形交换机会把消息发送到交换机上的所有队列中。


fanout类型工作原理

生产者

use PhpAmqpLib\Exchange\AMQPExchangeType;

require_once __DIR__ . '/../../vendor/autoload.php';

$queue = 'fanout_queue';
$exchange = 'fanout_exchange';
$conn = new \PhpAmqpLib\Connection\AMQPStreamConnection('localhost', '5672', 'guest', 'guest');
$channel = $conn->channel();
$channel->exchange_declare($exchange,AMQPExchangeType::FANOUT,false,false,false);
$channel->queue_declare($queue, false, false, false, false);
$channel->queue_bind($queue, $exchange);
$channel->basic_publish(new \PhpAmqpLib\Message\AMQPMessage('fanout msg coming'),$exchange);
$channel->close();
$conn->close();

消费者

$queue='fanout_queue';
$exchange='fanout_exchange';
$conn=new \PhpAmqpLib\Connection\AMQPStreamConnection('localhost','5672','guest','guest');
$channel = $conn->channel();
$channel->exchange_declare($exchange,'fanout',false,false,false);
$channel->queue_declare($queue,false,false,false,false);
$channel->queue_bind($queue,$exchange);
$channel->basic_consume($queue,'',false,true,false,false,function ($msg){
    echo '[fanout_queue]'.$msg->body.PHP_EOL;
});
while ($channel->is_consuming()){
    $channel->wait();
}
$channel->close();
$conn->close();

你可能感兴趣的:([PHP] Rabbit MQ 扇形交换机)