Introduction to rabbitmq

1、AMQP

    The AMQP protocol is a high-level abstraction layer message communication protocol, and RabbitMQ is an implementation of the AMQP protocol. It mainly includes the following components:

1.1 Server(broker)

    A process that accepts client connections and implements AMQP message queuing and routing functions.

1.2 Virtual Host

    In fact, it is a virtual concept, similar to the permission control group. There can be several Exchanges and Queues in a Virtual Host, but the minimum granularity of permission control is the Virtual Host.

1.3 Exchange

    Accept the message sent by the producer and route the message to the queue in the server according to the Binding rules. ExchangeType determines the behavior of Exchange routing messages. For example, in RabbitMQ, ExchangeType has three types: direct, Fanout, and Topic. The behaviors of different types of Exchange routing are different.

1.3.1 direct

    If the routing key matches, then the Message will be delivered to the corresponding queue. In fact, when the queue is created, it will automatically bind the exchange with the name of the queue as the routing key.

1.3.2 fanout

    Will broadcast to the responding queue.

1.3.3 topic

    Pattern matching on keys, such as ab* can be passed to all ab* queues.

1.4 Message Queue

    A message queue for storing messages that have not been consumed by consumers.

1.5 Message

    Composed of Header and Body, Header is a collection of various properties added by the producer, including whether the Message is persisted, which Message Queue accepts it, and what the priority is. The Body is the APP data that really needs to be transmitted.

1.6 Binding

    Binding contacted Exchange and Message Queue. Exchange will generate a routing table after binding with multiple Message Queues. The routing table stores the constraints of the messages required by the Message Queue, that is, the Binding Key. When Exchange receives the Message, it parses its Header to get the Routing Key, and the Exchange routes the Message to the Message Queue according to the Routing Key and Exchange Type. The Binding Key is specified by the Consumer when Binding Exchange and Message Queue are used, and the Routing Key is specified by the Producer when sending the Message. The matching method of the two is determined by the Exchange Type. 

1.7 Connection

    A connection, for RabbitMQ, is actually a TCP connection between the client and the Broker.

1.8 Channel

    Channel, only after the connection between the client and the broker is created, the client still cannot send messages. A Channel needs to be created for each Connection. The AMQP protocol stipulates that AMQP commands can only be executed through the Channel. A Connection can contain multiple Channels. The reason why Channel is needed is because the establishment and release of TCP connections are very expensive. If each thread of a client needs to interact with the Broker, if each thread establishes a TCP connection, regardless of whether the TCP connection is wasted or not, Even the operating system can't afford to make so many TCP connections per second. RabbitMQ recommends not to share Channels between client threads, at least to ensure that the threads sharing Channels must send messages serially, but it is recommended to share Connections as much as possible.

    Why use Channel, instead of using Connection (TCP connection) directly?

    For the OS, establishing and closing TCP connections is costly. Frequent establishment and closing of TCP connections has a great impact on the performance of the system, and the number of TCP connections is also limited, which also limits the system's ability to handle high concurrency. However, establishing a Channel in a TCP connection has no such cost. For Producer or Consumer, multiple Channels can be used concurrently for Publish or Receive.

1.9 Command

    AMQP command, the client completes the interaction with the AMQP server through the Command to realize its own logic. For example, in RabbitMQ, the client can send messages through the publish command, txSelect starts a transaction, and txCommit commits a transaction.

2. System Architecture

  •  The producer sends the message to the exchange.
  • After the exchange accepts the message, it is responsible for routing it to a specific queue.
  • Bindings are responsible for connecting exchanges and queues.
  • The message arrives in the queue (queue), and then waits to be processed by the message receiver.
  • The consumer handles the message.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326218167&siteId=291194637