使用Laravel 框架在应用中通过 Swoole 实现高性能 HTTP 服务器、WebSocket 服务器、协程、异步事件监听、异步任务队列、毫秒级任务调度、MySQL 线程池等高级特性,从而提高 Laravel 应用整体性能。
Swoole替代了nginx和fastcgi,避免了每次请求时框架文件的重复加载。Laravels是基于Swoole整合到Laravel框架中的三方扩展,可以很轻松实现Swoole的高性能特性,本篇将使用Laravels实现高性能HTTP服务器。
我是在自己构建的Docker环境下,用Dockerfile构建的PHP-WEB环境,方法请浏览《Dockerfile PHP环境apt包加速及扩展支持(优化)》
LaravelS文档:https://github.com/hhxsv5/laravel-s
Docker容器
docker run --name laravels -d -p 81:80 -v ~/Web/laravels/:/var/www/html -v ~/Docker/nginx/laravel.conf:/etc/nginx/nginx.conf web:2.0
安装Laravel
进入Docker容器,安装Laravel,我用的版本是Laravel6
composer create-project --prefer-dist laravel/laravel blog "6.*"
配置nginx
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
server {
listen 80;
server_name example.com;
root /var/www/html/laravels/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
}
配置完成后,会映射到容器的 nginx
配置,然后重启容器
docker restart laravels
ab吞吐率测试
结果:每秒吞吐率 37.96
下面开始使用LaravelS实现高性能HTTP服务器
修改nginx配置
我们知道在使用 Nginx 作为 Web 服务器的时候,前端资源文件,比如 CSS、JS、图片等静态资源都是通过 Nginx 进行处理的,比较高效,而 PHP 脚本请求这种动态资源都是转发到后端 PHP-FPM 进程进行处理,如果要基于 Swoole 实现高性能 HTTP 服务器,则这个 HTTP 服务器替代的也是 PHP-FPM 的职能,也就是说,我们将原本转发到 PHP-FPM 进程的请求转发给 Swoole 进行处理。在本例中,就是转发给 LaravelS 服务。
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
upstream swoole {
# Connect IP:Port
server 127.0.0.1:5200 weight=5 max_fails=3 fail_timeout=30s;
keepalive 16;
}
server {
listen 80;
server_name blog-s.test;
root /var/www/html/laravels/public;
index index.php index.html index.htm;
# Nginx 处理静态资源,LaravelS 处理动态资源
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://swoole;
}
}
}
重启Docker容器,在Docker容器中,启动LaravelS HTTP服务器
# 重启Docker容器
docker restart laravels
# 进入容器中
docker exec -it laravels bash
# 启动LaravelS HTTP
cd laravels
php bin/laravels start
命令:
ab吞吐率测试
结果:每秒吞吐率 296.53