当前位置:   article > 正文

Rabbitmq+php的使用_rabbit php

rabbit php

rabbitmq的安装:https://blog.csdn.net/u010533511/article/details/90752530

php安装amqp扩展

1.安装rabbitmq-c

下载地址:https://github.com/alanxz/rabbitmq-c/releases

  1. wget -c https://github.com/alanxz/rabbitmq-c/releases/download/v0.8.0/rabbitmq-c-0.8.0.tar.gz
  2. tar zxf rabbitmq-c-0.8.0.tar.gz
  3. cd rabbitmq-c-0.8.0
  4. ./configure --prefix=/usr/local/rabbitmq-c-0.8.0
  5. make && make install

2.安装amqp

官网下载地址:http://pecl.php.net/package/amqp

  1. wget -c http://pecl.php.net/get/amqp-1.9.3.tgz
  2. tar zxf amqp-1.9.3.tgz
  3. cd amqp-1.9.3
  4. /usr/local/php5.5/bin/phpize
  5. ./configure --with-php-config=/usr/local/php5.5/bin/php-config --with-amqp --with-librabbitmq-dir=/usr/local/rabbitmq-c-0.8.0
  6. make && make install

3.修改php.ini

用phpinfo查看是否加载成功

php测试代码:

消息生产者(rabbit_publisher.php)

  1. <?php
  2. //配置信息
  3. $conn_args = array(
  4. 'host' => '127.0.0.1',
  5. 'port' => '5672',
  6. 'login' => 'zcw',
  7. 'password' => '123456',
  8. 'vhost'=>'/'
  9. );
  10. $e_name = 'exchange1'; //交换机名
  11. //$q_name = 'queue1'; //无需队列名
  12. $k_route = 'route1'; //路由key
  13. //创建连接和channel
  14. $conn = new AMQPConnection($conn_args);
  15. if (!$conn->connect()) {
  16. die("Cannot connect to the broker!\n");
  17. }
  18. $channel = new AMQPChannel($conn);
  19. //创建交换机对象
  20. $ex = new AMQPExchange($channel);
  21. $ex->setName($e_name);
  22. date_default_timezone_set("Asia/Shanghai");
  23. //发送消息
  24. //$channel->startTransaction(); //开始事务
  25. for($i=0; $i<5; ++$i){
  26. sleep(1);//休眠1秒
  27. //消息内容
  28. $message = ($i+1)."Send Mssage Success!".date("h:i:sa");
  29. echo ($i+1)."Send Message:".$ex->publish($message, $k_route)."\n";
  30. }
  31. //$channel->commitTransaction(); //提交事务
  32. $conn->disconnect();
  33. ?>

消费者(rabbit_consumer.php)

  1. <?php
  2. //配置信息
  3. $conn_args = array(
  4. 'host' => '127.0.0.1',
  5. 'port' => '5672',
  6. 'login' => 'zcw',
  7. 'password' => '123456',
  8. 'vhost'=>'/'
  9. );
  10. $e_name = 'exchange1'; //交换机名
  11. $q_name = 'queue1'; //队列名
  12. $k_route = 'route1'; //路由key
  13. //创建连接和channel
  14. $conn = new AMQPConnection($conn_args);
  15. if (!$conn->connect()) {
  16. die("Cannot connect to the broker!\n");
  17. }
  18. $channel = new AMQPChannel($conn);
  19. //创建交换机
  20. $ex = new AMQPExchange($channel);
  21. $ex->setName($e_name);
  22. $ex->setType(AMQP_EX_TYPE_DIRECT); //direct类型(常用的有fanout、direct、topic、headers)
  23. $ex->setFlags(AMQP_DURABLE); //持久化
  24. echo "Exchange Status:".$ex->declare()."\n";
  25. //创建队列
  26. $q = new AMQPQueue($channel);
  27. $q->setName($q_name);
  28. $q->setFlags(AMQP_DURABLE); //持久化
  29. echo "Message Total:".$q->declare()."\n";
  30. //绑定交换机与队列,并指定路由键
  31. echo 'Queue Bind: '.$q->bind($e_name, $k_route)."\n";
  32. //阻塞模式接收消息
  33. echo "Message:\n";
  34. while(True){
  35. $q->consume('processMessage');
  36. //$q->consume('processMessage', AMQP_AUTOACK); //自动ACK应答
  37. }
  38. $conn->disconnect();
  39. /**
  40. * 消费回调函数
  41. * 处理消息
  42. */
  43. function processMessage($envelope, $queue) {
  44. $msg = $envelope->getBody();
  45. echo $msg."\n"; //处理消息
  46. $queue->ack($envelope->getDeliveryTag()); //手动发送ACK应答
  47. }
  48. ?>
  • 先运行rabbit_consumer.php

php rabbit_consumer.php

  • 在运行rabbit_publisher.php

php rabbit_publisher.php

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/575083
推荐阅读
相关标签
  

闽ICP备14008679号