0
点赞
收藏
分享

微信扫一扫

php中使用swoole加速lumen项目-laravelS实战


背景

 

公司项目严重依赖lumen系列,代码众多,重构困难,虽然访问量变大,性能问题越来越严重,急需要提升性能,于是找到了这个项目

 

​​https://github.com/hhxsv5/laravel-s​​

 

下面我们来实战一下

 

环境介绍

 

php7.2

centos7

swoole4.5.7

lumen5.8

 

1.安装

源码安装


wget  https://github.com/swoole/swoole-src/archive/v4.5.7.tar.gz
tar -xzvf v4.5.7.tar.gz
cd swoole-src-4.5.7 && \
phpize && \
./configure && \
make && sudo make install


 

启动扩展,需要在 php.ini 中加入如下配置来启用 Swoole 扩展


 extension=swoole.so


 

验证是否配置成功


php -m |grep swoole


 

如果没有可能是 php.ini 的路径不对。
可以使用 php --ini 来定位到 php.ini 的绝对路径,Loaded Configuration File 一项显示的是加载的 php.ini 文件,如果值为 none 证明根本没加载任何 php.ini 文件,需要自己创建。


#安装laravelS
composer require "hhxsv5/laravel-s:3.7.8" -vvv
# 确保你的composer.lock文件是在版本控制中


 

2.注册,咱们是lumen5.8需要手动注册,修改 bootstrap/app.php


$app->register(Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class);


 

3.发布配置和二进制文件

注意:每次升级LaravelS后,需重新publish;点击Release去了解各个版本的变更记录。


php artisan laravels publish
# 配置文件:config/laravels.php
# 二进制文件:bin/laravels bin/fswatch bin/inotify


4.修改配置​​config/laravels.php​​:监听的IP、端口等

 

请参考​​配置项​​。

5.运行


php bin/laravels {start|stop|restart|reload|info|help}


 

start  启动laravelS,展示已启动进程 ps -ef |grep laravels

stop  停止laravelS,并处罚自定义进程的onStop方法

restart  重启LaravelS:先平滑Stop,然后再Start;在Start完成之前,服务是不可用的

reload  平滑重启所有Task/Worker/Timer进程(这些进程内包含了你的业务代码),并触发自定义进程的onReload方法,不会重启Master/Manger进程;修改config/laravels.php后,你只有调用restart来完成重启

info 显示组件的版本信息

help  显示帮助信息

 

nginx配置


upstream swoole {
server 127.0.0.1:5200 weight=5 max_fails=3 fail_timeout=30s;
keepalive 16;
}
server {
listen 8081;
server_name 127.0.0.1;
root /data/nginx/wwwbeta/app/public;
index index.php index.html;

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

charset utf-8;
error_page 404 /index.php;
access_log /data/nginx/logs/nginx_access.log json;
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;
}

location ~ /\.(?!well-known).* {
deny all;
}

}


 

压测

 


#观察日志,看状态码是否正常
tail -f nginx_access.log |grep new_stock

#ab压测swoole
ab -c 200 -n 1000 127.0.0.1:8081/stocks/new_stock

#结果
Concurrency Level: 200
Time taken for tests: 2.094 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 12832000 bytes
HTML transferred: 12493000 bytes
Requests per second: 477.56 [#/sec] (mean)

#压测php-fpm
ab -c 200 -n 1000 127.0.0.1:8080/stocks/new_stock
#结果
Concurrency Level: 200
Time taken for tests: 6.057 seconds
Complete requests: 1000
Failed requests: 852
(Connect: 0, Receive: 0, Length: 852, Exceptions: 0)
Write errors: 0
Non-2xx responses: 852
Total transferred: 2166668 bytes
HTML transferred: 1980172 bytes
Requests per second: 165.11 [#/sec] (mean)


可见swoole并发远超php-fpm,而且功能更加稳定,php-fpm已经开始出现大量499错误

举报

相关推荐

0 条评论