RabbitMQ交换机fanout类型工作原理和PHP样例代码

1 fanout类型工作原理

这里写图片描述

fanout类型的Exchange路由规则非常简单,它会把所有发送到该Exchange的消息路由到所有与它绑定的Queue中:

当生成者将消息投递到exchange_fanout_clevercode。交互机exchange_fanout_clevercode将会将消息分别投递到和他绑定的queue_fanout_clevercode1,queue_fanout_clevercode2,queue_fanout_clevercode3三个队列中。

2 PHP样例代码

2.1 消费者进程

<?php
require_once __DIR__ . '/../../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPConnection;

//连接
$host = '127.0.0.1';//ip
$port = '5672';//端口
$user = 'guest'; //用户
$password = 'guest'; //密码
$vhost = '/'; //空间
$connection = new AMQPConnection($host, $port,$user, $password,$vhost);
$channel = $connection->channel();

 /*
     name: 交换机名字
     type: 交换机类型
     passive: false
     durable: true // 交换机将在服务器重启后生存。
     auto_delete: false //通道关闭的时候,交换机不会被删除
 */
 $channel->exchange_declare('exchange_fanout_clevercode', 'fanout', false, true, false);

/*
    name: 队列名称
    passive: false
    durable: true // 队列是否持久化 
    exclusive: false // 当前连接不在时,队列是否自动删除
    auto_delete: false // 没有consumer时,队列是否自动删除 
*/
$channel->queue_declare('queue_fanout_clevercode1', false, true, false, false);
#$channel->queue_declare('queue_fanout_clevercode2', false, true, false, false);
#$channel->queue_declare('queue_fanout_clevercode3', false, true, false, false);


/**
 * 绑定队列到一个交换机
 *
 * @param string $queue 队列名称
 * @param string $exchange 交换机名称
 * @param string $routing_key binding路由key
 * @param bool $nowait
 * @param null $arguments
 * @param null $ticket
 * @return mixed|null
 */
$channel->queue_bind('queue_fanout_clevercode1', 'exchange_fanout_clevercode');


/**
 * 消费回调函数
 * 处理消息
 */ 
function processMessage($msg) { 
    //处理消息
    $info =  'recive:,'.$msg->delivery_info['routing_key']. ':'. $msg->body. "\n";
    echo $info;
}


/**
 * 开始一个队列的消费
 *
 * @param string $queue
 * @param string $consumer_tag
 * @param bool $no_local
 * @param bool $no_ack
 * @param bool $exclusive
 * @param bool $nowait
 * @param callback|null $callback
 * @param int|null $ticket
 * @param array $arguments
 * @return mixed|string
 */
$channel->basic_consume('queue_fanout_clevercode1', '', false, true, false, false, 'processMessage');

/**
 * //注册结束时候,关闭连接情况
 * @param \PhpAmqpLib\Channel\AMQPChannel $ch
 * @param \PhpAmqpLib\Connection\AbstractConnection $conn
 */
function AMQP_shutdown($ch, $conn)
{
    $ch->close();
    $conn->close();
}

register_shutdown_function('AMQP_shutdown', $channel, $connection);

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

2.2 生成者进程

<?php
require_once __DIR__ . '/../../vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;

//连接
$host = '127.0.0.1';//ip
$port = '5672';//端口
$user = 'guest'; //用户
$password = 'guest'; //密码
$vhost = '/'; //空间
$connection = new AMQPConnection($host, $port,$user, $password,$vhost);
$channel = $connection->channel();

 /*
     name: 交换机名字
     type: 交换机类型
     passive: false
     durable: true // 交换机将在服务器重启后生存。
     auto_delete: false //通道关闭的时候,交换机不会被删除
 */
 $channel->exchange_declare('exchange_fanout_clevercode', 'fanout', false, true, false);


for($i = 1; $i <= 30;$i++)
{
    sleep(1);
    /**
     * Publishes a message
     *
     * @param AMQPMessage $msg
     * @param string $exchange
     * @param string $routing_key
     * @param bool $mandatory
     * @param bool $immediate
     * @param null $ticket
     */
    $data = "msg info:{$i}";
    $msg = new AMQPMessage($data);
    $channel->basic_publish($msg, 'exchange_fanout_clevercode');

    echo "send :{$data}\n";
}

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

这里写图片描述

这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/CleverCode/article/details/80714298