TarsPHP new version is released, support Protobuf agreement

Tars Tencent from 2008 to today has been unified application framework TAF (Total Application Framework) the logic behind layers used, currently supports C ++, Java, PHP, Nodejs the Go language. The framework provides users with a complete solution involves the development, operation and maintenance, and testing of a product or service to help the rapid development, deployment, testing, on-line. It combines scalable protocol codec, high performance RPC communications framework, the name of the route and found release monitoring, log statistics, configuration management is one that can quickly build your own stable and reliable distributed applications through its use of a micro-services, and achieve a complete and effective service governance.

TarsPHP as Tars in solutions PHP language, the main consideration when designing the following four aspects:

  • Fully functional: benchmarking existing C ++ Java and NodeJS system function,
  • Flexible: Flexible theory, who contend with PHP?
  • Lightweight: The lightest design, a superficial, Plug and Play
  • Efficient: Plug Swoole coroutine wings, had to fly

Protobuf Profile

Protocol buffers (referred to as PB) is an open source Google language-neutral, platform-independent, scalable format sequence data can be used for the communication protocol, data storage. It is similar to XML, but smaller than XML, faster and easier.

PB is a coding protocol, if it comes to the transmission network and RPC calls, we need to introduce communication protocols. Google open source framework gRPC RPC as the communication protocol on the use of Http2, PB as a coding protocol.

And TarsGo different about PB support

TarsGo regarding PB support, essentially proto-protocol file support, providing the ability to convert files to tars proto protocol agreement, the actual use of each call is tars agreement. This service can work with each other and other Tars service.

TarsPHP support on PB's, is to build a gRPC service, the service platform deployed on Tars, tars participation platform addressable, tars platform management. This service uses gRPC on Http2 as a network communications protocol, using Protobuf as coding protocol, and other PB client can work with each other.

The two different directions, can not mix, I hope you distinguish.

related data

We use the same Http service, and Pb were used Tars communications protocols and back-end services and pressure measurement.

  • Server environment: 2-core 4G, php 7.2.16, swoole 4.4.0
  • Non-transport service refers to the simple ping to the back-end services, business process without any direct return;
  • Simple single RPC access refers to the number of back-end service returns a barrage int, rand value generation number, and does not use mysql count;
  • Single complexity PRC will actually get a list of the barrage to the backend structure, comprising a plurality of structural integrity barrage object.
QDPS PB TARS
Non-transport services 3800 6200
Single simple RPC 3600 6150
Single complex RPC 1050 1150

Pressure measurement data from the point of view, Tars PB performance higher than a length, but the contrast between the two packing and unpacking found that the performance of PB packing and unpacking performance is slightly better than Tars, the main cause of this result I think is gRPC use Http2 as the communication protocol compared Tars custom communication protocols needed with more overhead.

Use TarsPHP build PB Server

Initialize the environment

  • Protoc installation

    • Protoc first need to install the library, the library's main role is packing and unpacking protobuf protocol data. Can refer  https://github.com/protocolbuffers/protobuf/tree/master/src  installed directly.

      ./autogen.sh
      ./configure
      make
      make install
      

      If protoc -version normal output, the installation is complete

  • php protobuf installation

    • After the need to install php protobuf extension, this extension is mainly used as a bridge in the middle of php and protoc library.

      git clone https://github.com/protocolbuffers/protobuf.git
      cd ./php/ext/google/protobuf/
      phpize
      ./configure
      make
      make install
      (需要编辑php.ini文件,开启protobuf扩展)
      

      If there php -ri protobuf output, the install properly.

  • Swoole installation

    • Recommends using version 4.4.0 or above, you need to open http2 and openssl support.

Write a proto file

tars file reference TarsPHP in ActDemo review services, we wrote a actComment.proto the agreement document.

Agreement document and tars different, predetermined protocol proto input and output parameters must only be a message structure, the input and output parameters is required for a single message in a package.

syntax = "proto3";  
  
package protocol.QD.ActCommentPbServer;  //包名,会根据包名生成 php类路径

