使用PHP存取RabbitMQ訊息佇列的方法

不言
發布: 2023-03-30 10:42:02
原創
2424 人瀏覽過

這篇文章主要介紹了使用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(&#39;exchange1&#39;);
$exchange->setType(&#39;fanout&#39;);
$exchange->declare();
登入後複製

##佇列建立

<?php
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
$queue = new AMQPQueue($channel);
$queue->setName(&#39;queue1&#39;);
$queue->declare();
登入後複製

#佇列綁定

##
<?php
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
$queue = new AMQPQueue($channel);
$queue->setName(&#39;queue1&#39;);
$queue->declare();
$queue->bind('exchange1', 'routekey');
登入後複製

訊息發送

<?php
$connection = new AMQPConnection();
$connection->connect();
$channel = new AMQPChannel($connection);
$exchange = new AMQPExchange($channel);
$exchange->setName(&#39;exchange5&#39;);
$exchange->setType(&#39;fanout&#39;);
$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(&#39;queue1&#39;);
$queue->declare();
$queue->bind('exchange1', 'routekey');
while (true) {
  $queue->consume(function($envelope, $queue){
   echo $envelope->getBody(), PHP_EOL;
  }, AMQP_AUTOACK);
}
登入後複製
#########相關推薦:##########PHP 訊息佇列服務############### ########

以上是使用PHP存取RabbitMQ訊息佇列的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!