RabbitMQ is a messaging broker. It means it can be used as a transport layer to communicate between some applications by sending and receding messages. Sounds quite simple but actually it’s not that simple and has a lot of features. For instance, unlike some other message broker it supports persistence . RabbitMQ is an Erlang application.
1. To install it we need to add the following to the sources configuration to let apt-get know where it should get the server:
echo "deb http://www.rabbitmq.com/debian/ testing main" > /etc/apt/sources.list.d/rabbitmq.list
You can also download deb package the from website but this way seems easier and better.
2. Add a signing key:
wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc apt-key add rabbitmq-signing-key-public.asc
3. Update apt cache:
4. Install. apt-get will handle all Erlang related dependencies:
apt-get install rabbitmq-server
At this point RabbitMQ should be already running. We can check it:
5. If everything is ok, enable the management plugin:
rabbitmq-plugins enable rabbitmq_management
6. Add admin account:
rabbitmqctl add_user admin password rabbitmqctl set_user_tags admin administrator rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
Note that for security reasons it would be better not to use ‘admin’ as a username, you are free to use any you want.
7. We also don’t need default pre-created user ‘guest’.
rabbitmqctl delete_user guest
8. Enable auto-start on boot:
update-rc.d rabbitmq-server defaults
At this point you can start using the admin area:
Log in with your admin creantials.
9. Let’s create user for your application:
rabbitmqctl add_vhost / rabbitmqctl add_user iamauser mypassword rabbitmqctl set_permissions -p / iamauser ".*" ".*" ".*"
You can specify some other virtual host, which is better if you’d like to have more than one applicatio to use RabbitMQ.
10. To get a list of all users run:
11. To check permissions:
rabbitmqctl list_user_permissions iamauser
By default RabbitMQ listens all network interfaces so you might need to restrict ports with the firewall. In general case you would need to restrict ports:
- 4369 for epmd which is a name server for Erlang applications;
- 25672 for clustering;
- 5672 (AMQP), your app will connect to this port;
- 15672 is open if the management plugin is enabled, web panel.
RabbitMQ binding are accessible for the most common languages like Python, Ruby or Go. We will try to test our message broker with dummy Python application. We would need pika library installed:
pip install pika
This is sender.py:
import pika credentials = pika.PlainCredentials('iamauser', 'mypassword') parameters = pika.ConnectionParameters(host='myhost', credentials=credentials) connection = pika.BlockingConnection(pika.ConnectionParameters(host='myhost', credentials=credentials)) channel = connection.channel() channel.queue_declare(queue='hello') channel.basic_publish(exchange='', routing_key='hello', body='Hello World!') print " [x] Sent 'Hello World!'" connection.close()
This one is a consumer.py. It gets a message:
#!/usr/bin/env python import pika credentials = pika.PlainCredentials('iamauser', 'mypassword') parameters = pika.ConnectionParameters(host='myhost', credentials=credentials) connection = pika.BlockingConnection(pika.ConnectionParameters(host='myhost', credentials=credentials)) channel = connection.channel() channel.queue_declare(queue='hello') print ' [*] Waiting for messages. To exit press CTRL+C' def callback(ch, method, properties, body): print " [x] Received %r" % (body,) channel.basic_consume(callback, queue='hello', no_ack=True) channel.start_consuming()
[root@runrabbitrun ~]# python sender.py [x] Sent 'Hello World!' [root@runrabbitrun ~]# [root@runrabbitrun ~]# python consumer.py [*] Waiting for messages. To exit press CTRL+C [x] Received 'Hello World!' ^\Quit [root@runrabbitrun ~]#