laravel php fpm 端口,实现FastCGI协议替代PHP-FPM提高Laravel接口性能50倍

关于Stone

Stone是一个基于swoole的php应用,目的是为了提高基于laravel实现的接口性能。当然, 理论上适用于任何框架。 目前这个项目有特定的应用场景限制, 主要适用于无状态的接口调用的场景。能在继续使用Laravel优雅代码的前提下得到极大的性能提升。

其实,包含视图和Session请求也可以通过这个手段来优化, 只是涉及到很多修改laravel的地方, 有点过于复杂。 对此感兴趣的朋友可以参考一个叫LaravelFly的项目。

我特意写了一个基于laravel5.2的例子, 性能对比大概是120QPS vs. 7600QPS。 详情请看:https://github.com/chefxu/stone-laravel-example

原理说明

本项目可以理解为一个胶水项目,把swoole和laravel较好的揉和起来,既享受Laravel优雅的编码体验又享受swoole的高性能。

通过swoole将应用常驻内存, 避免每次请求前都需要初始化框架代码,请求后都需要销毁框架代码,大大提高接口的性能。

实现fastcgi协议, 主要是为了继续使用成熟的nginx,也能保持架构的简单。

简单来说就是 nginx+stone(swoole+fastcgi) 替换 nginx+PHP-FPM。

性能对比

nginx + stone + laravel5.2 Server Software: stone

Server Hostname: 192.168.1.10

Server Port: 7412

Document Path: /server/workbench/listdata

Document Length: 211 bytes

Concurrency Level: 100

Time taken for tests: 1.305 seconds

Complete requests: 10000

Failed requests: 0

Total transferred: 3400000 bytes

HTML transferred: 2110000 bytes

Requests per second: 7662.11 [#/sec] (mean)

Time per request: 13.051 [ms] (mean)

Time per request: 0.131 [ms] (mean, across all concurrent requests)

Transfer rate: 2544.06 [Kbytes/sec] received

nginx + php-fpm + laravel5.2 Server Software: nginx

Server Hostname: 192.168.1.10

Server Port: 7412

Document Path: /workbench/listdata

Document Length: 211 bytes

Concurrency Level: 30

Time taken for tests: 8.335 seconds

Complete requests: 1000

Failed requests: 0

Total transferred: 1185252 bytes

HTML transferred: 211000 bytes

Requests per second: 119.97 [#/sec] (mean)

Time per request: 250.054 [ms] (mean)

Time per request: 8.335 [ms] (mean, across all concurrent requests)

Transfer rate: 138.87 [Kbytes/sec] received

使用说明

本项目仅为学习交流使用, 不建议在生成环境下使用,如因在生成环境下使用带来的损失, 本人一概不负责任。

composer安装包 composer require qufenqi/stone

引入artisan管理脚本, app/Console/Kernel.php 添加一行 protected $commands = [

Commands\Inspire::class,

\Qufenqi\Stone\Console\Commands\StoneServer::class,

];

引入ServiceProvider,config/app.php添加一行 Illuminate\Validation\ValidationServiceProvider::class,

Illuminate\View\ViewServiceProvider::class,

Qufenqi\Stone\StoneServiceProvider::class,

自定义handler类

每一次请求需要有一个handler去处理,因为处理请求属于业务的一部分, 所以交由业务层去处理。 要求实现RequestHandler接口。 <?php namespace App\Servers;

use Qufenqi\Stone\Contracts\RequestHandler;

use App;

use Log;

use Exception;

class Handler implements RequestHandler

{

public function process($url, $params = [])

{

list($class, $method) = $this->route($url);

if (!class_exists($class)) {

return $this->output(['code' => -1, 'message' => $class . '不存在', 'data' => null]);

}

$instance = App::make($class);

if (!method_exists($instance, $method)) {

return $this->output(['code' => -1, 'message' => $method . '不存在', 'data' => null]);

}

try {

$data = call_user_func_array([$instance, $method], $params);

} catch (Exception $e) {

Log::error($e);

return $this->output(['code' => -1, 'message' => '系统繁忙', 'data' => null]);

}

return $this->output($data);

}

public function output($data)

{

return json_encode($data);

}

public function route($url)

{

$path = parse_url($url, PHP_URL_PATH);

$path = explode('/', $path);

$class = $method = '';

if (!empty($path[2])) {

$class = 'App\\Servers\\' . ucfirst(camel_case($path[2] . '_server'));

}

if (!empty($path[3])) {

$method = camel_case($path[3]);

}

return [$class, $method];

}

}

新增自定义配置 config/stone.php <?php

return [

'handler' => 'App\Servers\Handler', // 设置具体的业务处理类

'user' => 'apple', // 设置运行的用户

'group' => 'apple', // 设置运行的组

'port' => 9101, // 监听的端口

];

管理服务 sudo php ./artisan stone:server --start --stop --reload

调试模式 sudo php ./artisan stone:server --start --debug

修改nginx配置 location /server/ {

fastcgi_pass 127.0.0.1:9101;

fastcgi_split_path_info ^(.+\.php)(/.+)$;

fastcgi_index index.php;

include fastcgi.conf;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译 ./configure --prefix=/usr/local/php --exec-prefix=/usr/local/php --bindir=/usr/local/php/bin --sbindir=/usr/local/php/sbin --includedir=/usr/local/php/include --libdir=/usr/local/php/lib/php --mandir=/usr/local/php/php/man --with-config-file-path=/usr/local/php/etc --with-mysql-sock=/var/run/mysql/mysql.sock --with-mcrypt=/usr/include --with-mhash --with-openssl --with-mysql=shared,mysqlnd --with-mysqli=shared,mysqlnd --with-pdo-mysql=shared,mysqlnd --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-gd-native-ttf --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-jpeg-dir --with-freetype-dir --enable-opcache --enable-fpm --enable-fastcgi --with-fpm-user=www --with-fpm-group=www --without-gdbm --with-mcrypt=/usr/local/apps/libmcrypt --disable-fileinfo 报错:1, **configure: error: system libzip must be upgraded to version >=**0.11。 使用Yum最新版只到0.10,不足以达到要求。 一、先删除libzip yum remove libzip -y SSH执行以上命令,先删除libzip 和 libzip-devel 二、下载安装并手动编译 wget https://nih.at/libzip/libzip-1.2.0.tar.gz tar -zxvf libzip-1.2.0.tar.gz cd libzip-1.2.0 ./configure make && make install 三、(可忽略)另外最新版本请参考官网:https://nih.at/libzip/ 1.5.0的libzip需要cmake wget https://libzip.org/download/libzip-1.5.0.tar.gz tar -zxvf libzip-* cd libzip* mkdir build && cd build && cmake .. && make && make install 报错2: error: off_t undefined; check your library configuration 根据报错信息分析 configure: error: off_t undefined; check your library configuration 未定义的类型 off_t。 off_t 类型是在 头文件 unistd.h中定义的,在32位系统 编程成 long int ,64位系统则编译成 long long int ,这里题主的系统应该是 64位的吧,在进行编译的时候 是默认查找64位的动态链接库,但是默认情况下 centos 的动态链接库配置文件/etc/ld.so.conf里并没有加入搜索路径,这个时候需要将 /usr/local/lib64 /usr/lib64 这些针对64位的库文件路径加进去。 采用下面的方法。 添加搜索路径到配置文件 echo '/usr/local

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值