GO-grpc-Gateway 的安装与编译(WIN环境)

#grpc-gateway

grpc-gateway是protoc的一个插件。它读取gRPC服务定义,并生成一个反向代理服务器,将RESTful JSON API转换为gRPC。此服务器是根据gRPC定义中的自定义选项生成的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

进入 github.com/grpc-ecosystem/grpc-gateway/rotoc-gen-grpc-gateway
执行go build 然后安装 go install
进入 github.com/grpc-ecosystem/grpc-gateway/pprotoc-gen-swagger
执行go build 然后安装 go install

**
需要额外下载
go get github.com/ghodss/yaml
go get google/api/annotations.proto (被墙使用:github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis)
git clone https://github.com/protocolbuffers/protobuf
**

#####grpc-gateway-proto文件例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//需要在编译的时候 指定包的路径,并且该包依赖 https://github.com/protocolbuffers/protobuf
import "google/api/annotations.proto";

service Menu {
rpc Save (MenuModel) returns (Res) {
option (google.api.http) = {
post: "/api/menu"
body: "*"
};
}

rpc View (RepMenuView) returns (Res) {
option (google.api.http) = {
get: "/api/menu"
};
}
}

#####编译

1
2
3
4
5
6
7
8
9
10
//生成服务端
protoc --go_out=plugins=grpc:. role_manage.proto

//生成网关使用使用部分
protoc -I$GOPATH/src \
-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
-I$GOPATH/src/protobuf/src/ \
-I$GOPATH/src/vsiapi/proto \
--grpc-gateway_out=logtostderr=true:. \
role_manage_gw.proto

####调用
$ curl -d ‘{“ID”:1}’ -X GET ‘http://localhost:3344/api/menu'

请参阅 案例分享