您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關PHP中如何使用MQ消息隊列,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
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消息隊列了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。