• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    使用PHP访问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
    $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);
    }
    
    

    更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数据结构与算法教程》、《php程序设计算法总结》、《php字符串(string)用法总结》、《PHP数组(Array)操作技巧大全》、《PHP常用遍历算法与技巧总结》及《PHP数学运算技巧总结》

    希望本文所述对大家PHP程序设计有所帮助。

    您可能感兴趣的文章:
    • php实现通过stomp协议连接ActiveMQ操作示例
    • php ActiveMQ的安装与使用方法图文教程
    • PHP使用ActiveMQ实现消息队列的方法详解
    • PHP使用ActiveMQ实例
    • PHP Beanstalkd消息队列的安装与使用方法实例详解
    • PHP高级编程之消息队列原理与实现方法详解
    • php+redis实现消息队列功能示例
    • PHP+RabbitMQ实现消息队列的完整代码
    • php实现websocket实时消息推送
    • php 使用ActiveMQ发送消息,与处理消息操作示例
    上一篇:PHP简单实现记录网站访问量功能示例
    下一篇:thinkPHP框架实现生成条形码的方法示例
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    使用PHP访问RabbitMQ消息队列的方法示例 使用,PHP,访问,RabbitMQ,消息,