grpc php 返回值过大,使用grpc实现php、java、go三方互调

grpc作为经典的rpc协议,虽然略重,但是是有学习的价值的

通过下面的内容可以快速上手这个grpc框架

安装命令行工具

php需要这个额外的protoc、grpc_php_plugin工具把这个protobuf格式的文件生成php语言里的类

go需要安装protoc-gen-go工具把protobuf格式的接口定义文件生成go语言里的类定义

java无需手动安装额外工具,有maven即可,再次体现了java的优越性(

protoc

fb91e3275766c62e665c11eb6a171ac7.png

解压后,把bin目录放到PATH里面

编译grpc_php_plugin

git clone https://github.com/grpc/grpc.git

cd grpc

brew install autoconf automake libtool shtool

LIBTOOL=glibtool LIBTOOLIZE=glibtoolize make -j8

复制代码

grpc源码库较大,可以直接开8核进行编译 编译好的grpc_php_plugin在bins/opt/目录下,把grpc_php_plugin放到PATH里面

2408873ed5e6e3af7193f5bf5b770b35.png

安装php的grpc扩展

编译protoc-gen-go

git clone https://github.com/golang/protobuf.git

cd protobuf/protoc-gen-go

go build

复制代码

操作效果如下

0863284fe1e419cf96c4563b4b33b3dc.png

把这个生成的protoc-gen-go放到PATH

案例设计

go: 提供grpc服务端,提供一个sayHello的接口,参数为代表名字的字符串,返回值为 “Hello"拼接上这个名字 java和php作为客户端调用这个go的客户端

grpc依赖

go

"golang.org/x/net/context"

"google.golang.org/grpc"

"google.golang.org/grpc/reflection"

复制代码

java

io.grpc

grpc-netty

1.0.0

io.grpc

grpc-protobuf

1.0.0

io.grpc

grpc-stub

1.0.0

复制代码

php

"grpc/grpc": "^v1.9",

"google/protobuf": "^v3.5"

复制代码

核心代码

proto文件

syntax = "proto3";

option go_package = "pbf";

package helloworld;

// The greeting service definition.

service Hello {

// Sends a greeting

rpc SayHello (HelloRequest) returns (HelloResponse) {}

}

// The request message containing the user's name.

message HelloRequest {

string name = 1;

}

// The response message containing the greetings

message HelloResponse {

string message = 1;

}

复制代码

这个protobuf文件定义了两个数据结构,HelloRequest和HelloResponse,和一个SayHello的接口

使用下面的命令把这个proto文件编译成对应的php和go的数据结构

#!/bin/bash

for file in ./protos/*.proto; do

echo "生成对应的php类文件: $file"

protoc -I ./protos --php_out=./php/pbf --grpc_out=./php/pbf --plugin=protoc-gen-grpc=$(which grpc_php_plugin) $file

done

protoc -I ./protos --go_out=plugins=grpc:./go/pbf ./protos/*.proto

复制代码

go服务端核心代码

// helloServer implements helloworld.HelloServer

type helloServer struct{}

// SayHello implements helloworld.HelloServer

func (s *helloServer) SayHello(ctx context.Context, in *pbf.HelloRequest) (*pbf.HelloResponse, error) {

return &pbf.HelloResponse{Message: "Hello " + in.Name}, nil

}

func main() {

s := grpc.NewServer()

pbf.RegisterHelloServer(s, &helloServer{})

reflection.Register(s)

s.Serve(lis)

}

复制代码

func (s *helloServer) SayHello(ctx context.Context, in *pbf.HelloRequest) (*pbf.HelloResponse, error)里面就是SayHello这个接口的具体实现了

php客户端核心代码

$name = '小明';

$client = new Helloworld\HelloClient('localhost:8488', [

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

]);

$request = new Helloworld\HelloRequest();

$request->setName($name);

list($response, $status) = $client->SayHello($request)->wait();

echo '服务端返回状态码: ' . $status->code . PHP_EOL;

echo "服务器端回复内容:" . $response->getMessage() . PHP_EOL;

复制代码

code 0表示正常

java客户端核心代码

private final HelloGrpc.HelloBlockingStub blockingStub;

public HelloClient(String host, int port) {

//初始化连接

channel = ManagedChannelBuilder.forAddress(host, port)

.usePlaintext(true)

.build();

//初始化远程服务Stub

blockingStub = HelloGrpc.newBlockingStub(channel);

}

public String sayHello(String name) {

//构造服务调用参数对象

Helloworld.HelloRequest request = Helloworld.HelloRequest.newBuilder().setName(name).build();

//调用远程服务方法

Helloworld.HelloResponse response = blockingStub.sayHello(request);

//返回值

return response.getMessage();

}

复制代码

sayHello()里就是通过grpc调用go的sayHello方法。

查看效果

556785698cdbdac13aecdc9edd6ed597.png

可以看到,这三个程序都ok了

一些注意的点

go、java、php的编译已经写到了Makefile中, 使用make即可一键编译

参考链接

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值