不用三方包 给 Laravel 开启 Swoole
32

Swoole 是一款优秀的 PHP 扩展,利用其可以实现原生 PHP 很难做到的常驻服务和异步。正好我有个 Laravel 项目可以折腾,就研究了下。

Laravel 项目是基于 composer 的,所以我先帖下我的 composer.json 中的 require 声明:

{
    "require": {
        "php": "^7.1.3",
        "cybercog/laravel-love": "^5.1",
        "dingo/api": "~v2.0.0-alpha2",
        "doctrine/dbal": "^2.8",
        "fideloper/proxy": "^4.0",
        "guzzlehttp/guzzle": "^6.3",
        "infyomlabs/adminlte-templates": "5.6.x-dev",
        "infyomlabs/laravel-generator": "5.6.x-dev",
        "jeroennoten/laravel-adminlte": "^1.23",
        "laravel/framework": "5.6.*",
        "laravel/tinker": "^1.0",
        "laravelcollective/html": "^5.6.0",
        "lshorz/luocaptcha": "^1.0",
        "overtrue/laravel-lang": "v3.0.08",
        "overtrue/laravel-wechat": "^4.0",
        "predis/predis": "^1.1",
        "spatie/laravel-permission": "^2.17",
        "tymon/jwt-auth": "~1.0.0-rc.2",
        "yajra/laravel-datatables-buttons": "^4.0",
        "yajra/laravel-datatables-oracle": "^8.7"
    }
}

如果我们要开启swoole,我们可选的包有这些:

但一般来说,项目中需要常驻容器的服务与每次均需重新构建的服务并不一样,所以我才剑走偏锋。

起步

我们需要将 public/index.php 替换成如下

<?php

use Illuminate\Http\Request;
use Illuminate\Http\Response;

define('LARAVEL_START', microtime(true));
require __DIR__ . '/../vendor/autoload.php';
$app = require_once __DIR__ . '/../bootstrap/app.php';

class Laravel
{
    /**
     * Illuminate\Foundation\Application
     *
     * @var \Illuminate\Foundation\Application
     */
    public $app;

    /**
     * App\Http\Kernel
     *
     * @var \App\Http\Kernel
     */
    public $kernel;

    /**
     * App\Http\Requests\Request
     *
     * @var \App\Http\Requests\Request
     */
    public $request;

    /**
     * Illuminate\Http\JsonResponse
     *
     * @var \Illuminate\Http\JsonResponse
     */
    public $response;

    /**
     * 构造
     *
     * @param \Illuminate\Foundation\Application $app
     */
    public function __construct(\Illuminate\Foundation\Application $app)
    {
        $this->app = $app;
    }

    /**
     * RUN
     *
     * @return void
     */
    public function run()
    {
        \Swoole\Runtime::enableCoroutine(true);

        $http = new swoole_http_server('127.0.0.1', '80');

        $http->set([
            'document_root' => public_path('/'),
            'enable_static_handler' => true,
        ]);

        $http->on('request', function ($req, $res) {
            try {
                $kernel = $this->app->make(Illuminate\Contracts\Http\Kernel::class);

                $get = $req->get ?? [];
                $post = $req->post ?? [];
                $input = array_merge($get, $post);
                $cookie = $req->cookie ?? [];
                $files = $req->files ?? [];
                $server = $req->server ?? [];

                $request = Request::create($req->server['request_uri'], $req->server['request_method'], $input, $cookie, $files, $server);

                if (isset($req->header['x-requested-with']) && $req->header['x-requested-with'] == "XMLHttpRequest") {
                    $request->headers->set('X-Requested-With', "XMLHttpRequest", true);
                }
                if (isset($req->header['accept']) && $req->header['accept']) {
                    $request->headers->set('Accept', $req->header['accept'], true);
                }

                $response = $kernel->handle($request);

                $res->status($response->getStatusCode());

                foreach ($response->headers->allPreserveCaseWithoutCookies() as $name => $values) {
                    foreach ($values as $value) {
                        $res->header($name, $value, false);
                    }
                }

                foreach ($response->headers->getCookies() as $cookie) {
                    $res->header('Set-Cookie', $cookie->getName() . strstr($cookie, '='), false);
                }
                dump(time());

                $res->end($response->getContent());
                $this->app->forgetInstance('request');
            } catch (\throwable $e) {
                echo $e->getMessage();
                echo PHP_EOL;
                echo $e->getFile();
                echo PHP_EOL;
                echo $e->getLine();
                echo PHP_EOL;
            }
        });
        $http->start();
    }
}

(new Laravel($app))->run();

运行时发现大多数页面均没有问题,只有几个用了 infyomlabs/laravel-generator 产生的列表页,AJAX拉取JSON时却返回了HTML。


排查

在有问题页面的 controller 代码中,找到如下

    /**
     * Display a listing of the Star.
     *
     * @param StarDataTable $starDataTable
     * @return Response
     */
    public function index(StarDataTable $starDataTable)
    {
        return $starDataTable->render('stars.index');
    }

定位 StarDataTable::render() 到了

    /**
     * Process dataTables needed render output.
     *
     * @param string $view
     * @param array $data
     * @param array $mergeData
     * @return mixed
     */
    public function render($view, $data = [], $mergeData = [])
    {
        if ($this->request()->ajax() && $this->request()->wantsJson()) {
            return app()->call([$this, 'ajax']);
        }
        ...
    }

这是判断 $this->request() 是不是 XHR 请求,且 Accept 请求头声明了 application/json

