| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This project sets up a single RabbitMQ instance using an Alpine-based Docker image. It includes the RabbitMQ Management Dashboard for monitoring and administrating the server. This README provides a comprehensive guide on the architecture, setup, and testing for effective RabbitMQ usage in a Python environment.
The architecture comprises:
graph TD
rabbitmq[RabbitMQ Server]
Client[Client Application]
Client -- External Access --> rabbitmq
To connect to this RabbitMQ server, use the server address and port in your client configuration.
Description: Producers create messages and send them to a specific RabbitMQ queue.
Usage:
Example of Use:
import pika
import json
import time
# Establish connection to RabbitMQ server
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672))
channel = connection.channel()
# Declare a queue named 'test_queue'
channel.queue_declare(queue='test_queue')
# Create a message
message = json.dumps({"field": "value", "timestamp": time.time()})
# Publish the message to the queue
channel.basic_publish(exchange='', routing_key='test_queue', body=message)
print(" [x] Sent message")
# Close the connection
connection.close()Description: Consumers listen to RabbitMQ queues to retrieve messages.
Usage:
Example of Use:
import pika
import json
# Define a callback function to process messages
def callback(ch, method, properties, body):
message = json.loads(body)
print(" [x] Received message:", message)
# Establish connection to RabbitMQ server
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672))
channel = connection.channel()
# Declare the queue to consume from
channel.queue_declare(queue='test_queue')
# Set up subscription with the callback
channel.basic_consume(queue='test_queue', on_message_callback=callback, auto_ack=True)
print('Waiting for messages. To exit press CTRL+C')
# Start consuming
channel.start_consuming()Create a test folder in your project directory with the following structure:
test/
├── producer.py
└── consumer.py
Set Up Virtual Environment (venv):
Create and activate a virtual environment to isolate dependencies.
Navigate to your project directory:
python3 -m venv venv
source venv/bin/activate # On Windows use: venv\Scripts\activateInstall Dependencies:
Inside the virtual environment, install the required libraries:
pip install pikaRun Docker Containers:
Start your RabbitMQ server using Docker Compose:
docker-compose up -dVerify Containers:
Ensure that the RabbitMQ container is running:
docker psRun the Consumer:
Open a new terminal, activate the virtual environment, and navigate to the test folder:
source venv/bin/activate # On Windows use: venv\Scripts\activate
python test/consumer.pyThe consumer should display:
Waiting for messages. To exit press CTRL+C
Run the Producer:
In another terminal, activate the virtual environment, and run the producer:
source venv/bin/activate # On Windows use: venv\Scripts\activate
python test/producer.pyThe producer should display:
[x] Sent messageVerify Output:
The consumer terminal should display the messages produced by the producer, confirming the connection and data flow. Example:
[x] Received message: {'field': 'value', 'timestamp': 1701308250.123456}Clean Up:
After testing, stop the Docker services and deactivate the virtual environment:
docker-compose down
deactivateBelow is the updated docker-compose.yml file to set up a single RabbitMQ server using an Alpine-based image with the management dashboard enabled.
version: '3.8'
services:
rabbitmq:
image: rabbitmq:3.11-management-alpine
container_name: rabbitmq
hostname: rabbitmq
ports:
- "5672:5672" # AMQP protocol
- "15672:15672" # Management UI
environment:
RABBITMQ_DEFAULT_USER: 'guest'
RABBITMQ_DEFAULT_PASS: 'guest'
networks:
- rabbitmq_net
volumes:
- rabbitmq_data:/var/lib/rabbitmq
networks:
rabbitmq_net:
driver: bridge
volumes:
rabbitmq_data:
driver: localversion: Specifies the version of Docker Compose syntax. Version 3.8 is used here for compatibility.
services:
networks:
volumes:
Network Not Defined Error:
Port Conflicts:
Issue: Ports 5672 or 15672 might already be in use on your host machine.
Solution: Ensure these ports are free or modify the port mappings in the docker-compose.yml to use different host ports. For example:
ports:
- "5673:5672"
- "15673:15672"Data Persistence:
For your convenience, here's the complete and corrected docker-compose.yml file:
version: '3.8'
services:
rabbitmq:
image: rabbitmq:3.11-management-alpine
container_name: rabbitmq
hostname: rabbitmq
ports:
- "5672:5672" # AMQP protocol
- "15672:15672" # Management UI
environment:
RABBITMQ_DEFAULT_USER: 'guest'
RABBITMQ_DEFAULT_PASS: 'guest'
networks:
- rabbitmq_net
volumes:
- rabbitmq_data:/var/lib/rabbitmq
networks:
rabbitmq_net:
driver: bridge
volumes:
rabbitmq_data:
driver: localSave the File:
Start the Container:
Navigate to your project directory in the terminal and run:
docker-compose up -dVerify the Setup:
Check the running containers:
docker psAccess the Management Dashboard at http://localhost:15672.
Stop the Container:
When you're done, you can stop and remove the containers with:
docker-compose downData Persistence:
Security Considerations:
Monitoring and Alerts:
Scaling and High Availability:
Automated Backups:
Logging:
This setup provides a lightweight and efficient RabbitMQ server configured with an Alpine-based Docker image and an accessible management dashboard. By following the setup and testing instructions, you can efficiently produce and consume messages, monitor your RabbitMQ instance, and ensure seamless communication within your Python applications.
| Back | FazBrowse Home | New Git URL |