.proto
文件定義sudo yum -y install unzip libtool git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc cd grpc git pull --recurse-submodules && git submodule update --init --recursive make & sudo make install # 編譯導出文件中,經常使用的有: # grpc/bins/opt/protobuf/protoc:proto文件編譯器 # grpc/bins/opt下生成了各種語言的gRPC編譯插件,用於配合protoc編譯器生成客戶端基類 sudo pecl install grpc sudo pecl install protobuf composer require grpc/grpc --profile --prefer-dist --optimize-autoloader composer require google/protobuf --profile --prefer-dist --optimize-autoloader
proto文件示例php
syntax = "proto3"; package grpc.services.list; #默認包路徑 //針對不一樣語言自定義包路徑 option php_package = "com.service.grpc"; //服務 service ListService { //方法定義 rpc FetchListByUid(ListRequest) returns (ListResponse) {}; } //請求 message ListRequest { string uid = 1; } //響應 message ListResponse { string result = 1; }
編譯proto文件,導出客戶端類git
protoc --php_out=src/generated_code/grpc \ --grpc_out=src/generated_code/grpc \ --plugin=protoc-gen-grpc=bins/opt/grpc_php_plugin \ path/your.proto
調用示例github
$request = (new ListRequest())->setUid($id); $client = new ListServiceClient("gRPC主機:端口", [ 'credentials' => ChannelCredentials::createInsecure(), 'timeout' => 1000000, ]); list($reply, $status) = $client->FetchListByUid($request)->wait(); $reply->getXxx(); $client->close();