service CommentObj {  
  rpc ping(PingRequest) returns (PingResponse) {};  
  
  rpc getCommentCount(CountRequest) returns (CountResponse) {};  
  
  rpc createComment(CreateRequest) returns (CreateResponse) {};  
  
  rpc getComment(GetRequest) returns (GetResponse) {};  
}  
  
//输入参数通用结构体  
message CommonInParam {  
    int32 appId = 1;  
    int32 areaId = 2;  
    int64 userId = 3; //用户信息  
    string userIp = 4; //来源名称  
    string serverIp = 5; //调用方服务器ip  
};  
  
//输出参数通用结构体  
message CommonOutParam {  
    int32 code = 1;  //接口返回码  
    string message = 2;  //接口返回提示信息  
};  
  
message SimpleComment {  
    int32 id = 1;  
    int64 activityId = 2;  
    int64 userId = 3;  
    string content = 4;  
    string title = 5;  
    string ext1 = 6;  
    int64 createTime = 7;  
};  
  
message QueryParam {  
    int64 activityId = 1;  
    int32 page = 2;  
    int32 size = 3;  
    int32 orderType = 4;  
};  
  
message CreateRequest {  
    CommonInParam inParam = 1;  
    SimpleComment comment = 2;  
};  
  
message CreateResponse {  
    CommonOutParam outParam = 1;  
};  
  
message GetRequest {  
    CommonInParam inParam = 1;  
    QueryParam queryParam = 2;  
};  
  
message GetResponse {  
    CommonOutParam outParam = 1;  
    repeated SimpleComment list = 2;  
};  
  
message PingRequest {  
};  
  
message PingResponse {  
};  
  
message CountRequest {  
};  
message CountResponse {  
    int32 count = 1;  
};

Generating server-side code

protoc proto file may be generated in accordance with the corresponding class code php, but does not support the official file generating proto server-side code may be used to generate gRPC widget client code. If you need to generate the client code that we also need to install grpc libraries and grpc php extension.

So our idea is to use php protoc generate the required class and then resolve their own proto file generation server-side interface, this process is very much like the existing tars2php process, so we call it proto2php.

Due to the use of two tools also generate too much trouble, we call proto in the process of integration into proto2php facilitate use.

Let's build a tars.proto.php set some basic information.

return array(
    'appName' => 'QD',
    'serverName' => 'ActCommentPbServer',
    'objName' => 'CommentObj',
    'withServant' => true, //决定是服务端,还是客户端的自动生成
    'tarsFiles' => array(
        './actComment.proto',
    ),
    'dstPath' => '../src/protocol', //这里指定的是 impl 基础interface 生成的位置
    'protocDstPath' => '../src', //这里指定的是 protoc 生成的问题
    'namespacePrefix' => 'Protocol',
);

Then execute php ... / src / vendor / phptars / tars2php / src / proto2php.php ./tars.proto.php
have been generated GPBMetadata directories and directory protocol. Wherein the protocol file is generated proto class php, it is additionally CommentObjServant.php proto2php file generated server-side class interface. Construction of TarsPHP pb server need to implement this class.

TarsPHP PB server deployment

According to the Demo Readme tarsphp pb server can be deployed.

Few things to note:

  1. We need to implement the interface logic impl directory.
  2. Home-api specified in the src in services.php, home-class position, protocolName is pb, serverType is grpc
  3. Tars platform on the type of protocol is tcp, non-tars agreement.
  4. The need to add composer.json require "google / protobuf", autoload Protocol needs to be configured and GPBMetadata, as shown here:
    {
        "name" : "tars-tcp-server-demo",
        "description": "tars tcp server",
        "require": {
            "phptars/tars-server": "~0.3",
            "phptars/tars-deploy": "~0.1",
            "phptars/tars-log": "~0.1",
            "phptars/tars2php": "~0.1",
            "ext-zip" : ">=0.0.1",
            "google/protobuf": "^3.8"
        },
        "autoload": {
            "psr-4": {
                "Server\\" : "./",
                "Protocol\\" : "./protocol",
                "GPBMetadata\\" : "./GPBMetadata"
            }
        },
        "minimum-stability": "stable",
        "scripts" : {
            "deploy" : "\\Tars\\deploy\\Deploy::run"
        }
    }
    

