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

配置queue(amqp)

'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

/**

* 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();

}

*/

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值