hprose-php icon indicating copy to clipboard operation
hprose-php copied to clipboard

服务端并发问题

Open zeroxxmmbm opened this issue 1 year ago • 2 comments

你好,服务端不管使用Socket的TCP版本或者是swoole的tcp版本 定义了两个rpc服务,其中一个有耗时处理请求,一个没有

客户端定义两个路由接口,分别用http\Client `同步调用,先调用耗时服务请求后,再调用无耗时的方法,无耗时的方法永远都要等待耗时服务处理完才能返回结果,请问两个方法同时访问服务的时候,如果是swoole的不是会协程接管请求跟响应吗?怎么解决这个问题?

RpcServer `use Hprose\Swoole\Server;

class RpcServer { public function init(){

    $services = config('rpc_service.');
    if(!$services){
        throw new \Exception('rpc_service config empty');
    }

    $server = new Server('tcp://0.0.0.0:8777');
    $server->setDebugEnabled(true); // 开启调试
    foreach($services as $cls => $prefix){
        $server->addInstanceMethods(new $cls,'',$prefix);
    }
    $server->addFilter(new AuthFilter());
    $server->start();
}

}`

RpcClient Route::get('rpc_test',function(){ $client = new Hprose\Socket\Client('tcp://192.168.3.251:8777', false); // $client = app\lib\rpcClient\RpcClient::getClient(); $client->addFilter(new app\lib\rpcClient\client\AuthFilter()); return $client->test_sub(5,10); }); Route::get('rpc_calc',function(){ $client = new Hprose\Socket\Client('tcp://192.168.3.251:8777', false); // $client = app\lib\rpcClient\RpcClient::getClient(); $client->addFilter(new app\lib\rpcClient\client\AuthFilter()); return $client->calc_concat('a','c'); });

zeroxxmmbm avatar Apr 19 '25 03:04 zeroxxmmbm

邮件已收到!

xinlongnews avatar Apr 19 '25 03:04 xinlongnews

补充问上述问题与新增一个http的提问 1、是否客户端也需要使用swoole协程异步的才行?使用Socket\Client就会导致阻塞? 2、还有一个问题就是使用普通的fpm模式的Http/Client用不了不知道什么原因 http服务定义成TP的一个路由 访问 http://127.0.0.1:5500/consumer/v1/rpc_server/service_list 正常输出内容Fa5{u#s8"calc_add"s11"calc_concat"s8"test_sub"s11"test_concat"}z

路由已定义成Route::any 支持 get post 然后客户端调用是报错 $host $port都确认过能正常读取配置文件内容 Operation timed out after 30012 milliseconds with 0 bytes received public static function getHttpClient() { if (!self::$instance) { $host = config('rpc_server.host'); $port = (string)config('rpc_server.port'); $uri = '/consumer/v1/rpc_server/service_list'; self::$instance = new HttpClient('http://'.$host.':'.$port.$uri, false); self::$instance->addFilter(new AuthFilter()); } return self::$instance; } $client = RpcClient::getHttpClient(); $a = $client->test_sub(5,10);

zeroxxmmbm avatar Apr 19 '25 03:04 zeroxxmmbm