The last execution composer run-script deploy, generate code package, upload them to the Tars platform.

Use client access

You can use gRPC generated php client access test, you can directly build a grpc client uses http2 client swoole of.

class TestGrpcClient
{
    public static function callGrpc($ip, $port, $path, $requestBuf)
    {
        $cli = new Swoole\Coroutine\Http2\Client($ip, $port, false);
        $cli->connect();
        $req = new swoole_http2_request;
        $req->method = 'POST';
        $req->path = $path;
        $req->headers = [
            "user-agent" => 'grpc-c/7.0.0 (linux; chttp2; gale)',
            "content-type" => "application/grpc",
            "grpc-accept-encoding" => "identity,deflate,gzip",
            "accept-encoding" => "identity,gzip",
            "te" => "trailers",
        ];
        $req->pipeline = false;
        $req->data = $requestBuf;
        $cli->send($req);
        $response = $cli->recv();
        return $response->data;
    }

    public static function main()
    {
        $commonIn = new CommonInParam();
        $commonIn->setUserId(0);
        $commonIn->setAppId(1);
        $commonIn->setAreaId(10);
        $commonIn->setServerIp('127.0.0.1');
        $commonIn->setUserIp('');

        $query = new QueryParam();
        $query->setActivityId(123);
        $query->setPage(1);
        $query->setSize(10);
        $query->setOrderType(1);

        $request = new GetRequest();
        $request->setInParam($commonIn);
        $request->setQueryParam($query);
        
        $requestBuf = $request->serializeToString();
        $packBuf = pack('CN', 0, strlen($requestBuf)) . $requestBuf;

        go(function () use ($packBuf){
            $path = "/protocol.QD.ActCommentServer.CommentObj/getComment";
            $ret = self::callGrpc('127.0.0.1', 10008, $path, $packBuf); //这里注意要修改成你服务在tars上绑定的ip 127.0.0.1不一定可以
            $response = new GetResponse();
            $response->mergeFromString(substr($ret, 5));
            foreach ($response->getList() as $row) {
                var_dump($row->getContent());
            }
        });
    }
}

Execute php client.php observed return.

Generate client-side code

client mentioned earlier, but we simply demo access PB server, you can help us test the state of PB server. If you need to call in other Tars services PB server how to use it? Tars and we also offer a similar way to generate PB client-side code.

As used herein QD.ActHttpServer under TarsActDemo as an example of how to generate Tars PB client code and call the PB Service.

  1. ActComment.proto copy files to a directory tars
  2. ActCommentPb.proto.php build files, content, and generating a content code server tars.proto.php coincides modified withServant = false
  3. Execute php ... / src / vendor / phptars / tars2php / src / proto2php.php ./actCommentPb.proto.php
  4. After the generated code can see the relevant protocol / QD / ActCommentPbServer in. (Server-side code and similar, CommentObjServant.php proto2php is generated, other documents proto2php call protoc plug-generated)
  5. Server-side and the like need to be added to the Protocol and GPBMetadata composer.json in the psr-4.
  6. And Tars similar call, you can call directly related to the method CommentObjServant class and PB communications services. Note that the incoming CommunicatorConfig in socketModel need to be set to 4 grpc mode.

Here's an example:

$inParam = new CountRequest();  
$outParam = new CountResponse();  
  
$conf = self::getConfig();  
$conf->setSocketMode(4);  
  
$servant = new CommentObjServant($conf);  
$servant->getCommentCount($inParam, $outParam);  
  
return $outParam->getCount();

Welcome to contribute and participate in the project point star.

https://github.com/TarsPHP/TarsPHP

Author: Zhang Yong

Guess you like

Origin www.oschina.net/news/108869/tarsphp-support-protobuf