旗下导航:搜·么
当前位置:网站首页 > PHP框架 > Laravel > 正文

如何用RoadRunner加速Laravel应用_laravel框架,Laravel

作者:搜搜PHP网发布时间:2020-04-03分类:Laravel浏览:70


导读:关于Laravel实用小功能的介绍_laravel框架原理:通过回传三个响应头X-RateLimit-Limit,X-RateLimit-Remaining,Retry-After...

关于Laravel实用小功能的介绍_laravel框架

原理:通过回传三个响应头X-RateLimit-Limit,X-RateLimit-Remaining,Retry-After实现控制访问次数。

RoadRunner是一个用Go语言实现的高性能PSR-7服务器,你没看错,是Go语言实现的。

RoadRunner首先用Go实现了一个高性能的服务器,然后通过goridge IPC把请求转发给多个PHP CLI Worker,Worker会把请求重新封装成PSR-7 Request交给框架进行处理。

推荐:laravel教程

娱乐测试

使用siege测试Laravel的Welcome页面,250个并发用户,持续30秒,对比Nginx + FPM和RoadRunner。

Nginx + FPM:
$ siege --no-parser -c 250 -t 30s http://127.0.0.1:3000/  > /dev/null
** SIEGE 4.0.4
** Preparing 250 concurrent users for battle.
The server is now under siege...
Lifting the server siege...
Transactions:                   2032 hits
Availability:                  97.83 %
Elapsed time:                  29.19 secs
Data transferred:               4.51 MB
Response time:                  2.64 secs
Transaction rate:              69.61 trans/sec
Throughput:                     0.15 MB/sec
Concurrency:                  183.50
Successful transactions:        2032
Failed transactions:              45
Longest transaction:           27.34
Shortest transaction:           0.

RoadRunner(4 worker):

给大家分享一些简单的 Laravel 编码实践_laravel框架

将任何 PHP 框架称为最好的框架都是错误的,因为不同的框架都有各自的优点。 通常来说,一个PHP开发者会根据项目需求来选择合适的框架。 但相信我, 我现在已经完全爱上了 Laravel。

$ siege --no-parser -c 250 -t 30s http://127.0.0.1:8080/  > /dev/null
** SIEGE 4.0.4
** Preparing 250 concurrent users for battle.
The server is now under siege...
Lifting the server siege...
Transactions:                  61417 hits
Availability:                 100.00 %
Elapsed time:                  29.26 secs
Data transferred:             135.95 MB
Response time:                  0.12 secs
Transaction rate:            2099.01 trans/sec
Throughput:                     4.65 MB/sec
Concurrency:                  249.06
Successful transactions:       61417
Failed transactions:               0
Longest transaction:            0.20
Shortest transaction:           0.05

如何加速Laravel应用

RoadRunner安装

RoadRunner已经提供了预编译的执行文件,下载对应平台的版本即可,配置文件可用RoadRunner提供的:Using RoadRunner.

Laravel配置

安装依赖:

composer require spiral/roadrunner zendframework/zend-diactoros symfony/psr-http-message-bridge

在Laravel项目创建一个psr-worker.php,代码修改来自tobias-kuendig:

<?php
require __DIR__ . "/vendor/autoload.php";
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
$relay = new Spiral\Goridge\StreamRelay(STDIN, STDOUT);
$psr7 = new Spiral\RoadRunner\PSR7Client(new Spiral\RoadRunner\Worker($relay));
$app = require_once __DIR__ . '/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
while ($req = $psr7->acceptRequest()) {
    try {
        $httpFoundationFactory = new HttpFoundationFactory();
        if (strpos($req->getHeaderLine("content-type"), "application/json") === 0) {
            $body = $req->getBody();
            $parsedBody = json_decode($body, true);
            $req = $req->withParsedBody($parsedBody);
        }
        $symfonyReq = $httpFoundationFactory->createRequest($req);
        $request = Illuminate\Http\Request::createFromBase($symfonyReq);
        $response = $kernel->handle($request);
        $psr7factory = new DiactorosFactory();
        $psr7response = $psr7factory->createResponse($response);
        $psr7->respond($psr7response);
    } catch (\Throwable $e) {
        $psr7->getWorker()->error((string)$e);
    }
}

运行rr serve -v搞定。

以上就是如何用RoadRunner加速Laravel应用的详细内容,更多请关注ki4网其它相关文章!

laradock中安装Laravel Dusk详解_laravel框架

在本地安装 Laravel Dusk 一直失败,查了文档才发现在 laradock 中并不是只需要 composer require 就可以的,还有其他配置。下面记录一下。

标签:Laravel