The journey to building a full microservice app. Creating the Report microservice APIs.

Cleison Ferreira Melo
Estía Training
Published in
4 min readJun 16, 2019

Hi everyone, today I’m publishing the 20th article of this track. Today we are going to create and implement a new microservice, Report back-end. We will generate the bundle using the Express CLI, and create the integration with the other microservices we created before using the RPC strategy. Let’s start.

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

The first step is to generate a new project “microservice-back-end-report”, below you can see the project created with the highlighted command used for me.

Once you generated the project skeleton, now change the “package.json” as you can see below, remember to run “npm install” inside of project root folder after.

The next step is to create the file “.babelrc” and “.env” inside the root project folder. The first one is to permit us to use Javascript 2015, and the second one is to define our environment variables.

The next step is to rename the file “www” to “www.js”, and replace the source code to the ES2005 format, as you can see below.

The next step is to add the folder “connection” and the file “rabbit-conn.js” inside. Below the code to create the connection between microservice and RabbitMQ server.

The next step is to create several files; each file is to send a request to a RabbitMQ queue. The files “client-consumer.js”, “client-order.js” and “client-product.js” have basically the same content; however, keep in mind that each one will send messages to a different queue. Look at highlighted items below.

The next step is to create a new route “report-router.js”, you should import the files created before. Here we should use “promises” to synchronise each message received from RabbitMQ with the response. This route should send an empty message for each queue, “order”, “product” and “consumer”. Once we got the response from each queue, we will handle the data to create two collections: “Quantity orders by consumer” and “Quantity products by order”. These collections will be used in front-end to create charts for the users. Look at GitHub project to get the full source code.

The next step is to change the imports in “app.js” to ES2015 and other actions that you can see below.

The last changes are outside of the new “microservice-back-end-report”. If you remember we created the file “server.js” in each microservice back-end. These files are responsible to “listen” the RabbitMQ queue if you send a message for a specific queue, the function “doRpcJob”, as you can see below, will run and respond the necessary actions. All microservices created should be changed, import the “RedisConn” and the prefix. The source code to retrieve all data from Redis is the same that we created in “getAll” endpoint, the unique difference is that we will use Promise to synchronise the functions.

Once all changes are done, you should start all microservices, run inside of the projects “npm run dev” and open the Postman client to test.

With Postman opened, call the new endpoint “localhost:3003/report”, below we have two groups of arrays.

Here I finish this article, and I hope you enjoyed it. The next article we will create a new front-end project to call and show the charts using the data generated before. See you soon.

Link GitHub:

Cleison Ferreira de Melo

Instructor Estía Training

#estiatraining #nodejs #microservices #softwareengineer #programmer #redis #rabbitmq #angular #mongodb

--

--

Cleison Ferreira Melo
Estía Training

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.