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
$ 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
原因肯定是:在别的项目下载过composer没有设置全局变量或者没有安装composer
解决:打开命令行并依次执行下列命令安装最新版本的 Composer:
php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
https://blog.csdn.net/jizha1917/article/details/94245063
Yii2 composer更新
在Yii安装目录下执行composer任一命令时,报错:
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
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文件路径”
重启apache phpinfo显示如下
https://segmentfault.com/a/1190000004266165
yii2 amqp 接收和发送数据(和外部系统对接)
1,配置queue(amqp)
<?php
'bootstrap' => [
'queue', // The component registers own console commands
],
'components' => [
'queue' => [
'class' => 'zhuravljov\yii\queue\amqp\Queue',
'host' => '192.168.221.56',
'port' => 5672,
'user' => 'admin',
'password' => 'admin',
'queueName' => 'productDropshipQN',
'exchangeName' => 'productDropshipEX',
],
],
?>
2,console controller
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\console\modules\Amqp\controllers;
use Yii;
use yii\console\Controller;
use fecshop\app\console\modules\Amqp\block\PushTest;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
/**
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
* 这是一个测试RabbitMq 的一个例子。这里作为消息生产方。
* 你可以通过执行 ./yii amqp/test/test 来生产数据。
*/
class TestController extends Controller
{
const EXCHANGE_DIRECT = 'direct';
const EXCHANGE_TOPIC = 'topic';
const EXCHANGE_FANOUT = 'fanout';
public $host = '192.168.221.56';
public $port = 5672;
public $user = 'admin';
public $password = 'admin';
public $queueName = 'productDropshipQN';
public $exchangeName = 'productDropshipEX';
public $routingKey = 'productDropshipRT';
public $exchangeType = self::EXCHANGE_DIRECT;
/**
* @var AMQPStreamConnection
*/
private $connection;
/**
* @var AMQPChannel
*/
private $channel;
/**
* 生产数据
*/
public function actionTest()
{
Yii::$app->queue->push([
'name' => 'water',
'age' => 331,
]);
}
/**
* 接收数据
*/
public function actionListen()
{
$this->open();
$callback = function(AMQPMessage $message) {
if ($this->handleMessage($message->body)) {
$message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
}
};
$this->channel->basic_qos(null, 1, null);
$this->channel->basic_consume($this->queueName, '', false, false, false, false, $callback);
while(count($this->channel->callbacks)) {
$this->channel->wait();
}
}
/**
* Opens connection and channel
*/
protected function open()
{
if ($this->channel) return;
$this->connection = new AMQPStreamConnection($this->host, $this->port, $this->user, $this->password);
$this->channel = $this->connection->channel();
$this->channel->queue_declare($this->queueName,true, true);
$this->channel->exchange_declare($this->exchangeName, $this->exchangeType, false, true, false);
$this->channel->queue_bind($this->queueName, $this->exchangeName,$this->routingKey);
}
/**
* 这里处理接收到的数据
*/
protected function handleMessage($message)
{
// $message = unserialize($message);
var_dump($message);
// do some thing ...
// \Yii::info($message,'fecshop_debug');
return true;
}
/*
public function actionListen3()
{
Yii::$app->queue->listen();
}
*/
}