Home > Article > Backend Development > How to use message queue queue and asynchronous queue in PHP's Laravel framework, laravelqueue_PHP tutorial
queue configuration
First, let me explain how to use queue in my previous project.
Our current projects all use symfony, older projects use symfony1.4, and newer projects use symfony2. The overall feeling of using symfony is very pleasant, especially symfony2, which generally uses a lot of design ideas from Java frameworks. But it does not support queue. In symfony, we have also gone through several processes using queue. I first used Zhang Yan’s httpsqs. This one is simple to use, but has a single point. After all, our project is still officially for external services, so we studied ActiveMQ, an open source project under Apache, and found that there is a newer MQ under Apache, which is Apollo. In the end we decided to use Apollo.
The main application scenario of queue in our project is to asynchronously process some time-consuming functions, such as synchronizing third-party data, synchronously notifying our third-party data users of data changes, etc. Our general idea is this. If asynchronous processing is needed in each controller, just encode a json object and stuff it into Apollo. Write another work Command, parse the json object in this Command, and call different methods based on the actions and parameters inside. Running Command as a daemon process on different machines at the same time according to business needs can also be considered as a solution to implement asynchronous multi-tasking applications. I kept using it until I discovered laravel. Plan to research it. It's not impossible to replace it if possible. hehe.
Since I just started learning, of course I went straight to laravel5. Routes, controllers, and views are basically the same as symfony, so it’s not difficult to get started. Finally, study the queue.
1. Installing laravle and using composer is very simple.
composer global require "laravel/installer=~1.1" vi ~/.bash_profile
Add ~/.composer/vendor/bin to the environment variables.
source ~/.bash_profile
You can use laravel directly from the command line. Give it a try.
laravel -V
If you can see the following, it means success.
Laravel Installer version 1.2.1
2. Create a project.
laravel new guagua
3. Configure redis and queue.
4. Create controller,
php artisan make:controller DefaultController
Push 100 queue tasks in the controller's action.
for($i = 0; $i < 100; $i ++) { Queue::push(new SendEmail("ssss".$i)); }
5. Command to create queue
php artisan make:command SendEmail --queued
Modify app/Commands/SendEmail.php and add a private variable.
protected $msg;
Also modify the constructor.
public function __construct($msg) { $this->msg = $msg; }
Modified handle method
public function handle() { sleep(4); echo $this->msg."\t".date("Y-m-d H:i:s")."\n"; $this->delete(); }
6. Modify routes
Route::get('/', [ 'as' => 'index', 'uses' => 'DefaultController@index' ]);
7. Monitor queue
php artisan queue:listen
To verify multitasking, we opened three windows at the same time and ran the same command.
8. Start the service using laravel’s built-in server
php artisan serve --port 8080
Open the browser and visit the http://localhost:8080/ page. Of course, you can also use nginx, apache, etc. However, various configurations are required, and the built-in ones are easy to use.
You can see the execution status of each queue in the console, as shown below. You can see that 100 tasks are divided equally among three jobs.
At this point, I have basically achieved the effect I wanted. It is verified that laravel can easily implement queue and can handle multi-tasking.
use AppCommandsCommand is used in the code generated by make command, but when running, it prompts that there is no such file. The solution is to change it to use IlluminateConsoleCommand; I don’t know why this low-level problem occurs. Is it a problem with my mac system or a problem with my character?
When pushing the queue in the controller's action, there is no asynchronous execution, and it is still executed in the action's script. It was found that it was a configuration problem. It turned out that not only queue.php in config must be modified, but also related configurations in .evn must be modified. Although the problem has been solved, I still feel pain in my balls and cannot understand it. Still need to learn laravel.
How to use asynchronous queue
1. Configuration
The definition of queue will not be introduced here. There are two keys to using asynchronous queues:
(1) Where to store queues
(2) Services that perform tasks
Open config/queue.php, which is Laravel5’s queue configuration file. First, we can specify the default queue driver through the default parameter. The default configuration is sync, which is a synchronous queue. To make an asynchronous queue, we must first change this. Assuming we use database as the driver, the queue tasks will be stored in the database, and we will start another background service to process the queue tasks later. This is the asynchronous method.
'default' => 'database'
After modifying the configuration, we need to create a table to store the queue tasks. Laravel5 has a built-in instruction in the artisan command to generate data migration. It only requires two commands. Of course, you must implement the configuration. Database connection.
php artisan queue:table php artisan migrate
This automatically creates the jobs table in the database.
2. Start the queue listening service
通过下面这条指令启动队列监听服务,它会自动处理 jobs 表中的队列任务:
php artisan queue:listen
在linux中,如果想让它在后台执行,可以这样:
nohup php artisan queue:listen &
3.添加队列任务
关于队列任务的添加,手册里说的比较详细,这里就简单举个例子吧。
首先,通过artisan创建一个队列命令:
php artisan make:command SendEmail --queued
这样会生成 app/Commands/SendEmail.php 这个类文件,这个类会被标识为队列命令,你可以在 handle 方法中写自己的业务逻辑。
在控制器中,可以简单通过 Bus::dispatch 分发任务:
Bus::dispatch(new \App\Commands\SendEmail());
你会发现任务不会立即执行,而是被放到 jobs 表中,由队列监听服务处理。
更详细的用法建议参考 command bus 和 queue 相关的手册章节。