A Step-by-Step Guide to Building Event-Driven Microservices With RabbitMQ
Incorporating the RabbitMQ tutorial into our own project
Recently, when I tried to go through the RabbitMQ tutorials, I found myself struck by a wave of boredom and sleepiness. They were tough to get through!
Hence, I thought I would create something interesting with RabbitMQ instead of just following the tutorial. Turns out it works so well that my adrenaline gets flowing and it keeps me awake till after midnight.
I love motivational quotes. Thus, I want to scrape motivational quotes every minute. This led me to create two microservices. Each microservice operates independently of each other and only performs its own respective task. This is how we achieve separation of concern.
- Publishes a message every minute to RabbitMQ server
- Scrapes quotes from the Internet when it receives the event from TimerService
Now that we have completed the solution design. Let’s get our hands dirty and start building.
Here is TekLoon’s Dev Rule No 1:
Always start with the easiest part.
The easy entry will allow you to complete the first task and gain the confidence to face the upcoming challenges.
Step 1: TimerService development
Let’s start by creating our TimerService:
Let’s look at what we did here. We:
- Created a connection to our RabbitMQ server.
- Created a channel
- Created a direct, non-durable ‘quote’ exchange
- Published the message to the exchange during each interval (60 seconds)
This code fulfills the purpose of TimerService.
Step 2: QuoteService development
Let’s continue with our QuoteService development. The main function of this service is able to scrape a motivational quote from the Internet when it receives a message from the RabbitMQ queue.
First, create a consumer and listen for the event sent by TimerService. Let’s make it very simple; when we receive the message we call the
Next, we proceed to scrape the quote from the Internet:
I’m getting my quotes from wisdomquotes.coms and writing them to a JSON file. Pretty straight forward right?
Now that we have our business logic in place, let’s make an HTML page to display the quotes that have been scrapped. After doing some research, the simplest way to dynamically render the HTML in ExpressJS is by using the template engine Pug. Let me show you how I did it:
This is the UI that will be created based on the
You can get the full source code for QuoteService here.
Step 3: Express Server settings for QuoteService
In order for QuoteService able to listen to RabbitMQ queue, we would have to do some setup during our server initialization. Besides, there is also rendering the quotes HTML based on the
index.pug template we have created in step two.
Aside from booting up the web server, this configuration also fulfills the following purpose:
- Renders the
index.pugtemplate (line 17)
- Listens to RabbitMQ queue (line 23)
Step 4: Run and test It !!!
Let’s run our project and test it locally. Ultimately, you can get my full source code from Github.
- Let’s boot our QuoteService component. Go to QuoteService folder and do
npm run start
- Boot up our TimerService component by going to the TimerService folder and running
npm run start
- Below are screenshots for you if you run it successfully:
RabbitMQ is definitely very easy to learn and has a clear tutorial on their own website, however, tutorials are way more fun when you incorporate into your hobbies or interests.
I personally like RabbitMQ a lot and would love to use it to build event-driven microservices for upcoming projects.
Finally, there are a lot of ideas that flashed into my mind while I was developing this, such as:
- How to deploy these microservices to Heroku.
- How to update the HTML page in real-time to display quotes.
Thanks for reading and feel free to give feedback.
Here are the references that I used while building this project: