RPC、Yar、gRPC

自 11 月后端结构调整正式启动已有月余,效果初现;
PHP Yar(Yet Another RPC Framework)是我们采取的措施之一;
版本 1.3 开发过程中,Yar 服务 5XX 错误频频,问题解决速度远低于期望;
调试(Debug)技术、测试技术、观测手段的匮乏,恐怕是其中最大的原因之一;
源于超人在 PHPUnit 上的 最新尝试,我们试图在这方面上一个台阶:高质量的敏捷交付;

RPC(Remote Procedure Calling) Mechanism

《RPC、Yar、gRPC》

A remote procedure is uniquely identified by the triple: (program number, version number, procedure number) The program number identifies a group of related remote procedures, each of which has a unique procedure number. A program may consist of one or more versions. Each version consists of a collection of procedures which are available to be called remotely. Version numbers enable multiple versions of an RPC protocol to be available simultaneously. Each version contains a a number of procedures that can be called remotely. Each procedure has a procedure number.

Yar 在 GitHub

Yar is a RPC framework which aims to provide a simple and easy way to do communication between PHP applications.
It has the ability to concurrently call multiple remote services.

  • 是一个 PECL 扩展,属于 Web Service 分类;
  • 采用 HTTP 协议传输数据;
  • 多种 data packager:php,json,msgpack;
    msgpack 需要单独安装扩展;
  • Runtime Configure

    《RPC、Yar、gRPC》

示例
  • yarserver.php
<?php
class API {
    public function testName($name) {
        error_log("Hello, $name\n", 3, "/logs/yar.log");
        return "Hello, $name";
    }
    public function testAdd($a, $b) {
        $c = $a + $b;
        error_log("$a + $b = $c\n", 3, "/logs/yar.log");
        return "$a + $b = $c";
    }
}
$service = new Yar_Server(new API());
$service->handle();
<?php
    $client = new Yar_Client("http://api.example.com/yarserver.php");
    echo $client->testName("michael");
    echo $client->testAdd(2,3);
如何对 RPC 服务进行单独测试?
关于 gRPC

Google 名下的开源项目;

《RPC、Yar、gRPC》 Works across languages and platforms

  • gRPC Concepts
    Like many RPC systems, gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types. By default, gRPC uses protocol buffers as the Interface Definition Language (IDL) for describing both the service interface and the structure of the payload messages.
  • A Basic PHP Programmer’s Introduction to Working with gRPC
  • Why use gRPC?
    With gRPC you can define your service once in a .proto file and implement clients and servers in any of gRPC’s supported languages, which in turn can be run in environments ranging from servers inside Google to your own tablet – all the complexity of communication between different languages and environments is handled for you by gRPC. You also get all the advantages of working with protocol buffers, including efficient serialization, a simple IDL(Interface Definition Language), and easy interface updating.
  • Protocol Buffers – Google’s data interchange format.
    Protocol Buffers (a.k.a., protobuf) are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data. You can find protobuf’s documentation on the Google Developers site.
  • Protocol Buffers are similar to the Apache Thrift (used by Facebook) or Microsoft Bond protocols.
    Comparison of data serialization formats.
  • gRPC library for PHP
  • SOASOA Reference Model
  • HTTP Status Code 499
    HTTP 499 in Nginx means that the client closed the connection before the server answered the request. In my experience it is usually caused by client side timeout. As I know it’s an Nginx specific error code.

https://gist.github.com/dzlab/2e6e79419877dc29d2efc63ae5974fd5
https://danielmiessler.com/blog/log-post-data-nginx/#gs.ZM_5pEY
http://developers.redhat.com/blog/2016/05/23/configuring-nginx-to-log-post-data-on-linux-rhel/
http://stackoverflow.com/questions/4939382/logging-post-data-from-request-body
https://laracasts.com/discuss/channels/general-discussion/laravel-and-rpc
http://restfulapi.nl/#what1
http://www.jsonrpc.org/
https://miniflux.net/documentation/json-rpc-api
http://pages.cs.wisc.edu/~remzi/OSTEP/dist-intro.pdf

    原文作者:michael_jia
    原文地址: https://www.jianshu.com/p/28bb28212ee6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