===== RabbitMQ example =====
git clone https://github.com/knehez/isi.git
cd isis/rabbitmq-python
docker-compose up -d rabbitmq
This is a docker-compose.yml file that defines a single service called rabbitmq using the official rabbitmq Docker image with management plugin.
version: '3.7'
services:
rabbitmq:
image: 'rabbitmq:3-management'
ports:
- 5672:5672
- 15672:15672
consumer.py
import pika
import sys
import os
connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq'))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
channel.basic_consume(
queue='hello', on_message_callback=callback, auto_ack=True)
print(' [*] Waiting for messages. ')
while (True):
channel.start_consuming()
producer.py
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitmq'))
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()