$this->request() 实现如下


    /**
     * Get DataTables Request instance.
     *
     * @return \Yajra\DataTables\Utilities\Request
     */
    public function request()
    {
        return $this->request ?: $this->request = resolve('datatables.request');
    }

不难看出,如果第一次构建,会走到

    $this->request = resolve('datatables.request');

而 resolve 的实现是啥?

if (! function_exists('resolve')) {
    /**
     * Resolve a service from the container.
     *
     * @param  string  $name
     * @return mixed
     */
    function resolve($name)
    {
        return app($name);
    }
}

就是从容器中取出 datatables.request 的过程。

所以我们只需让每次请求结束,$app 容器忘掉 datatables.request 就好了


改进

增加遗忘 datatables.request

    $res->end($response->getContent());
    $this->app->forgetInstance('request');
    $this->app->forgetInstance('datatables.request');
    $this->app->forgetInstance(\Dingo\Api\Http\Middleware\Request::class);

完整最终版:

<?php

use Illuminate\Http\Request;
use Illuminate\Http\Response;

define('LARAVEL_START', microtime(true));
require __DIR__ . '/../vendor/autoload.php';
$app = require_once __DIR__ . '/../bootstrap/app.php';

class Laravel
{
    /**
     * Illuminate\Foundation\Application
     *
     * @var \Illuminate\Foundation\Application
     */
    public $app;

    /**
     * App\Http\Kernel
     *
     * @var \App\Http\Kernel
     */
    public $kernel;

    /**
     * App\Http\Requests\Request
     *
     * @var \App\Http\Requests\Request
     */
    public $request;

    /**
     * Illuminate\Http\JsonResponse
     *
     * @var \Illuminate\Http\JsonResponse
     */
    public $response;

    /**
     * 构造
     *
     * @param \Illuminate\Foundation\Application $app
     */
    public function __construct(\Illuminate\Foundation\Application $app)
    {
        $this->app = $app;
    }

    /**
     * RUN
     *
     * @return void
     */
    public function run()
    {
        \Swoole\Runtime::enableCoroutine(true);

        $http = new swoole_http_server('127.0.0.1', '80');

        $http->set([
            'document_root' => public_path('/'),
            'enable_static_handler' => true,
        ]);

        $http->on('request', function ($req, $res) {
            try {
                $kernel = $this->app->make(Illuminate\Contracts\Http\Kernel::class);

                $get = $req->get ?? [];
                $post = $req->post ?? [];
                $input = array_merge($get, $post);
                $cookie = $req->cookie ?? [];
                $files = $req->files ?? [];
                $server = $req->server ?? [];

                $request = Request::create($req->server['request_uri'], $req->server['request_method'], $input, $cookie, $files, $server);

                if (isset($req->header['x-requested-with']) && $req->header['x-requested-with'] == "XMLHttpRequest") {
                    $request->headers->set('X-Requested-With', "XMLHttpRequest", true);
                }
                if (isset($req->header['accept']) && $req->header['accept']) {
                    $request->headers->set('Accept', $req->header['accept'], true);
                }

                $response = $kernel->handle($request);

                $res->status($response->getStatusCode());

                foreach ($response->headers->allPreserveCaseWithoutCookies() as $name => $values) {
                    foreach ($values as $value) {
                        $res->header($name, $value, false);
                    }
                }

                foreach ($response->headers->getCookies() as $cookie) {
                    $res->header('Set-Cookie', $cookie->getName() . strstr($cookie, '='), false);
                }
                dump(time());

                $res->end($response->getContent());
                $this->app->forgetInstance('request');
                //$this->app->forgetInstance('session');
                //$this->app->forgetInstance('session.store');
                //$this->app->forgetInstance('cookie');
                $this->app->forgetInstance('datatables.request');
                $this->app->forgetInstance(\Dingo\Api\Http\Middleware\Request::class);
                //$kernel->terminate($request, $response);
            } catch (\throwable $e) {
                echo $e->getMessage();
                echo PHP_EOL;
                echo $e->getFile();
                echo PHP_EOL;
                echo $e->getLine();
                echo PHP_EOL;
            }
        });
        $http->start();
    }
}

(new Laravel($app))->run();

测试

比原生 laravel 确实快不少(这还有4句SQL查询)

file

已知问题

  • flash 闪存数据以及表单验证错误的展示有问题
  • PDO会报 Cannot execute queries while other unbuffered queries are active https://github.com/symfony/symfony/issues/6745
  • Throttle 的 IP 获取设定默认会产生问题

    打个小广告,上海凭远教育,招聘中级/高级前端工程师,经验要求分别1/3年,业务框架为 angular 1.x(后面会改)。简历扔 xiaohuilam@qq.com。

Fine or not, never stop. Laravel 运行生命周期解析

本帖已被设为精华帖!
本帖由系统于 3个月前 自动加精
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 6
lchola

很六

4个月前
dynamohuang

很具有教学意义 看laravels源码之前 看看这个会有很大帮助

3个月前

666
困惑:把整个 Laravel 改成只使用 swoole,这种需求应该还是比较少。 这样大的改动后期困难较多,也难以扩展。
swoole 已经是 PHP 标准扩展,开启 swoole 为什么要用使用这么麻烦的

3个月前

@FreeMason swoole的运行模式有别于fpm

3个月前
panco

既然是用swoole,就用基于swoole的web框架比较好,这样改的话会有很多莫名其妙的问题,不可能这样做线上环境的

3个月前

@to2False swoole 还不怎么了解。比如使用 swoole 的 websocket ,在 Laravel 的 console 的模式下。使用时 swoole 与 fpm 应该也没有什么问题吧

3个月前

  请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!