Microservices with Go Micro and NATS
Micro is a set of utilities and libraries that makes it easier to write and manage microservices. It was written and maintained by Asim Aslam, a developer from London who is getting help from an enthusiastic community.
A microservice written for micro can be written in any language but it's easier to write it in Go (until ports of Micro will be available in other languages). For non-Go microservices, there exists the Sidecar (an HTTP proxy into the microservice) to handle this situation.
Microservices often typically communicate with JSON but micro uses protobuf encoding by default. Protobufs offer solid serialization capabilities, static types, and can be used to also declare the API of a specific microservice.
By default the messages are sent over HTTP but a message broker (RabbitMQ, Kafka, NSQ, etc) can allow improvements on top of this such as persistence, scalability or reliability. In this case we use NATS since it's very simple, fast and lean. It also has enough functionality to act as transport, broker and registry (the latter due to its broadcast queries). More information can be found at Asim's blogpost.
John Nguyen, an Australian developer, uses Micro to collect and visualize financial instruments (Bitcoin and foreign exchange). I asked him to demo his setup and we recorded it in a 30 minute video:
- 0:00 Overview of Micro and NATS
- 8:00 Demo
- 16:17 Publishing a message to NATS
- 23:50 Code of a publisher and a subscriber
- 27:44 protobuf
- 29:03 Python subscriber
- 31:31 Persistance with project STAN