使用 Swoole 加速 Lumen 框架

前言

众所周知,Web应用中,每次HTTP请求都需要经过PHP-FPM,由它来处理PHP脚本。这一点在很大程度上降低了QPS,如果使用Swoole扩展开启一个HTTP服务来代替PHP-FPM(同时一般情况下使用Nginx作为反向代理服务器),则即可解决这一问题。

安装

composer require kimistar/lumen_swoole

配置

在bootstrap/app.php中注册service provider

$app->register(Star\LumenSwoole\SwooleServiceProvider::class);

自定义配置文件swoole.php 覆盖默认配置

return [
   'host' => '127.0.0.1',
    'port' => 8080,
    'daemonize' => 0,
    'dispatch_mode' => 3,
    'worker_num' => 4,
    'max_request' => 5000,
    'log_file' => storage_path('logs/swoole.log'),
    'log_level' => 5,
    'pid_file' => storage_path('logs/swoole.pid'),
    'open_tcp_nodelay' => 1,
    'heart_beat_internal' => 300,
];

同时在bootstrap/app.php加载此文件

$app->configure('swoole');

使用

php artisan sumen start | restart | stop | reload | status

默认监听127.0.0.1 8080端口,开启4个worker进程

注意无法reload的文件 @https://wiki.swoole.com/wiki/page/p-server...

包括但不限于

  • bootstrap/app.php
  • app/Providers/*
  • config/*
  • app/Console/*

注意

由于Swoole常驻内存的特性,Lumen中Mysql、Redis的连接需要做心跳检测,每隔一段时间ping一次。做法如下:

// bootstrap/app.php
$app->singleton('heartBeat', function() {
    // 在此写心跳检测的代码
});

配置Nginx

server {
    listen 80;
    server_name your.domain.com;

    location / {
        try_files /_not_exists @swoole;
    }

    location @swoole {
        proxy_http_version 1.1;
        #proxy_set_header Connection "keep-alive";
        proxy_set_header Host $host;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        # proxy_set_header HTTPS "on";

        proxy_pass http://127.0.0.1:8080;
    }
}

ab测试

ab -c 100 -n 20000  http://api.swoole.cn/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking api.swoole.cn (be patient)
Completed 2000 requests
Completed 4000 requests
Completed 6000 requests
Completed 8000 requests
Completed 10000 requests
Completed 12000 requests
Completed 14000 requests
Completed 16000 requests
Completed 18000 requests
Completed 20000 requests
Finished 20000 requests

Server Software:        nginx/1.10.2
Server Hostname:        api.swoole.cn
Server Port:            80

Document Path:          /
Document Length:        9 bytes

Concurrency Level:      100
Time taken for tests:   2.373 seconds
Complete requests:      20000
Failed requests:        0
Write errors:           0
Total transferred:      6240000 bytes
HTML transferred:       180000 bytes
Requests per second:    8427.15 [#/sec] (mean)
Time per request:       11.866 [ms] (mean)
Time per request:       0.119 [ms] (mean, across all concurrent requests)
Transfer rate:          2567.65 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.1      1       7
Processing:     1   11   5.2     10      40
Waiting:        1   10   5.1      9      40
Total:          1   12   5.2     11      41

Percentage of the requests served within a certain time (ms)
  50%     11
  66%     13
  75%     15
  80%     16
  90%     19
  95%     22
  98%     25
  99%     28
 100%     41 (longest request)

Git地址:https://github.com/kimistar/lumen_swoole

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 3

没看出来怎么加速框架的.

5年前 评论
风飘零

@motecshine 他这个应该是用swoole代替php-fpm来加速php,自然也就加速lumen了

5年前 评论

@风飘零 是的,而且Swoole Http Server开启后,会使脚本常驻内存,因此也会加快响应速度,但同时代码修改发布后不会生效,必须手动reload或者使用inotify配合swoole自动reload才有效。

5年前 评论

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