yii-queue介绍

https://www.yiiframework.com/extension/yiisoft/yii2-queue/doc/guide/2.3/zh-cn/usage
https://www.yiiframework.com/extension/yiisoft/yii2-queue/doc/guide/2.3/zh-cn/driver-amqp
https://www.yiiframework.com/extension/yiisoft/yii2-queue/doc/api/2.3/yii-queue-amqp-queue


使用composer安装php-amqplib

  1. $ php composer.phar require php-amqplib/php-amqplib 2.6.*”

安装好之后我们可以在vendor下找到php-amqplib包。

https://blog.csdn.net/lj_550566181/article/details/53240475
https://github.com/php-amqplib/php-amqplib


使用composer提示Could not open input file: composer.phar

win10 Yii2 rabitmq - 图1

原因肯定是:在别的项目下载过composer没有设置全局变量或者没有安装composer
解决:打开命令行并依次执行下列命令安装最新版本的 Composer:

  1. php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
  2. php composer-setup.php
  3. php -r "unlink('composer-setup.php');"

https://blog.csdn.net/jizha1917/article/details/94245063


Yii2 composer更新

在Yii安装目录下执行composer任一命令时,报错:

  1. The "yiisoft/yii2-composer" plugin was skipped because it requires a Plugin API version ("1.0.0") that does not match your Composer installation (" 1.1.0"). You may need to run composer update with the "--no-plugins" option.

解决:
运行composer update yiisoft/yii2-composer获取最新版本的插件,即可。

https://www.cnblogs.com/meetuj/p/11429103.html


php的amqp扩展安装

php的amqp扩展下载地址:http://pecl.php.net/package/amqp
bVr3YA (2).jpg

1、复制php_amqp.dll到php/ext 如我的放到 G:/php/php-5.5.6-Win32-VC11-x64/ext 目录下 2、php.ini中添加如下代码 [amqp]extension=php_amqp.dll 3、复制rabbitmq.1.dll到php目录 如我的放到 G:/php/php-5.5.6-Win32-VC11-x64 目录下 4、修改apache配置文件httpd.conf添加入 LoadFile “rabbitmq.1.dll文件路径” win10 Yii2 rabitmq - 图3

重启apache phpinfo显示如下
win10 Yii2 rabitmq - 图4
https://segmentfault.com/a/1190000004266165


yii2 amqp 接收和发送数据(和外部系统对接)

1,配置queue(amqp)

  1. <?php
  2. 'bootstrap' => [
  3. 'queue', // The component registers own console commands
  4. ],
  5. 'components' => [
  6. 'queue' => [
  7. 'class' => 'zhuravljov\yii\queue\amqp\Queue',
  8. 'host' => '192.168.221.56',
  9. 'port' => 5672,
  10. 'user' => 'admin',
  11. 'password' => 'admin',
  12. 'queueName' => 'productDropshipQN',
  13. 'exchangeName' => 'productDropshipEX',
  14. ],
  15. ],
  16. ?>

2,console controller

  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\app\console\modules\Amqp\controllers;
  10. use Yii;
  11. use yii\console\Controller;
  12. use fecshop\app\console\modules\Amqp\block\PushTest;
  13. use PhpAmqpLib\Channel\AMQPChannel;
  14. use PhpAmqpLib\Connection\AMQPStreamConnection;
  15. use PhpAmqpLib\Message\AMQPMessage;
  16. /**
  17. * @author Terry Zhao <2358269014@qq.com>
  18. * @since 1.0
  19. * 这是一个测试RabbitMq 的一个例子。这里作为消息生产方。
  20. * 你可以通过执行 ./yii amqp/test/test 来生产数据。
  21. */
  22. class TestController extends Controller
  23. {
  24. const EXCHANGE_DIRECT = 'direct';
  25. const EXCHANGE_TOPIC = 'topic';
  26. const EXCHANGE_FANOUT = 'fanout';
  27. public $host = '192.168.221.56';
  28. public $port = 5672;
  29. public $user = 'admin';
  30. public $password = 'admin';
  31. public $queueName = 'productDropshipQN';
  32. public $exchangeName = 'productDropshipEX';
  33. public $routingKey = 'productDropshipRT';
  34. public $exchangeType = self::EXCHANGE_DIRECT;
  35. /**
  36. * @var AMQPStreamConnection
  37. */
  38. private $connection;
  39. /**
  40. * @var AMQPChannel
  41. */
  42. private $channel;
  43. /**
  44. * 生产数据
  45. */
  46. public function actionTest()
  47. {
  48. Yii::$app->queue->push([
  49. 'name' => 'water',
  50. 'age' => 331,
  51. ]);
  52. }
  53. /**
  54. * 接收数据
  55. */
  56. public function actionListen()
  57. {
  58. $this->open();
  59. $callback = function(AMQPMessage $message) {
  60. if ($this->handleMessage($message->body)) {
  61. $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
  62. }
  63. };
  64. $this->channel->basic_qos(null, 1, null);
  65. $this->channel->basic_consume($this->queueName, '', false, false, false, false, $callback);
  66. while(count($this->channel->callbacks)) {
  67. $this->channel->wait();
  68. }
  69. }
  70. /**
  71. * Opens connection and channel
  72. */
  73. protected function open()
  74. {
  75. if ($this->channel) return;
  76. $this->connection = new AMQPStreamConnection($this->host, $this->port, $this->user, $this->password);
  77. $this->channel = $this->connection->channel();
  78. $this->channel->queue_declare($this->queueName,true, true);
  79. $this->channel->exchange_declare($this->exchangeName, $this->exchangeType, false, true, false);
  80. $this->channel->queue_bind($this->queueName, $this->exchangeName,$this->routingKey);
  81. }
  82. /**
  83. * 这里处理接收到的数据
  84. */
  85. protected function handleMessage($message)
  86. {
  87. // $message = unserialize($message);
  88. var_dump($message);
  89. // do some thing ...
  90. // \Yii::info($message,'fecshop_debug');
  91. return true;
  92. }
  93. /*
  94. public function actionListen3()
  95. {
  96. Yii::$app->queue->listen();
  97. }
  98. */
  99. }

http://www.fancyecommerce.com/2017/06/07/yii2-amqp-%E6%8E%A5%E6%94%B6%E5%92%8C%E5%8F%91%E9%80%81%E6%95%B0%E6%8D%AE%EF%BC%88%E5%92%8C%E5%A4%96%E9%83%A8%E7%B3%BB%E7%BB%9F%E5%AF%B9%E6%8E%A5%EF%BC%89/