RabbitMQ is an open-source message broker that enables applications to communicate with each other using messages. It helps in decoupling the system's components, managing the workload distribution, and ensuring the reliability and scalability of your application. RabbitMQ supports multiple messaging protocols and can be easily integrated into various architectures.
The objective of this project is to demonstrate the basic capabilities of RabbitMQ by setting up a distributed task queue system. We'll create a simple producer that sends messages representing tasks and a consumer that processes these tasks. The project aims to showcase message queuing, durability, fair message dispatch, and basic error handling using RabbitMQ.
- Windows, Linux, or macOS -- I used windows/vscode
- Python 3.x -- I used 3.10, and of course use a venv
- Access to a terminal or command prompt as admin
-
Install Erlang:
- Download and install the Erlang v 26.2.1 runtime from the official Erlang website.
-
Install RabbitMQ Server:
- Download and install RabbitMQ Server v 3.12.11 from the official RabbitMQ website.
-
Install pika:
pip install pika
-
Enable RabbitMQ Management Plugin (Optional):
- Run the following command in the RabbitMQ Command Prompt:
rabbitmq-plugins enable rabbitmq_management
- Run the following command in the RabbitMQ Command Prompt:
-
Test Your Installation:
- Access the RabbitMQ Management Console at http://localhost:15672/ (default login: guest/guest).
producer/producer.py
: The script to publish messages (tasks) to the queue.consumer/consumer.py
: The script to consume and process the messages from the queue.
The producer script connects to the RabbitMQ server, creates a queue, and sends a series of task messages. It demonstrates how to declare a queue, create a persistent message, and publish it to the queue. Of course we used our industry standard "Hello World!"
The consumer script waits for messages from the RabbitMQ queue and processes them upon arrival. It showcases message acknowledgment, ensuring that messages are only removed from the queue after they have been fully processed.
- Message Queuing: Tested how messages are enqueued and dequeued in RabbitMQ.
- Durability: Ensured that messages persist even if the RabbitMQ service restarts.
- Fair Dispatch: Demonstrated how RabbitMQ distributes messages evenly among multiple consumers.
- Start RabbitMQ Service:
- Ensure the RabbitMQ server is running.
- Run the Consumer Script:
- Execute
python consumer.py
to start the consumer.
- Execute
- Run the Producer Script:
- Execute
python producer.py "Your message"
to send a new task.
- Execute
- Observe the Results:
- Watch how messages are processed by the consumer script.
To further explore the capabilities of RabbitMQ and practice on your own, try the following exercises:
- Multiple Producers and Consumers:
- Create multiple producer scripts sending different types of tasks and multiple consumers each handling a specific type of task.
- Priority Queuing:
- Modify the queue to prioritize certain messages.
- Advanced Message Properties:
- Experiment with different message properties like timestamps and headers.
- Integrate with a Real Application:
- Apply what you've learned to integrate RabbitMQ into a real-world application you're working on or familiar with.
This project provides a hands-on approach to understanding RabbitMQ and the principles of message queuing. By building and observing a simple distributed task queue, you've gained insights into how RabbitMQ operates and how it can be used to improve the scalability and reliability of applications.