php rabbitmq 扇形交换机

扇形交换机

扇形交换机会把能接收到的消息全部发送给绑定在自己身上的队列。因为广播不需要“思考”,所以扇形交换机处理消息的速度也是所有的交换机类型里面最快的。

1.生产者

channel();
$channel->exchange_declare($exchange,'fanout',false,false,false);
for($i=0;$i<100;$i++) {
    $data= array('name'=>'zyw'.$i,'time'=>time(),'i'=>$i);
    $msg = new AMQPMessage(json_encode($data));
    $channel->basic_publish($msg,$exchange);
    echo ' [x] Sent ', json_encode($data), "\n";
}
$channel->close();
$connection->close();

2.消费者

channel();
$channel->exchange_declare($exchange,'fanout',false,false,false);
$channel->queue_declare($queue_name, false, false, true, false);
$channel->queue_bind($queue_name, $exchange);
echo " [*] Waiting for logs. To exit press CTRL+C\n";
$callback = function ($msg) {
    echo ' [x] ', $msg->body, "\n";
};
$channel->basic_consume($queue_name, '', false, true, false, false, $callback);
while ($channel->is_consuming()) {
    $channel->wait();
}
$channel->close();
$connection->close();

 

你可能感兴趣的:(php)