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文件是在版本控制中
'providers' => [ //... Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class, ],
$app->register(Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class);
php artisan laravels publish
// 不必手动加载,但加载了也不会有问题 $app->configure('laravels');
Run
php artisan laravels {start|stop|restart|reload|publish}
upstream laravels { server 192.168.0.1:5200 weight=5 max_fails=3 fail_timeout=30s; #server 192.168.0.2:5200 weight=3 max_fails=3 fail_timeout=30s; #server 192.168.0.3:5200 backup; } server { listen 80; server_name laravels.com; root /xxxpath/laravel-s-test/public; access_log /yyypath/log/nginx/$server_name.access.log main; autoindex off; index index.html index.htm; # Nginx处理静态资源,LaravelS处理动态资源。 location / { try_files $uri @laravels; } location @laravels { proxy_http_version 1.1; # proxy_connect_timeout 60s; # proxy_send_timeout 60s; # proxy_read_timeout 120s; proxy_set_header Connection "keep-alive"; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://laravels; } }
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 });
$events->listen('laravels.generated_response', function (\Illuminate\Http\Request $req, \Symfony\Component\HttpFoundation\Response $rsp) { $rsp->headers->set('header-key', 'hhxsv5');// 修改header });
/** * @var \swoole_http_server */ $swoole = app('swoole');// Singleton var_dump($swoole->stats());
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(); //... }
die()、header()、setcookie()、http_response_code()。 public function json() { return response()->json(['time' => time()])->header('header1', 'value1')->withCookie('c1', 'v1'); }
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'); }
##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!