Laravel 在高并发下很卡?求大神解答

区内找到了这篇文章和我遇到的查不到,在执行index.php的时候开启php-fpm慢日志发现php-fpm执行时间长,导致php-fpm占用着cpu一直居高不下.
没有几个人回答,是因为这个问题不值得讨论?

script_filename = /home/wwwroot/vote/public/index.php
[0x00007f885cc182c0] curl_exec() /home/wwwroot/vote/app/Tools/HttpsResponse.php:11
[0x00007f885cc18130] https_request() /home/wwwroot/vote/app/Tools/WeixinUtil.php:268
[0x00007f885cc18020] get_userinfo() /home/wwwroot/vote/app/Http/Middleware/WXVote.php:56
[0x00007f885cc17870] handle() /home/wwwroot/vote/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148
[0x00007f885cc17650] Illuminate\Pipeline\{closure}() /home/wwwroot/vote/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
[0x00007f885cc174b0] Illuminate\Routing\{closure}() /home/wwwroot/vote/app/Http/Middleware/SiteLog.php:19
[0x00007f885cc172f0] handle() /home/wwwroot/vote/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148
[0x00007f885cc170d0] Illuminate\Pipeline\{closure}() /home/wwwroot/vote/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
[0x00007f885cc16f30] Illuminate\Routing\{closure}() /home/wwwroot/vote/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41
[0x00007f885cc16e10] handle() /home/wwwroot/vote/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148
[0x00007f885cc16bf0] Illuminate\Pipeline\{closure}() /home/wwwroot/vote/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
[0x00007f885cc16a50] Illuminate\Routing\{closure}() /home/wwwroot/vote/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:65
[0x00007f885cc16790] handle() /home/wwwroot/vote/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148
[0x00007f885cc16570] Illuminate\Pipeline\{closure}() /home/wwwroot/vote/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
[0x00007f885cc163d0] Illuminate\Routing\{closure}() /home/wwwroot/vote/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php:49
[0x00007f885cc162b0] handle() /home/wwwroot/vote/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:148
[0x00007f885cc16090] Illuminate\Pipeline\{closure}() /home/wwwroot/vote/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
[0x00007f885cc15ef0] Illuminate\Routing\{closure}() /home/wwwroot/vote/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php:64

其中查了curl_exec(),get_userinfo()发现他们在网站人数少的时候不会出现。
现在当在线人数300左右就一直很卡,php-fpm一直是在80以上.
4G4核服务器100M共享带宽撑不住500人即时在线访问。PHP-FPM一直高居不下

配置如下

Laravel5.4框架

PHP版本7.01

Mysql用2核2G的RDS

服务器CentOS7.2

cat /usr/local/php/etc/php-fpm.d/www.conf |egrep -v '^$|^;|^$'

#php-fpm 配置
[www]
user = nginx
group = nginx
listen = 127.0.0.1:9000
pm = static
pm.max_children = 8
pm.start_servers = 8
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 800
request_terminate_timeout = 100
request_slowlog_timeout = 5s
slowlog = /tmp/slow.log

cat /etc/nginx/nginx.conf| egrep -v '^;|^$|^#'

#nginx 配置
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections  1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
}

百度的统计,在线人数303

浏览量(PV) 访客数(UV) IP数 跳出率 平均访问时长
195,557 78,823 60,862 41.31% 00:03:50

图片上传不来,只能打表格,同志们,帮忙分析下?是php-fpm的配置不行还是laravel不适合做这种高并发的程序?

即时连接数

netstat -an|awk '/^tcp/{a[$NF]++}END{for (i in a) print i,a[i]}'

TIME_WAIT 3938
CLOSE_WAIT 6
FIN_WAIT1 72
SYN_SENT 160
FIN_WAIT2 423
ESTABLISHED 1959
SYN_RECV 9
LAST_ACK 19
LISTEN 7
本帖已被设为精华帖!
本帖由 Summer 于 6年前 加精
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 19
leo

很明显slow log出现在curl的部分,也就是你会去请求微信的接口,http请求慢和Laravel没有毛线关系。

6年前 评论

@leo 但是在低并发时,curl的时候请求不慢,php-fpm的cpu占用率也没那么高。人一多,http请求就慢了。这是一一部分比较慢的,下面更多的是Pipeline/Pipeline.php这个类执行的时间太长了。同一个进程下,一直占着不放

6年前 评论
leo

@welkin 建议先掌握php slow log日志所代表的含义,以及pipeline是什么东西再来考虑要不要把锅甩给pipeline

6年前 评论

laravel的中间件用的是管道(Pipeline)模式实现的,所以你日志里的WXVote中间件的上一层就是Pipeline了,根源还是在curl

6年前 评论

@klgd curl 的设置是

    public static function https_request($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 2);
        $data = curl_exec($curl);
        if (curl_errno($curl)){
            return json_encode(array('status'=>'1','response'=>curl_error($curl)));
        }
        curl_close($curl);
        return $data;
    }

超时时间curlopt_timeout设置为2s,为什么会在5s的慢日志里面出现?

6年前 评论

@welkin
CURLOPT_TIMEOUT 设置cURL允许执行的最长秒数。
CURLOPT_TIMEOUT_MS 设置cURL允许执行的最长毫秒数。
CURLOPT_CONNECTTIMEOUT 在发起连接前等待的时间,如果设置为0,则无限等待。
CURLOPT_CONNECTTIMEOUT_MS 尝试连接等待的时间,以毫秒为单位。如果设置为0,则无限等待。

6年前 评论
leo

@我是谁 如果一次请求只调用一次curl,那么curl之间是并行关系,一次能有多少个curl同时进行取决于你的php-fpm进程数目。

6年前 评论

@leo 好的,明白了,谢谢

6年前 评论

pm.max_children = 8

是不是有点太低了???

6年前 评论

一台服务器撑不住500人同时在线,我感觉很正常啊,加一台服务器不就好了么

6年前 评论

猜想一下,你这个是投票系统,然后是需要用到微信授权吗?你看下请求是不是都卡在用户获取(get_userinfo)里面了,就是直接调用微信的接口,如果每次都调用的话肯定会很慢。

6年前 评论
Corwien

@welkin 你可以测试下你的这个 curl 方法 https_request($url) 请求耗时,可能是你这个方法请求的时间过长,试着对请求的URL 的域名做个解析,然后用IP做请求,这样就很快了

6年前 评论

在请求的时候随机开 xhprof ,然后收集一段时间的记录分析一下。

6年前 评论
snow1

其实你这并发并不个高,可以调整下pm.max_children = 8
pm.start_servers = 8
pm.min_spare_servers = 2 这个三个值,稍微调大点,还有主要还是看看你代码逻辑?是不是有些地方比较耗费性能。

6年前 评论

php-fpm占用着cpu一直居高不下
是不是某张数据量过百万的表缺少索引
是不是那里有字段写错的
如果是用的启用laravel log日志看下是那些接口请求时间过长优化代码

6年前 评论

感觉换 Apache 可以一步到位彻底解决。

6年前 评论

@johnlui 牛逼 膜拜大神

6年前 评论

@johnlui php的话用swoole , 同步阻塞模型做不了IO高并发的。
或者直接考虑 golang 和 node.js 。
Laravel 单机并发 200 左右是极限了, swoole golang node.js 异步非阻塞单机 2W 以上没什么问题的~

我们项目有 php 版 和 node 版的实现,可以对比测试下: https://github.com/notadd/notadd

6年前 评论

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