Saturday, September 15, 2018

Getting Started With RabbitMQ in 10 Minutes (On Ubuntu)

Getting Started With RabbitMQ in 10 Minutes



RabbitMQ is one of the most popular opensource message broker/Queuing engine available in the market.. It has been used as communication(Message Oriented) bridge in many software architectures.. Communication between Microservices is one of the obvious use case where we can use MQ to make things simple .. RabbitMQ makes communication between services easy with it's simple messaging system.. Rabbit MQ supports multiple protocols like AMQP

In one of our projects, We've used it to delegate the heavy processing things to worker process written in python form single threaded NodeJs process.. thus it makes easy to scale our services and opened the doors to polyglot architecture for our services... we enjoyed it a lot.. So I thought to write a simple article which will help people to get start with RabbitMQ in 10 minutes..

Installing RabbitMQ (In Ubuntu based systems)

First update source-list and add signing key

sudo apt update wget -O - 'https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc' | sudo apt-key add -

Now add repository to source list

echo "deb https://dl.bintray.com/rabbitmq/debian $(lsb_release -cs) main erlang" | sudo tee /etc/apt/sources.list.d/bintray.rabbitmq.list

Now update the source list and install the RabbitMQ ....

sudo apt update sudo apt install rabbitmq-server -y

The server is started as a daemon by default when the RabbitMQ is installed.

Verify the installation.. with the following command...

sudo systemctl status rabbitmq-server.service
shivaraj@shivaraj-Aspire-A315-21:~$ sudo systemctl status rabbitmq-server.service [sudo] password for shivaraj: ● rabbitmq-server.service - RabbitMQ broker Loaded: loaded (/lib/systemd/system/rabbitmq-server.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2018-09-15 15:25:31 IST; 1h 29min ago Main PID: 1217 (beam.smp) Status: "Initialized" Tasks: 86 (limit: 4486) CGroup: /system.slice/rabbitmq-server.service ├─1217 /usr/lib/erlang/erts-9.2/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 1280000 -K true -- -root /usr/lib/erlang -progname erl -- -home /var/lib/rabbitmq -- -p ├─1333 /usr/lib/erlang/erts-9.2/bin/epmd -daemon ├─1572 erl_child_setup 1024 ├─1624 inet_gethost 4 └─1625 inet_gethost 4 Sep 15 15:25:26 shivaraj-Aspire-A315-21 rabbitmq-server[1217]: ## ## Sep 15 15:25:26 shivaraj-Aspire-A315-21 rabbitmq-server[1217]: ## ## RabbitMQ 3.7.7. Copyright (C) 2007-2018 Pivotal Software, Inc. Sep 15 15:25:26 shivaraj-Aspire-A315-21 rabbitmq-server[1217]: ########## Licensed under the MPL. See http://www.rabbitmq.com/ Sep 15 15:25:26 shivaraj-Aspire-A315-21 rabbitmq-server[1217]: ###### ## Sep 15 15:25:26 shivaraj-Aspire-A315-21 rabbitmq-server[1217]: ########## Logs: /var/log/rabbitmq/rabbit@shivaraj-Aspire-A315-21.log Sep 15 15:25:26 shivaraj-Aspire-A315-21 rabbitmq-server[1217]: /var/log/rabbitmq/rabbit@shivaraj-Aspire-A315-21_upgrade.log Sep 15 15:25:26 shivaraj-Aspire-A315-21 rabbitmq-server[1217]: Starting broker... Sep 15 15:25:31 shivaraj-Aspire-A315-21 rabbitmq-server[1217]: systemd unit for activation check: "rabbitmq-server.service" Sep 15 15:25:31 shivaraj-Aspire-A315-21 systemd[1]: Started RabbitMQ broker. Sep 15 15:25:32 shivaraj-Aspire-A315-21 rabbitmq-server[1217]: completed with 3 plugins. shivaraj@shivaraj-Aspire-A315-21:~$

And also I'll show you small how to with NodeJS and RabbitMQ.. for this you need to have Node installed in your system.. If you haven't installed NodeJs you can check the instructions here..

First you need to install amqplib.. it's RabbitMQ client for NodeJs..

npm install amqplib

Then create two files named.. sender.js and receiver.js

Now you can run the receiver.js and and publisher.js and you can see the output like one shown below...

node sender.js
shivaraj@shivaraj-Aspire-A315-21:~/mqtest$ node sender.js Connected Published New Task Published New Task Published New Task
node receiver.js
shivaraj@shivaraj-Aspire-A315-21:~/mqtest$ node receiver.js Connected Do Something.... Do Something.... Do Something.... Do Something.... Do Something....

If you want to use it with other languages.. please check official guide.. https://www.rabbitmq.com/getstarted.html

That's it for now.. If you like.. don't forget to share it guys.. You can follow us on fb.com/opensourceinside and also subscribe our channel on Youtube..