使用PHP訪問RabbitMQ消息隊(duì)列的方法示例
本文實(shí)例講述了使用PHP訪問RabbitMQ消息隊(duì)列的方法。分享給大家供大家參考,具體如下:
擴(kuò)展安裝
PHP訪問RabbitMQ實(shí)際使用的是AMQP協(xié)議,所以我們只要安裝epel庫中的php-pecl-amqp這個(gè)包即可
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();
隊(duì)列建立
<?php $connection = new AMQPConnection(); $connection->connect(); $channel = new AMQPChannel($connection); $queue = new AMQPQueue($channel); $queue->setName('queue1'); $queue->declare();
隊(duì)列綁定
<?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è)計(jì)算法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》及《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php實(shí)現(xiàn)通過stomp協(xié)議連接ActiveMQ操作示例
- php ActiveMQ的安裝與使用方法圖文教程
- PHP使用ActiveMQ實(shí)現(xiàn)消息隊(duì)列的方法詳解
- PHP使用ActiveMQ實(shí)例
- PHP Beanstalkd消息隊(duì)列的安裝與使用方法實(shí)例詳解
- PHP高級(jí)編程之消息隊(duì)列原理與實(shí)現(xiàn)方法詳解
- php+redis實(shí)現(xiàn)消息隊(duì)列功能示例
- PHP+RabbitMQ實(shí)現(xiàn)消息隊(duì)列的完整代碼
- php實(shí)現(xiàn)websocket實(shí)時(shí)消息推送
- php 使用ActiveMQ發(fā)送消息,與處理消息操作示例
相關(guān)文章
php導(dǎo)出excel格式數(shù)據(jù)問題
本篇文章主要是對(duì)php導(dǎo)出excel格式數(shù)據(jù)的問題進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-03-03PHP5.5.15+Apache2.4.10+MySQL5.6.20配置方法分享
這篇文章主要為大家詳細(xì)介紹了PHP5.5.15、Apache2.4.10和MySQL5.6.20配置方法,感興趣的小伙伴們可以參考一下2016-05-05php實(shí)現(xiàn)等比例不失真縮放上傳圖片的方法
這篇文章主要介紹了php實(shí)現(xiàn)等比例不失真縮放上傳圖片的方法,結(jié)合實(shí)例形式分析了php基于自定義函數(shù)實(shí)現(xiàn)等比例不失真縮放上傳圖片的具體功能定義與使用方法,需要的朋友可以參考下2016-11-11PHP正則的Unknown Modifier錯(cuò)誤解決方法
PHP正則時(shí)出現(xiàn)Unknown Modifier錯(cuò)誤解決方法2010-03-03php正則匹配html中帶class的div并選取其中內(nèi)容的方法
這篇文章主要介紹了php正則匹配html中帶class的div并選取其中內(nèi)容的方法,涉及curl的使用及正則匹配的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01調(diào)整優(yōu)化您的LAMP應(yīng)用程序的5種簡單方法
Wikipedia、Facebook 和 Yahoo! 等主要 web 屬性使用 LAMP 架構(gòu)來為每天數(shù)百萬的請(qǐng)求提供服務(wù),而 Wordpress、Joomla、Drupal 和 SugarCRM 等 web 應(yīng)用程序軟件使用其架構(gòu)來讓組織輕松部署基于 web 的應(yīng)用程序。2011-06-06PHP和Java 集成開發(fā)詳解分析 強(qiáng)強(qiáng)聯(lián)合
很久以前,有人從www上看到看到天空上一個(gè)很亮的亮點(diǎn),它就是Java語言,與此同時(shí),在另一個(gè)地方一位夢(mèng)想家也看到了一個(gè)亮點(diǎn),它就是PHP.2008-11-11