当前位置: 首页 > news >正文

RabbitMQ-消息队列之routing使用

1、安装rabbitmq

怎么安装rabbitmq请查看之前课程,如果已经安装,请略过此步。

2、创建vendor文件夹或是直接采用PHP框架

mkdir vendor

3、进入文件

cd vendor

4、安装php扩展

composer require php-amqplib/php-amqplib

5、进入上级创建routing文件夹

cd ../
mkdir routing

6、进入routing文件并创建生产者php

cd routing
touch publish.php

7、输入routing生产者内容

<?phprequire_once  '../vendor/autoload.php';use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;$v_host = 'order';$exc_name ='direct_log';$routing_key = 'warn';$connection = new AMQPStreamConnection('localhost',5672,'wangyulong','wangyulong',$v_host);$channel = $connection->channel();$channel->exchange_declare($exc_name,'direct',false,false,false);$data = 'this is '.$routing_key.' message';$msg = new AMQPMessage($data,['delivery_mode'=>AMQPMEssage::DELIVERY_MODE_PERSISTENT]);$channel->basic_publish($msg,$exc_name,$routing_key);$channel->close();$connection->close();

8、创建消费者php

touch error.php
touch info.php
touch warn.php

9、输入routing消费者内容

vi error.php
<?phprequire_once  '../vendor/autoload.php';use PhpAmqpLib\Connection\AMQPStreamConnection;$v_host ='order';$exc_name = 'direct_log';$routing_key = 'error';$connection = new AMQPStreamConnection('localhost',5672,'wangyulong','wangyulong',$v_host);$channel = $connection->channel();$channel->exchange_declare($exc_name,'direct',false,false,false);list($queue_name,,) = $channel->queue_declare('',false,false,true,false);$channel->queue_bind($queue_name,$exc_name,$routing_key);$callback = function($msg){echo 'received ' ,$msg->body,"\n";$msg->ack();
};$channel->basic_qos(null,1,null);$channel->basic_consume($queue_name,'',false,false,false,false,$callback);while($channel->is_open()){$channel->wait();
}$channel->close();$connection->close();
vi info.php
<?phprequire_once  '../vendor/autoload.php';use PhpAmqpLib\Connection\AMQPStreamConnection;$v_host ='order';$exc_name = 'direct_log';$routing_key = 'info';$connection = new AMQPStreamConnection('localhost',5672,'wangyulong','wangyulong',$v_host);$channel = $connection->channel();$channel->exchange_declare($exc_name,'direct',false,false,false);list($queue_name,,) = $channel->queue_declare('',false,false,true,false);$channel->queue_bind($queue_name,$exc_name,$routing_key);$callback = function($msg){echo 'received ' ,$msg->body,"\n";$msg->ack();
};$channel->basic_qos(null,1,null);$channel->basic_consume($queue_name,'',false,false,false,false,$callback);while($channel->is_open()){$channel->wait();
}$channel->close();$connection->close();
vi warn.php
<?phprequire_once  '../vendor/autoload.php';use PhpAmqpLib\Connection\AMQPStreamConnection;$v_host ='order';$exc_name = 'direct_log';$routing_key = 'warn';$connection = new AMQPStreamConnection('localhost',5672,'wangyulong','wangyulong',$v_host);$channel = $connection->channel();$channel->exchange_declare($exc_name,'direct',false,false,false);list($queue_name,,) = $channel->queue_declare('',false,false,true,false);$channel->queue_bind($queue_name,$exc_name,$routing_key);$callback = function($msg){echo 'received ' ,$msg->body,"\n";$msg->ack();
};$channel->basic_qos(null,1,null);$channel->basic_consume($queue_name,'',false,false,false,false,$callback);while($channel->is_open()){$channel->wait();
}$channel->close();$connection->close();

10、执行消费者

php error.php
php info.php
php warn.php

11、执行生产者

php publish.php
此时你会发现只有warn.php对应的消费者才有数据,如果你把publish.php生产者里面的$routing_key对应的值改成error,你会发现只有error.php对应的消费者才有数据。这就是rabbitmq routing工作模式硬实力

http://www.mrgr.cn/news/2537.html

相关文章:

  • 如何用Java SpringBoot+Vue搭建七彩云南文化旅游网站?
  • 币价与数据持续低迷,比特币和以太坊能否从低谷中恢复?
  • 链表【6】完结篇)
  • Java OkHttp使用(二)
  • VAuditDemo文件漏洞
  • 回溯算法——LeetCode491 递增子序列
  • 如何设计一个分布式任务调度器
  • 软件测试学习笔记丨数据查询语言DQL
  • SD-WAN安全:在灵活性与安全性之间找到平衡
  • 如何对Ajax进行进度控制
  • Ubuntu网络服务无法启动问题
  • 数据的动态舞蹈:SQL中的动态计算艺术
  • 【Linux 从基础到进阶】高可用性与负载均衡(HAProxy、Keepalived)
  • CentOS上安装和配置Docker与Docker Compose的详细指南
  • OpenCV几何图像变换(2)计算仿射变换矩阵的函数getAffineTransform()的使用
  • 成为创作者的第1024天:成长与技术积累的旅程
  • 前端面试——js作用域
  • 高性能日志系统 代理模式构建全局日志器获取接口
  • windows hook之进程防杀(任务管理器)
  • 基于UE5和ROS2的激光雷达+深度RGBD相机小车的仿真指南(一)---UnrealCV获取深度+分割图像