国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

使用PHP訪問RabbitMQ消息隊列的方法示例

2024-05-04 21:50:02
字體:
供稿:網(wǎng)友

本文實例講述了使用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

交換建立

  1. $connection = new AMQPConnection(); 
  2. $connection->connect(); 
  3. $channel = new AMQPChannel($connection); 
  4. $exchange = new AMQPExchange($channel); 
  5. $exchange->setName('exchange1'); 
  6. $exchange->setType('fanout'); 
  7. $exchange->declare(); 

隊列建立

  1. $connection = new AMQPConnection(); 
  2. $connection->connect(); 
  3. $channel = new AMQPChannel($connection); 
  4. $queue = new AMQPQueue($channel); 
  5. $queue->setName('queue1'); 
  6. $queue->declare(); 

隊列綁定

  1. $connection = new AMQPConnection(); 
  2. $connection->connect(); 
  3. $channel = new AMQPChannel($connection); 
  4. $queue = new AMQPQueue($channel); 
  5. $queue->setName('queue1'); 
  6. $queue->declare(); 
  7. $queue->bind('exchange1''routekey'); 

消息發(fā)送

  1. $connection = new AMQPConnection(); 
  2. $connection->connect(); 
  3. $channel = new AMQPChannel($connection); 
  4. $exchange = new AMQPExchange($channel); 
  5. $exchange->setName('exchange5'); 
  6. $exchange->setType('fanout'); 
  7. $exchange->declare(); 
  8. for($i = 0; $i < 2000000; $i++) { 
  9.  $exchange->publish("message $i""routekey"); 

消息接收

  1. $connection = new AMQPConnection(); 
  2. $connection->connect(); 
  3. $channel = new AMQPChannel($connection); 
  4. $queue = new AMQPQueue($channel); 
  5. $queue->setName('queue1'); 
  6. $queue->declare(); 
  7. $queue->bind('exchange1''routekey'); 
  8. while (true) { 
  9.   $queue->consume(function($envelope$queue){ 
  10.    echo $envelope->getBody(), PHP_EOL; 
  11.   }, AMQP_AUTOACK); 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 商都县| 宁都县| 霸州市| 育儿| 永州市| 东城区| 邻水| 华阴市| 湘潭市| 闽侯县| 赞皇县| 潮州市| 泸溪县| 本溪| 南平市| 洛南县| 尚义县| 柘荣县| 滦南县| 巴彦淖尔市| 奉化市| 邹平县| 石阡县| 云林县| 绩溪县| 天长市| 缙云县| 白沙| 平远县| 高青县| 历史| 井研县| 巴彦淖尔市| 沛县| 安宁市| 额尔古纳市| 兰西县| 新泰市| 襄城县| 平顶山市| 应城市|