最近項目需求,研究了laravel的異步隊列。官方文檔雖然很是詳細,但也有些晦澀難懂,在此記錄下步驟,供大家參考。
1、修改/config/queue.php文件

<?php return [ /* |-------------------------------------------------------------------------- | Default Queue Connection Name |-------------------------------------------------------------------------- | | Laravel's queue API supports an assortment of back-ends via a single | API, giving you convenient access to each back-end using the same | syntax for every one. Here you may define a default connection. | */ 'default' => env('QUEUE_CONNECTION', 'sync'), /* |-------------------------------------------------------------------------- | Queue Connections |-------------------------------------------------------------------------- | | Here you may configure the connection information for each server that | is used by your application. A default configuration has been added | for each back-end shipped with Laravel. You are free to add more. | | Drivers: "sync", "database", "beanstalkd", "sqs", "redis", "null" | */ 'connections' => [ 'sync' => [ 'driver' => 'sync', ], 'database' => [ 'driver' => 'database', 'table' => 'jobs', 'queue' => 'default', 'retry_after' => 90, ], 'beanstalkd' => [ 'driver' => 'beanstalkd', 'host' => 'localhost', 'queue' => 'default', 'retry_after' => 90, 'block_for' => 0, ], 'sqs' => [ 'driver' => 'sqs', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'), 'queue' => env('SQS_QUEUE', 'your-queue-name'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), ], 'redis' => [ 'driver' => 'redis', 'connection' => 'default', 'queue' => env('REDIS_QUEUE', 'default'), 'retry_after' => 90, 'block_for' => null, ], ], /* |-------------------------------------------------------------------------- | Failed Queue Jobs |-------------------------------------------------------------------------- | | These options configure the behavior of failed queue job logging so you | can control which database and table are used to store the jobs that | have failed. You may change them to any database / table you wish. | */ 'failed' => [ 'database' => env('DB_CONNECTION', 'mysql'), 'table' => 'failed_jobs', ], ];
注意:修改.env文件如下參數,設置隊列連接默認為數據庫連接
QUEUE_CONNECTION=database
2、新建/app/Job/EmailJob.php,此文件為隊列主文件

<?php namespace App\Job; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; use App\Service\EmailService; class EmailJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; private $content,$to; public function __construct($content,$to){ $this->content=$content; $this->to=$to; } public function handle(){ $res=false; $times=0; while($res!==true && $times<3){ try{ $times++; $res=EmailService::send($this->content,$this->to); }catch (\Exception $e){ Log::error(date('Y-m-d h:i:s',time()).' send email error:'.$e->getMessage()); } } if($res===true){ Log::info(date('Y-m-d h:i:s',time()).' send email success:'); } } }
3、新建/app/Service/EmailJobService.php服務,此文件為封裝服務文件,可以不用,直接在使用的地方調用隊列。

<?php namespace App\Service; use App\Job\EmailJob; class EmailJobService { public static function add($content,$to){ $job=new EmailJob($content,$to); dispatch($job); } }
4、打開終端切換目錄進入Laravel項目根目錄,執行如下命令,創建隊列任務需要的數據表。
php artisan queue:table php artisan queue:failed-table php artisan migrate
5、通過下面這條指令啟動隊列監聽服務,它會自動處理 jobs 表中的隊列任務。
php artisan queue:listen
監聽指定隊列:
php artisan queue:work --queue=default,mytask --tries=2
這是監聽 default和mytask兩個隊列,區分先后順序。
6、如果需要在linux中后台運行,有兩種方法:
6.1 執行如下命令:
nohup php artisan queue:listen > /tmp/artisan.log 2>&1 &
6.2.1 安裝Supervisor,我的服務器系統為CentOs7.5,所以使用yum安裝。
yum install supervisor
6.2.2 在/etc/supervisord.d下新建ini文件,eg:laraver-worker.ini,設置自動運行命令等相關參數

[program:laravel-worker] process_name=%(program_name)s_%(process_num)02d command=php 這里需要寫項目目錄/artisan queue:work --sleep=3 --tries=3 autostart=true autorestart=true user=root numprocs=8 stdout_logfile=/root/queue/daily_english_queue.log
6.2.3 啟動supervisor,laravel隊列監聽進程便在后台運行了。
supervisord -c /etc/supervisord.conf
6.2.4 配置supervisor開機啟動(否則服務器重啟后必須手動啟動supervisor)
cd /usr/lib/systemd/system/ //切換目錄 touch supervisord.service //新建文件 vim supervisord.service //編輯文件
文件內容:
[Unit] Description=Supervisor daemon [Service] Type=forking ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf ExecStop=/usr/bin/supervisorctl shutdown ExecReload=/usr/bin/supervisorctl reload KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
設置開機啟動
systemctl enable supervisord
驗證是否設置成功
systemctl is-enabled supervisord
7、注意:如果修改了job內的代碼(包括job調用的方法類),需要重啟queue。
php artisan queue:restart
Enjoy it !