安裝 RabbitMQ 延遲隊列插件
RabbitMQ 延遲隊列插件未安裝直接使用的話,會報錯:
unknown exchange type 'x-delayed-message'
插件下載地址:https://www.rabbitmq.com/community-plugins.html 。下載 Erlang 可執行文件之後,複製到rabbit服務的插件目錄(自己的安裝目錄,我的是 C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.14\plugins
)中,然後開啓插件服務:
rabbitmq-plugins enable rabbitmq_delayed_message_exchange
測試步驟
- 創建測試項目的目錄 mq
- 從 https://github.com/php-amqplib/php-amqplib 下載AMQP庫(當然也可以通過 composer 安裝,這裏爲了簡單直接自己處理了),放入 mq 目錄
- 編寫 index.php,實現自動加載
- 創建 test 目錄,裏面分別創建 mqc.php 消費者和 mqp.php 生產者兩個文件
- 跑腳本,測試消息的生產和消費:
- php -f index.php delayP p 3 生產消息,延時3秒
- php -f index.php delayC c 消費消息
目錄結構
├─PhpAmqpLib
│ ├─Channel
│ ├─Connection
│ ├─Exception
│ ├─Exchange
│ ├─Helper
│ │ └─Protocol
│ ├─Message
│ └─Wire
│ └─IO
├─test
│ ├─delayP.php
│ └─delayC.php
└─index.php
源碼
index.php
<?php
function my_autoloader($cName) {
include(__DIR__."/".$cName.".php");
}
spl_autoload_register("my_autoloader");
print_r($argv);
if (isset($argv[2])) {
$cname = '\test\\'.$argv[1];
if (!class_exists($cname)) {
exit("class (".$cname.") not exists".PHP_EOL);
}
$c = new $cname();
if (!method_exists($c, $argv[2])) {
exit("method (".$argv[2].") not exists".PHP_EOL);
}
if (isset($argv[3])) {
call_user_func(array($c, $argv[2]), $argv[3]);
} else {
call_user_func(array($c, $argv[2]));
}
} else if (isset($argv[1])) {
if (!function_exists($argv[1])) {
exit("function (".$argv[1].") not exists".PHP_EOL);
}
$argv[1]();
} else {
exit("please input at least one argument".PHP_EOL);
}
delayP.php
<?php
namespace test;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;
class delayP {
private $host = 'localhost';
private $port = 5672;
private $user = 'guest';
private $password = 'guest';
// 可能丟失
public function p($delayTime = 5) {
$connection = new AMQPStreamConnection($this->host, $this->port, $this->user, $this->password, '/', false, 'AMQPLAIN', null, 'en_US', 3.0, 120.0, null, true, 60);
$channel = $connection->channel();
$channel->exchange_declare('delay.myExchange', 'x-delayed-message', false, true, false, false, false, new AMQPTable(['x-delayed-type' => 'direct']));
$channel->queue_declare('delay.myQueue', false, true, false, false);
$channel->queue_bind('delay.myQueue', 'delay.myExchange', 'my');
// 準備消息
$msg = new AMQPMessage(
json_encode([
'data' => "延遲隊列數據".time(),
'sendTime' => time(),
'expectRunTime' => time() + $delayTime
]), [
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT // 持久化
]
);
$msg->set('application_headers', new AMQPTable([
'x-delay' => $delayTime * 1000 // 延遲時間,單位毫秒
]));
$channel->basic_publish($msg, 'delay.myExchange', 'my');
echo "basic_publish success";
$channel->close();
$connection->close();
}
}
delayC.php
<?php
namespace test;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;
class delayC {
private $host = 'localhost';
private $port = 5672;
private $user = 'guest';
private $password = 'guest';
// 自動 ACK
public function c() {
$connection = new AMQPStreamConnection($this->host, $this->port, $this->user, $this->password, '/', false, 'AMQPLAIN', null, 'en_US', 3.0, 120.0, null, true, 60);
$channel = $connection->channel();
$channel->exchange_declare('delay.myExchange', 'x-delayed-message', false, true, false, false, false, new AMQPTable(['x-delayed-type' => 'direct']));
$channel->queue_declare('delay.myQueue', false, true, false, false);
//閉包回調函數
$callback = function ($msg) {
echo $msg->body.' '.time();
echo PHP_EOL;
};
$channel->basic_qos(null, 1, null);
$channel->basic_consume('delay.myQueue', '', false, true, false, false, $callback);
while (count($channel->callbacks)) {
$channel->wait();
}
$channel->close();
$connection->close();
}
}