ps:准备条件,需要学会使用消息队列,我之前的文章有写过,不懂的可以去看下
1、创建测试的控制器
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020/3/30
* Time: 22:08
*/
namespace App\Http\Controllers;
use App\Jobs\Notice;
use App\User;
class HomeController extends Controller
{
public function store(){
$user = User::query()->first();
Notice::dispatch($user)->delay(now()->addSeconds(3));
dd('文章发布成功');
}
}
2、创建任务类
<?php
namespace App\Jobs;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class Notice implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$user = $this->user->first();
$content = $user->name . "发表了新文章" . time() . "\n";
file_put_contents('./notice.txt', $content, FILE_APPEND);
}
}
3、cmd命令进入apache的bin目录(或者nginx的执行目录)
4、发起1200个请求,并发量为200,后面为请求地址
ab -n 1200 -c 200 http://www.lredis.top/
5、最后可看到,文件如下: