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

1 Topic类型工作原理

这里写图片描述

1.1原理:

前面讲到direct类型的Exchange路由规则是完全匹配binding key与routing key,但这种严格的匹配方式在很多情况下不能满足实际业务需求。
topic类型的Exchange在匹配规则上进行了扩展,它与direct类型的Exchage相似,也是将消息路由到binding key与routing key相匹配的Queue中,但这里的匹配规则有些不同,它约定:

1 routing key
routing key为一个句点号“. ”分隔的字符串(我们将被句点号“. ”分隔开的每一段独立的字符串称为一个单词),如“quick.clevercode.log”、“my.clevercode.fox”、“my.brown.fox”

2 binding key
binding key与routing key一样也是句点号“. ”分隔的字符串
binding key中可以存在两种特殊字符“”与“#”,用于做模糊匹配,其中“”用于匹配一个单词,“#”用于匹配多个单词(可以是零个) 。如:“*.clevercode.*”,“*.*.log”,“my.#”

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();

//交换机名字
$exchange_name = 'exchange_topic_clevercode';

//队列名字
$queue_name = 'queue_topic_clevercode1';
#$queue_name = 'queue_topic_clevercode2';

//binding路由key
$binding_routing_key = '*.clevercode.*';
#$binding_routing_key = '*.*.log';
#$binding_routing_key = 'my.#';

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

/*
    name: 队列名称
    passive: false
    durable: true // 队列是否持久化 
    exclusive: false // 当前连接不在时,队列是否自动删除
    auto_delete: false // 没有consumer时,队列是否自动删除 
*/
$channel->queue_declare($queue_name, 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_name, $exchange_name, $binding_routing_key);


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


/**
 * 开始一个队列的消费
 *
 * @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_name, '', 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();

//交换机名字
$exchange_name = 'exchange_topic_clevercode';

//路由key
$routing_key = 'quick.clevercode.log';
$routing_key = 'my.clevercode.fox';
$routing_key = 'my.brown.fox';
$routing_key = 'my.pink.log';
$routing_key = 'quick.brown.fox';
$routing_key = 'clevercode';
$routing_key = 'my';
/*
$routing_key = 'quick.clevercode.male.log';
$routing_key = '';
*/

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

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

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

这里写图片描述

猜你喜欢

转载自blog.csdn.net/CleverCode/article/details/80719014
今日推荐