一、通过mysql数据库实现队列驱动
1、修改配置文件
.env配置文件修改
# mysql数据库
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=root
# 队列驱动
QUEUE_DRIVER=database
config配置文件修改 queue.php
'default' => env('QUEUE_CONNECTION', 'database'),
2、为了使用 database 队列驱动,你需要一张数据表来存储任务。运行 queue:table Artisan 命令来创建这张表的迁移文件。当迁移文件创建好后,你就可以使用 migrate 命令来进行迁移。这样会生成两张表 jobs表和failed_jobs表
php artisan queue:table
php artisan migrate
3、创建任务类,监听队列获取数据,处理消息
php artisan make:job SendEmail
<?php
namespace App\Jobs;
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;
class GoodsInfoToLog implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $goods;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($goods)
{
$this->goods = $goods;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$goods = $this->goods;
Log::info(['title' => $goods['title'], 'price' => $goods['price'],'type'=>'数据库']);
}
}
4、创建控制器,做队列消息推送使用
php artisan make:controller JobProcessController--resource
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use \App\Jobs\GoodsInfoToLog;
use phpDocumentor\Reflection\Types\Array_;
class JobProcessController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$chineseName = array('枯', '藤', '老', '树', '昏', '鸦', '小', '桥', '流', '水', '人', '家', '古', '道', '西', '风', '瘦', '马');
$name = $chineseName[rand(0, 17)] . $chineseName[rand(0, 17)] . $chineseName[rand(0, 17)];
$data = array(
'price' => rand(1, 1000),
'title' => $name,
);
GoodsInfoToLog::dispatch($data);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
配置路由执行脚本,可看到会生成数据到数据库表jobs中

f312d533d68d1819c1bfeab5345d9c4.png
5、任务执行
php artisan queue:work
也可以通过supervisor守护进程任务进行执行
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /www/wwwroot/html/artisan queue:work