The journey to building a full microservice app. RabbitMQ container and connection.

Cleison Ferreira Melo
Jun 8 · 3 min read

Hi guys, today I’m publishing the 14th article of this track. I hope that the journey is instigating you to learn more about microservices and the several technologies that we should learn to create a real microservice app. Today we will add a new Docker Compose service, the RabbitMQ, and create the connection between the microservice and RabbitMQ. Suppose you don’t know what RabbitMQ is and why we should use it, read the introduction topic below. Let’s start.

Before we start, look at the last article of the track.

When we define a microservice architecture, we must keep in mind, the isolation of all services, avoiding any kind of dependency. For this, we use a message-broker like is RabbitMQ https://www.rabbitmq.com. If microservice A wants a data from microservice B, the microservice A(client) will send the request to a queue, on the RabbitMQ server, and once this request has been processed by microservice B(server), RabbitMQ will reply an answer for the microservice A(client). Below the RCP(Remote Procedure Call) strategy, picture.

The first step is to create a new service inside “docker-compose.yml”, as you can see below, you should add the service “rabbitmq-server”.

The next step is to run “docker-compose up — build -d”, remember to running it inside the microservice product root folder.

To check if the service is up, open “http://localhost:15672/”. Username “user”, password “1”.

The next step is to open the “package.json” to install the new dependencies. Pay attention in highlighted items below. Run “npm install” to install the dependencies.

The last step is to create a new file “rabbitmq-conn.js” inside connection folder, inside this file we will create one connection with the RabbitMQ server, and I added the parameters to keep the connection always opened case we get some network issues.

The next screenshot is a “tip” for you. To avoid every time us, developers, define the system variables, I added a new dependency “dotenv” inside “devDependencies” to read the system variables from “.env” file inside of the project root folder. This file isn’t committed to GitHub; you should create a new one as an example below to use this resource.

Here I finish this article; I hope you enjoyed it. In the next article, we will create the broker server to read messages sent for a queue. See you soon.

Link GitHub:


Cleison Ferreira de Melo

Instructor Estía Training

Cleison Ferreira Melo

Written by

I’m a father, and I started working at age 11 — over 25 years, learning, teaching and trying to make this world a better place. Write is just one way for that.