Microservices with Golang and gRPC

Reactive Microservices

(λx.x)eranga
Effectz.AI
3 min readAug 28, 2020

--

Background

gRPC which known as Google Remote Procedure Call is an open source RPC(Remote Procedure Call) framework. It uses HTTP/2 for transport and Protocol Buffers for data serialization. Protobuf is a type-safe binary transfer format that is designed for efficient network communications. With using Protobuf, gRPC supports in both synchronous and asynchronous communication. Similar to REST, gRPC allows to expose the application functionality to other applications(e.g clients). But there are some fundamental difference between gRPC and REST. gRPC utilize HTTP 2 while REST utilize HTTP 1.1. gRPC uses Protobuf for data sterilization, REST uses JSON. gRPC can utilize HTTP 2 features such as streaming(client streaming, server streaming and bi-directional streaming). Performance benchmark results indicate that gRPC is a far better choice than HTTP/HTTP2 if performance and native call like experience is what the developer wants.

In this post I’m gonna discuss about building Golang based gRPC server application. All the source codes which related to this post available in gitlab. Please clone the repo and continue the post.

Protobuf spec

First I have defined the Protobuf specification which used in the service. It defines two things, Services and Messages. A service is a collection of functions which server can performs. The function takes message arguments and returns a message replies. gRPC supports four types of service APIs.

  1. unary API(clients sends single request and server returns single response).
  2. server stream API(client sends single request and server returns replies as streams).
  3. client stream API (client sends request as streams and server returns a single response).
  4. bi-directional stream API(both server and client send and receive stream of request and responses).

In MessageService I have defined unary API, server stream API and bi-directional stream API.

Following is the way to compile the Protobuf spec and generate the gRPC code. It will generate output Golang files in the $GOPATH/src/gitlab.com/rahasaklabs/messanger/spec directory.

gRPC service

Next I have implemented the services defined in the MessageService. Following is the service implementation in server.go. It implemented all three functions defend in the MessageService.

gRPC server

Following is the main.go file which configures the gRPC server. It registers the services implemented in the server.go and expose a TCP port to serve the service APIs. In here I have enabled the gRPC server reflection API. The gRPC server reflection can be used by the client to determine the methods and types expose in the gRPC server.

Run service

Following is the way to compile and run the gRPC service. It will expose the gRPC server on TCP port 9000.

Test service

I have used grpcurl command line client to test the gRPC service. grpcurl is a command-line tool that lets you interact with gRPC servers. It's basically curl for gRPC servers.

Reference

  1. https://dzone.com/articles/unary-streaming-via-grpc
  2. https://tutorialedge.net/golang/go-grpc-beginners-tutorial/
  3. https://bitbucket.org/blog/writing-a-microservice-in-golang-which-communicates-over-grpc
  4. https://medium.com/pantomath/how-we-use-grpc-to-build-a-client-server-system-in-go-dd20045fa1c2
  5. http://www.inanzzz.com/index.php/post/fczr/creating-a-simple-grpc-client-and-server-application-with-golang
  6. https://github.com/nicholasjackson/all-things-microservices/tree/master/grpc-curl

--

--