[RabbitMQ Practical Combat] 04 Basic concepts of RabbitMQ: Exchange, Queue, Channel, etc.

1. Introduction

The demand for Message Queue has been around for a long time. In financial transactions in the 1980s, companies such as Goldman Sachs first adopted Teknekron's products. The Message queuing software at that time was called: the information bus (TIB). TIB was adopted by telecommunications and communications companies, and Reuters acquired Teknekron. After that, IBM developed MQSeries and Microsoft developed Microsoft Message Queue (MSMQ). The problem with these commercial MQ vendors is vendor lock-in and high prices. In 2001, Java Message queuing tried to solve the problems of locking and interactivity, but it made it more troublesome for applications.

So in 2004, JPMorgan Chase and iMatrix began to develop the Advanced Message Queuing Protocol (AMQP) open standard. In 2006, the AMQP specification was released. In 2007, RabbitMQ 1.0, developed by Rabbit Technology Company based on the AMQP standard, was released.

2. Basic concepts of AMQP

AMQP (RabbitMQ) must consist of three parts: exchange, queue and binding
AMQP basic concepts

  • Message : consists of payload and label. The payload is the transmitted data.
  • Producer : Creates messages and publishes them to the proxy server (Message Broker).
  • Proxy server (Message Broker) : An application that receives and distributes messages. RabbitMQ Server is a message broker server, which contains many concepts. Take RabbitMQ as an example: channel, queue, exchange, routing key ( routing key), binding key, virtual host (vhost), etc.
  • Channel : A virtual connection within the TCP connection between the application (production and/or consumption) and the proxy server, which solves the limit on the number of TCP connections and reduces the cost of TCP connections. Each channel has an ID, and its concept is similar to "frequency division multiplexing". Refer to the picture above: Basic concepts of AMQP
  • Queue : The message finally arrives in the queue and is waiting for consumption by consumers. Use "basic.consume" subscription to get all messages in the queue, and "basic.get" subscription to get only one message in the queue (if there is a message in the queue). If no one subscribes, the message will be stored in the queue, waiting for subscription; if multiple consumers subscribe to the queue at the same time, the message will be sent to the consumer (Fanout Exchange) in an automatic polling (round-robin) manner, each consumer only Can obtain a subset of messages in the queue)
  • Exchange : The first stop where the message reaches the proxy server. According to the distribution rules, it matches the routing key in the query table (except Fanout Exchange) and distributes the message to the queue. Commonly used types are: direct (point-to-point), topic (publish-subscribe) and fanout (multicast)
  • Routing key : When a message is sent to the exchange, the message will have a routing key (empty by default), and the exchange will send the message to the matching queue based on this routing key.
  • Binding key : The queue needs to be bound to the exchanger through the binding key (default is empty). The exchanger matches the routing key of the message with the binding key of the bound queue. The correctly matched message will Send to queue. The routing key is a production-oriented concept, while the binding key is a consumption-oriented concept.
  • Virtual host (vhost) : The basis of the AMQP concept, which is essentially a mini version of the proxy server (with its own queue, exchanger and binding, and more importantly, its own permission mechanism), RabbitMQ's default vhost: "/" (similar to namespace in the network), each user can only access his own vhost (usually assigned at least one vhost), and then the user can only access his own queue, exchanger and binding, so between vhosts Is absolutely isolated (security and portability).
  • Consumer : Connect to the proxy server and subscribe to the queue. The proxy server will send the message to a subscribed/listening consumer. The consumer can only receive part of the message: the payload. .

3. What are switches, queues, and bindings?

The producer sends messages to the broker server (RabbitMQ). Within the Broker, users create Exchange/Queue and connect the two through Binding rules. Exchange distributes messages, and the distribution strategies vary depending on the type/binding. The message finally comes to the Queue, waiting for consumers to pick it up.

3.1 Switch types

In RabbitMQ, when a producer sends a message, he will not directly deliver the message to the queue. Instead, he will deliver the message to the switch first, and then forward it to the specific queue by the switch. The queue will then push or pull the message to the consumer. consume

  • direct exchange
    If the routing key is matched successfully, the message will be delivered to the corresponding queues. The binding key does not support "*" and "#". Consumers can give feedback to producers on the channel that receives messages.
    Insert image description here
    If the "routing key" in the Message is consistent with the "binding key" in Binding, Direct Exchange will send the message to the corresponding queue.
  • fanout
    does not exist (routing key), the message is sent to the corresponding queue in the form of automatic polling (round-robin) (kindergarten distributes apples to children, if there are enough apples, they will be distributed equally to everyone), the queue does not exist Binding key, the consumer can give feedback to the producer on the current channel that receives the message.
    Insert image description here

Every message sent to Fanout type Exchange will be distributed to all bound queues.

  • The topic
    exists (routing key), and the message is sent in the form of broadcast to each queue matching the binding key (bing key). The binding key (binding key) supports " " and "#" "#" wildcard any
    zero Or multiple words
    "
    " wildcard any single word.
    Here we also recommend a website to students who want to learn about RabbitMQ, http://tryrabbitmq.com, which provides an online RabbitMQ simulator that can help understand Exchange/queue/binding concepts.
    According to the routing key and wildcard rules, the Topic exchange will be distributed to the target queue.
    Insert image description here

  • headers (deprecated)
    omitted

Guess you like

Origin blog.csdn.net/suyuaidan/article/details/133218625