RabbitMQ concept, principle, process, working mode, etc. The most complete, detailed and easy to understand super star collection!

MQ definition

Message queue, emphasizing messages and queues.

Message: The producer completes a certain business and sends a message to the switch. On behalf of me, I have completed this function. What should I do next? The consumer will claim the message and consume it.

Queue: When the exchange distributes the message to each consumer, it goes to its own queue according to the bound key, and then the consumer consumes it according to the first-in first-out principle.

working principle

When using message queues, the key point is to allow consumers to consume reasonably. Consumers use the listener to monitor the routed messages at all times, and then perform corresponding operations.

work process

Insert picture description here
The workflow is divided into two parts, one is message delivery and the other is message consumption.

  • Message delivery

1. The producer establishes a TCP connection with the broker and builds a channel.

2. The producer sends the message to the exchange in the broker through the channel.

3. The exchange delivers the message to the corresponding queue according to demand.

  • Consumer news

1. The consumer and the broker establish a TCP connection and establish a channel.

2. The consumer monitors the corresponding queue.

3. Consumers consume when there are messages in the queue.

Operating mode

Simple mode,
Insert picture description here
competition mode,
Insert picture description here
broadcast mode,
Insert picture description here
perfect match mode,
Insert picture description here
rule match mode

Insert picture description here

Basic concept

channel

Channel, the transmission of messages is through the channel.

exchange

Switch, to relay the message. There are three types of switches.

1. Exact match (direct). The routing key and the binding key must be exactly the same in order to transfer messages.

2. Broadcast matching (fanout). The switch broadcasts the message to all bound queues.

3. Rule matching (topic). Similar to the fuzzy query of es, message transfer can be carried out if certain rules are met.

queue

Queue, where the message finally arrives, and where consumers get and consume.

Routing Key

When the producer sends a message to the switch, it specifies the routing key and the routing rules for this message.

Binding Key

The switch specifies the binding key to contact the message queue. The binding key is used to match the routing key. The diagram of these two keys is as follows.

Insert picture description here
··················································· ·····························
Concept related issues

ask:

The beginning of the program is to establish a TCP connection, the virtual connection is established on the TCP connection, and the data flow is carried out in channels.

We can use the TCP connection to complete the function of message transmission. Why not use the TCP connection directly and use the channels additionally?

answer:

Because TCP connections are costly, frequent establishment and closing have a great impact on system performance, and the number of TCP connections is limited.

The channels channel does not have this problem. Producers and consumers can use multiple channels to publish and subscribe concurrently. Many channels can be hung on a TCP connection.

RabbitMQ advantages

asynchronous:

An example of asynchronous call is called takeaway. After the user successfully places an order, he needs to find a takeaway brother for delivery, but this process is very long. Because of the limited personnel during peak periods, it is impossible to keep the user stuck in this place, so use the message queue , Improve the corresponding speed of the entire call link.

Decoupling:

Service A is an upstream service, responsible for generating data, and services B and C need to process these data.

With the continuous development and maintenance of the first, second, and third phases of the project, service D and service E will appear later. The code for this one-stop service is very bloated, and it is difficult to maintain when troubleshooting.

Peak clipping:

In the spike system, there are instantaneous peaks, and requests can be placed in the message middleware and processed in a queue.

Message queue scenario

Give a few examples

1. The points system of the CSDN blog. You obviously have posted a new article, why the points have not increased in real time? You are so slow, because you are still waiting in line for the news of a new article, don't worry.

2. Everyone has used Github, and you have forgotten your password. If you want to reset your password, you will see a page showing the expected time and time. You will receive an email in this mailbox. Why is the message not synchronized? , Why did you not receive our information in time? This is the role of message middleware. It may be due to too many people or delays or network reasons, so the website response is slow, and it will tell you a time. If you don’t use it In the case of message middleware, imagine a scenario where you click to send an email, and then the circle keeps turning around, turning around for ten minutes, and finally stops turning, and then tells you that the sending is successful. Two kinds of experiences, one tells you the estimated time directly, and the other turns around.

3. When you register as a user, you sometimes use an email address to let you receive an email, but this email is often not as fast as the SMS verification code, and it may take a few minutes to receive it.

··················································· ·····························
Have you abolished school

Guess you like

Origin blog.csdn.net/numbbe/article/details/109164222