This half month, I was involved in rewriting a micro-credit back-end system, using Laravel 4.2 for the first time, and Laravel's proud queue service.
Because the whole system involves multi-terminal interaction, and a large number of voice transmission, processing business, we found in some places too long response time. Before the system based on Node.js and MongoDB, because node is inherently asynchronous, there is a daemon, so there is no such problem, and this rewrite is bound to introduce asynchronous process. The queue entered our sight.
Based on this page is almost entirely English "Chinese document", Laravel just in the 4.2 version of the introduction of Redis as a queue storage, this is a very good news. OK, Background Introduction here, the following dry goods.
The queue services in Laravel are no different from other queuing services. Are the simplest and most common processes that best fit the human mind: there is a place to store queue information, a PHP process to write tasks at run time, and another PHP daemon to poll queue information to execute and delete tasks that meet the requirements. Since PHP is a URL-driven synchronization language and is itself blocked, it is not surprising that Laravel provides a daemon tool to query and execute queue information.
The Laravel queue profile is/app/config/queue.php, and in the Default queue Driver, you can choose "Sync", "BEANSTALKD", "SQS", "Iron", "Redis" Five types of drives.
1. Sync is a sync drive for local debugging
2. BEANSTALKD is a professional queue service driver: http://kr.github.io/beanstalkd/
3. Sqs and iron are foreign third party queue services
4. The last Redis gave us a reason to use Redis, so we migrated the caching service and the session service to the Redis.
0. By the way, session drive do not use MySQL, processing time 1S is not a dream, hey, look who, said is you, 1S brother!
The queue service needs to specialize in creating new task classes, and as a standalone class, they do not need to inherit classes because the tasks in the queue are invoked by the PHP daemon when they are executed, and there is no error if you want to use a different class to invoke it. I've put a lot of extra services into a separate folder/app/services, such as the input information validation validator, the Special Security verification module, and so on, this is where the queue categories are located.
The use of the queue is very simple, and here's a simple example:
Copy Code code as follows:
Use Queue;
Queue::p ush (' Curljsonqueue ', [
' URL ' => $url,
' JSON ' => $json
]);
This is a standard queue push process. Of course, here I put the Curljsonqueue class in the Services root directory, which has been registered by me to the "Classmap" of the Composer.json "AutoLoad", which is in the top-level namespace and can be called directly, You can write App\ooxx if you need to invoke a non-top-level namespace. Our system needs a lot of interaction with the micro-trust server, so it's independent of this class.
Copy Code code as follows:
<?php
Class Curljsonqueue extends basecontroller{
Public function Fire ($job, $data)
{
$url = $data [' url '];
$json = $data [' json '];
Parent::base_post_curl ($url, $json);
$job->delete ();
}
}
The default method for this class is Fire (), which is also a fixed two $job and $data, because I encapsulated the Curl module of the post in Basecontroller, so I called it. In addition there is also a small pit, when writing base_post_curl () when the use of protected, resulting in a basecontroller invalid, must inherit.
By executing the code above, a new task is placed in the queue, laravel the daemon by using the following command:
Copy Code code as follows:
The daemon then begins to process the queue. The path to the PHP command and artisan file in this code is adjusted yourself.
As you may notice, we are using the Redis and PHP command line, if in the test environment, add a start-up or even manual boot can, but in the production environment requires a more solid tool to protect these two programs, we use the supervisor, About the installation configuration of supervisor You can refer to this article: http://blog.segmentfault.com/qianfeng/1190000000532561 note, the article has a small pit to step on your own ...
OK, after all the configuration, run up redis and PHP command line, the whole system began to run happily ~
Usage feeling:
Queue Service Super Good, the previous and app interaction process needs 6-7s, asynchronous later reduced to 2S, basically is the transmission time and PHP code run time, time-consuming special operation has been asynchronous. However, the queue service default 1S open a process to check if there is any service in the Redis, on the Aliyun server, about 10% of the single core, the consumption is slightly larger, and the queue processing time is relatively long, because there is no previous synchronization of the file loading benefits. However, if there are multiple tasks, the PHP process will be executed continuously, not 1S to execute one.
Let's talk about the pit:
1. Because the queue core class uses a special function, a variable with no definite type is stored in the form of a unit of prime number in JSON and then stored in the Redis. The solution is to add the words "in front of each data to be put in." 。 The above $url and $json are not doing this because they have been declared in quotes before.
2. If you want to pass the URL to the queue, the system queue class adds two \ \ to each/front. This can have a fatal effect on some special operations. (joking, is that fatal on top!) )