How LaravelS speeds up Laravel/Lumen through Swoole

不言
Release: 2023-04-01 17:14:01
Original
2432 people have browsed it

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

##Gzip[optional] zlib, check whether the local libz is available ldconfig -p|grep libz
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

Installation

1. Install through Composer (packagist)

# 在你的Laravel/Lumen项目的根目录下执行 composer require "hhxsv5/laravel-s:~1.0" -vvv # 确保你的composer.lock文件是在版本控制中
Copy after login

2. Add service provider

Laravel: Modify the file config/app.php

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

Lumen: Modify the file bootstrap/app.php

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

3. Publish the configuration file

php artisan laravels publish
Copy after login

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

// 不必手动加载,但加载了也不会有问题 $app->configure('laravels');
Copy after login

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}
Copy after login

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; } }
Copy after login
##Command start stop restart reload publish ## is used with Nginx
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

Listening events

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

laravels.received_request After converting swoole_http_request into Illuminate\Http\Request, before the Laravel kernel processes the request.

// 修改`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 });
Copy after login

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 });
Copy after login

Use swoole_http_server instance in your project

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

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(); //... }
Copy after login

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'); }
Copy after login

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'); }
Copy after login

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!

  • Related recommendations:

How to use the laravel framework to implement the search function

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!