当前位置:   article > 正文

RabbitMQ-消息队列之routing使用

RabbitMQ-消息队列之routing使用

1、安装rabbitmq

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

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

mkdir vendor

3、进入文件

cd vendor

4、安装php扩展

composer require php-amqplib/php-amqplib

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

  1. cd ../
  2. mkdir routing

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

  1. cd routing
  2. touch publish.php

7、输入routing生产者内容

  1. <?php
  2. require_once '../vendor/autoload.php';
  3. use PhpAmqpLib\Connection\AMQPStreamConnection;
  4. use PhpAmqpLib\Message\AMQPMessage;
  5. $v_host = 'order';
  6. $exc_name ='direct_log';
  7. $routing_key = 'warn';
  8. $connection = new AMQPStreamConnection('localhost',5672,'wangyulong','wangyulong',$v_host);
  9. $channel = $connection->channel();
  10. $channel->exchange_declare($exc_name,'direct',false,false,false);
  11. $data = 'this is '.$routing_key.' message';
  12. $msg = new AMQPMessage($data,['delivery_mode'=>AMQPMEssage::DELIVERY_MODE_PERSISTENT]);
  13. $channel->basic_publish($msg,$exc_name,$routing_key);
  14. $channel->close();
  15. $connection->close();

8、创建消费者php

  1. touch error.php
  2. touch info.php
  3. touch warn.php

9、输入routing消费者内容

  1. vi error.php
  2. <?php
  3. require_once '../vendor/autoload.php';
  4. use PhpAmqpLib\Connection\AMQPStreamConnection;
  5. $v_host ='order';
  6. $exc_name = 'direct_log';
  7. $routing_key = 'error';
  8. $connection = new AMQPStreamConnection('localhost',5672,'wangyulong','wangyulong',$v_host);
  9. $channel = $connection->channel();
  10. $channel->exchange_declare($exc_name,'direct',false,false,false);
  11. list($queue_name,,) = $channel->queue_declare('',false,false,true,false);
  12. $channel->queue_bind($queue_name,$exc_name,$routing_key);
  13. $callback = function($msg){
  14. echo 'received ' ,$msg->body,"\n";
  15. $msg->ack();
  16. };
  17. $channel->basic_qos(null,1,null);
  18. $channel->basic_consume($queue_name,'',false,false,false,false,$callback);
  19. while($channel->is_open()){
  20. $channel->wait();
  21. }
  22. $channel->close();
  23. $connection->close();
  1. vi info.php
  2. <?php
  3. require_once '../vendor/autoload.php';
  4. use PhpAmqpLib\Connection\AMQPStreamConnection;
  5. $v_host ='order';
  6. $exc_name = 'direct_log';
  7. $routing_key = 'info';
  8. $connection = new AMQPStreamConnection('localhost',5672,'wangyulong','wangyulong',$v_host);
  9. $channel = $connection->channel();
  10. $channel->exchange_declare($exc_name,'direct',false,false,false);
  11. list($queue_name,,) = $channel->queue_declare('',false,false,true,false);
  12. $channel->queue_bind($queue_name,$exc_name,$routing_key);
  13. $callback = function($msg){
  14. echo 'received ' ,$msg->body,"\n";
  15. $msg->ack();
  16. };
  17. $channel->basic_qos(null,1,null);
  18. $channel->basic_consume($queue_name,'',false,false,false,false,$callback);
  19. while($channel->is_open()){
  20. $channel->wait();
  21. }
  22. $channel->close();
  23. $connection->close();
  1. vi warn.php
  2. <?php
  3. require_once '../vendor/autoload.php';
  4. use PhpAmqpLib\Connection\AMQPStreamConnection;
  5. $v_host ='order';
  6. $exc_name = 'direct_log';
  7. $routing_key = 'warn';
  8. $connection = new AMQPStreamConnection('localhost',5672,'wangyulong','wangyulong',$v_host);
  9. $channel = $connection->channel();
  10. $channel->exchange_declare($exc_name,'direct',false,false,false);
  11. list($queue_name,,) = $channel->queue_declare('',false,false,true,false);
  12. $channel->queue_bind($queue_name,$exc_name,$routing_key);
  13. $callback = function($msg){
  14. echo 'received ' ,$msg->body,"\n";
  15. $msg->ack();
  16. };
  17. $channel->basic_qos(null,1,null);
  18. $channel->basic_consume($queue_name,'',false,false,false,false,$callback);
  19. while($channel->is_open()){
  20. $channel->wait();
  21. }
  22. $channel->close();
  23. $connection->close();

10、执行消费者

  1. php error.php
  2. php info.php
  3. php warn.php

11、执行生产者

php publish.php
此时你会发现只有warn.php对应的消费者才有数据,如果你把publish.php生产者里面的$routing_key对应的值改成error,你会发现只有error.php对应的消费者才有数据。这就是rabbitmq routing工作模式硬实力
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号