This repository contains examples and notes on RabbitMQ, a distributed message broker. RabbitMQ facilitates communication between applications through message exchanges, supporting asynchronous messaging and microservices architecture. It can be deployed on various cloud environments as well as on-premise.
RabbitMQ is a robust message broker that supports various messaging patterns, enabling scalable and reliable communication between services. This repository covers installation, core concepts, and common messaging patterns used with RabbitMQ.
- About
- Description
- Installation
- Core Concepts
- Common Messaging Patterns
- Example Code
- Notes
- References
- Install Erlang (≥ 26.0 && < 27.0)
- Install RabbitMQ
choco install rabbitmq
RabbitMQ acts as a post office that accepts and forwards messages between applications, allowing for loosely-coupled systems and seamless scalability.
A producer is a program that sends messages to an exchange.
A consumer is a program that waits to receive messages from a queue.
Producers send messages to exchanges, which then route the messages to queues. The main types of exchanges are:
- Direct: Routes messages based on routing keys.
- Fanout: Routes messages to all bound queues.
- Topic: Routes messages based on pattern matching.
- Headers: Routes messages based on header attributes.
Queues store messages until they are consumed. Key properties include:
- Durability: Whether the queue survives broker restarts.
- Exclusive: Whether the queue is used by only one connection.
- Auto-delete: Whether the queue is deleted when the last consumer unsubscribes.
Bindings define the relationship between exchanges and queues.
- Connection: A TCP connection between your application and the RabbitMQ broker.
- Channel: A virtual connection inside a connection, allowing multiple logical flows.
Distributes tasks to multiple workers, with each task delivered to exactly one worker, enabling parallel processing and scalability.
Delivers messages to multiple consumers. Each message is broadcasted to all queues bound to the exchange.
Routes messages to queues based on routing keys. Useful for delivering messages selectively based on specific criteria.
Routes messages based on patterns in routing keys, supporting wildcard matches for flexible routing.
Enables two-way communication between services, with a requestor sending a message and waiting for a reply from the replier.
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='letterbox')
message = "Hello world"
channel.basic_publish(exchange='', routing_key='letterbox', body=message)
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='letterbox')
def callback(ch, method, properties, body):
print(f"Received new message: {body}")
channel.basic_consume(queue='letterbox', auto_ack=True, on_message_callback=callback)
channel.start_consuming()
If you're interested in learning more or want more detailed explanations, please refer to this Google Doc.
Notes compiled with thanks to the above references.