怎么配置php模块,PHP GRPC 模块安装配置

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 Lisa\GreeterClient('localhost:12345', [

'credentials' => Grpc\ChannelCredentials::createInsecure(),

]);

$request = new Lisa\LisaRequest();

$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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值