EX22 - RabbitMQ Installation
We have seen in the course, how a message broker like RabbitMQ can help us to create distributed applications. The exchange of the messages is managed by the broker. A lot of different programming languages have libraries for easy interaction with RabbitMQ.
In these exercises we will prepare our local environment for working together with RabbitMQ and do our first steps with this broker.
Question 1 - Installation of the pika library for RabbitMQ usage
We need to install the library to provide our Python environment with easy access to the RabbitMQ broker.
You don’t need to install your own RabbitMQ broker server for these exercises, because
we will do all our exercises on the broker at concurp1.isc.heia-fr.ch
Of course, for further usage and possible labs, an own instance of RabbitMQ environment is a good choice (see question 2)
Install the pika library for Python
For all the exercises here, we need pika, so please do the installation for your Python environment.
Todo
Install the pika library in your Python environment (take a Python 3.x version).
Follow the very easy installation guide (its just a pip command) on the pika documentation
Check your installation
To be sure that your pika installation works, execute the following commands directly in the Python interpreter shell:
python 3
>>> import pika
If you got a error like:
>>> import pika
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pika'
The correct behavior is:
$ python 3
>>> import pika
>>> pika.__version__
'1.2.0'
Question 2 - Installation of a local RabbitMQ broker
For testing purposes it is always good to have a local broker, which is under your control. You have different possibilities to install your own RabbitMQ broker:
- directly on your host computer
- in your Linux VM
- as a container in a Docker environment (preferred solution)
I suggest you a Docker environment, as it gives you the fastest and cleanest way to install your broker. Have a look here Downloading and Installing RabbitMQ and RabbitMQ broker in Docker
Important
To offer better debugging possibilities, the installation of the administration plugin is highly encouraged.
Disable any security features (e.g. loopback_user=none) for your testing environment. Of course, any productive environment should be correctly secured.
Question 3 - Hello world
You find in the code-snippets a hello world example (inspired by the very good Getting started RabbitMQ’s documentation).
So take the two parts (hello_world_sender.py and hello_world_receiver.py) have a
look at them.
Important
Before playing/executing them, you need to choose an unique name for your queue. Take care that no other student uses the same name, otherwise you get/receive possible messages from other students.
Set (in both files) the constant QUEUE_NAME to your desired queue name.
Question 4 - Collaborative task distribution
This forth question will be a collaborative hands-on that we will do all together, online.
The purpose of this exercise is that all students have a worker process (the real work will be only waiting for a given time) and ack the message once the task has finished.
We will see how the queue fills up, if only one worker is taking the messages and working. And then how fast the queue get emptied if a lot of parallel worker processes take tasks and ack them.
Important
This hands-on will be done together during the lecture session.
Just assure that you have the worker process code task_receiver.py already
pulled from the code-snippets git repo