Laravels how to accelerate laravel/lumen through Swoole

Source: Internet
Author: User
This article mainly introduces to you about Laravels through the swoole accelerated laravel/lumen of related materials, the text through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, need friends to study together.

Laravels-standing on the shoulders of giants

This article mainly introduces the related content of laravels accelerating laravel/lumen through Swoole, about: Rocket: Swoole to accelerate Laravel/lumen, wherein s represents swoole, speed, high performance.

Characteristics

    • 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

Dependent Description
Php >= 5.5.9
Swoole >= 1.7.19 recommended the latest stable version from 2.0.12 to no longer support PHP5
Laravel/lumen >= 5.1
gzip[Optional] Zlib, check if the native Libz is available ldconfig-p|grep libz

Installation

1. Installation via Composer (packagist)

# Execute composer require "hhxsv5/laravel-s:~1.0"-vvv# at the root of your laravel/lumen project to make sure your composer.lock files are 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 the configuration file

PHP Artisan Laravels Publish

Special situation: You do not need to manually load the configuration laravels.php, laravels the bottom is loaded automatically.

It doesn't have to be loaded manually, but it doesn't have a problem $app->configure (' laravels ');

4. Modify the configuration config/laravels.php: Listen to the IP, port, etc., please refer to the configuration item.

Run

PHP artisan laravels {Start|stop|restart|reload|publish}
Command Description
Start Start Laravels, show the list of started processes Ps-ef|grep Laravels
Stop Stop Laravels
Restart Restart Laravels
Reload Smooth restart all worker processes, which 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=30s; #server 192.168.0.2:5200 weight=3 Max_ Fails=3 fail_timeout=30s; #server 192.168.0.3:5200 Backup;} server {listen; 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 handles static resources and laravels handles 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; }}

Monitoring Events

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

Laravels.received_request Swoole_http_request is converted to Illuminate\http\request, before the Laravel kernel processes the request.

Modify ' app/providers/eventserviceprovider.php ', add the following listener 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 finishes processing the request, the illuminate\http\response is turned into swoole_http_response (the next response is 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});

Using Swoole_http_server instances in your project

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

Precautions

It is recommended to obtain request information through Illuminate\http\request object, compatible with $_server, $_get, $_post, $_files, $_cookie, $_request, not $_session, $_ENV.

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 requests by returning Illuminate\http\response objects that are compatible with Echo, Vardump (), Print_r (), and cannot use functions like exit (),

Die (), header (), Setcookie (), Http_response_code (). Public function json () {return response ()->json ([' time '] = time ()])->header (' Header1 ', ' value1 ') Withcookie (' C1 ', ' V1 ');}

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

Infinite append elements to static or global variables will result in full memory.

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

Todo items

    • Connection pool for Mysql/redis.

    • The Mysql/redis/http client that wraps the thread.

    • Automatic coprocessor support for Swoole 2.1+.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.