RPC

驱动类型

驱动 描述
Jsonrpc Jsonrpc协议rpc客户端
Http rpc调用风格的httpClient封装
Rest rpc调用风格的Rest httpClient封装
Thrift Thrift rpc客户端
Grpc Grpc rpc客户端

配置示例

return [
'standard'=> [
'driver' => 'http',
'host' => 'http://standard.example.com',
'response_decode' => 'json'
],
'rest'=> [
'driver' => 'rest',
'host' => 'http://rest.example.com',
'response_decode' => 'json'
],
'jsonrpc'=> [
'driver' => 'jsonrpc',
'host' => 'http://jsonrpc.example.com',
//'requset_serialize' => 'msgpack_serialize',
//'response_unserialize' => 'msgpack_unserialize',
],
'grpc' => [
'driver' => 'grpc',
'host' => '127.0.0.1',
'port' => 50051,
'prefix' => 'TestGrpc',
'auto_bind_param' => true,
'service_schemes' => [
'prefix' => [
'TestGrpc' => APP_DIR.'scheme/grpc/TestGrpc/'
],
'map' => [
'GPBMetadata\User' => APP_DIR.'scheme/grpc/GPBMetadata/User'
]
]
],
'thrift' => [
'driver' => 'thrift',
'host' => '127.0.0.1',
'port' => 9090,
'prefix' => 'pingpong_thrift',
'service_schemes' => [
'prefix' => [
'pingpong_thrift' => APP_DIR.'resource/thrift/pingpong_thrift/'
],
'files' => [
APP_DIR.'resource/thrift/pingpong_thrift/Types'
]
]
],
'zhihu'=> [
'driver' => 'rest',
'host' => 'https://www.zhihu.com/api/v4',
'headers' => [
'Authorization:oauth your_key'
],
'response_decode' => 'json',
],
'github'=> [
'driver' => 'rest',
'host' => 'https://api.github.com',
'headers' => [
'Accept: application/vnd.github.v3+json',
'User-Agent: test',
'Authorization: Basic your_key'
],
'curlopt' => [
'timeout' => 10
],
'response_decode' => 'json',
],
];

使用示例

// 知乎rest api调用
$zhihu->answers->get($id);
// jsonrcp 服务调用
$jsonrpc->User->getName($id);
// jsonrcp 服务批量调用
$jsonrpc->batch()
->User->getName(1)
->User->getName(2)
->User->getName(3)
->call();
// thrift 服务调用,使用thriftpy创建的测试服务
$thrift->PingPong->add(1, 2);
// grpc 服务调用
$grpc->User->getName($id);