LaravelS uses Swoole to accelerate Laravel/Lumen.

Source: Internet
Author: User

LaravelS uses Swoole to accelerate Laravel/Lumen.

LaravelS-standing on the shoulders of giants

This article describes how LaravelS uses Swoole to accelerate Laravel/Lumen. For details, refer to: rocket: Uses Swoole to accelerate Laravel/Lumen. S represents Swoole, with high speed and performance.

Features

  • High-performance Swoole
  • Built-in Http Server
  • Resident memory
  • Smooth restart
  • Supports Laravel and Lumen, and is compatible with mainstream versions.
  • Simple and out-of-the-box

If it is helpful to you, Star Me LaravelS

Requirements

Dependency Description
PHP > = 5.5.9
Swoole > = 1.7.19 the latest stable version is recommended. PHP5 is no longer supported since 2.0.12.
Laravel/Lumen & Gt; = 5.1
Gzip [Optional] Zlib, check whether the local libz is available ldconfig-p | grep libz

Install

1. Install packagist through Composer)

# Run composer require "hhxsv5/Laravel-s: ~ in the root directory of your laravel/Lumen Project :~ 1.0 "-vvv # Make sure that your composer. lock file is in Version Control

2. Add service provider

Laravel: Modify the config/app. php file.

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

Lumen: Modify the bootstrap/app. php file.

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

3. Publish the configuration file

php artisan laravels publish

Special case: you do not need to manually load and configure laravels. php. LaravelS is automatically loaded at the underlying layer.

// You do not need to manually load the file, but it does not have to be loaded. $ app-> configure ('laravels ');

4. Modify the config/laravels. php: the IP address and port of the listener. For more information, see configuration items.

Run

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

Command Description
Start Start LaravelS to display the list of started processes ps-ef | grep laravels
Stop Stop LaravelS
Restart Restart LaravelS
Reload Smoothly restart all worker processes. These worker Processes contain your business code and framework (Laravel/Lumen) Code and do not restart the master/manger process.
Publish Publish the configuration file to your project config/laravels. php

Use with Nginx

Upstream laravels {server 192.168.0.1: 5200 weight = 5 max_fails = 3 fail_timeout = 30 s; # server 192.168.0.2: 5200 weight = 3 max_fails = 3 fail_timeout = 30 s; # 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 processes static resources and LaravelS processes dynamic resources. Location/{try_files $ uri @ laravels;} location @ laravels {proxy_http_version 1.1; # proxy_connect_timeout 60 s; # limit 60 s; # proxy_read_timeout 120 s; proxy_set_header Connection "keep-alive "; proxy_set_header X-Real-IP $ remote_addr; proxy_set_header Host $ host; proxy_pass http: // laravels ;}}

Listen to events

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

Laravels. received_request converts swoole_http_request to Illuminate \ Http \ Request, before the Laravel kernel processes the Request.

// Modify 'app/Providers/EventServiceProvider. php, add the following listening code to the 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 processes the request, convert Illuminate \ Http \ Response to swoole_http_response (the next step is to send the Response 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 swoole_http_server instance in your project

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

Notes

We recommend that you use the Illuminate \ Http \ Request object to obtain REQUEST information. It is compatible with $ _ SERVER, $ _ GET, $ _ POST, $ _ FILES, $ _ COOKIE, and $ _ Request, $ _ SESSION and $ _ 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 return the Illuminate \ Http \ Response object to respond to the request. It is compatible with echo, vardump (), print_r (), and cannot use functions such as exit (),

die()、header()、setcookie()、http_response_code()。public function json(){ return response()->json(['time' => time()])->header('header1', 'value1')->withCookie('c1', 'v1');}

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

If you append an element to a static or global variable infinitely, the memory is full.

// Class Test {public static $ array = []; public static $ string = '';} // a controller public function test (Request $ req) {// memory full Test: $ array [] = $ req-> input ('param1'); Test: $ string. = $ req-> input ('param2 ');}

To-do list

  • Connection Pool for MySQL/Redis.
  • The coroutine client that encapsulates MySQL, Redis, and Http.
  • For Swoole 2.1 + automatic coroutine support.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.