Microservices in Go

Anton Klimenko
Jun 3, 2018 · 8 min read
Photo by Simon Abrams on Unsplash

I recently did a talk at the Melbourne Golang meetup about how to develop microservices and frameworks. In this post I’ll share my knowledge with you (plus it’s a good reminder for me).

These are the frameworks I’ll compare:

Introduction to the frameworks

Go Micro

Ok, what is Go Micro? It is a pluggable RPC framework for writing microservices in Go. Out of the box, you will receive:

  • Service discovery - applications automatically registered with service discovery system.
  • Load balancing - client side load balancing used to balance requests between instances of a service.
  • Synchronous communication - request/response transport layer provided.
  • Asynchronous communication - built in publish/subscribe capabilities.
  • Message encoding - encoding/decoding based on Content-Type header of a message.
  • RPC Client/Server packages - leverage the above features and expose interface to build microservices.

Go Micro architecture can be described as a three layers stack.

Figure 1. Go Micro architecture

The top layers consist of a client-server model and a service abstraction. The server is the building block for writing a service. And the client provides an interface to make requests to services.

The bottom layer consists of plugins of the following types:

  • Broker - provides an interface to a message broker for asynchronous pub/sub communication.
  • Codec - used for encoding/decoding messages. Supported formats include json, bson, protobuf, msgpack, etc.
  • Registry - provides a service discovery mechanism (default is Consul).
  • Selector - load balancing abstraction which builds on the registry. It allows services to be “selected” using such algorithms as random, roundrobin, leastconn, etc.
  • Transport - interface for synchronous request/response communication between services.

Go Micro also provides such feature as Sidecar. This allows you to work with services written in languages other than Go. Sidecar provides service registration, gRPC encoding/decoding, and HTTP handlers. It’s available in many languages.

Go Kit

Go Kit follows simple rules such as:

  • No global state
  • Declarative composition
  • Explicit dependencies
  • Interfaces as contracts
  • Domain driven design

In Go Kit you can find packages for:

  • Authentication - basic and JWT.
  • Transport - HTTP, Nats, gRPC, and others.
  • Logging - generic interface for structured logging in services.
  • Metrics - CloudWatch, Statsd, Graphite, and others.
  • Tracing - Zipkin and Opentracing.
  • Service discovery - Consul, Etcd, Eureka, and others.
  • Circuitbreaker - Hystrix implementation in Go.

One of the best descriptions you can find of the Go Kit, is in Peter Bourgon’s article and presentation slides:

Also, in the “Go + microservices” slides you will find an example of a service architecture built with Go Kit. For a quick reference, here’s a diagram of the service architecture.

Figure 2. Example of the service architecture built with Go Kit (original image is in “Go + microservices” slides)

Gizmo

  • server - offers two server implementations: SimpleServer (over HTTP), RPCServer (over gRPC).
  • server/kit - experimental package based on Go Kit.
  • config - contains functions to configuration from JSON files, JSON blobs in Consul k/v, or environment variables.
  • pubsub - provides generic interfaces for publishing and consuming data from the queues.
  • pubsub/pubsubtest - contains test implementations of the publisher and subscriber interfaces.
  • web - exposes functions for parsing types from request queries and payloads.

Pubsub package provides interfaces to work with the following queues:

So, in my opinion, Gizmo sits somewhere between Go Micro and Go Kit. It’s not a complete “blackbox” like Go Micro. At the same time, it’s not as raw as Go Kit. It provides higher-level building components such as config and pubsub packages.

Kite

Comparing the frameworks

  • Objective comparison - GitHub statistics
  • Documentation and examples
  • Users and community
  • Code quality.

GitHub statistics

Table 1. Go microservices frameworks statistics (collected in April 2018)

Documentation and examples

For Go Micro lots of information and announcements are available at micro.mu, microhq, and social media @MicroHQ.

In case of Go Kit the best documentation you will find is in Peter Bourgon’s blog. And one of the best examples I found was in the ru-rocker blog.

With Gizmo, the source code provides the best documentation and examples.

To sum up, if you come from the NodeJS world and expect to see ExpressJS-like tutorials you will be disappointed. On the other hand, it’s a great opportunity to create your own tutorial.

Users and community

