Exploring the Power of gRPC-Gateway for Writing RESTful Services

Rajiv Ranjan Singh

 

India 🇮🇳

gRPC Global Meetup

Rajiv Ranjan Singh

Software Engineer @ A.P. Moller - Maersk
I graduated in August, 2022 from JSSATE Bengaluru, India

Agenda

Prerequisites: gRPC, HTTP/JSON, Protobuf

  • REST and gRPC
  • gRPC-Gateway
  • Demonstrating how to use gRPC Gateway

REST and gRPC

gRPC doesn't have a default speed advantage over REST, but it provides features that can help enhance its speed.

  • selective message compression
  • load balancing
  • etc

 

 

We all know that gRPC is not a tool for everything. There are cases where we still want to provide a traditional HTTP/JSON API. The reasons can range from maintaining backward compatibility to supporting programming languages or clients not well supported by gRPC. But writing another service just to expose an HTTP/JSON API is quite a time-consuming and tedious task.

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 HTTP/JSON format at the same time.

gRPC-Gateway

  • A plugin of protobuf compiler
  • Generate proxy codes from protobuf
  • Translates HTTP JSON Call to gRPC
  • In-process translation: only for unary gRPC
  • Separate proxy server: both unary and streaming gRPC

gRPC-Gateway

gRPC-Gateway

gRPC-Gateway

Demonstrating how to use gRPC Gateway

  • Defining Hello World gRPC service using protocol buffers.
  • Creating main.go where we will write our client and server logic.
  • Adding gRPC-Gateway annotations to an existing proto file.
  • Generating the gRPC-Gateway stubs.

References

Thank You, 🙏 🎉