国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 全椒县| 湘阴县| 淄博市| 揭西县| 梅州市| 江永县| 塔城市| 石台县| 保靖县| 吴忠市| 扶风县| 台中市| 黄陵县| 延庆县| 富宁县| 贵港市| 麻城市| 公主岭市| 长泰县| 喀喇| 平和县| 柯坪县| 沛县| 会同县| 萨迦县| 哈尔滨市| 揭西县| 正阳县| 南部县| 长治市| 页游| 定兴县| 元氏县| 道孚县| 临安市| 衡东县| 浑源县| 盐山县| 含山县| 文昌市| 广饶县|