使用PHP訪問RabbitMQ消息隊列的方法示例
本文實例講述了使用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); }
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP數(shù)據結構與算法教程》、《php程序設計算法總結》、《php字符串(string)用法總結》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結》及《PHP數(shù)學運算技巧總結》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
PHP5.5.15+Apache2.4.10+MySQL5.6.20配置方法分享
這篇文章主要為大家詳細介紹了PHP5.5.15、Apache2.4.10和MySQL5.6.20配置方法,感興趣的小伙伴們可以參考一下2016-05-05php正則匹配html中帶class的div并選取其中內容的方法
這篇文章主要介紹了php正則匹配html中帶class的div并選取其中內容的方法,涉及curl的使用及正則匹配的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01PHP和Java 集成開發(fā)詳解分析 強強聯(lián)合
很久以前,有人從www上看到看到天空上一個很亮的亮點,它就是Java語言,與此同時,在另一個地方一位夢想家也看到了一個亮點,它就是PHP.2008-11-11