600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > php yat grpc PHP GRPC 模块安装配置-Go语言中文社区

php yat grpc PHP GRPC 模块安装配置-Go语言中文社区

时间:2020-10-07 22:56:07

相关推荐

php yat grpc PHP GRPC 模块安装配置-Go语言中文社区

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.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。