1. First install the redis extension package
composer require "predis/predis:~1.0"
2. Configure redis parameters and modify queue drivers
.env file QUEUE_DRIVER=redis
3. config/database.php configures the connection parameters of redis
'say again' => [
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'port' => 6379,
'database' => 0,
'password' => env('REDIS_PASSWORD', '123456')
],
],
4. Create a task class in appJobs and execute the handle method when the queue processes the task .
<?php
namespace App\Jobs;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
class Queue extends Job implements SelfHandling,ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/**
* 用户id
* @var int
*/
protected $customer_id;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($customer_id)
{
$this->customer_id = $customer_id;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//Specific business
}
}
5. Push tasks to the queue
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Jobs\Queue;
class IndexController extends Controller
{
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
}
protected function index()
{
$job = (new Queue(543574))->delay(60);//Delay 60s execution
$this->dispatch($job);
}
}
6. Run queue monitoring
( 1 ) Application server monitoring
If there is a php artisan config:cache cache configuration before, it is best to clear the configuration cache first: php artisan config:clear .
During the test, you can use ==php artisan queue:listen== to monitor.
Online can use ==php artisan queue:work --daemon==
( 2 ) The redis server listens
After redis-cli connects to redis , you can use ==monitor== to view the queue status.
Reference https://segmentfault.com/a/1190000011787353