RabbitMQ learning-the first theoretical basis

1. Definition

Message queue : A container that holds messages during the transmission of messages.

This is a more classic consumer-producer model. It is more abstract and analogy: the A thread needs to send a message to the B thread (A and B threads are not necessarily on the same machine). Sent to the message queue server, and then the B thread reads or subscribes to the message in the message queue on the message server. There is no direct communication between threads A and B. The MQ server acts as a relay in the middle.

2. Applicable scenarios

More suitable for asynchronous transmission.

Asynchronous : The sender does not care whether the message is sent successfully, but only sends the message, and does not get whether the message was sent successfully.

Synchronization : The sender is concerned about whether the message was successfully sent. After sending the message, it will wait for the receiver to return a status code, determine whether the message was successfully sent according to the status code, and then perform the corresponding action.

Take synchronous and asynchronous in Http as an example :

For example, the communication between the common B / S architecture client and the server is synchronous, that is, submit the request —> wait for the server to process and return the message —> get the message returned by the server, and the process is completed.

For example: Ajax technology is asynchronous, the request is triggered by an event-> server processing (the browser does not have to wait, you can still do other things)-> processing is complete.

3. RabbitMQ

There are many similar products in Message Queuing, such as RabbitMQ, Redis, ZeroMQ, ActiveMQ, Jafka / Kafka, etc.

RabbitMQ is a standard implementation of AMQP (Advanced Message Queuing Protocol), and a quick introduction to it can be viewed here: http://www.rabbitmq.com/getstarted.html.

3.1 Role concept

Every open source project has its own design method and module role, and RabbitMQ is no exception. The structure diagram is as follows:

Insert picture description here

Broker: the message queue server entity

Exchange: Message exchange, which specifies the rules by which messages are routed to which queue.

Queue: Message queue carrier, each message will be put into one or more queues.

Binding: Binding, its role is to bind exchange and queue according to routing rules.

Routing Key: routing key, exchange delivers messages based on this key.

vhost: A virtual host. Multiple vhosts can be set up in a broker to separate the permissions of different users.

producer: A message producer is a program that delivers messages.

consumer: A message consumer is a program that receives messages.

channel: Message channel. In each connection of the client, multiple channels can be established, and each channel represents a session task.

3.2 Working process

  • Producer client :
  1. The client connects to the RabbitMQ server and opens a message channel (channel);
  2. The client declares a message exchange (exchange) and sets related attributes;
  3. The client declares a message queue (queue) and sets related properties;
  4. The client uses the routing key to establish a binding relationship between the message exchange (exchange) and the message queue (queue);
  5. Client messages are delivered to the message exchange (exchange);
  6. The client closes the message channel (channel) and the connection with the server.
  • Server side :

After the exchange receives the message, it performs message routing based on the message key and the set binding, and delivers the message to one or more message queues.

There are also several types of exchanges:

(1). Direct switch: deliver based on key. For example, when the binding key is set to abc during binding, only the key set to abc will be posted to the queue when the client submits the information.

(2) .Topic switch: deliver the key after pattern matching. For example: the symbol "#" matches one or more characters, the symbol "*
" matches a series of consecutive alphabetic characters, for example "abc. #" Can match "abc.def.ghi", and "abc.
*" Can only match "Abc.def".

(3). Fanout switch: It adopts the broadcast mode. When the message comes in, it will be delivered to all queues bound to the switch.

  • Consumer client :
    (not researched yet)

3.3 RabbitMQ message persistence

RabbitMQ supports data persistence, that is, writing data on disk can increase data security. Message queue persistence includes three parts:

  1. Message exchange (exchange) persistence, specify the durable as 1 when declaring
  2. Message queue (queue) persistence, specify durable as 1 when declaring
  3. Message persistence, specify delivery_mode as 2 when delivering (1 is non-persistent)

If both the message exchange (exchange) and the message queue (queue) are persistent, then the binding between them (Binding) is also persistent. If there is a persistent and a non-persistent between the message exchange and the message queue, then no binding is allowed.

Published 40 original articles · 25 praises · 100,000+ views

Guess you like

Origin blog.csdn.net/yym373872996/article/details/105651557