go-zero rest框架中融合grpc-gateway
调用链: http client -> ( go-zero-restapi -> grpc-gateway -> grpc-client ) -> grpc-server
git clone https://github.com/googleapis/googleapis ~/protoc/googleapis
https://github.com/protocolbuffers/protobuf/releases
解压到 ~/protoc
文件夹中,如:~/protoc/protoc-23.2-osx-aarch_64
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
见 exapmle
目录中的示例,关键代码文件:
rpc部分
example/simple_rpc/simple_rpc.proto
grpc-gateway参数的编写example/simple_rpc/build.sh
protobuf生成grpc-gateway代码
rest部分
example/simple_api/simpleapi.go
更换为前缀优先路由example/simple_api/internal/svc/servicecontext.go
GatewayProxy的初始化
启动服务
> # 启动rpc
> cd simple_rpc
> go run .
> # 启动api
> cd simple_api
> go run .
发起测试请求
> curl -XPOST 'http://127.0.0.1:8888/grpc1/simple/ping' \
--header 'Content-Type: application/json' \
--data-raw '{"ping":"SimpleApi"}'
- go-zero: https://github.com/zeromicro/go-zero
- grpc-gateway:https://github.com/grpc-ecosystem/grpc-gateway
- grpc-go: https://github.com/grpc/grpc-go
- protobuf: https://github.com/protocolbuffers/protobuf
- googleapis: https://github.com/googleapis/googleapis