eliep / avro-rpc-php

PHP中的Avro RPC客户端(兼容Java中的Avro RPC服务器)

1.7.7-p0 2016-08-20 12:39 UTC

This package is not auto-updated.

Last update: 2024-09-26 03:55:20 UTC


README

有关Avro及其在PHP中使用的完整文档,请参阅Avro

此库是原始Avro库的分支,仅添加了PHP Avro RPC客户端

安装

composer require eliep/avro-rpc-php

用法

为您自己的协议创建一个客户端

/**
 * $protocol: your Avro protocol as a string
 * $serverHost: Avro RPC Server Host
 * $serverPort: Avro RPC Server Port
 **/
 
// Parse your avro protocol
$avroProtocol = AvroProtocol::parse($protocol);
// Connect to the server
$client = NettyFramedSocketTransceiver::create($serverHost, $serverPort);
// Retrieve a client
$requestor = new Requestor($avroProtocol, $client);

请求服务器

只需使用Requestor实例的request方法。此方法有两个参数

  • 消息名称,如您的avro协议中定义的
  • 一个由消息请求部分定义的命名参数数组

例如,如果您的协议是

{
 ...
 "types": [
     {"type": "record", "name": "SimpleRequest",
      "fields": [{"name": "subject",   "type": "string"}]
     },
     {"type": "record", "name": "SimpleResponse",
      "fields": [{"name": "response",   "type": "string"}]
     }
 ],

 "messages": {
     "testSimpleRequestResponse": {
         "doc" : "Simple Request Response",
         "request": [{"name": "message", "type": "SimpleRequest"}],
         "response": "SimpleResponse"
     }
 }
}
try {
  $response = $requestor->request('testSimpleRequestResponse', array("message" => array("subject" => "pong")));
  echo "Response received: ".json_encode($response)."\n";
} catch (AvroRemoteException $e) {
  // an error occured on the server while handling the request.
}

示例

一个RPC客户端示例位于examples/sample_rpc_client.php中。它可以与examples/sample_rpc_server.php一起使用来测试客户端/服务器通信。

  • 运行php examples/sample_rpc_server.php
  • 然后在另一个控制台中运行php examples/sample_rpc_client.php

测试

测试可以使用以下方式运行

phpunit test/AllTests.php

这些大多是原始的Avro测试,除了test/IpcTest.php文件外