Understanding Microservices Choreography Using RabbitMQ and Node.js
We take a brief look at the concept of microservices choreography and how this would work in an app using RabbitMQ and Node.js.
Join the DZone community and get the full member experience.Join For Free
A colleague and I had discussed the choreography of microservices a while ago and the topic came up for discussion again recently. In order to ensure that I understood the nuances correctly, I decided to write some code to implement the approach. Here are the tools/technologies that I chose:
- Node.js for building the microservices.
- Restify for exposing Restful APIs.
- CloudAMQP — a RabbitMQ as a service offering as the integration bus.
- amqp.node — a Node.js client library for RabbitMQ.
CloudAMQP offers a free tier good enough for learning and trying things out. It also comes with an easy to use management console that gives good statistics. The tutorials and articles are useful too. There is a host of options for the Node.js RabbitMQ client libraries. I tried a few and then settled with amqp.node as the RabbitMQ site had their tutorials based on it. The library comes in two flavors, one with callbacks and the other with promises. Being comfortable with callbacks, I opted for it.
The approach that I followed was to mimic a part of the order management process where a POST method triggers the order creation service. The service processes the order, creates an entry in the order management database and sends a message to a RabbitMQ topic. Another service listens to the topic for a specific key that denotes that the order creation is complete and gets triggered when a message with that key is published. This service checks for inventory and either calls a service to replenish stock if it is low or calls an order shipment service if the stock is available. It also publishes a message on the topic with the respective keys to trigger the following services.
Apache Kafka can be used as an alternative to RabbitMQ as an event bus. This approach is suitable while creating new microservices as they can be made to start on the basis of an event.
Full code is available here.
“The views expressed in this article are mine and my employer does not subscribe to the substance or veracity of my views.”
Published at DZone with permission of Danesh Hussain Zaki. See the original article here.
Opinions expressed by DZone contributors are their own.