gRPC to JSON proxy generator following the gRPC HTTP spec
About
The gRPC-Gateway is a plugin of the Google protocol buffers compiler
protoc.
It reads protobuf service definitions and generates a reverse-proxy server which
translates a RESTful HTTP API into gRPC. This server is generated according to the
google.api.http
annotations in your service definitions.
This helps you provide your APIs in both gRPC and RESTful style at the same time.
We use the gRPC-Gateway to serve millions of API requests per day,
and have been since 2018 and through all of that,
we have never had any issues with it.
gRPC is great -- it generates API clients and server stubs in many programming
languages, it is fast, easy-to-use, bandwidth-efficient and its design is
combat-proven by Google. However, you might still want to provide a traditional
RESTful JSON API as well. Reasons can range from maintaining
backward-compatibility, supporting languages or clients that are not well supported by
gRPC, to simply maintaining the aesthetics and tooling involved with a RESTful
JSON architecture.
This project aims to provide that HTTP+JSON interface to your gRPC service.
A small amount of configuration in your service to attach HTTP semantics is all
that's needed to generate a reverse-proxy with this library.
Installation
The following instructions assume you are using
Go Modules for dependency
management. Use a
tool dependency
to track the versions of the following executable packages:
With this file in place, you can generate your files using buf generate.
For a complete example of using buf generate to generate protobuf stubs, see
the boilerplate repo.
For more information on generating the stubs with buf, see
the official documentation.
If you are using protoc to generate stubs, here's an example of what a command
might look like:
Generate reverse-proxy using protoc-gen-grpc-gateway
At this point, you have 3 options:
no further modifications, use the default mapping to HTTP semantics (method, path, etc.)
this will work on any .proto file, but will not allow setting HTTP paths, request parameters or similar
additional .proto modifications to use a custom mapping
relies on parameters in the .proto file to set custom HTTP mappings
no .proto modifications, but use an external configuration file
relies on an external configuration file to set custom HTTP mappings
mostly useful when the source proto file isn't under your control
Using the default mapping
This requires no additional modification to the .proto file but does require enabling a specific option when executing the plugin.
The generate_unbound_methods should be enabled.
Here's what a buf.gen.yaml file might look like with this option enabled:
You will need to provide the required third party protobuf files to the protobuf compiler.
If you are using buf, this dependency can
be added to the deps array in your buf.yaml under the name
buf.build/googleapis/googleapis:
If you are using protoc to generate stubs, you need to ensure the required
dependencies are available to the compiler at compile time. These can be found
by manually cloning and copying the relevant files from the
googleapis repository, and providing
them to protoc when running. The files you will need are:
External configuration
If you do not want to (or cannot) modify the proto file for use with gRPC-Gateway you can
alternatively use an external
gRPC Service Configuration file.
Check our documentation
for more information. This is best combined with the standalone=true option
to generate a file that can live in its own package, separate from the files
generated by the source protobuf file.
Here's what a buf.gen.yaml file might look like with this option enabled:
To use the custom protobuf annotations supported by protoc-gen-openapiv2, we need
another dependency added to our protobuf generation step. If you are using
buf, you can add the buf.build/grpc-ecosystem/grpc-gateway dependency
to your deps array:
If you are using protoc to generate stubs, you will need to copy the protobuf
files from the protoc-gen-openapiv2/options directory of this repository,
and providing them to protoc when running.
Note that this plugin also supports generating OpenAPI definitions for unannotated methods;
use the generate_unbound_methods option to enable this.
It is possible with the HTTP mapping for a gRPC service method to create duplicate mappings
with the only difference being constraints on the path parameter.
/v1/{name=projects/*} and /v1/{name=organizations/*} both become /v1/{name}. When
this occurs the plugin will rename the path parameter with a "_1" (or "_2" etc) suffix
to differentiate the different operations. So in the above example, the 2nd path would become
/v1/{name_1=organizations/*}. This can also cause OpenAPI clients to URL encode the "/" that is
part of the path parameter as that is what OpenAPI defines in the specification. To allow gRPC gateway to
accept the URL encoded slash and still route the request, use the UnescapingModeAllCharacters or
UnescapingModeLegacy (which is the default currently though may change in future versions). See
Customizing Your Gateway
for more information.
Usage with remote plugins
As an alternative to all of the above, you can use buf with
remote plugins
to manage plugin versions and generation. An example buf.gen.yaml using remote
plugin generation looks like this:
This requires no local installation of any plugins. Be careful to use the same
version of the generator as the runtime library, i.e. if using v2.6.0-1, run
using additional --<tool_suffix>_opt parameters: --<tool_suffix>_opt=<flag>[,<flag>]*
--grpc-gateway_opt logtostderr=true,repeated_path_param_separator=ssv
# or separately
--grpc-gateway_opt logtostderr=true --grpc-gateway_opt repeated_path_param_separator=ssv
--openapiv2_opt logtostderr=true,repeated_path_param_separator=ssv
# or separately
--openapiv2_opt logtostderr=true --openapiv2_opt repeated_path_param_separator=ssv
More examples
More examples are available under the examples directory.
proto/examplepb/echo_service.proto, proto/examplepb/a_bit_of_everything.proto, proto/examplepb/unannotated_echo_service.proto: service definition
proto/examplepb/echo_service.pb.go, proto/examplepb/a_bit_of_everything.pb.go, proto/examplepb/unannotated_echo_service.pb.go: [generated] stub of the service
proto/examplepb/echo_service.pb.gw.go, proto/examplepb/a_bit_of_everything.pb.gw.go, proto/examplepb/uannotated_echo_service.pb.gw.go: [generated] reverse proxy for the service
proto/examplepb/unannotated_echo_service.yaml: gRPC API Configuration for unannotated_echo_service.proto
server/main.go: service implementation
main.go: entrypoint of the generated reverse proxy
To use the same port for custom HTTP handlers (e.g. serving swagger.json),
gRPC-Gateway, and a gRPC server, see
this example by CoreOS
(and its accompanying blog post).
Features
Supported
Generating JSON API handlers.
Method parameters in the request body.
Method parameters in the request path.
Method parameters in the query string.
Enum fields in the path parameter (including repeated enum fields).
Mapping streaming APIs to newline-delimited JSON streams.
Mapping HTTP headers with Grpc-Metadata- prefix to gRPC metadata (prefixed with grpcgateway-)
HTTP request source IP is added as X-Forwarded-For gRPC request header.
HTTP request host is added as X-Forwarded-Host gRPC request header.
HTTP Authorization header is added as authorization gRPC request header.
Remaining Permanent HTTP header keys (as specified by the IANA
here)
are prefixed with grpcgateway- and added with their values to gRPC request
header.
HTTP headers that start with 'Grpc-Metadata-' are mapped to gRPC metadata
(prefixed with grpcgateway-).
While configurable, the default {un,}marshaling uses
protojson.
The path template used to map gRPC service methods to HTTP endpoints supports the google.api.http
path template syntax. For example, /api/v1/{name=projects/*/topics/*} or /prefix/{path=organizations/**}.
请发表评论