RabbitMQ交換機Topic型別工作原理和PHP樣例程式碼
阿新 • • 發佈:2019-01-08
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();