项目作者: dray92

项目描述 :
gRPC sample with gRPC gateway + Swagger UI
高级语言: Go
项目地址: git://github.com/dray92/go-grpc-tutorial.git
创建时间: 2018-05-27T23:06:47Z
项目社区:https://github.com/dray92/go-grpc-tutorial

开源协议:

下载


gRPC Tutorial + Swagger JSON

Basic tutorial into setting up a gRPC reverse proxy that will
transform REST JSON into gRPC, along with instructions on how
to generate Swagger definitions.
Instructions on how to run client/server in other languages.

Goals

  • service defintion in service.proto
  • generate client and server code using protoc.
  • wrap generated code to expose light client (and server).
  • generate gateway code for HTTP/JSON reverse proxy.

TODO

  • get Swagger UI to render service’s Swagger JSON (with auth)
  • auth definitions in proto
  • investigate python reverse proxy support #398
  • write up instructions for java {grpc-jersey}
  • find a contributor for nodejs

Basic Setup

  • use gvm or something to get Go >= 1.6
  • brew install protobuf or apt install libprotobuf-dev
  • $ go help gopath # make sure GOPATH is set

Golang installations

  1. go get google.golang.org/grpc
  2. go get -u github.com/golang/protobuf/proto
  3. go get -u github.com/golang/protobuf/protoc-gen-go
  4. go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
  5. go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger

Architecture

This is the basic architecture of a grpc-gateway application {from grpc-ecosystem/grpc-gateway/}.

Architecture

Service definition

pb/service.proto

Golang Client/Server code generation

pb/service.pb.go

To generate:

  1. $ protoc -I/usr/local/include -I. \
  2. -I$GOPATH/src \
  3. -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
  4. --go_out=google/api/annotations.proto=github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api,plugins=grpc:. \
  5. pb/service.proto

Golang Reverse proxy gateway code generation

pb/service.pb.gw.go

To generate:

  1. $ protoc -I/usr/local/include -I. \
  2. -I$GOPATH/src \
  3. -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
  4. --grpc-gateway_out=logtostderr=true:. \
  5. pb/service.proto

Golang Client code

client/client.go

Golang Server code

server/server.go

Golang Reverse proxy server code

server/server-rproxy.go

Golang Server <-> Client communication

  1. go run server/server.go
  2. ...
  3. go run client/client.go

Will display 2018/05/28 12:57:11 Hello debo!

Golang Server <-> Client via gRPC Gateway

  1. go run server/server.go
  2. ...
  3. go run server/server-rproxy.go
  4. ...
  5. curl -X POST "http://localhost:8080/v1/example/hello/v1/world" | jq .

Will display

  1. {
  2. "id": "v1",
  3. "msg": "world"
  4. }

Python Client/Server code generation

PR to update gRPC docs: #671
pb/service_pb2.py
pb/service_pb2_grpc.py

To generate:

  1. virtualenv env
  2. . env/bin/activate
  3. pip install grpcio-tools googleapis-common-protos
  4. python -m grpc_tools.protoc --python_out=. \
  5. --grpc_python_out=. -I. -I/usr/local/include \
  6. -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
  7. -I/usr/local/include \
  8. pb/service.proto

Python Client code

client/client.py

Python Server code

server/server.py

Python Server <-> Client communication

  1. python -m server.server
  2. ...
  3. python -m client.client

Will display Hello world!

Python Reverse proxy server code

I don’t believe this can be done in the state of the world at the time of writing.

Generate Swagger JSON

pb/service.swagger.json

To generate:

  1. protoc -I/usr/local/include -I. \
  2. -I$GOPATH/src \
  3. -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
  4. --swagger_out=logtostderr=true:. \
  5. pb/service.proto