基于Laravel8下的LaravelS实现高性能HTTP服务器

216 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 8 天,点击查看活动详情

准备工作

虚拟生命:vm16
环境:LNMP
系统版本:CentoOS8.2
框架:Laravel8

我使用的测试环境大多数是基于最新版本,目的是用于测试学习。既然推出了最新的版本,不管你接不接受或愿不愿意使用,它都是一个趋势。因此,基于学习的目的,使用新版本。
关于环境的搭建不再重复了,博客里面写了源码安装搭建PHP环境。

什么是LaravelS

LaravelS 是一个胶水项目,用于快速集成 Swoole 到 Laravel 或 Lumen,然后赋予它们更好的性能、更多可能性。

创建Laravel8项目

第一步:安装Laravel8

composer create-project --prefer-dist laravel/laravel laravel_swoole

第二步:配置Laravel8虚拟主机

cd /usr/local/nginx/conf/vhosts/

# 注意:这个虚拟主机机配置Laravel8项目
vim laswoole.conf

注意:我这里是vhosts目录下。在nginx.conf文件中通过 include 引入vhosts 目录下所有配置文件

server {
   listen        80;
   server_name laravelswoole.com;
   root   "/www/laravel_swoole/public";
   location / {
       index index.php index.html;
       autoindex  off;
   }
   location ~ .php(.*)$ {
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_split_path_info  ^((?U).+.php)(/?.+)$;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       fastcgi_param  PATH_INFO  $fastcgi_path_info;
       fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
       include        fastcgi_params;
   }
}

第三步:重启nginx

systemctl restart nginx.service

第四步:windows10本机配置hosts文件

192.168.157.129 laravelswoole.com

安装Laravel-s

第一步:安装Laravels

cd laravel_swoole/

# 安装
composer require hhxsv5/laravel-s

第二步:生成配置

php artisan laravels publish

Laravel5.5以上版本不再需要手动配置app.php了。此命令完成之后即可使用。

第三步:运行Laravel-S

php bin/laravels start
_                               _  _____
| |                             | |/ ____|
| |     __ _ _ __ __ ___   _____| | (___  
| |    / _` | '__/ _` \ \ / / _ \ |___ \
| |___| (_| | | | (_| |\ V /  __/ |____) |
|________,_|_|  __,_| _/ ___|_|_____/
                                         
Speed up your Laravel/Lumen
>>> Components
+---------------------------+---------+
| Component                 | Version |
+---------------------------+---------+
| PHP                       | 7.4.11  |
| Swoole                    | 4.5.5   |
| LaravelS                  | 3.7.8   |
| Laravel Framework [local] | 8.11.2  |
+---------------------------+---------+
>>> Protocols
+-----------+--------+-------------------+----------------+
| Protocol  | Status | Handler           | Listen At      |
+-----------+--------+-------------------+----------------+
| Main HTTP | On     | Laravel Framework | 127.0.0.1:5200 |
+-----------+--------+-------------------+----------------+

运行之后通过 ip+端口即可访问,如192.168.157.129:5200

第四步:配置nginx

注意了,这里是为Laravel-S单独配置一个服务器。传统的配置是nginx接收到一个php请求后转发给php-fmp处理;实现高性能Swoole HTTP服务器,这里是将请求转发给LaravelS处理。

cd /usr/local/nginx/conf/vhosts

vim laravel_s.conf
upstream laravels {
   server 127.0.0.1:5200 weight=5 max_fails=3 fail_timeout=30s;
   keepalive 16;
}
server {
   listen 80;

   server_name laravel-s.com;
   root /www/laravel_swoole/public;
   index index.php index.html index.htm;

   location / {
       try_files $uri @laravels;
   }

   location @laravels {
       proxy_http_version 1.1;
       proxy_set_header Connection "";
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Real-PORT $remote_port;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Host $http_host;
       proxy_set_header Scheme $scheme;
       proxy_set_header Server-Protocol $server_protocol;
       proxy_set_header Server-Name $server_name;
       proxy_set_header Server-Addr $server_addr;
       proxy_set_header Server-Port $server_port;
       proxy_pass http://laravels;
   }
}

第五步:重启nginx

systemctl restart nginx.service

第六步:windows10配置hosts文件

192.168.157.129 laravel-s.com

到这里就已经配置完成。配置基于域名之后,端口访问失败了。
说明:laravelswoole.com 是基础传统的HTTP服务器;laravel-s是基于Swoole。

性能测试对比

使用ab压力测试工具进行对比测试。基于php-fpm的压力测试

ab -n 500 -c 100 http://laravelswoole.com
Server Software:        nginx/1.18.0
Server Hostname:        laravelswoole.com
Server Port:            80

...#省略

Concurrency Level:      10
Time taken for tests:   5.806 seconds
Complete requests:      100
Failed requests:        0
Non-2xx responses:      100
Total transferred:      52046500 bytes
HTML transferred:       52024900 bytes
Requests per second:    17.22 [#/sec] (mean)
Time per request:       580.583 [ms] (mean)
Time per request:       58.058 [ms] (mean, across all concurrent requests)
Transfer rate:          8754.41 [Kbytes/sec] received

...#省略

Percentage of the requests served within a certain time (ms)
 50%    574
 66%    587
 75%    596
 80%    607
 90%    642
 95%    668
 98%    677
 99%    686
100%    686 (longest request)

可以看到每秒处理请求是 17.22个。
基于swoole的压力测试

ab -n 500 -c 100 http://laravel-s.com/
Server Software:        nginx/1.18.0
Server Hostname:        laravel-s.com
Server Port:            80

...#省略

Concurrency Level:      100
Time taken for tests:   0.803 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      9254500 bytes
HTML transferred:       8729000 bytes
Requests per second:    622.59 [#/sec] (mean)
Time per request:       160.620 [ms] (mean)
Time per request:       1.606 [ms] (mean, across all concurrent requests)
Transfer rate:          11253.38 [Kbytes/sec] received
...#省略

Percentage of the requests served within a certain time (ms)
 50%    138
 66%    144
 75%    161
 80%    167
 90%    204
 95%    229
 98%    240
 99%    244
100%    273 (longest request)

可以看到基于swoole服务器之后的处理请求数是622.59。

总结

从中可以看到基于swoole之后很明显的优化,处理请求数大大提高。php-fmp是17.22,swoole是622.59。差距非常大。