Introduction
Recently, I have been learning gRPC, and found grpc-gateway, an awesome project, which allows us to call our gRPC service through RESTful JSON API.
It supports languages or clients not well-supported by gRPC to simply maintain the aesthetics and tooling involved with a RESTful architecture. And it's more friendly to front-end developers.
In this article, I will share my rough experience with it.
At first, let's take a look at how grpc-gateway does.
![]()
Preparation
Use go get -u to download the following packages.
Define And Generate
Before we create a gRPC service, we should create a proto file to define what we need, here we create a file named hello.proto to show.
As you can see, there is an option for API Configuration, /hello_world is the request URL we defined for HTTP JSON. It means that we can visit SayHello with http://yourdomain.com/hello_world.
Then we should generate a gRPC stub via protoc
Then we will get the following files.
![Files]()
Implement And Run The Service
Here we just return a string value to the implementation.
Then we will configure this service so that it can run well.
Use the following command to run up the service.
After running up, we may get the following result.
![Result]()
HTTP reverse-proxy server
This is the most important step for translation!
What we need to do is to tell something about our gRPC service.
Call Via HTTP Client
Here we use a dotnet core console app to call the reverse proxy server.
After running up, we can get the following result.
![Output]()
Summary
grpc-gateway helps us translate a RESTful JSON API into gRPC, this is a good idea. But there are some limitations here because if our gRPC server is written in other languages, it can not support it.