Skip to content

Latest commit

 

History

History
68 lines (52 loc) · 1.74 KB

rabbitmq.md

File metadata and controls

68 lines (52 loc) · 1.74 KB
description
Frequently used code for rabbitmq related code snippets

RabbitMQ

Recipes

Docker-compose usage

When using a Docker image like the following:

rabbitmq-server:
  image: "rabbitmq:3-management"
  hostname: "rabbitmq-host"
  environment:
    RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG"
    RABBITMQ_DEFAULT_USER: "test"
    RABBITMQ_DEFAULT_PASS: "test"
    RABBITMQ_DEFAULT_VHOST: "/"
  ports:
    - "15672:15672"
    - "5672:5672"

The host name for the service from other container would be rabbitmq-server and credentials would need to be passed to it, as follows:

import pika
credentials = pika.PlainCredentials('test','test')
conn_params = pika.ConnectionParameters('rabbitmq', 5672, '/', credentials)
connection = pika.BlockingConnection(conn_params)
# publish
try:
    channel = connection.channel()
    channel.exchange_declare(exchange="name", exchange_type="fanout")
    channel.basic_publish(
        exchange="name", routing_key="", body=payload_json
    )
finally:
    connection.close()
# many consumer 
connection = pika.BlockingConnection(conn_params)
channel = connection.channel()
channel.exchange_declare(exchange="name", exchange_type="fanout")
result = channel.queue_declare(queue="", exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange="name", queue=queue_name)
channel.basic_consume(
    queue=queue_name, on_message_callback=callback, auto_ack=False
)
channel.start_consuming()

References