Go + gRPC with Go kit

Junerey Casuga
3 min readNov 21, 2020

Building applications with Go can be sometimes intimidating especially when you’re just starting to learn it. Let alone transporting your APIs through gRPC. In this post, I will guide you on writing a simple Go microservice using gRPC with the help of Go kit.

What is Go kit? and why?

Go kit is the brain child of Peter Bourgon which he talked about during a FOSDEM and the Google Campus London meetup in 2015. As what he has envisioned, Go kit is a toolkit for building microservices in Go

It provides you a set of standard libraries (packages) that are considered essentials when building microservices so you can focus on what’s important, your application’s business logic. Here is a list of packages Go kit provides:

  • Authentication (Basic/Casbin/JWT)
  • Circuit Breaker (Hystrix/GoBreaker/HandyBreaker)
  • Logging: Provides an interface for structured logging.
  • Metrics: Provides an interface for service instrumentation. Has adapters for CloudWatch, Prometheus, Graphite, and more.
  • Rate Limiting
  • Service Discovery Utilities
  • Tracing (OpenCensus, OpenTracing, Zipkin)
  • Transport (HTTP/gRPC/ NATS/AWS Lambda, and more)

Other than being a toolkit, it also encourages good design principles for your application such as SOLID design principles, Domain-Driven Design (DDD), and Hexagonal architecture.

Go kit’s Concept

Go kit’s concept is based on three major components:

  • Services
  • Endpoints
  • Transports

Services (Business Logic)

This is where the core business logic of your API is located. In Go kit, each service method are converted into an Endpoint.

Endpoints

An endpoint represents a single RPC method. Each Service method converts into an Endpoint to make RPC style communication between servers and clients.

Transports

In a microservice architecture, a microservice often communicate through transports such as HTTP or gRPC. A single endpoint can be exposed by multiple transports.

Let’s build our microservice

For our example microservice, we will build a very simple function that would get the sum of two numbers. Before we get started, make sure that you have Protocol buffer and Go plugins for the protocol compiler installed.

To give you an overview what our project structure would look like by the end of this article, you can refer to the directory tree below.

.
├── cmd
│ └── main.go # main entrypoint file
├── endpoints
│ └── endpoints.go # contains the endpoint definition
├── pb
│ ├── math.pb.go # our gRPC generated code
│ └── math.proto # our protobuf definitions
├── service
│ └── api.go # contains the service's core business logic
└── transports
└── grpc.go # contains the gRPC transport

Protobuf

First of, we would need to create our Protobuf file inside our pb directory. Since you’re reading this, I would assume that you already know the basics of Protobuf; and the code snippet below should be pretty straightforward to understand 😄

Once we have our Protobuf file set, we would then need to generate our gRPC code using the following command.

protoc --go_out=plugins=grpc:. pb/math.proto

Service

Next up, we will create the core business logic of our microservice. So in service/api.go , we would add our service definition by using the following code below.

Endpoint

From the code above for endpoints/endpoints.go, we have the Endpoints struct that serves as a helper struct to collect all of the endpoints into a single parameter.

The MathReq and MathResp simply defines the request parameters and response values for the Add method.

makeAddEndpoint func constructs our Add endpoint wrapping the service.

And finally, MakeEndpoints func returns a Endpoints that wraps the provided server and wires in all of the expected endpoints.

Transport

In our code for transports/grpc.go, we have NewGRPCServer which makes a set of endpoints available as a gRPC MathServiceServer .

Main entrypoint

This is where we stitch things together. Here we initiate our services, endpoints and our gRPC server, then we serve it using google.golang.org/grpc

Source Code

You can find the source code of this example at https://github.com/junereycasuga/gokit-grpc-demo

--

--