Message Queue (0) - Background knowledge

foreword

I don’t know if you are unfamiliar with the message queue. If the message queue is unfamiliar, there is a model that everyone must have heard of. That is the producer-consumer model. The message queue is based on the producer-consumer model. into a program for everyone to use

message queue

The role of the message queue

①: Decoupling
Decoupling means: Generally, our communication parties A and B are directly connected to each other. In this way, if one party has a problem, the other party will also wait, and the message queue is equivalent to a bridge linking communication Both AB and AB, if one party has a problem, the other party will not go on strike immediately

②: Peak shaving
and valley shaving means: We all know that different servers have different performances. Assume that the performance of the server A of the communication parties is good, and the performance of B is worse. If a certain period of time suddenly requests a large increase , then server A can live on top, server B may not necessarily live on top, so if it is directly connected, then it is easy for the request sent from A to collapse B completely, and the existence of the message queue makes the request sent by A The request must first enter the message queue. Server B can take data from the message queue according to its working mode, respond and then return it to server A. In this way, the server will not be caused by the sudden increase in the request volume of A. B's strike

common message queue

What are the common message queues on the market?
RabbitMQ,
Kafka
, and RocketMQ

Our main purpose is how to simulate a message queue

insert image description here

The core concept of message queue

  1. producer (producer): the party that produces the data
  2. Consumer (consumer): the party that obtains the data
  3. Broker: The party that stores the data
  4. Publish (publish): The process of the producer delivering data to the intermediary
  5. Subscribe: The process in which consumers greet the intermediary to obtain data
  6. Consumption (consumer): the process in which the consumer fetches data from the intermediary

BrokerServer core concept

BrokerServer is a middleman and a server. What are its core concepts?

  1. Virtual host: BrokeServer can have multi-state virtual hosts. The role of virtual hosts is similar to the management of different assembly lines in factories, which store different logical data sets (it can be compared to databases in databases. There are multiple databases in a database, and there are multiple databases in a database. have different datasets)
  2. Switch: The switch is the object for the producer to deliver the message, similar to the company's front desk, leading you to the appropriate position, and then the switch sends the data to the corresponding message queue
  3. Queue: A queue is an entity that is actually used to store and process messages, and subsequent consumers also obtain data from the queue
  4. Binding: It is to establish an association relationship between the switch and the corresponding queue (there is a many-to-many relationship between the switch and the queue, a queue may correspond to multiple switches, and a switch may also be corresponding to multiple queues))
  5. Message: A message refers to the request sent by server A to server B or the response returned by server B to server A (the data in the message is user-defined, it can be a string or binary data)

The core API of the message queue

  1. Creating a queue (queueDeclare)
    The meaning of not using Create here is: create is just a simple creation, and the effect of Declared is that if the queue exists, it will not be created, and if it does not exist, it will be created
  2. Destroy the queue (queueDelete)
  3. Create an exchange (exchangeDeclare)
  4. Destroy an exchange (exchangeDelete)
  5. Create binding (queueBind)
  6. Destroy binding (queueUnbind)
  7. Publish a message (basicPublish)
  8. Subscribe to Messages (basicConsume)
  9. Acknowledgment message (basicACK)
    Acknowledgment message means that the consumer sends a display request to the intermediary that the message has been processed.

Working mode between message queue and consumer

  1. The first type of Push (push): Broke actively sends the received data to subscribed consumers
  2. The second type of Poll (garbage): Consumers actively mobilize Broke's API to fetch data

type of switch

In the message queue, there are different types of switches, which are used to specify different transmission rules. When the switch transmits a message, it determines which queue to transmit according to the rules.

  1. Direct - direct exchange: specifies the name of the target queue, direct exchange

The direct exchange can be understood as an exclusive red envelope, and only designated people can receive it

  1. Fanout - Fanout exchange: send messages to each queue bound to this exchange

Understand it as all red envelopes, everyone can receive it, and the money received is still the same

  1. Topic - topic switch: There is a password (bindKey) between the switch and the queue, and the sender sends the message to the specified queue through the password (routingKey)

It is understood as a red envelope for drawing pictures, and you can only get it if you complete the specified action

  1. Header - message header switch

Persistence of message queues

Of course, the message queue must be persistent. Otherwise, if you restart the computer, the data will be lost. So the
storage method of the message queue is one memory and one hard disk. The memory is for fast storage, and the hard disk is for data not to be lost.

Summarize

Implementation of a message queue: It is necessary to implement the producer-consumer model. The producer-consumer corresponds to the client and the server. It is necessary to provide a set of APIs for the client to remotely call the Borker Server provided by the network communication method. Method
To implement Borker Server, it is necessary to implement the core API and some basic concepts (queues, etc.)
and finally persist the data, how to store it in the hard disk, and the message queue is completed

Guess you like

Origin blog.csdn.net/qq_56454895/article/details/131947557