這篇文章主要介紹了使用PHP存取RabbitMQ訊息佇列的方法,結合實例形式分析了RabbitMQ訊息佇列的相關擴充安裝、佇列建立、佇列綁定、訊息傳送、訊息接收等相關操作技巧,需要的朋友可以參考下方
本文實例講述了使用PHP存取RabbitMQ訊息佇列的方法。分享給大家供大家參考,如下:
擴充安裝
PHP存取RabbitMQ實際使用的是AMQP協議,所以我們只要安裝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');
<?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存取RabbitMQ訊息佇列的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!<?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);
}