How usually you implement communication layer between your microservices?
Let’s check, how it can be done via message broker.
In our example - RabbitMQ.
As described in RabbitMQ site:
“RabbitMQ is the most widely deployed open source message broker”.
Sounds great, yes?
In this article I prefer to skip installation of RabbitMQ on your machine, you can find a lot of tutorials about it with Google.
Today I want to implement simple Admin Dashboard panel, which will monitor last 10 published jobs.
So, we should have two Rails apps - our Remootee Job Board app and Dashboard application.
How it works?
- Job is created in Remoote application and will go to Producer (JobPublisher)
- Job will then send a message to RabbitMQ exchange
- RabbitMQ exchange will put the message into a queue
- Then the Consumer, connected to this queue, takes the message and update Dashboard’s Redis-based datastore.
Let’s add bunny gem.
And implement our JobPublisher:
Great, now let’s call our JobPublisher in our controller:
Let’s go to our second app - Job Dashboard Admin panel:
Interesting thing here - Sneakers gem , it’s super fast background processing framework for Ruby and RabbitMQ.
Then setup Redis:
And add line into Rakefile:
So, now we need to fetch recently added jobs into our dashboard from Redis.
Let’s get our data from Redis Store in HomeController:
Now we need JobsWorker, which will push job JSON into Redis via RecentJobs:
Then we need to start our JobsWorker:
And the last step. We need to connect our remoote.jobs exchange with dashboard.jobs queue.
We can use simple rake task for it.
Let’s start our applications, and take a look, how it works:
First create new job in Remoote application:
Then go into Dashboard application and you see last added jobs: