grpc-gateway转化grpc与http api规则

1、介绍

gRPC Transcoding 是一种定义了grpc与http rest api转化规则,这种规则适用于 Google APIs, Cloud Endpoints, gRPC Gateway等,目前已被广泛使用。

英文文档见:

https://cloud.google.com/endpoints/docs/grpc-service-config/reference/rpc/google.api#google.api.HttpRule

代码可见:

https://github.com/googleapis/googleapis/blob/master/google/api/http.proto

本篇算是对以上文档的翻译与主要观点提炼,再加上个人测试结果。

2、环境准备

2.1 protoc版本

由于我写的是proto3,所以protoc选用了3.11,这个只要选用最新的,差异不大

2.2 protoc-gen-go与protoc-gen-grpc-gateway版本

这两个我踩坑了。。.主要之前安装过protoc-gen-go,它是生成pb.go的工具,后来再安装的protoc-gen-grpc-gateway时不清楚之前protoc-gen-go的版本,所以直接安装最新了,代码跑起来就会报各种方法未实现。研究了一下protoc-gen-grpc-gateway是生成pb.gw.go的工具,由于protoc-gen-go有版本v1和版本v2,且两个差异较大,相应的protoc-gen-grpc-gateway也应该使用对应的版本。

怎么区分protoc-gen-go新旧版本呢?

了解到v1版的protoc-gen-go,在执行protoc-gen-go --version命令后会一直挂起,而v2版本执行完此命令后会立刻返回版本号。因为我的protoc-gen-go是v1版本,在下载github.com/grpc-ecosystem/grpc-gateway后,把tag切到v1后,重新go build 生成protoc-gen-grpc-gateway ,移到本机GOPATH下

最后还遇到个问题仅供参考:undefined: runtime.ServerTransportStream,github.com/grpc-ecosystem/grpc-gateway错误,查询官网把

grpc-gateway版本改成 v1.14.8就可以解决了。

最后推荐https://github.com/jergoo/go-grpc-tutorial这个git项目,适合初学grpc的,我是直接在它项目上改改验证此次结论的。

3、http 映射规则

3.1  测试代码

//request
message msgtype{
    string msgname = 1;
}
message HelloURLPathRequest{
    string name = 1;
    repeated string  msg = 2;
    msgtype  msgfield = 3;
}

//response
message HelloHTTPResponse {
    string message = 1;
}

//grpc func
func (h helloHTTPService) SayHelloURLPath(ctx context.Context, in *pb.HelloURLPathRequest) (*pb.HelloHTTPResponse
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java + gRPC + grpc-gateway 的实践主要分为以下几个步骤: 1. 定义 proto 文件 在 proto 文件中定义需要调用的服务以及方法,同时指定请求和响应的数据类型。例如: ``` syntax = "proto3"; package example; service ExampleService { rpc ExampleMethod (ExampleRequest) returns (ExampleResponse) {} } message ExampleRequest { string example_field = 1; } message ExampleResponse { string example_field = 1; } ``` 2. 使用 protoc 编译 proto 文件 使用 protoc 编译 proto 文件,生成 Java 代码。例如: ``` protoc --java_out=./src/main/java ./example.proto ``` 3. 实现 gRPC 服务 在 Java 代码中实现定义的 gRPC 服务,例如: ``` public class ExampleServiceImpl extends ExampleServiceGrpc.ExampleServiceImplBase { @Override public void exampleMethod(ExampleRequest request, StreamObserver<ExampleResponse> responseObserver) { // 实现具体逻辑 ExampleResponse response = ExampleResponse.newBuilder().setExampleField("example").build(); responseObserver.onNext(response); responseObserver.onCompleted(); } } ``` 4. 启动 gRPC 服务器 使用 gRPC 提供的 ServerBuilder 构建 gRPC 服务器,并启动服务器。例如: ``` Server server = ServerBuilder.forPort(8080).addService(new ExampleServiceImpl()).build(); server.start(); ``` 5. 集成 grpc-gateway 使用 grpc-gateway 可以将 gRPC 服务转换为 HTTP/JSON API。在 proto 文件中添加以下内容: ``` import "google/api/annotations.proto"; service ExampleService { rpc ExampleMethod (ExampleRequest) returns (ExampleResponse) { option (google.api.http) = { post: "/example" body: "*" }; } } ``` 在 Java 代码中添加以下内容: ``` Server httpServer = ServerBuilder.forPort(8081).addService(new ExampleServiceImpl()).build(); httpServer.start(); String grpcServerUrl = "localhost:8080"; String httpServerUrl = "localhost:8081"; ProxyServerConfig proxyConfig = new ProxyServerConfig(grpcServerUrl, httpServerUrl, "/example"); HttpProxyServer httpProxyServer = new HttpProxyServer(proxyConfig); httpProxyServer.start(); ``` 6. 测试 使用 HTTP/JSON API 调用 gRPC 服务,例如: ``` POST http://localhost:8081/example Content-Type: application/json { "example_field": "example" } ``` 以上就是 Java + gRPC + grpc-gateway 的实践步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值