PHP中如何使用MQ消息队列
这期内容当中小编将会给大家带来有关PHP中如何使用MQ消息队列,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
10年积累的成都网站建设、做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站设计后付款的网站建设流程,更有襄阳免费网站建设让你可以放心的选择与我们合作。
1.安装php-amqplib 服务
在composer.json配置
{"require": {"php-amqplib/php-amqplib": ">=2.6.1"}}
2.执行composer.phar install 来安装
3.引入mq文件
define('EXTEND_PATH', '../vendor/autoload.php');use app\synchronous\model\RabbitMqModel;use PhpAmqpLib\Connection\AMQPStreamConnection;
4.发送到队列数据代码
/*** MQ生产数据* @param $queueName 队列名称* @param $msg 发送数据* @name MqPushLish* @author * @return*/public function MqPublish($queueName , $msg = []){try{if(empty($queueName))return false;//获取mq配置$mqConfig = $this->getConfig();//创建连接和channel$connection = new AMQPStreamConnection($mqConfig['host'] , $mqConfig['port'] , $mqConfig['user'] , $mqConfig['password']);$channel = $connection->channel();$name = $queueName;$type = "direct";$passive = false;$durable = true;$auto_delete = true;$channel->exchange_declare($name, $type, $passive, $durable, $auto_delete);$message = new AMQPMessage('[{"data_id":184981,"complex_id":7821,"area":"bj","dj":0,"mj":84,"shi":"2室","ting":"2厅","wei":"1卫","chu":"1厨","cate_status":"4","thumb":"http://img.zgsta.com/1-2-1-1-9057/11cf99e0a95cfc2a10a06af7e5a5f367_addfinger.png","tujis":["http://img.zgsta.com/1-2-1-1-9057/11cf99e0a95cfc2a10a06af7e5a5f367_addfinger.png"],"create_time":1512937831,"update_time":1533907683,"house_toward":"南北","reference_totalprice":0,"reference_down_payment":""},{"data_id":184981,"complex_id":9057,"area":"sh","dj":0,"mj":84,"shi":"2室","ting":"2厅","wei":"1卫","chu":"1厨","cate_status":"4","thumb":"http://img.zgsta.com/1-2-1-1-9057/11cf99e0a95cfc2a10a06af7e5a5f367_addfinger.png","tujis":["http://img.zgsta.com/1-2-1-1-9057/11cf99e0a95cfc2a10a06af7e5a5f367_addfinger.png"],"create_time":1512937831,"update_time":1533907683,"house_toward":"南北","reference_totalprice":0,"reference_down_payment":""}]');$channel->basic_publish($message,'', $queueName);//发送数据到MQ$channel->close();$connection->close();//打印日志DeShangLog::log(0, $msg ,$queueName.'MQ发送队列数据正常');return true;}catch (\Exception $e){//打印日志DeShangLog::log(1, $e->getMessage() ,'MQ发送队列数据异常!!!!!!!!!!!!!!!!');return false;}}
5.消费队列数据代码
/*** MQ消费数据 监视数据* @param $queueName 队列名称* @name MqConsumer* @author Admin* @return*/public function mqConsumer(){try{$queueName = $this->getx('queue' , 'complex_info_test');if(empty($queueName)){echo "not queue ";die;}//创建连接和channel$connection = new AMQPStreamConnection(C('config_mq.host') , C('config_mq.port') , C('config_mq.user') , C('config_mq.password'));$channel = $connection->channel();$channel->queue_declare($queueName, false, true, false, false);echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";$callback = function($msg) {//打印日志,记录消费的数据DeShangLog::log(0, $msg->body ,'MQ接收户型同步数据');$returnData = json_decode($msg->body , true);//调用插入户型数据$this->addDoorList($returnData);echo " [x] Received ", $msg->body, "\n";};$channel->basic_consume($queueName, '', false, false, false, false, $callback);//消费出数据while(count($channel->callbacks)) {$channel->wait();}$channel->close();$connection->close();}catch (\Exception $e){//打印日志DeShangLog::log(1, $e->getMessage() ,'MQ发送队列数据异常!!!!!!!!!!!!!!!!');return false;}}
6.在linux 配置守护进程
命令:``` nohup php index.php /synchronous/synchronous/mqconsumer & ``` “&” 代表不间断运行 在/etc/rc.local文件中,将 ``` nohup php index.php /synchronous/synchronous/mqconsumer ```这个命令加入即可。ps:注意路径
上述就是小编为大家分享的PHP中如何使用MQ消息队列了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
本文题目:PHP中如何使用MQ消息队列
文章位置:http://hbruida.cn/article/jdciis.html