用 swoole 扩展实现 Laravel 持久性:再也不用每次请求重新加载资源啦

swoole是用c写的php 扩展,注意不支持windows,我是在虚拟机里玩的

1:安装swoole

pecl install swoole
并在 php.ini 中启用之

2 运行代码:

在项目根目录建立文件 swoole-server.php
代码参考 https://github.com/LinkedDestiny/swoole-ya...

<?php
    require __DIR__.'/bootstrap/autoload.php';

    class HttpServer
    {
        public static $instance;
        public $http;
        public static $get;
        public static $post;
        public static $header;
        public static $server;
        private $application;
        public function __construct() {
            $app = require_once __DIR__.'/bootstrap/app.php';
            $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
            // $http = new swoole_http_server("127.0.0.1", 9501); // 只侦听 localhost
            $http = new swoole_http_server("0.0.0.0", 9501); //侦听所有地址来的请求
            $http->on('request', function ($request, $response) use($kernel) {

                // 代码借鉴自 Illuminate\Http\Request::capture
                $l_request= new Symfony\Component\HttpFoundation\Request(
                    isset($request->get)?$request->get:[], isset($request->post)? $request->post:[], [], 
                    isset($request->cookie)?$request->cookie:[], isset($request->files)? $request->files:[], $request->server);

                if (0 === strpos($l_request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
                    && in_array(strtoupper($l_request->server->get('REQUEST_METHOD', 'GET')), array('PUT', 'DELETE', 'PATCH'))
                    ) {
                    parse_str($l_request->getContent(), $data);
                    $l_request->request = new  Symfony\Component\HttpFoundation\ParameterBag($data);
                }

                $l_request=Illuminate\Http\Request::createFromBase( $l_request);

                $l_response = $kernel->handle( $l_request );

                ob_start();

                $l_response->send();
                $kernel->terminate($l_request, $l_response);

                $result = ob_get_clean();
                $response->end($result);
            });
            $http->start();
        }

        public static function getInstance() {
            if (!self::$instance) {
                self::$instance = new HttpServer;
            }
            return self::$instance;
        }

    }
    HttpServer::getInstance();

3 运行:

php swoole-server.php
访问 127.0.0.1:9501

4 和nginx合作:

swoole server 没有实现所有http特性,所以可用nginx作代理

    location / {
        if (!-e $request_filename) {
             proxy_pass http://127.0.0.1:9501;
             proxy_http_version 1.1;
             proxy_set_header Connection "keep-alive";
        }
    }

5 疑问

传统的php运行方式,就是每次请求重新加载资源,完成后释放所有资源。所以这样用swoole的方式是否会有问题,还需要实践来考验。

swoole算是nodejs在php中的一种实现,异步响应请求,性能超强

本帖已被设为精华帖!
讨论数量: 17

我目前在造的轮子LaravelS https://github.com/hhxsv5/laravel-s 通过 Swoole 来加速 Laravel/Lumen。有兴趣可以尝试下。

6年前 评论

用swoole加速laravel意义不大,laravel依旧很慢

6年前 评论

我目前在造的轮子LaravelS https://github.com/hhxsv5/laravel-s 通过 Swoole 来加速 Laravel/Lumen。有兴趣可以尝试下。

6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!