本文實例講述了使用PHP訪問RabbitMQ消息隊列的方法。分享給大家供大家參考,具體如下:
擴展安裝
PHP訪問RabbitMQ實際使用的是AMQP協(xié)議,所以我們只要安裝epel庫中的php-pecl-amqp這個包即可
rpm -ivh http://mirror.neu.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install php-pecl-amqp
交換建立
?php
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
$exchange = new AMQPExchange($channel);
$exchange->setName('exchange1');
$exchange->setType('fanout');
$exchange->declare();
隊列建立
?php
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
$queue = new AMQPQueue($channel);
$queue->setName('queue1');
$queue->declare();
隊列綁定
?php
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
$queue = new AMQPQueue($channel);
$queue->setName('queue1');
$queue->declare();
$queue->bind('exchange1', 'routekey');
消息發(fā)送
?php
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
$exchange = new AMQPExchange($channel);
$exchange->setName('exchange5');
$exchange->setType('fanout');
$exchange->declare();
for($i = 0; $i 2000000; $i++) {
$exchange->publish("message $i", "routekey");
}
消息接收
?php
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
$queue = new AMQPQueue($channel);
$queue->setName('queue1');
$queue->declare();
$queue->bind('exchange1', 'routekey');
while (true) {
$queue->consume(function($envelope, $queue){
echo $envelope->getBody(), PHP_EOL;
}, AMQP_AUTOACK);
}
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》及《PHP數(shù)學(xué)運算技巧總結(jié)》
希望本文所述對大家PHP程序設(shè)計有所幫助。
您可能感興趣的文章:- php實現(xiàn)通過stomp協(xié)議連接ActiveMQ操作示例
- php ActiveMQ的安裝與使用方法圖文教程
- PHP使用ActiveMQ實現(xiàn)消息隊列的方法詳解
- PHP使用ActiveMQ實例
- PHP Beanstalkd消息隊列的安裝與使用方法實例詳解
- PHP高級編程之消息隊列原理與實現(xiàn)方法詳解
- php+redis實現(xiàn)消息隊列功能示例
- PHP+RabbitMQ實現(xiàn)消息隊列的完整代碼
- php實現(xiàn)websocket實時消息推送
- php 使用ActiveMQ發(fā)送消息,與處理消息操作示例