grpc-gateway 用来遵循 gRPC HTTP 规范的 gRPC 至 JSON 代理生成器
介绍
google.api.http 示例
service DemoService {
rpc Sum(SumRequest) returns (SumResponse) {
option (google.api.http) = {
post: "/sum"
body: "*"
};
}
rpc Diff(DiffRequest) returns (DiffResponse) {}
rpc ReadFile(ReadFileRequest) returns (ReadFileResponse) {}
rpc Version(VersionRequest) returns (VersionResponse) {
option (google.api.http) = {get: "/version"};
}
}
更多规则参考
安装
go install \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \
google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc
或使用 buf 安装,配置参考
version: v2
plugins:
# Use protoc-gen-go at v1.31.0
- remote: buf.build/protocolbuffers/go:v1.31.0
out: gen/go
opt: paths=source_relative
# Use protoc-gen-go-grpc at v1.4.0
- remote: buf.build/grpc/go:v1.4.0
out: gen/go
opt: paths=source_relative
# ref https://buf.build/grpc-ecosystem/gateway
- remote: buf.build/grpc-ecosystem/gateway:v2.20.0
out: gen/go
opt:
- paths=source_relative
- remote: buf.build/grpc-ecosystem/openapiv2:v2.20.0
out: gen/swagger
opt:
- allow_merge=false
示例