慢業(yè)務(wù)處理
有時(shí)候我們需要處理慢業(yè)務(wù),為了避免慢業(yè)務(wù)影響webman的其它請(qǐng)求處理,這些業(yè)務(wù)根據(jù)情況不同可以使用不同的處理方案。
方案一 使用消息隊(duì)列
優(yōu)點(diǎn)
可以應(yīng)對(duì)突發(fā)海量業(yè)務(wù)處理請(qǐng)求
缺點(diǎn)
無(wú)法直接返回結(jié)果給客戶端。如需推送結(jié)果需要配合其它服務(wù),例如使用 webman/push 推送處理結(jié)果。
方案二 新增HTTP端口
新增HTTP端口處理慢請(qǐng)求,這些慢請(qǐng)求通過(guò)訪問(wèn)這個(gè)端口進(jìn)入特定的一組進(jìn)程處理,處理后將結(jié)果直接返回給客戶端。
優(yōu)點(diǎn)
可以直接將數(shù)據(jù)返回給客戶端
缺點(diǎn)
無(wú)法應(yīng)對(duì)突發(fā)的海量請(qǐng)求
實(shí)施步驟
在 config/process.php
里增加如下配置。
return [
// ... 這里省略了其它配置 ...
'task' => [
'handler' => \Webman\App::class,
'listen' => 'http://0.0.0.0:8686',
'count' => 8, // 進(jìn)程數(shù)
'user' => '',
'group' => '',
'reusePort' => true,
'constructor' => [
'requestClass' => \support\Request::class, // request類(lèi)設(shè)置
'logger' => \support\Log::channel('default'), // 日志實(shí)例
'appPath' => app_path(), // app目錄位置
'publicPath' => public_path() // public目錄位置
]
]
];
這樣慢接口可以走 http://127.0.0.1:8686/
這組進(jìn)程,不影響其它進(jìn)程的業(yè)務(wù)處理。
為了讓前端無(wú)感知端口的區(qū)別,可以在nginx加一個(gè)到8686端口的代理。假設(shè)慢接口請(qǐng)求路徑都是以/tast
開(kāi)頭,整個(gè)nginx配置類(lèi)似如下:
upstream webman {
server 127.0.0.1:8787;
keepalive 10240;
}
# 新增一個(gè)8686 upstream
upstream task {
server 127.0.0.1:8686;
keepalive 10240;
}
server {
server_name webman.com;
listen 80;
access_log off;
root /path/webman/public;
# 以/tast開(kāi)頭的請(qǐng)求走8686端口,請(qǐng)按實(shí)際情況將/tast更改為你需要的前綴
location /tast {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://task;
}
# 其它請(qǐng)求走原8787端口
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Connection "";
if (!-f $request_filename){
proxy_pass http://webman;
}
}
}
這樣客戶端訪問(wèn)域名.com/tast/xxx
時(shí)將會(huì)走單獨(dú)的8686端口處理,不影響8787端口的請(qǐng)求處理。
方案三 利用http chunked 異步分段發(fā)送數(shù)據(jù)
優(yōu)點(diǎn)
可以直接將數(shù)據(jù)返回給客戶端
安裝 workerman/http-client
composer require workerman/http-client
app/controller/IndexController.php
<?php
namespace app\controller;
use support\Request;
use support\Response;
use Workerman\Protocols\Http\Chunk;
class IndexController
{
public function index(Request $request)
{
$connection = $request->connection;
$http = new \Workerman\Http\Client();
$http->get('https://example.com/', function ($response) use ($connection) {
$connection->send(new Chunk($response->getBody()));
$connection->send(new Chunk('')); // 發(fā)送空的的chunk代表response結(jié)束
});
// 先發(fā)送一個(gè)http頭,后續(xù)數(shù)據(jù)通過(guò)異步發(fā)送
return response()->withHeaders([
"Transfer-Encoding" => "chunked",
]);
}
}
提示
本例中使用了workerman/http-client
客戶端異步獲取http結(jié)果并返回?cái)?shù)據(jù),也可以使用其它異步客戶端例如 AsyncTcpConnection 。