5
5
The grpc-gateway is a plugin of the Google protocol buffers compiler
6
6
[ protoc] ( https://github.com/protocolbuffers/protobuf ) .
7
7
It reads protobuf service definitions and generates a reverse-proxy server which
8
- translates a RESTful JSON API into gRPC. This server is generated according to the
8
+ ' translates a RESTful HTTP API into gRPC. This server is generated according to the
9
9
[ ` google.api.http ` ] ( https://github.com/googleapis/googleapis/blob/master/google/api/http.proto#L46 )
10
10
annotations in your service definitions.
11
11
12
- It helps you provide your APIs in both gRPC and RESTful style at the same time.
12
+ This helps you provide your APIs in both gRPC and RESTful style at the same time.
13
13
14
14
![ architecture introduction diagram] ( https://docs.google.com/drawings/d/12hp4CPqrNPFhattL_cIoJptFvlAqm5wLQ0ggqI5mkCg/pub?w=749& ; h=370 )
15
15
@@ -21,7 +21,7 @@ languages, it is fast, easy-to-use, bandwidth-efficient and its design is
21
21
combat-proven by Google. However, you might still want to provide a traditional
22
22
RESTful JSON API as well. Reasons can range from maintaining
23
23
backwards-compatibility, supporting languages or clients not well supported by
24
- gRPC to simply maintaining the aesthetics and tooling involved with a RESTful
24
+ gRPC, to simply maintaining the aesthetics and tooling involved with a RESTful
25
25
JSON architecture.
26
26
27
27
This project aims to provide that HTTP+JSON interface to your gRPC service.
@@ -37,7 +37,7 @@ manager or by downloading one of the releases from the official repository:
37
37
https://github.com/protocolbuffers/protobuf/releases
38
38
39
39
40
- Then, ` go get -u ` as usual the following packages:
40
+ Then use ` go get -u ` to download the following packages:
41
41
42
42
``` sh
43
43
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
@@ -55,9 +55,9 @@ Make sure that your `$GOBIN` is in your `$PATH`.
55
55
56
56
## Usage
57
57
58
- 1 . Define your service in gRPC
58
+ 1 . Define your [ gRPC ] ( https://grpc.io/docs/ ) service using protocol buffers
59
59
60
- your_service.proto:
60
+ ` your_service.proto ` :
61
61
``` protobuf
62
62
syntax = "proto3";
63
63
package example;
@@ -69,10 +69,11 @@ Make sure that your `$GOBIN` is in your `$PATH`.
69
69
rpc Echo(StringMessage) returns (StringMessage) {}
70
70
}
71
71
```
72
+
72
73
2 . Add a [ ` google.api.http ` ] ( https://github.com/googleapis/googleapis/blob/master/google/api/http.proto#L46 )
73
74
annotation to your .proto file
74
75
75
- your_service.proto:
76
+ ` your_service.proto ` :
76
77
``` diff
77
78
syntax = "proto3";
78
79
package example;
@@ -102,6 +103,7 @@ annotation to your .proto file
102
103
103
104
3 . Generate gRPC stub
104
105
106
+ The following generates gRPC code for Golang based on ` path/to/your_service.proto ` :
105
107
``` sh
106
108
protoc -I/usr/local/include -I. \
107
109
-I$GOPATH /src \
@@ -111,28 +113,30 @@ annotation to your .proto file
111
113
```
112
114
113
115
It will generate a stub file ` path/to/your_service.pb.go ` .
116
+
114
117
4 . Implement your service in gRPC as usual
115
- 1 . (Optional) Generate gRPC stub in the language you want.
116
118
117
- e.g.
119
+ 1 . (Optional) Generate gRPC stub in the [ other programming languages] ( https://grpc.io/docs/ ) .
120
+
121
+ For example, the following generates gRPC code for Ruby based on ` path/to/your_service.proto ` :
118
122
``` sh
119
123
protoc -I/usr/local/include -I. \
120
124
-I$GOPATH /src \
121
125
-I$GOPATH /src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
122
126
--ruby_out=. \
123
- path/to/your/service_proto
127
+ path/to/your_service.proto
124
128
125
129
protoc -I/usr/local/include -I. \
126
130
-I$GOPATH /src \
127
131
-I$GOPATH /src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
128
132
--plugin=protoc-gen-grpc=grpc_ruby_plugin \
129
133
--grpc-ruby_out=. \
130
- path/to/your/service .proto
134
+ path/to/your_service .proto
131
135
```
132
136
2. Add the googleapis-common-protos gem (or your language equivalent) as a dependency to your project.
133
- 3. Implement your service
137
+ 3. Implement your gRPC service stubs
134
138
135
- 5. Generate reverse-proxy
139
+ 5. Generate reverse-proxy using ` protoc-gen-grpc-gateway `
136
140
137
141
` ` ` sh
138
142
protoc -I/usr/local/include -I. \
@@ -144,54 +148,58 @@ annotation to your .proto file
144
148
145
149
It will generate a reverse proxy ` path/to/your_service.pb.gw.go` .
146
150
147
- 6. Write an entrypoint
151
+ 6. Write an entrypoint for the HTTP reverse-proxy server
148
152
149
- Now you need to write an entrypoint of the proxy server.
150
153
` ` ` go
151
154
package main
152
-
155
+
153
156
import (
157
+ " context" // Use " golang.org/x/net/context" for Golang version < = 1.6
154
158
" flag"
155
159
" net/http"
156
-
160
+
157
161
" github.com/golang/glog"
158
- " golang.org/x/net/context"
159
162
" github.com/grpc-ecosystem/grpc-gateway/runtime"
160
163
" google.golang.org/grpc"
161
-
162
- gw " path/to/your_service_package"
164
+
165
+ gw " path/to/your_service_package" // Update
163
166
)
164
-
167
+
165
168
var (
166
- echoEndpoint = flag.String(" echo_endpoint" , " localhost:9090" , " endpoint of YourService" )
169
+ // command-line options:
170
+ // gRPC server endpoint
171
+ grpcServerEndpoint = flag.String(" grpc-server-endpoint" , " localhost:9090" , " gRPC server endpoint" )
167
172
)
168
-
173
+
169
174
func run () error {
170
175
ctx := context.Background ()
171
176
ctx, cancel := context.WithCancel(ctx)
172
177
defer cancel ()
173
-
178
+
179
+ // Register gRPC server endpoint
180
+ // Note: Make sure the gRPC server is running properly and accessible
174
181
mux := runtime.NewServeMux ()
175
182
opts := []grpc.DialOption{grpc.WithInsecure()}
176
- err := gw.RegisterYourServiceHandlerFromEndpoint(ctx, mux, * echoEndpoint , opts)
183
+ err := gw.RegisterYourServiceHandlerFromEndpoint(ctx, mux, * grpcServerEndpoint , opts)
177
184
if err ! = nil {
178
185
return err
179
186
}
180
-
181
- return http.ListenAndServe(" :8080" , mux)
187
+
188
+ // Start HTTP server (and proxy calls to gRPC server endpoint)
189
+ return http.ListenAndServe(" :8081" , mux)
182
190
}
183
-
191
+
184
192
func main () {
185
193
flag.Parse ()
186
194
defer glog.Flush ()
187
-
195
+
188
196
if err := run (); err ! = nil {
189
197
glog.Fatal(err)
190
198
}
191
199
}
192
200
` ` `
193
201
194
- 7. (Optional) Generate swagger definitions
202
+ 7. (Optional) Generate swagger definitions using ` protoc-gen-grpc-swagger `
195
203
196
204
` ` ` sh
197
205
protoc -I/usr/local/include -I. \
@@ -226,7 +234,7 @@ More examples are available under `examples` directory.
226
234
227
235
To use the same port for custom HTTP handlers (e.g. serving `swagger.json`),
228
236
gRPC-gateway, and a gRPC server, see
229
- [this code example by CoreOS](https://github.com/philips/grpc-gateway-example/blob/master/cmd/serve.go)
237
+ [this example by CoreOS](https://github.com/philips/grpc-gateway-example/blob/master/cmd/serve.go)
230
238
(and its accompanying [blog post](https://coreos.com/blog/gRPC-protobufs-swagger.html)).
231
239
232
240
## Features
0 commit comments