测试环境:ThinkPHP5.0
测试方法:
1、将下载的PHPamqplib文件放在extend目录下;
2、在控制器引入相应类
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
3、在控制器或者配置文件中定义相关参数
public static $amqp = array(
'name' => 'admin',
'password' => 'admin',//填写加密用的EncodingAESKey,
'exchange' => 'publish-topic',
'command'=>'admin',//主题口令
'mode' => 'topic'//方式
);
4、定义qmqp方法
/**
* [amqp 消息队列]
* @param [arr] $data [发送数据]
*
*/
public function amqp($data="aaa",$topic="hello")
{
$connection = new AMQPStreamConnection('39.107.122.245', 5672, Index::$amqp['name'], Index::$amqp['password'],'admin');
$channel = $connection->channel();
$channel->exchange_declare(Index::$amqp['exchange'], Index::$amqp['mode'], true, false, false);
$msg = new AMQPMessage($data);
$channel->basic_publish($msg, Index::$amqp['exchange'],$topic);
$channel->close();
$connection->close();
}
这里只简单演示消息发送;消息的消费可参考rabbit官网