Docker környezetben futtatható példa.
git clone https://github.com/knehez/isi.git cd isi/rabbitmq-python docker-compose up -d rabbitmq
Az alábbi docker-compose.yml definiál egy szolgáltatást a rabbitmq hivatalos docker image-el, kiegészítve a management web-es felülettel, ami a 15672-es porton érhető el.
version: '3.7' services: rabbitmq: image: 'rabbitmq:3-management' ports: - 5672:5672 - 15672:15672
Indítsuk el a producert:
docker-compose up producer
Majd a cunsumert:
docker-compose up consumer
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()