With over 3600 stars, 27 contributors and 385 forks - Go Micro takes second place. One of the biggest sponsors of Go Micro is Sixt.

And that leaves third place for Gizmo. Over 2200 stars, 31 contributors, and 137 forks. Supported and created by The New York Times.

Code quality

Coding microservices

Figure 3. Practical example architecture

These are the services which implement one business function - greeting. When a user passes a ‘name’ parameter to a service, then the service sends a greeting response. Also, all services meet the following requirements:

  • Service should self register with the Service Discovery system.
  • Service should have health check endpoint.
  • Service should support at least HTTP and gRPC transports.

For those of you who prefer to read the source code. You can stop here and read the source code in the repository.

Go Micro greeter

Greeter service protobuf definition

The interface consists of one method - ‘Greeting’. There’s one parameter in a request - ‘name’, and one parameter in a response - ‘greeting’.

Then I used modified protoc to generate service interfaces from the protobuf. The generator was forked by Go Micro and modified to support some of the framework’s features. I wired this all together in the ‘greeter’ service. At that point, the service was starting and registering with the service discovery system. It only supported gRPC transport protocol:

gRPC service implementation in Go Micro

To support HTTP transport I had to add the other module. It mapped HTTP request to protobuf defined request. And called gRPC service. Then it mapped service response to HTTP response and replied it to a user.

Web service implementation in Go Micro

Quite simple and straightforward. Many things were handled by Go Micro behind the scenes - such as registration in the service discovery system. On the other hand, it’s hard to create a pure HTTP service.

Go Kit greeter

Finally, I created four packages for:

  • Service logic implementation.
  • Transport agnostic Service endpoints.
  • Transport specific endpoints (gRPC, HTTP)
  • Service discovery registrar.
Service logic implementation in Go Kit

As you can see, the code does not have any dependencies. It just implements logic. The next code snippet shows endpoints definition:

Go Kit service endpoints definition (transport agnostic)

After service and endpoints were defined, I started exposing endpoints via different transport protocols. I started from HTTP transport:

Go Kit service HTTP endpoints

Before I started gRPC endpoints implementation I did not need protobuf definition. I copied the Go Micro service protobuf. But in the case of Go Kit, I used the default service generator to create service interfaces.

Service interface generator from protobuf definition
Go Kit service gRPC endpoints

Finally, I implemented service discovery registrar:

Go Kit service discovery registrar

After all building blocks were prepared, I wired them together in service starter:

Go Kit service starter

As you may have noticed, I used logging middleware in a couple of places. It allowed me to decouple logging logic from the main service/endpoints workflow.

Service level logging middleware
Endpoints level logging middleware

Gizmo greeter

Service implementation and service discovery system registrar shared the same code with the Go Kit service. But endpoints definition and transport implementation had to be done according to Gizmo features.

Gizmo greeter endpoints

As you can see, the code snippet is similar to Go Kit. The main difference is in the interface type which should be returned:

Gizmo greeter HTTP endpoints
GIzmo greeter gRPC

The noticeable difference between Go Kit and Gizmo was in transport implementation. Gizmo provides several Service types which you can utilise. All I had to do was to map HTTP paths to endpoints definition. The low-level HTTP request/response processing was handled by Gizmo.

Conclusion

It might take some time for you to become familiar with Go Kit. It requires a good knowledge of Go lang features and experience in software architecture. On the other hand, there are no framework restrictions. All parts can be changed and updated independently.

Gizmo sits in-between Go Micro and Go Kit. It provides some higher level abstractions such as the Service package. But the lack of documentation and examples meant I had to read through the source code to understand how different Service types work. It was easier to work with Gizmo than with Go Kit. But it was not as smooth as Go Micro.


That’s all for today. Thanks for reading. Please check out the microservices code repository for more information. If you have any experience with Go and microservice frameworks please share it in the comments below.

SEEK blog

At SEEK we’ve created a community of valued, talented, diverse individuals that really know their stuff. Enjoy our Product & Technical insights…

Anton Klimenko

Written by

Software Engineer @seekjobs and chef @CloudRecipesIO

SEEK blog

SEEK blog

At SEEK we’ve created a community of valued, talented, diverse individuals that really know their stuff. Enjoy our Product & Technical insights…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade