Wrapping a microservice boilerplate in golang
First things first. Happy New Years 🎉🎉🎉 Wishing you a fatal error free year!
Complexity of applications are expanding in many aspects such as scalability, development, testing and deployment. The old monolithic architecture seems entering its retirement era in enterprise-level application development. In addition, I see a desire to build systems by simply plugging together components in most software companies I’ve worked in/with. That’s why many companies developed their products based on microservice architecture. Netflix, PayPal, Amazon, eBay, and Twitter are just a few enterprises currently using microservices.
In this post I won’t describe microservice architecture fundamentals. If you’re unsure what a microservice is, I suggest reading Martin Fowler’s article. After reading several articles and testing some tools we ended up using golang for developing most of our microservices. Where that decision came from, benchmarking and comparing golang with others need a separate post and I won’t touch that either!
By a quick googling you’ll find some frameworks and boilerplates developed in golang but they are generally too complicated. So we built our own! I’m going to introduce the packages we used for wrapping up our microservices.
go get github.com/spf13/viper
Viper is a comprehensive configuration solution for Go applications including 12-Factor apps. It is designed to work within an application, and can handle all types of configuration needs and formats. It supports:
- setting defaults
- reading from JSON, TOML, YAML, HCL, and Java properties config files
- live watching and re-reading of config files (optional)
- reading from environment variables
- reading from remote config systems (etcd or Consul), and watching changes
- reading from command line flags
- reading from buffer
- setting explicit values
Viper can be thought of as a registry for all of your applications configuration needs.
go get github.com/sirupsen/logrus
Logrus is a structured logger for Go (golang), completely API compatible with the standard library logger.
go get github.com/nats-io/go-nats
A Go client for the NATS messaging system.
go get github.com/gorilla/mux
gorilla/mux implements a request router and dispatcher for matching incoming requests to their respective handler.
The name mux stands for “HTTP request multiplexer”. Like the standard
mux.Router matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL or other conditions. The main features are:
- It implements the
http.Handlerinterface so it is compatible with the standard
- Requests can be matched based on URL host, path, path prefix, schemes, header and query values, HTTP methods or using custom matchers.
- URL hosts, paths and query values can have variables with an optional regular expression.
- Registered URLs can be built, or “reversed”, which helps maintaining references to resources.
- Routes can be used as subrouters: nested routes are only tested if the parent route matches. This is useful to define groups of routes that share common conditions like a host, a path prefix or other repeated attributes. As a bonus, this optimizes request matching.
go get github.com/urfave/negroni
Negroni is an idiomatic approach to web middleware in Go. It is tiny, non-intrusive, and encourages use of
If you like the idea of Martini, but you think it contains too much magic, then Negroni is a great fit.
Squirrel helps you build SQL queries from composable parts”
Redigo is a Go client for the Redis database.
- A Print-like API with support for all Redis commands.
- Pipelining, including pipelined transactions.
- Connection pooling.
- Script helper type with optimistic use of EVALSHA.
- Helper functions for working with command replies.
go get github.com/dgrijalva/jwt-go
A go (or ‘golang’ for search engine friendliness) implementation of JSON Web Tokens.
A package of validators and sanitizers for strings, structs and collections. Based on validator.js.
A SQL database migration toolkit in Golang.
I didn’t mean to courage any one to switch to microservice architecture! Of course it has pros and cons but to those of you who researched enough and made the decision for implementing a microservice based application: I hope you find this post informative and don’t hesitate to send your feedback and complete my list.