這種架構下,這樣的優化策略能實現嗎?能有作用嗎?
php 服務端請求 ES tcp server 部分代碼
$streamClient = stream_socket_client("tcp://{$tcpHost}:{$tcpPort}", $errno, $errstr);
// 該數組是所有業務線的分類結構,及每條業務線要搜索的關鍵字,單個元素對應一條業務線
$categoriesFilter = [];
foreach ($categoriesFilter as $categoryFilter) {
fwrite($streamClient, json_encode($categoryFilter);
}
然后我就不知道該如何實現寫的同時接收數據了。并保證每條業務線的數據都發送了,也收到響應了。
ES tcp server 部分代碼
$searchServer = new \swoole_server('0.0.0.0', 9501);
$searchServer->on('connect', function (\Swoole\Server $server, $fd) {
dump($server->connection_info($fd));
});
$searchServer->on('receive', function (\Swoole\Server $server, $fd, $reactor_id, $request) {
global $keyword;
global $elastic;
$requestArray = json_decode($request, true);
$keyword = $requestArray;
// es 搜索代碼
$result = $elastic->indexFilterAndSearch($requestArray);
$server->send($fd, json_encode($result));
});
$searchServer->on('WorkerStart', function (\Swoole\Server $server, $work_id) {
global $elastic;
$elastic = new ElasticSearch;
});
$searchServer->on('close', function (\Swoole\Server $server, $fd, $reactorId) {
global $keyword;
$clientConnectionInfo = $server->connection_info($fd);
file_put_contents('swoole_log.log', json_encode([
'connectionTime' => date('Y-m-d H:i:s', $clientConnectionInfo['connect_time']),
'closeTime' => date('Y-m-d H:i:s'),
'keyword' => $keyword,
'ip' => $clientConnectionInfo['remote_ip']
]) . PHP_EOL, FILE_APPEND);
});
$searchServer->start();
求各位大佬指點一下,這樣優化行不行?如果行的話,我上面的兩個問題該從那方面,那里入手解決啊。。。