Laravel部署后,CPU 使用率过高,配合使用swoole。
我在部署 Laravel 应用程序时遇到严重问题。当访问量稍微大一点的时候,cpu马上就到100%了,
找了一大堆文档和说明,都是说明laravel处理并发的能力太弱,还不如原生的php。最后找到swoole解决问题。
1、php下载swoole插件,这个不用多介绍,自己自行百度即可。
2、larave 安装 laravel-swoole
composer require swooletw/laravel-swoole
如果存在版本冲入,可强制下载
composer require swooletw/laravel-swoole --ignore-platform-reqs
3、发布配置文件,并修改对应端口。
php artisan vendor:publish --tag=laravel-swoole
这个是成功后的界面:
修改对应配置端口号:
4、注册:config/app.php
'providers' => [
...........
SwooleTW\Http\LaravelServiceProvider::class,
]
4、配置代理(伪静态)
902 端口根据自己的配置
location / {
proxy_pass http://127.0.0.1:902;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
rewrite /sw/(.*) /$1 break;
proxy_redirect off;
}
location /admin {
try_files $uri $uri/ /index.php?$query_string;
}
5、命令
开始运行
php artisan swoole:http start
常驻运行:
php artisan swoole:http start &
停止
php artisan swoole:http stop
注意事项
如果存在异常,首先查看命令行的php版本是否正确
在 Laravel 中启用 Swoole 后,使用 Guzzle HTTP 客户端(或类似的 HTTP 客户端)进行请求时出现错误,可能是由于 Swoole 的协程特性与某些同步代码不兼容。
post请求出错
原文:$response = $client->post('https://xxx.com/api/id-verify/two-element', [ 'headers' => [ 'Content-Type'=>'text/plain', 'app-id' => 80003, 'req-sn' => '"'.$this->CreateOrder().'"', ], 'body' => $encryptedBase64, ]); $body = $response->getBody()->getContents();
需要修改为:
use Swoole\Coroutine\Http\Client as swoole_client;
$client = new swoole_client('apx.yuanxinw.com', 443, true);
$client->setHeaders([
'Content-Type' => 'text/plain',
'app-id' => 80003,
'req-sn' => '"' . $this->CreateOrder() . '"',
]);
$client->post('/api/id-verify/two-element', $encryptedBase64);
$body = $client->body;
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: