Home > Article > Backend Development > How LaravelS speeds up Laravel/Lumen through Swoole
This article mainly introduces you to the relevant information about LaravelS accelerating Laravel/Lumen through Swoole. The article introduces it in detail through the example code. It has certain reference learning value for everyone's study or work. Friends who need it can come together. Study and study.
LaravelS - Standing on the shoulders of giants
This article mainly introduces the related content of LaravelS to accelerate Laravel/Lumen through Swoole. About:rocket: Accelerate Laravel/Lumen through Swoole, where the S stands for Swoole, speed, and high performance.
Features
High performance Swoole
Built-in Http server
Resident memory
Smooth restart
Supports both Laravel and Lumen, Compatible with mainstream versions
Simple, ready to use out of the box
If it helps you, Star Me LaravelS
Requirements
Dependencies | Description |
---|---|
PHP | >= 5.5.9 |
Swoole | >= 1.7.19 Recommend the latest stable version starting from 2.0.12 PHP5 |
Laravel / Lumen | >= 5.1 |
is no longer supported | zlib, check whether the local libz is available ldconfig -p|grep libz
Installation
# 在你的Laravel/Lumen项目的根目录下执行 composer require "hhxsv5/laravel-s:~1.0" -vvv # 确保你的composer.lock文件是在版本控制中2. Add service providerLaravel: Modify the file config/app.php
'providers' => [ //... Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class, ],Lumen: Modify the file bootstrap/app.php
$app->register(Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class);3. Publish the configuration file
php artisan laravels publishSpecial case: You do not need to manually load the configuration laravels.php, the bottom layer of LaravelS has been loaded automatically.
// 不必手动加载,但加载了也不会有问题 $app->configure('laravels');4. Modify the configuration config/laravels.php: monitoring IP, port, etc., please refer to Configuration Items.
Run
php artisan laravels {start|stop|restart|reload|publish}
Instructions | |
---|---|
Start LaravelS and display the list of started processes ps -ef|grep laravels | |
Stop LaravelS | |
Restart LaravelS | |
Restart all worker processes smoothly, These worker processes contain your business code and framework (Laravel/Lumen) code and will not restart the master/manger process | |
Publish the configuration file to your In the project, config/laravels.php |
Usually, you can reset or destroy some global or static variables in these events, or modify the current request and response.
// 修改`app/Providers/EventServiceProvider.php`, 添加下面监听代码到boot方法中 // 如果变量$exents不存在,你也可以调用\Event::listen()。 $events->listen('laravels.received_request', function (\Illuminate\Http\Request $req) { $req->query->set('get_key', 'hhxsv5');// 修改querystring $req->request->set('post_key', 'hhxsv5'); // 修改post body });laravels.generated_response After the Laravel kernel processes the request, Illuminate\Http\Response is converted into swoole_http_response (the next step will be to respond to the client).
$events->listen('laravels.generated_response', function (\Illuminate\Http\Request $req, \Symfony\Component\HttpFoundation\Response $rsp) { $rsp->headers->set('header-key', 'hhxsv5');// 修改header });Use swoole_http_server instance in your project
/** * @var \swoole_http_server */ $swoole = app('swoole');// Singleton var_dump($swoole->stats());Notes
Recommended through Illuminate\Http \Request object to obtain request information, compatible with $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_REQUEST, $_SESSION, $_ENV cannot be used.
public function form(\Illuminate\Http\Request $request) { $name = $request->input('name'); $all = $request->all(); $sessionId = $request->cookie('sessionId'); $photo = $request->file('photo'); $rawContent = $request->getContent(); //... }It is recommended to respond to the request by returning the Illuminate\Http\Response object, which is compatible with echo, vardump(), and print_r(). You cannot use functions like exit() and
die()、header()、setcookie()、http_response_code()。 public function json() { return response()->json(['time' => time()])->header('header1', 'value1')->withCookie('c1', 'v1'); }Globally declared by you , static variables must be manually cleaned or reset.
Infinitely appending elements to static or global variables will cause the memory to become full.
// 某类 class Test { public static $array = []; public static $string = ''; } // 某控制器 public function test(Request $req) { // 内存爆满 Test::$array[] = $req->input('param1'); Test::$string .= $req->input('param2'); }TODO
##Connection pool for MySQL/Redis.
Coroutine client that wraps MySQL/Redis/Http.
Automatic coroutine support for Swoole 2.1.
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!
How the Laravel framework implements the CURD operation of the model layer
The above is the detailed content of How LaravelS speeds up Laravel/Lumen through Swoole. For more information, please follow other related articles on the PHP Chinese website!