新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

怎么在PHP项目中访问RabbitMQ消息队列-创新互联

本篇文章给大家分享的是有关怎么在PHP项目中访问RabbitMQ消息队列,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联建站于2013年开始,先为茫崖等服务建站,茫崖等地企业,进行企业商务咨询服务。为茫崖企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

扩展安装


PHP访问RabbitMQ实际使用的是AMQP协议,所以我们只要安装epel库中的php-pecl-amqp这个包即可

rpm -ivh /tupian/20230522/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项目中访问RabbitMQ消息队列,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


网页题目:怎么在PHP项目中访问RabbitMQ消息队列-创新互联
网页网址:http://www.wjwzjz.com/article/cdiodi.html
在线咨询
服务热线
服务热线:028-86922220
TOP