163网站源码,wordpress 图片自适应,画册什么网站做方便,建设银行appgrpc作为经典的rpc协议#xff0c;虽然略重#xff0c;但是是有学习的价值的通过下面的内容可以快速上手这个grpc框架安装命令行工具php需要这个额外的protoc、grpc_php_plugin工具把这个protobuf格式的文件生成php语言里的类go需要安装protoc-gen-go工具把protobuf格式的接口…grpc作为经典的rpc协议虽然略重但是是有学习的价值的通过下面的内容可以快速上手这个grpc框架安装命令行工具php需要这个额外的protoc、grpc_php_plugin工具把这个protobuf格式的文件生成php语言里的类go需要安装protoc-gen-go工具把protobuf格式的接口定义文件生成go语言里的类定义java无需手动安装额外工具有maven即可再次体现了java的优越性(protoc解压后把bin目录放到PATH里面编译grpc_php_plugingit clone https://github.com/grpc/grpc.gitcd grpcbrew install autoconf automake libtool shtoolLIBTOOLglibtool LIBTOOLIZEglibtoolize make -j8复制代码grpc源码库较大可以直接开8核进行编译 编译好的grpc_php_plugin在bins/opt/目录下把grpc_php_plugin放到PATH里面安装php的grpc扩展编译protoc-gen-gogit clone https://github.com/golang/protobuf.gitcd protobuf/protoc-gen-gogo build复制代码操作效果如下把这个生成的protoc-gen-go放到PATH案例设计go: 提供grpc服务端提供一个sayHello的接口参数为代表名字的字符串返回值为 “Hello拼接上这个名字 java和php作为客户端调用这个go的客户端grpc依赖gogolang.org/x/net/contextgoogle.golang.org/grpcgoogle.golang.org/grpc/reflection复制代码javaio.grpcgrpc-netty1.0.0io.grpcgrpc-protobuf1.0.0io.grpcgrpc-stub1.0.0复制代码phpgrpc/grpc: ^v1.9,google/protobuf: ^v3.5复制代码核心代码proto文件syntax proto3;option go_package pbf;package helloworld;// The greeting service definition.service Hello {// Sends a greetingrpc SayHello (HelloRequest) returns (HelloResponse) {}}// The request message containing the users name.message HelloRequest {string name 1;}// The response message containing the greetingsmessage HelloResponse {string message 1;}复制代码这个protobuf文件定义了两个数据结构HelloRequest和HelloResponse和一个SayHello的接口使用下面的命令把这个proto文件编译成对应的php和go的数据结构#!/bin/bashfor file in ./protos/*.proto; doecho 生成对应的php类文件: $fileprotoc -I ./protos --php_out./php/pbf --grpc_out./php/pbf --pluginprotoc-gen-grpc$(which grpc_php_plugin) $filedoneprotoc -I ./protos --go_outpluginsgrpc:./go/pbf ./protos/*.proto复制代码go服务端核心代码// helloServer implements helloworld.HelloServertype helloServer struct{}// SayHello implements helloworld.HelloServerfunc (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();//初始化远程服务StubblockingStub 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方法。查看效果可以看到这三个程序都ok了一些注意的点go、java、php的编译已经写到了Makefile中, 使用make即可一键编译参考链接