一户辞、通過mysql數(shù)據(jù)庫實現(xiàn)隊列驅(qū)動
1弹砚、修改配置文件
.env配置文件修改
# mysql數(shù)據(jù)庫
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=root
# 隊列驅(qū)動
QUEUE_DRIVER=database
config配置文件修改 queue.php
'default' => env('QUEUE_CONNECTION', 'database'),
2茅诱、為了使用 database 隊列驅(qū)動契邀,你需要一張數(shù)據(jù)表來存儲任務(wù)。運行 queue:table Artisan 命令來創(chuàng)建這張表的遷移文件。當遷移文件創(chuàng)建好后述暂,你就可以使用 migrate 命令來進行遷移疼蛾。這樣會生成兩張表 jobs表和failed_jobs表
php artisan queue:table
php artisan migrate
3皮钠、創(chuàng)建任務(wù)類赠法,監(jiān)聽隊列獲取數(shù)據(jù)麦轰,處理消息
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'=>'數(shù)據(jù)庫']);
}
}
4、創(chuàng)建控制器,做隊列消息推送使用
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)
{
//
}
}
配置路由執(zhí)行腳本款侵,可看到會生成數(shù)據(jù)到數(shù)據(jù)庫表jobs中
f312d533d68d1819c1bfeab5345d9c4.png
5末荐、任務(wù)執(zhí)行
php artisan queue:work
也可以通過supervisor守護進程任務(wù)進行執(zhí)行
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /www/wwwroot/html/artisan queue:work