Relevant concepts

RabbitMQ is essentially a producer and consumer model, responsible for receiving, storing, and forwarding messages. In computer terms, the RabbitMQ model is more like a switch model. RabbitMQ consists of the following parts:

The overall model architecture of RabbitMQ is shown below.

Producers and consumers

Producer: the party that delivers messages

The producer creates the message and publishes it to RabbitMQ. A message can generally contain two parts:

  1. The message body can also be called payload. In practical applications, the message body is usually a piece of data with a business logical structure, such as a JSON string. You can, of course, serialize the message body further
  2. Label, used to represent the message, such as the name of a switch and a routing key. The producer hands the message to RabbitMO, which RabbitMQ then sends to interested consumers based on the label.

Consumer: Consumer – the party that receives the message

The consumer connects to the RabbitMQ server and subscribes to the queue. When a consumer consumes a message, it consumes only the payload of the message. In the process of message routing, the label of the message is discarded, and only the message body is stored in the queue. The consumer only consumes the message body and does not know who the producer of the message is. Of course, the consumer does not need to know.

Broker: service node of the messaging middleware

A RabbitMQ Broker can be regarded as a RabbitMQ service node, or RabbitMQ service instance

The following figure shows the complete process of storing messages from producers into the RabbitMQ Broker and consuming data from the Broker by consumers:

  1. The producer first wraps the business-side data as possible, then encapsulates it as a message and sends it (the AMQP action corresponds to basic.publish) to the Broker.
  2. Consumers subscribe and receive messages (Basic.Consume or basic. Get correspond to this action in AMQP), Get the raw data through possible unpacking, and then proceed to the business processing logic.

The business processing logic does not necessarily need to use the same thread as the logic that receives the message. A consumer process can use a thread to receive messages and store them in memory, for example using BlockingQueue in Java. The business processing logic uses another thread to read data from memory, which further decouples the application and improves the processing efficiency of the entire application.

The Queue, Queue

RabbitMQ internal object for storing messages

RabbitMQ messages can only be stored in queues, as opposed to messaging middleware such as Kafka. Kafka stores messages at the logical level of the topic (topic), and the corresponding queue logic is just a displacement identifier in the actual storage file of the topic. The RabbitMQ producer produces the message and ultimately delivers it to the queue, from which the consumer can retrieve and consume the message. Multiple consumers can subscribe to the same queue, in which case messages in the queue are sent byThe average contributionInstead of each consumer receiving all the messages and processing them, Round Robin is given to multiple consumers, as shown in the figure.RabbitMQ does not support queue-level broadcast consumption, and if broadcast consumption is required, secondary development on it can become extremely complex and is not recommended.

Switches, routing keys, bindings

Exchange: a switch.

In the above figure we can temporarily understand that the producer sends a message to the queue, but this does not happen in RabbitMQ. In reality, a producer sends a message to an Exchange (usually also represented by a capital “X”), which routes the message to one or more queues. If the route is not available, it may be returned to the producer or discarded. A detailed schematic of the switch is shown below.

There are four types of switches in RabbitMQ:

  • fanout
  • direct
  • topic
  • headers

Different types have different routing policies.

RoutingKey: routing key

When a producer sends a message to an exchange, it typically specifies a RoutingKey that specifies the Routing rule for the message. This RoutingKey needs to be used in conjunction with the exchange type and the BindingKey to take effect. With a fixed exchange type and BindingKey, producers can specify a RoutingKey when sending a message to the exchange to determine where the message will go.

Binding: Binding

RabbitMQ binds the switch to the queue, usually specifying a BindingKey so RabbitMQ knows how to route messages to the queue correctly

RabbitMQ operation process

With the RabbitMQ model in mind, here’s how RabbitMQ works:

Producer sends message

  1. Producers connect to RabbitMO brokers, establish a Connection, and open a Channel.
  2. The producer declares a switch and sets properties such as switch type, persistence, and so on
  3. The producer declares a queue and sets related properties, such as whether it is exclusive, persistent, and automatically deleted
  4. The producer binds the switch to the queue through the routing key
  5. Producers send messages to the RabbitMO Broker containing routing keys, switches, and so on
  6. The corresponding switch looks for a matching queue based on the routing key it receives.
  7. If found, the message sent from the producer is placed in the appropriate queue.
  8. If not, discard or roll back to the producer based on the attributes of the producer configuration
  9. Close the channel.
  10. Close the connection.

Consumer receives message

  1. The consumer connects to the RabbitMQ Broker, establishes a Connection, and opens a Channel.
  2. The consumer requests the RabbitMQ Broker to consume messages in the corresponding queue, possibly setting up the corresponding callback function and doing some preparatory work
  3. The consumer receives the message after the RabbitMQ Broker responds and delivers the message to the corresponding queue.
  4. Consumer acknowledgement (ACK) of the received message.
  5. RabbitMQ removes the corresponding acknowledged message from the queue.
  6. Close the channel.

RabbitMQ Combat Guide

This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.