Detailed steps for microservice deployment

TekLoon
TekLoon
Aug 5 · 5 min read
Photo by Émile Perron on Unsplash

I wrote an article about how I build microservices using RabbitMQ. This is the second part of that article.


Background

In the previous article, I built a microservice project that scrapes motivational quotes from the Internet. Now, I’m exploring how could I deploy this project for free.

After some research, I’ve found that there are a lot of free resources out there. I decided to get started with Heroku as it seems like a good fit for my project.

  • Free Tier
  • Free CloudAmqp Service (RabbitMQ as a Service)

Hands-On

This hands-on section features several parts:

  • Project set up in Heroku
  • RabbitMQ set up in Heroku
  • Integrating RabbitMQ with your projects
  • Problems that I encountered

Project set up in Heroku

  1. Set up Heroku on Mac.
brew tap heroku/brew && brew install heroku

2. Create an app in the Heroku Dashboard.

Create an App Dashboard Page

3. Login to Heroku. Insert your credentials and you should be able to login.

heroku login
heroku: Press any key to open up the browser to login or q to exit:

4. I have my Node project ready. Thus, I only need to add my existing project to Heroku Remote. Here are the steps required.

If you haven’t committed your code into git, you might need to perform the following commands.

// Add all your existing file 
git add .
// Commit all your changed file
git commit -am "Added feature ???"
// Push to Heroku Master
git push heroku master

If you already committed all your codes, you just have to push it to Heroku origin, master branch.

// Push to Heroku Master
git push heroku master

As long as you push to Heroku master branch, Heroku will help you build and run the Node project. You’ll see your build log on your dashboard, and it will show whether the build succeeded or failed, followed by whether it was successfully deployed.

Screenshot for latest activity

RabbitMQ set up in Heroku

Now, let’s start the RabbitMQ service on Heroku.

  1. Go to your Heroku Dashboard.
  2. Pick one of the microservices that you deployed. For me, I picked the TimerService that I built.
  3. You’ll see a dashboard that looks like the one in the screenshot below. I’ll provide several screenshots to guide you on how to set up RabbitMQ Add-ons. I believed screenshots illustrate how to do this better than my words here.
Screenshot for TimerService Heroku Dashboard
Select Find more add-ons
You’ll see a list of add-ons. Look for Messaging and Queueing. You’ll see CloudAMQP, too.
Click on Install AMQP

After clicking on the installation, you’ll see the different types of plans available for RabbitMQ. Select an app that you want to host the CloudAMQP. Lastly, click on Provision add-on, as shown in the screenshot below.

Now you have a working RabbitMQ service.

Integrate Heroku RabbitMQ with the project

Now we have a working project deployed to Heroku and a working RabbitMQ service. What we’re missing is integration. We have to configure our projects to publish messages and listen to messages from the defined RabbitMQ service.

Integrating was easy. We just have to update the process.env.CLOUDAMQP_URL , as shown in the gist above.

In order to get the RabbitMQ URL, you just have to click on the CloudAMQP add-ons from your Heroku app dashboard. Below is a screenshot of what you’ll see after you click on the CloudAMQP add-ons.

You shall see the AMQP URL

Just copy the AMQP URL and paste it in the .env file. Then, redeploy it again to your application on Heroku, and it will be working.

Below is the screenshot of my RabbitMQ Manager when my microservices connected to it.

There are two connections and 6345 messages sent between microservices

Problems that I encountered

During the whole deployment learning process, I encountered two problems.

  • You must declare your start scripts in package.json. In order to start your project, you need to do node server.js . Then you have to declare it in your package.jsonscripts. This is because Heroku will start your project by performing npm start command.
  • The second problem is if I have 10 microservices, do I have to declare 10 RabbitMQ add-ons for each node app? This somehow defeats the purpose, right? It turns out it doesn’t. I just try to use the same RabbitMQ service across all the Node projects — it turns out it can be shared. That RabbitMQ service only requires a single app to be attached. Then, it can be shared among all microservices as long as the URL is correct.

If you encountered the same problems as me, I hope this helps you. If you encounter other problems, you can always Google them or ask for help.


Conclusion

In short, I felt like it was pretty easy to deploy to Heroku after you know how to use it. It feels great when you don’t have to manage the server set up and tools installation.

References

Thanks for reading and enjoy developing.

Better Programming

Advice for programmers.

TekLoon

Written by

TekLoon

Javascript Developer. I explain via code and words. Personal Mantra: Life is about trying stuff, validating stuff and retry some stuff if it is not working

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade