connor/rpc-client

微服务RPC调用客户端

1.0.1 2018-04-03 13:10 UTC

This package is not auto-updated.

Last update: 2024-09-29 06:06:21 UTC


README

在微服务和服务化场景下,项目之间存在许多远程调用场景,在一次请求中存在多次的远程RPC调用,这种远程调用有的是基于TCP的,有的是基于HTTP的。为了提升调用性能,我们必须支持异步并发调用,同时,我们还必须跟踪每次调用的调用链。RpcClient正好解决这两个问题。

安装

直接通过composer安装

功能和使用说明

基于HTTP协议的RPC调用

HttpRpcClientUtil在guzzle promise的基础上提供了同步和异步的HTTP远程调用类封装,在日志中收集了调用的信息,可以用于调用链的性能分析

//配置不同的handler来处理
$config = [
//        'handler' => new \GuzzleHttp\Handler\StreamHandler(),
//    'handler' => new \CClehui\RpcClient\GuzzleHandler\StreamSocketHandler(),
    'handler' => new \CClehui\RpcClient\GuzzleHandler\SocketHandler(),
];

//同步调用demo
$url = 'http://0.0.0.0/temp/test.php;
$params = [];
$rpc_client = new \CClehui\RpcClient\HttpRpcClientUtil();
$rpc_client->setGuzzleClientConfig($config);
$res = $rpc_client->callRemote($url, $params);

//异步调用demo (promise机制)
$url = 'http://0.0.0.0/temp/test.php';
$params = [];
$promises = [];
$rpc_client = new \CClehui\RpcClient\HttpRpcClientUtil();();
$rpc_client->setGuzzleClientConfig($config);

for ($i = 1; $i <= 2; $i++) {
    $promises[$i] = $rpc_client->callRemote($url, $params, 'GET', [], true);
}

$result_list = \GuzzleHttp\Promise\settle($promises)->wait();

foreach($result_list as $key => $item) {
    $response = $item['value'];
    
    $response = (string)$response->getBody();
    echo $response . "\n";
}

更详细的使用示例在 examples\HttpRpcDemo.php中

StreamSocketHandler

见demo中的config配置,可以配置该handler

SocketHandler

见demo中的config配置,可以配置该handler