Go micro services using gRPC & gRPC Gateway servers

Suresh Yekasiri
cisco-fpie
Published in
4 min readDec 12, 2021

Introduction:

It’s go time!!

Article helps you understanding, setting up gRPC & gRPC gateway servers with Golang and implementing a simple micro service. Target audience for this article are developers and tech enthusiasts. gRPC is a great framework, it generates client and server protobuf’s (stubs) in many languages, faster, easy to implement and highly scalable.

There are some situations or clients does not support this modern technology from client side due to lack of support for HTTP/2, so gRPC gateway is a great way to integrate with very few additions on top of gRPC stuff.

Why would you need to consider gRPC & gRPC gateway over traditional Go micro services?

Very easy to implement & work with protobuf’s, it avoids writing HTTP routes in the code and manually passing the handlers. So it will let developers to focus on business logic implementation. Highly performable, scalable and proven concept from Google.

gRPC:

  • A modern open-source high performance Remote Procedure Call framework.
  • Simple service definition
  • Start quickly and scale
  • Works across languages and platforms
  • Bi-directional streaming with HTTP/2 and integrated auth.
  • Protobuf: Binary format crafted by Google and supports based off HTTP/2. Captures less size compare to JSON or XML, hence faster to transport.

gRPC gateway:

  • Acts as a reverse proxy to the gRPC server.
  • A server those routes with HTTP/1.1 with JSON payloads
  • Internally converts the requests into a gRPC protobuf’s .
  • Allows you to define API’s as gRPC methods with requests & response defined as protobuf, and then implement those gRPC handlers on your api server.

What knowledge do you need to understand this article?

  • Basic knowledge on REST services with Go, HTTP and JSON etc.

Setup

  1. Golang

2. Docker

3. Protobuf

Refer: http://google.github.io/proto-lens/installing-protoc.html for any issues with protobuf setup.

4. Update Env

Implementation

Let us get started with creating gRPC & gRPC gateway servers, wiring them, and implementing a test service.

Step 1: Define the proto

Start defining a .proto file with Request & Response protobuf messages. The api example that we are trying to implement here is a simple HTTP POST request with a name field and response as Greet message with the provided name.

Define gRPC messages for req, resp and rpc method declaration.

Now let us make above “SayHello” declaration to gRPC gateway compatible by providing additional info like HTTP method, url pattern etc.

Complete proto snippet:

Step 2: Generate protobuf’s

This step generates gRPC protobuf’s and gRPC gateway files. gRPC gateway file holds standard HTTP handlers that will convert the request into protobuf’s & routes the request to gRPC controllers.

Step 3: Start gRPC Server

Below function sets up gRPC server running at port 50000, this is internal one and can not exposed to outside of the world.

Major components:

i. Creating gRPC server

ii. Get a provider that will hold actual service definitions. Refer #Step 5 for more details about server struct.

s := &server{log: log} // line #5

iii. Register server and provider. Below one is generated method from #Step 2

api.RegisterTestServicesServer // line #9

Below is complete snippet for gRPC server creation & setup:

Step 4: Start gRPC Gateway Server

Setting up gateway server to run at 9091. As mentioned in the above diagram, it acts as a Reverse Proxy and redirects all requests to gRPC server.

Following statement wires the connection between two servers.

api.RegisterTestServicesHandlerFromEndpoint(ctx, gatewayMux, dialAddr, dialOpts) // line #34

Step 5: main and utility functions

Here we using the Uber’s dependency injection framework to start & maintain gRPC servers.

Step 6: Implement the “SayHello” service

We registered “server” object as a provider while creating the gRPC server. This way server knows the handler method for the SayHello request.

Step 7: Deploy and Test

Build and run the app and use the curl cmd mentioned in #Step 1 to test the SayHello api.

Conclusion

Step 3,4,5 are one time setups. So this process avoids all other ad-hoc steps and you can focus straight away to define proto and start implementing the business logic for your service end points.

gRPC gateway server is a best option when you deal with micro/mini (group) services where each module can use gRPC as internal api’s and only necessary api’s can be exposed to outside of the world for your client(s).

Also it’s the best alternative when you don’t have a gRPC client or a client that doesn’t support HTTP/2 versions.

Git repo for complete example.

Happy Coding!!

--

--