概述
之前项目新部署的服务器刚换为CentOS7时,发现项目延时增高不少,后来发现是由于请求第三方接口时curl过慢导致的,问题CentOS版本为7.1.1503
,需要通过升级libcurl类库来解决。
查找问题
查找问题时通过执行以下命令来测试curl各过程的速度:1
curl -o /dev/null -s -w %{time_namelookup}::%{time_connect}::%{time_starttransfer}::%{time_total}::%{speed_download}"\n" 'http://www.baidu.com'
得到输出:1
0.156::0.159::0.163::0.163::14565.000
各项数据的含义为:
字段 | 含义 |
---|---|
time_namelookup | DNS 解析域名的时间 |
time_connect | client和server端建立TCP连接的时间 |
time_starttransfer | 从client发出请求,到web的server响应第一个字节的时间 |
time_total | client发出请求,到web的server发送会所有的相应数据的时间 |
speed_download | 下载速度,单位byte/s |
从上面的数据可以看到在域名解析这一步就耗费了大量的时间,所以导致整个curl的请求变慢。
解决方案
最后通过Google找到一篇文章里的解决方法,就是升级libcurl。
升级libcurl
1 | mkdir /tmp/libcurl && cd /tmp/libcurl && wget http://dl.fedoraproject.org/pub/archive/fedora/linux/updates/19/x86_64/curl-7.29.0-27.fc19.x86_64.rpm && wget http://dl.fedoraproject.org/pub/archive/fedora/linux/updates/19/x86_64/libcurl-7.29.0-27.fc19.x86_64.rpm && wget http://dl.fedoraproject.org/pub/archive/fedora/linux/updates/19/x86_64/libcurl-devel-7.29.0-27.fc19.x86_64.rpm && yum -y install *.fc19.x86_64.rpm |
重启php-fpm
如果是PHP项目的话,升级libcurl之后要重启fpm。重启命令为:
1 | kill -USR2 `cat /opt/php-*.*.*/var/run/php-fpm.pid` |
注: pid文件的位置根据服务器情况进行修改,也可以直接查出php-fpm的master进程id后,kill -USR2 pid
。