您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php的amqp应该怎样使用?

2024/3/31 23:55:16发布29次查看
我安装了rabbitmq和php的amqp扩展,但是不知道怎么使用,我在网上搜了教程,但是不是太懂,
这是send.php文件,
$conf=[ 'host'=>'localhost', 'port'=>'5672', 'login'=>'guest', 'password'=>'guest', 'vhost'=>'/'];$connect=new \amqpconnection($conf);if(!$connect->connect()){ die('connetc error');}$message = test message! 测试消息!;$channel=new amqpchannel($connect);$exchange=new amqpexchange($channel);$exchange->setname('zblexchange');$queue = new amqpqueue($channel);$queue->setname('zblqueue');for($i=0; $ipublish($message, 'zblqueue').\n;}//$channel->committransaction(); //提交事务$connect->disconnect();

这是receive.php文件
$conf=[ 'host'=>'localhost', 'port'=>'5672', 'login'=>'guest', 'password'=>'guest', 'vhost'=>'/'];$connect=new amqpconnection($conf);if(!$connect->connect()){ die('connetc error');}$channel=new amqpchannel($connect);$exchange=new amqpexchange($channel);$exchange->setname('zblexchange');$exchange->settype(amqp_ex_type_direct);//echo exchange status:.$exchange->declare().\n;$queue = new amqpqueue($channel);$queue->setname('zblqueue');$queue->setflags(amqp_durable); //持久化echo message total:.$queue->declare().\n;echo 'queue bind: '.$queue->bind('zblexchange', 'zblqueue').\n;echo message:\n;//while(true){ $queue->consume('processmessage'); //echo'hello'; //$q->consume('processmessage', amqp_autoack); //自动ack应答//}$connect->disconnect();function processmessage($envelope, $queue) { $msg = $envelope->getbody(); echo $msg.\n; //处理消息 $queue->ack($envelope->getdeliverytag()); //手动发送ack应答}

使用浏览器先访问send.php没什么问题,再访问receive.php是504 gateway timeout,使用命令行访问send.php的时候出现致命错误amqpconnection类找不到,我不知道是怎么回事,使用php -m的时候找不到安装的amqp扩展,但是使用phpinfo通过浏览器访问的时候显示的是amqp是加载的,使用环境是centos 7,nginx 1.6.3,php 5.6.9,请问我这里到底是哪里出了问题?
回复内容: 我安装了rabbitmq和php的amqp扩展,但是不知道怎么使用,我在网上搜了教程,但是不是太懂,
这是send.php文件,
$conf=[ 'host'=>'localhost', 'port'=>'5672', 'login'=>'guest', 'password'=>'guest', 'vhost'=>'/'];$connect=new \amqpconnection($conf);if(!$connect->connect()){ die('connetc error');}$message = test message! 测试消息!;$channel=new amqpchannel($connect);$exchange=new amqpexchange($channel);$exchange->setname('zblexchange');$queue = new amqpqueue($channel);$queue->setname('zblqueue');for($i=0; $ipublish($message, 'zblqueue').\n;}//$channel->committransaction(); //提交事务$connect->disconnect();

这是receive.php文件
$conf=[ 'host'=>'localhost', 'port'=>'5672', 'login'=>'guest', 'password'=>'guest', 'vhost'=>'/'];$connect=new amqpconnection($conf);if(!$connect->connect()){ die('connetc error');}$channel=new amqpchannel($connect);$exchange=new amqpexchange($channel);$exchange->setname('zblexchange');$exchange->settype(amqp_ex_type_direct);//echo exchange status:.$exchange->declare().\n;$queue = new amqpqueue($channel);$queue->setname('zblqueue');$queue->setflags(amqp_durable); //持久化echo message total:.$queue->declare().\n;echo 'queue bind: '.$queue->bind('zblexchange', 'zblqueue').\n;echo message:\n;//while(true){ $queue->consume('processmessage'); //echo'hello'; //$q->consume('processmessage', amqp_autoack); //自动ack应答//}$connect->disconnect();function processmessage($envelope, $queue) { $msg = $envelope->getbody(); echo $msg.\n; //处理消息 $queue->ack($envelope->getdeliverytag()); //手动发送ack应答}

使用浏览器先访问send.php没什么问题,再访问receive.php是504 gateway timeout,使用命令行访问send.php的时候出现致命错误amqpconnection类找不到,我不知道是怎么回事,使用php -m的时候找不到安装的amqp扩展,但是使用phpinfo通过浏览器访问的时候显示的是amqp是加载的,使用环境是centos 7,nginx 1.6.3,php 5.6.9,请问我这里到底是哪里出了问题?
使用php -m的时候找不到安装的amqp扩展,但是使用phpinfo通过浏览器访问的时候显示的
这是因为amqp的扩展只在fpm中加载了, 而cli脚本方式中没有加载, 配置一下就好了, 至于504, 我觉得很可能是你fpm没有配置好
请参考: http://segmentfault.com/a/1190000002963223
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product