Micro-services Using Go-kit: gRPC endpoint

This article will show how to create micro-services using Golang programming language and Go-kit as its framework. The service will be exposed via gRPC protocol.

Sharing is caring!


In the previous article, I talked about creating micro-services using go programming language. Also, I introduced go-kit as micro-services framework. As you can see, both of them work very nicely to construct a service.

Just like before, in this article I will make another endpoint as a service. But, this time is a little bit different. Rather than exposing service through REST, I will use gRPC protocol as a communication interface.


Image taken from http://www.grpc.io/docs/guides

I will be brief about gRPC, since  grpc.io already explain nicely about it.

In general, gRPC is a framework from google to support remote procedure call. By using gRPC, a client can invoke methods directly on a server in a different machine.

Moreover, by default gRPC uses protocol buffers as a mechanism for serializing structure data. This serialization is smaller and faster to encode and decode compare to XML or JSON.

If you want how good is protocol buffers performance vs JSON, you can visit this link.

Use Case

The use case is still as same as the previous article. I will create a service to generate “lorem ipsum” text. But there will be a slight difference this time. Rather than creating three functions (Word, Sentence and Paragraph) in one service, I will create one function called Lorem. And from that function, it will dispatch request type whether it will generate word, sentence or paragraph.

Step by Step

I assume we already have required library from the previous article. But it is not enough, we need additional libraries and protocol buffer installation.

  1. Download protocol buffer compiler from https://github.com/google/protobuf/releases. Extract and export bin folder into $PATH.
  2. Execute on terminal:
  3. Remember to put your GOBIN under your PATH.

Then create new folder, give it a name: lorem-grpc. In my case, I did it under $GOPATH/github.com/ru-rocker/gokit-playground/lorem-grpc. I will call this path WORKDIR.

Step 1:  Proto

To define the data structure, we will use protocol buffer language (of course). We will use third version, or known as proto3. Firstly, we need to create directory, inside WORKDIR, called pb, and a proto file under it. In the proto file, we specify our syntax using proto3 version. Also, we will define our service(s) and request response pair as well.

For this article, the service name is Lorem, with LoremRequest as input parameter and return LoremResponse.

The field inside message has following format:

The type value describes data type property. It can be string, bool, double, float, int32, int64, etc. The index is integer value for data stream to indicate the field position.

With protocol buffer compiler and Go plugin for protobuf, we will generate Go files based on proto file. Under pb folder:

This will create file lorem.pb.go.

Step 2: Defining Service

Just like previous article, create file service.go under our WORKDIR. Then create Lorem function inside it.

Step 3: Creating Endpoints

The endpoints is not much different between the previous one. One thing you need to take care of is, I implement Service interface with Endpoints struct. This mechanism is needed when you creating gRPC client connection.

Step 4: Model Request and Response

As usual, we need to encode / decode the request and response. For this purpose, create model.go and define encode/decode functions.

Step 5: Transport

In this step, we need to implement LoremServer interface for grpcServer type. Then create function to return grpcServer.

Step 6: Server

Creating gRPC server in Go lang is almost as easy as creating HTTP server. The different is, we are using tcp protocol rather than http. Under your WORKDIR, create folder server and make file server_grpc_main.go.

Step 7: Client

It is time to make the client side. What we need is create new client function that return Service. Because the NewClient function will be converted into Endpoint.

Take note, I return Endpoint for this function (see Step 3 about implementing Service interface).

Then create executable client under cmd directory.


To test how it is working, at first we need to run the gRPC server.

Next, execute the client



Creating gRPC endpoint is quite simple and only has a little bit different with REST endpoint. However, gRPC runs faster rather than REST. My feeling is, gRPC will replace JSON in the future.

Meanwhile, if you are interested in this sample, you can check the source code on my github.

Have a blessed day.

Author: ru rocker

I am a professional software developer with more than 10 years experiences. I am a certified Java Developer (SCJP and SCWCD). However, In the recent months, I have more interest in DevOps and start to become a polyglot developer. Python and Go-lang become my favorite programming languages besides Java.

1 thought on “Micro-services Using Go-kit: gRPC endpoint”

Leave a Reply

Your email address will not be published. Required fields are marked *