In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to use MQ message queue in PHP. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
1. Install the php-amqplib service
Configure in composer.json
{"require": {"php-amqplib/php-amqplib": "> = 2.6.1"}}
two。 Execute composer.phar install to install
3. Import mq file
Define ('EXTEND_PATH','.. / vendor/autoload.php'); use app\ synchronous\ model\ RabbitMqModel;use PhpAmqpLib\ Connection\ AMQPStreamConnection
4. Send to queue data code
/ * MQ production data * @ param $queueName queue name * @ param $msg send data * @ name MqPushLish* @ author * @ return*/public function MqPublish ($queueName, $msg = []) {try {if (empty ($queueName)) return false;// gets mq configuration $mqConfig = $this- > getConfig (); / / create connections and 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 rooms", "ting": "2 rooms", "wei": "1 bathroom", "chu": "1 kitchen", "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":" North and South", "reference_totalprice": 0, "reference_down_payment": "}, {" data_id ": 184981," complex_id ": 9057," area ":" sh "," dj ": 0," mj ": 84," shi ":" Room 2 " "ting": "Hall 2", "wei": "1Wei", "chu": "1Chef", "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":" North and South" "reference_totalprice": 0, "reference_down_payment": ""}]') $channel- > basic_publish ($message,'', $queueName); / / send data to MQ$channel- > close (); $connection- > close (); / / print log DeShangLog::log (0, $msg, $queueName.'MQ send queue data is normal'); return true;} catch (\ Exception $e) {/ / print log DeShangLog::log (1, $e-> getMessage (),'MQ send queue data exception!') ; return false;}}
5. Consumption queue data code
/ * * MQ Consumer data Monitoring data * @ param $queueName queue name * @ name MqConsumer* @ author Admin* @ return*/public function mqConsumer () {try {$queueName = $this- > getx ('queue',' complex_info_test'); if (empty ($queueName)) {echo "not queue"; die } / / create a connection and 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) {/ / print log, record consumption data DeShangLog::log (0, $msg- > body,'MQ receiving Household synchronization data'); $returnData = json_decode ($msg- > body, true); / / call insert Household data $this- > addDoorList ($returnData); echo "[x] Received", $msg- > body, "\ n";}; $channel- > basic_consume ($queueName,'', false, $callback) / / consume data while (count ($channel- > callbacks)) {$channel- > wait ();} $channel- > close (); $connection- > close ();} catch (\ Exception $e) {/ / print log DeShangLog::log (1, $e-> getMessage (),'MQ send queue data exception!') ; return false;}} 6. Configure daemons in linux
Command: ```nohup php index.php / synchronous/synchronous/mqconsumer & ````"&" means to run continuously in the / etc/rc.local file. Just add the command ```nohup php index.php / synchronous/synchronous/mqconsumer ```. Ps: note that the path above is how to use MQ message queues in the PHP shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.