How To Create Health Check For RESTful Microservice In Golang

Imagine you’ve recently released and deployed to production a cool RESTful microservice you worked on for a while. You heaved a sigh of relief just to hear from Ops team that your service is unstable. You are damn sure that the service should be fine, but you get a feeling that there could be something wrong with services it depends on. What should you do?

Health check will come to your rescue. It is an endpoint in your service returning status of your application including statuses of connections to all external services your service directly depends on. In this post I’ll show how to create a health check for a microservice running on multiple nodes, storing its state in MongoDB and calling Elasticsearch.

Unit Testing Golang Code Calling Elasticsearch

There is an amazing Elasticsearch client for Go and in Working With Elasticsearch I demonstrated with example how to index and search for documents using it. However, if you want to ensure your code will continue working correctly and will not be inadvertently broken during refactoring, you have to cover the code with tests.

In this post I’ll show how to unit test Go code interoperating with Elasticsearch. Keep in mind, however, that the same approach is applicable to unit testing almost any code calling external RESTful API.

How To Correctly Serialize JSON String In Golang

json is one of the most wildly used Go packages. It is simple and, what is more important, very intuitive. So what could be easier than marshalling a string with JSON and unmarshalling it to struct? If you believe (as I did) that the issue is trivial and json.Marshal does the job, read on.

What’s wrong with json.Marshal?

It’s easier to demonstrate on example. Let’s write a simple program which serializes JSON string to bytes and deserializes the bytes into matching struct:

Working With Elasticsearch in Go

Elasticsearch is one of the most popular highly scalable full-text search engines on the market. It is based on Lucene engine and allows you to store, search, and analyze big volumes of data quickly and in near real time. It has rich REST API and clients for most popular programming languages including Go. In this post I’ll demonstrate on example how to index and search for application logs using Go.

Installing Elasticsearch

If you haven’t set it up already, installation of Elasticsearch is as easy as downloading it (from here) and running the executable file. I installed it on localhost and will use it in my examples.

When installation is over, let’s test that Elasticsearch is up and running:

curl http://localhost:9200

The response should be similar to:

{
  "name" : "Specialist",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.3.4",
    "build_hash" : "e455fd0c13dceca8dbbdbb1665d068ae55dabe3f",
    "build_timestamp" : "2016-06-30T11:24:31Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.0"
  },
  "tagline" : "You Know, for Search"
}

Example Of Using Templates In Golang

Almost every programming language has a library implementing templating. In epoch of server side MVC dominance, templating was so important that it could determine language success or failure. Nowadays, however, when single page applications get momentum, templates are used only occasionally.

The niche where templates are still absolutely indispensable is email content generation. In this post I will demonstrate on an example how to use Golang template package to generate email content based on a template defined in a file. I’ll keep example as simple as possible, but beefy enough to show most commonly used features of the package.

You can get full source code of the example from GitHub.

Persisting Application Configuration In Golang

I often observe that configuration is overlooked in books and posts devoted to application development. Authors only slightly touch it at best. I believe it’s quite an important topic on its own and deserves a dedicated post. So in this post you will find explained examples of how to make persistent configurations in Go applications.

I will demonstrate how to work with configuration for two most popular formats: json and yaml. In examples we will store and load config of hypothetical cluster manager captured with the following structs:

type Cluster struct {
	Name       string
	DataCentre string
	Nodes      []string
}
type Configuration struct {
	Clusters    []Cluster
	MinReplicas int
	MaxReplicas int
}

Working with InfluxDB in Go

InfluxDB is one of the most popular time series databases on the market. In this post you will learn its key concepts, as well as, how to interact with InfluxDB in Go on example.

InfluxDB

InfluxDB design conceptually reminds relational database. Don’t get me wrong, it is not a relational database in any sense. It doesn’t impose schema on your data and doesn’t implement SQL. But… it has equivalent concepts and is operated via DSL looking quite similar to SQL.

Key concepts

If you are new to InfluxDB, have a look at key concepts with examples here. If you need a quick brush-up, read on.

The largest entity in InfluxDB is a database. You can have multiple databases in one InfluxDB instance.

How To Build Microservice With MongoDB In Golang

These days Golang grows in popularity for writing RESTful microservices. Quite often these services utilize MongoDB as persistence storage. In this post we will build a simple book store microservice using both Go and MongoDB. We will connect to MongoDB with mgo driver and use curl to test the microservice.

MongoDB

MongoDB took market with storm by its simplicity, high availability and document orientation. The advantages of using documents over relational tables are:

  • Documents correspond to native data types in many programming languages.
  • Embedded documents and arrays reduce need for expensive joins.
  • Dynamic schema supports fluent polymorphism.

goimports vs gofmt

It is hard to find a Go developer who have never heard of gofmt. From the moment we start learning the language, Go documentation including Effective Go preaches us that code MUST be formatted with standard gofmt tool. While gofmt attracks much attention, goimport is overlooked despite often being more useful. In this post we will learn how they differ on example.

Example

To save some space, I would provide only a snippet from a file, but feel free to get the whole one from GitHub and experiment with it.

Snippet of our unformatted example:

package main
import (
        "encoding/json"
        "fmt"
        "os"  // unused package
        "goji.io"
        "golang.org/x/net/context"
        "net/http"
        "goji.io/pat"
)
type book struct {
        ISBN  string "json:isbn"
        Title  string "json:name"
        Authors string "json:author"
        Price string "json:price"
}
...

RESTful web service in Go using Goji

Although net/http provides all necessary abstractions to create web services out of the box, there are alternative packages providing additional features and allowing to minimize boilerplate code. Today we will have a look at how to implement a web service with Goji.