protobuf 文件编译成PHP文件
lisa.proto文件syntax = "proto3";
package lisa;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayName (LisaRequest) returns (LisaReply) {}
}
// The request message containing the user's name.
message LisaRequest {
string name = 1;
}
// The response message containing the greetings
message LisaReply {
string message = 1;
}
编译protobuf 文件生成PHP代码$ protoc --proto_path=./ --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=/usr/local/bin/grpc_php_plugin ./lisa.proto
#执行成功之后可以看到生成生成以下文件
#Lisa/GreeterClient.php
#Lisa/LisaReply.php
#Lisa/LisaRequest.php
#GPBMetadata/Lisa.php
PHP客户端代码lisa_client.php
include __DIR__ . '/vendor/autoload.php';
include __DIR__ . '/GPBMetadata/Lisa.php';
include __DIR__ . '/Lisa/LisaReply.php';
include __DIR__ . '/Lisa/LisaRequest.php';
include __DIR__ . '/Lisa/GreeterClient.php';
$client = new LisaGreeterClient('localhost:12345', [
'credentials' => GrpcChannelCredentials::createInsecure(),
]);
$request = new LisaLisaRequest();
$name = !empty($argv[1]) ? $argv[1] : 'world';
$request->setName($name);
list($reply, $status) = $client->SayName($request)->wait();
$message = $reply->getMessage();
echo $message,PHP_EOL;
?>
//要先运行gRPC服务端代码
//服务端用node 实现 PHP 不支持
//服务端代码看d介绍
//php lisa_client.php 执行文件
//执行成功数据 Hello world
gRPC 不支持PHP服务端用NODE代替lisa_server.js
//需要安装grpc模块,npm install grpc
//node 不需要把protbuf文件翻译成相应代码,可以直接引入protobuf 文件
//lisa_server.js
var PROTO_PATH = __dirname + '/lisa.proto';
var grpc = require('grpc');
var lisa_proto = grpc.load(PROTO_PATH).lisa;
/**
* Implements the SayHello RPC method.
*/
function sayName(call, callback) {
callback(null, {message: 'Hello ' + call.request.name});
}
/**
* Starts an RPC server that receives requests for the Greeter service at the
* sample server port
*/
function main() {
var server = new grpc.Server();
server.addProtoService(lisa_proto.Greeter.service, {sayName: sayName});
server.bind('0.0.0.0:12345', grpc.ServerCredentials.createInsecure());
server.start();
}
main();
// node lisa_server.
如果觉得《php yat grpc PHP GRPC 模块安装配置-Go语言中文社区》对你有帮助,请点赞、收藏,并留下你的观点哦!