I. implementation and application of PHP message queue
Advantages and disadvantages of message queuing
III. configuration and preparation of message queue
The cases are as follows:
The following are the team list fields and properties
IV. Mysql order queue
1. Accept the order and write the data order.php
<?php //This file is used to accept the order information of the user and write it to the queue //Import database link class include '../include/db.php'; //When receiving the mobile phone number of the user is not empty if(!empty($_GET['mobile'])){ //Here is the processing flow of the order center //Because the order system is a separate process that needs to be processed here //... it's not written here alone //Filter the data submitted by users to prevent sql injection //Assume order number directly $order_id = rand(10000,99999); //Generate order information $insert_data = array( 'order_id'=>$order_id, 'mobile'=>$_GET['mobile'], 'created_at'=>date('Y-m-d H:i:s',time()), 'status'=>0,//0 Unprocessed 1 processed 2 processing ); //Store order information in team list $db = DB::getIntance();//Here is the operation database. Note that different frameworks have corresponding built-in operation methods $res = $db->insert('order_queue',$insert_data);//insert($table,$data) if($res){ echo 'Insert success'; }else{ echo 'Insert failure'; } }
2. The queue system processes the orders in the queue and identifies goods.php
<?php //This file is mainly a file that the distribution system processes and marks the orders in the queue //Load database operation class include '../include/db.php'; //Initialization $db = DB::getIntance(); //1: First, update the records to be processed to wait for processing (here, lock the order to prevent it from being used before the processing is completed) $waiting = array('status'=>0);//Data waiting to be processed $lock = array('status'=>2);//Locked data $res_lock = $db->update('order_queue',$lock,$waiting,2);//update($table,$data,$where,$limit=0) //2: select the data just updated, and then process the distribution system if($res_lock){ //Select the content of the order to be processed $res = $db->selectAll('order_queue',$lock);// //Then the distribution system will handle the distribution //... / / this is the distribution system //3: update the processed program to completed $success = array( 'status'=>1,//Completed 'updated_at'=>date('Y-m-d H:i:s',time()), ); $res_last = $db->update('order_queue',$success,$lock); if($res_last){ echo 'Success'.$res_last; }else{ echo 'Fail'.$res_last; } }else{ echo 'ALL Finished'; }
3. Scheduled tasks. Execute goods.sh once a minute to understand the shell foundation
#!/bin/bash date "+%G-%m-%d %H:%M:S" cd /home/html/ssggw/queue_mysql/ php goods.php
4. Schedule task crontab-e under server deployment
For the convenience of viewing the results, output the timing task results to the log file and set it as standard output. The command is as follows
Execute command to save file: wq
New log file command
touch /home/html/ssggw/log.log
Monitoring log file
Two pieces of data in the database have been successful
Four pieces of data in the database have been successful
End of decoupling case
Five, Redis
Vi. Redis queue achieves second kill
1. Create a redis queue
2. The program user.php that accepts the user's request
<?php //Load first redis assembly $redis = new Redis(); $redis->connect('127.0.0.1',6379); $redis_name = "miaosha"; //Accept users id $uid = $_GET['uid']; //Get down redis Quantity already in it $num = 10;//Top 10 seconds //If the number of people in the day is less than 10, join the queue if($redis->lLen($redis_name)<10){ $redis->rFush($redis_name,$uid.'%'.microtime());//Users uid(Connect a subtle) join $fredis_name The end of the queue echo "Spike kill"; }else{ //If the number of people on the day has reached 10, the second kill is completed echo "The second kill is over"; } //turn off redis link $redis->close();
3. The handler, that is, the report data is written to the data table savetodb.php corresponding to mysql
<?php //Introduce operation database class include '../include/db.php'; //Load redis assembly $redis = new Redis(); $redis->connect('127.0.0.1',6379); $redis_name = "miaosha"; $db = DB::getlntance(); //Dead cycle while(1){ //Take a value from the far left of the queue $user = $redis->IPop($redis_name); //Then determine whether the value exists if(!$user || $user =='nil'){ //Reduce the pressure on the server. The real time to execute the second kill every 1 second is very short. No need to set it sleep(1); //Jump out of circulation continue; } //Cut out time, uid $user_arr = explode('%',$user); //Define an array $insert_data = array( 'uid'=>$user_arr[0], 'time_stamp'=>$user_arr[1], ); //Save to database $res = $db->insert('redis_queue',$insert_data); //Rollback mechanism in case of database insertion failure if(!$res){ $redis->rPush($redis_name,$user); } } //Release it. redis $redis->close();
test
Access the user.php file url
Result
Database table status