使用swoole代理HTTP加速laravel

使用swoole代理HTTP加速laravel

laravel-swoole拓展git地址:https://github.com/swooletw/laravel-swoole/wiki

1.安装swoole拓展

sudo apt-get install php7.3-dev php-pear
sudo pecl install swoole

##升级版本为 pecl upgrade swoole
##如果一直下载失败先更新下下载源 apt-get update

 扩展配置

centos

php --ini
sudo echo "extension=swoole.so" >> /path/to/php.ini

ubuntu

需要手动在php.ini上一级级目录的的mods-available里面手动创建swoole.ini,文件内容是:

extension=swoole.so

并且将swoole.ini软链接至cli/conf.d和fpm/conf.d里面,代码为

ln -s /your/php/path/cli/conf.d/20-swoole.ini
和
ln -s /your/php/path/cli/conf.d/20-swoole.ini


查看swoole拓展是否启动

php -m | grep swoole
##查看swoole版本
php --ri swoole
2.laravel配置

下载框架拓展

composer require swooletw/laravel-swoole

2.在 config/app.php 服务提供者数组添加该服务提供者

[
    'providers' => [
        SwooleTW\Http\LaravelServiceProvider::class,
    ],
]

3.启动

php artisan swoole:http start

其他命令

php artisan swoole:http {start|restart|reload|infos}

进程和线程处理数量可以在config/swoole_http.php里面配置,例子如下

return [
    'server' => [
        'host' => env('SWOOLE_HTTP_HOST', '127.0.0.1'),
        'port' => env('SWOOLE_HTTP_PORT', '1215'),
        'options' => [
            'pid_file' => env('SWOOLE_HTTP_PID_FILE', base_path('storage/logs/swoole_http.pid')),
            'log_file' => env('SWOOLE_HTTP_LOG_FILE', base_path('storage/logs/swoole_http.log')),
            'daemonize' => env('SWOOLE_HTTP_DAEMONIZE', true),
            'reactor_num' => env('SWOOLE_HTTP_REACTOR_NUM', swoole_cpu_num() * 2),
            'worker_num' => env('SWOOLE_HTTP_WORKER_NUM', swoole_cpu_num() * 2),
            'task_worker_num' => env('SWOOLE_HTTP_TASK_WORKER_NUM', swoole_cpu_num() * 2),
            'package_max_length' => 20 * 1024 * 1024,
            'buffer_output_size' => 10 * 1024 * 1024,
            'socket_buffer_size' => 128 * 1024 * 1024,
            'max_request' => 3000,
            'send_yield' => true,
            'ssl_cert_file' => null,
            'ssl_key_file' => null,
        ],
    ],

    'pre_resolved' => [
        'view', 'files', 'session', 'session.store', 'routes',
        'db', 'db.factory', 'cache', 'cache.store', 'config', 'cookie',
        'encrypter', 'hash', 'router', 'translator', 'url', 'log',
    ],

    'providers' => [
        Illuminate\Auth\AuthServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        ], 
];

至此swoole已经可以代理http了,你可以通过http://localhost:1215访问应用,但仍需要通过nginx转发来通过域名访问

配置nginx转发

下面是nginx配置

server {
    listen 80;
    server_name your.domain.com;
    root /your/application/path/public;
    index index.php;
    access_log /var/log/nginx/laravel-swoole-access.log;
    error_log /var/log/nginx/laravel-swoole-error.log;
    location = /index.php {
        # Ensure that there is no such file named "not_exists"
        # in your "public" directory.
        try_files /not_exists @swoole;
    }

    location / {
        try_files $uri $uri/ @swoole;
    }

    location @swoole {
        set $suffix "";

        if ($uri = /index.php) {
            set $suffix "/";
        }

        proxy_set_header Host $host;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # IF https
        # proxy_set_header HTTPS "on";

        proxy_pass http://127.0.0.1:1215$suffix;
    }
}

至此使用swoole加速laravel已经完成,可以使用ab或者wrk压测一下
ab -n10000 -c100 http://yourpath
经过测试:
测试条件:
1.简单API列表查询,无复杂子查询和联表
2.有配置,查询,路由缓存
3.无中间件
4.总请求为10000,并发100
5.服务器环境为4核8G,50M带宽,PHP7.3
相同测试条件下,用ab测试工具测试结果为:
在swoole代理下,laravel5.8 的QPS为1280/sec,TP6为3988/sec
在PHP-FPM代理下,aravel5.8 的QPS为462/sec,TP为1980/sec
同时在测试中发现swoole代理下平均响应时间更稳定,保持在180ms;
而php-fpm出现波动较大,最大响应时间可到达1240ms,平均时间为460ms,且失败次数较多

总结

swoole带来的性能提升还是比较高的,但是相对应的项目部署和代码需要做部分调整,个人觉得是值得的

添加新评论