LaravelS accelerates Laravel and Lumen through Swoole

  • 2021-09-12 00:34:37
  • OfStack

LaravelS-Standing on the Shoulders of Giants

This paper mainly introduces the related contents of LaravelS accelerating Laravel/Lumen through Swoole, about: rocket: accelerating Laravel/Lumen through Swoole, in which S stands for Swoole, speed and high performance.

Characteristic

High Performance Swoole Built-in Http server Memory resident Smooth restart Support Laravel and Lumen at the same time, compatible with mainstream versions Simple and ready to use out of the box

If it helps you, Star Me LaravelS

Requirements

依赖 说明
PHP >= 5.5.9
Swoole >= 1.7.19 推荐最新的稳定版 从2.0.12开始不再支持PHP5
Laravel / Lumen >= 5.1
Gzip[可选的] zlib , 检查本机libz是否可用 ldconfig -p|grep libz

Installation

1. Install via Composer (packagist)


#  In your Laravel/Lumen Execute under the root directory of the project 
composer require "hhxsv5/laravel-s:~1.0" -vvv
#  Make sure your composer.lock File is in version control 

2. Add service provider

Laravel: Modify file config/app. php


'providers' => [
 //...
 Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class,
],

Lumen: Modify file bootstrap/app. php


$app->register(Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class);

3. Publish configuration files


php artisan laravels publish

Special case: You do not need to manually load the configuration laravels. php, the bottom layer of LaravelS has been automatically loaded.


//  You don't have to load it manually, but you won't have a problem if you load it 
$app->configure('laravels');

4. Modify the configuration config/laravels. php: IP for listening, ports, etc. Refer to configuration items.

Run


php artisan laravels {start|stop|restart|reload|publish}

命令 说明
start 启动LaravelS,展示已启动的进程列表 ps -ef|grep laravels
stop 停止LaravelS
restart 重启LaravelS
reload 平滑重启所有worker进程,这些worker进程内包含你的业务代码和框架(Laravel/Lumen)代码,不会重启master/manger进程
publish 发布配置文件到你的项目中 config/laravels.php

Used in conjunction with Nginx


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 Handle static resources, LaravelS Handle dynamic resources. 
 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;
 }
}

Monitor events

Usually, you can reset or destroy 1 global or static variables in these events, and you can also modify the current request and response.

laravels. received_request After swoole_http_request is converted to Illuminate\ Http\ Request, before the Laravel kernel processes the request.


//  Modify `app/Providers/EventServiceProvider.php`,  Add the following listening code to boot Method 
//  If the variable $exents Does not exist, you can also call \Event::listen() . 
$events->listen('laravels.received_request', function (\Illuminate\Http\Request $req) {
 $req->query->set('get_key', 'hhxsv5');//  Modify querystring
 $req->request->set('post_key', 'hhxsv5'); //  Modify post body
});

laravels. generated_response After the Laravel kernel has processed the request, before converting Illuminate\ Http\ Response to swoole_http_response (the next step will 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');//  Modify header
});

Use the swoole_http_server instance in your project


/**
* @var \swoole_http_server
*/
$swoole = app('swoole');// Singleton
var_dump($swoole->stats());

Matters needing attention

It is recommended to obtain request information through Illuminate\ Http\ Request objects, which are compatible with $_ SERVER, $_ GET, $_ POST, $_ FILES, $_ COOKIE, $_ REQUEST, and cannot use $_ SESSION, $_ ENV.


'providers' => [
 //...
 Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class,
],
0

It is recommended to respond to requests by returning Illuminate\ Http\ Response objects, which are compatible with echo, vardump (), print_r (), and cannot use functions like exit (),


'providers' => [
 //...
 Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class,
],
1

The global and static variables you declare must be cleaned or reset manually.

Appending elements to static or global variables indefinitely will cause memory to be full.


'providers' => [
 //...
 Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class,
],
2

To-do items

Connection pooling for MySQL/Redis. Wrapping the MySQL/Redis/Http co-processing client. Automated co-processing support for Swoole 2.1 +.

Summarize


Related articles: