knowledge/technology/dev/RabbitMQ.md
2024-03-07 00:10:01 +01:00

3.2 KiB

obj website repo wiki
application https://rabbitmq.com https://github.com/rabbitmq/rabbitmq-server https://en.wikipedia.org/wiki/RabbitMQ

RabbitMQ

RabbitMQ is a widely-used open-source message broker that implements the Advanced Message Queuing Protocol (AMQP). It serves as a mediator between different components of distributed systems, allowing them to communicate asynchronously via messages. RabbitMQ uses Raft for shared state in clusters.

Concepts

Message Broker

RabbitMQ acts as a message broker, facilitating communication between producers and consumers. Producers send messages to RabbitMQ, which stores and routes them to the appropriate consumers based on specified criteria.

Exchange

Messages are sent to RabbitMQ through exchanges, which act as message routing mechanisms. RabbitMQ provides different types of exchanges, including direct, fanout, topic, and headers exchanges, allowing for various routing strategies.

Queue

Queues are storage buffers within RabbitMQ where messages are held until they are consumed by consumers. Multiple queues can be bound to an exchange, and messages are delivered to queues based on routing rules defined by the exchange.

Consumer

Consumers retrieve messages from queues and process them according to the application's logic. RabbitMQ supports multiple consumer processes or threads, allowing for concurrent message processing.

Publisher-Subscriber Model

RabbitMQ enables the implementation of the publisher-subscriber model, where publishers (producers) publish messages to exchanges, and subscribers (consumers) receive messages from queues.

How RabbitMQ Works

  • Message Production: Producers publish messages to exchanges, specifying the exchange type and routing key.
  • Message Routing: Exchanges receive messages from producers and route them to queues based on routing rules. Queues store messages until they are consumed by consumers.
  • Message Consumption: Consumers retrieve messages from queues and process them according to application logic.
  • Acknowledgment: After processing a message, consumers send acknowledgments to RabbitMQ to confirm message delivery and processing. RabbitMQ removes acknowledged messages from queues.
  • Error Handling: RabbitMQ provides features for handling message delivery failures, such as dead-letter exchanges and message requeuing.

Use Cases

  • Microservices Communication: RabbitMQ facilitates communication between microservices in distributed systems, ensuring reliable message delivery and decoupling services.
  • Task Queues: RabbitMQ is used for distributing tasks across multiple workers in task queue systems, enabling parallel processing and workload balancing.
  • Event-Driven Architectures: RabbitMQ supports event-driven architectures, allowing components to react to events and messages asynchronously.

Docker-Compose

version: "3.2"
services:
  rabbitmq:
    image: rabbitmq:3-management-alpine
    container_name: 'rabbitmq'
    ports:
        - 5672:5672 # RabbitMQ
        - 15672:15672 # Management Console
    volumes:
        - ./data/:/var/lib/rabbitmq/
        - ./log/:/var/log/rabbitmq
    environment:
      RABBITMQ_DEFAULT_USER: myuser
      RABBITMQ_DEFAULT_PASS: mypassword