第三方库
https://github.com/grpc-ecosystem/grpc-gateway
安装步骤
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger go get -u github.com/golang/protobuf/protoc-gen-go
proto文件头部需要引入import "google/api/annotations.proto";
在rpc出需要添加option
syntax="proto3"; package services; import "google/api/annotations.proto"; import "models.proto"; message OrderRequest { OrderMain order_main = 1; } message OrderResponse { string status = 1; string message = 2; } service OrderService{ rpc NewOrder(OrderRequest) returns(OrderResponse){ option (google.api.http) = { post: "/v1/orders" body:"order_main" }; } }
生成文件(一个proto文件需要执行下面2个命令,一个是生成proto的go文件,一个是生成gateway文件)
protoc --go_out=plugins=grpc:../services Prod.proto protoc --grpc-gateway_out=logtostderr=true:../services Prod.proto
访问使用 路径为 /v1/orders