您现在的位置是:网站首页> 编程资料编程资料
使用PHP访问RabbitMQ消息队列的方法示例_php技巧_
2023-05-25
279人已围观
简介 使用PHP访问RabbitMQ消息队列的方法示例_php技巧_
本文实例讲述了使用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
交换建立
connect(); $channel = new AMQPChannel($connection); $exchange = new AMQPExchange($channel); $exchange->setName('exchange1'); $exchange->setType('fanout'); $exchange->declare(); 队列建立
connect(); $channel = new AMQPChannel($connection); $queue = new AMQPQueue($channel); $queue->setName('queue1'); $queue->declare(); 队列绑定
connect(); $channel = new AMQPChannel($connection); $queue = new AMQPQueue($channel); $queue->setName('queue1'); $queue->declare(); $queue->bind('exchange1', 'routekey'); 消息发送
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"); } 消息接收
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简单实现记录网站访问量功能示例_php技巧_
- Laravel框架实现利用监听器进行sql语句记录功能_php实例_
- Laravel框架实现利用中间件进行操作日志记录功能_php实例_
- PHP实现的curl批量请求操作示例_php技巧_
- thinkPHP3.2.3实现阿里大于短信验证的方法_php实例_
- PHP使用curl请求实现post方式上传图片文件功能示例_php技巧_
- PHP程序员学习使用Swoole的理由_php技巧_
- PHP实现的装箱算法示例_php技巧_
- PHP基于curl模拟post提交json数据示例_php技巧_
- PHP获取日期对应星期、一周日期、星期开始与结束日期的方法_php技巧_
