RabbitMQ---Into MQ (1)

RabbitMQ

1 What is MQ

  • MQ (message queue), which means by phrase消息队列
  • MQ is a container for saving messages during the transmission of messages
  • The queue follows FIFOthe first-in, first-out rule (water that comes in first goes out)
  • image-20220111104755175
  • It is typical: producer, consumer model
  • The production and consumption of messages are asynchronous
  • MQ is a cross-process communication mechanism used to transmit messages between upstream and downstream systems
  • RabbitMQ uses AMQP (Advanced Message Queuing Protocol)
  • MQ is a kind of "logical decoupling + physical decoupling" for upstream and downstream systems.消息通讯服务

2 Why use MQ

2.1 Flow peak reduction

When a large amount of request data appears at a specific time, MQ can queue requests that exceed the system's capacity.

Suppose we have an application with an average traffic volume of 300 requests per second. We can easily cope with it with a single server
low flow
. However, during peak periods, the traffic volume instantly increases tenfold to 3,000 requests per second. Then a single server must not be able to. In response, at this time we can consider increasing to 10 servers to achieve load balancing of access pressure,
traffic peak
but if this instantaneous peak occurs only once a day, and each time is only half an hour, then our 10 servers will only share each Dozens of requests per second, which is a bit of a waste of resources. In
traffic peak
this case, we can use MQ to cut traffic peaks. Under peak conditions, a large amount of request data that appears instantaneously is sent to the message queue server first, and queued to be processed. In our application, we can slowly receive request data from the message queue for processing, which prolongs the data processing time and reduces the instantaneous pressure.

2.2 Application Decoupling

MQ is equivalent to an intermediary. The producer interacts with the consumer through MQ. It decouples the application and reduces the strong dependency between upstream and downstream systems.

If there is an e-commerce system now, the order module depends on the payment module to exist, so if the payment system is broken, then the order system is meaningless; if there is MQ, it will be different, the two systems are no longer directly connected, Instead, the producer sends the message to the queue for queuing. The consumer goes to the queue to get it. If there is a problem with the payment system, the producer sends a message to the MQ queue normally, and MQ stores the message in the queue. Until the payment system returns to normal, the message stored in the queue is taken out to continue processing.

2.3 Asynchronous processing

Because the production and consumption of messages are asynchronous, and only care about the sending and receiving of messages, there is no intrusion of business logic, which realizes the decoupling of producers and consumers

3 AMQP and JMS

MQ is a model of message communication, and it is implemented concurrently. Now there are two mainstream ways to implement MQ: AMQP, JMS

Differences and connections between the two:

AMQP Etc
Unify the format of data interaction by specifying protocols A unified interface is defined to unify message operations
It's just a protocol, it doesn't specify an implementation, so it's cross-language Java language must be used
Multiple message models Two message models

4 Which MQ to use

The mainstream MQ is as follows. Some of the online comparison charts are, but the specific differences are not read, and the summary is directly:

  • ActiveMQ: based on JMS; relatively old, and not updated now, not selected for special cases
  • RabbitMQ: Based on the AMQP protocol, because it is in the elang language, the learning cost is high if the application is deep, but it is sufficient for small and medium-sized companies
  • RocketMQ: Based on JMS, it is suitable for teams with high technical level and energy to do the underlying research.
  • kafka: Distributed messaging system, high throughput, dedicated to big data

5 RabbitMQ keywords

Future learning and usage will always revolve around these words

  • Broker: message queue service process. This process includes two parts: Exchange and Queue. It provides a transmission service. Its role is to maintain a route from producers to consumers to ensure that data can be transmitted in a specified way (Broker). =Exchange+Queue)
  • vhost: Each vhost is essentially a mini version of the RabbitMQ server, with its own switches, queues, bindings, etc., and its own permission mechanism; vhosts are to Rabbit as virtual machines are to physical machines. They allow running data to be kept safe and confidential for different applications by providing logical separation between instances
  • Producer: An application that sends messages
  • Connection: TCP connection connecting RabbitMQ to the application server
  • Channel: A virtual channel in the connection. When you send or receive messages through the message queue, this operation is carried out through the channel
  • Exchange (exchange): a message exchange that accepts messages sent by producers and routes messages to queues in the server according to Binding rules
    • Exchange is similar to a switch in a data communication network, providing message routing strategies
    • The producer sends the message to Exchange first
    • The binding of Exchange and Queue can be a many-to-many relationship
    • When the producer transmits a message, it will pass a ROUTING_KEY, and the Exchange will route the message to the specified queue according to the ROUTING_KEY according to a specific routing algorithm.
    • Exchange can also be set to persist, temporarily or automatically delete
  • Routing Key: When a producer sends a message to Exchange, it generally specifies a routing key to specify the routing rules for the message. The routing key can be said to be the destination address of the message
  • Binding Key: Exchange and Message Queue are connected through binding key
  • Queue (queue): the carrier of the message, each message will be put into one or more queues
  • Message (message): the information sent by the producer to the consumer through RabbitMQ
  • Consumer: An application that receives messages.
  • ACK (message acknowledgment mechanism): After the consumer receives the message from RabbitMQ and processes it, it feeds back to RabbitMQ, and RabbitMQ deletes the message from the queue after receiving the feedback
  • Users: In RabbitMQ, it is possible to connect by specifying a username and password. Each user can be assigned different permissions, such as read permissions, write permissions, and permissions to configure the instance

6 Communication process

Linking the above keywords is as follows

image-20220112161857828

  • When a user wants to produce/consume messages, the user first establishes a connection between himself and the MQ service
  • Then open a channel for sending messages to MQ
  • After connecting, the producer sends the message with the routing key Routing Key to the MQ exchange Exchange
  • Exchange routes the message to the queue after binding the relationship between itself and the queue according to the set forwarding policy type and the Binding Key
  • The queue caches the message, the consumer goes to the queue to consume the message, and after consumption, it will return an ACK to the queue, and the queue will delete the cache of the message after receiving the ACK corresponding to the message (you can decide whether to persist or delete it through configuration)

7 Four kinds of switches of RabbitMQ

7.1 Directly connected switch: Direct exchange

  • The default switch of the system works similar to unicast. Exchange will send messages to a Queue that exactly matches the ROUTING_KEY.
  • A directly connected switch is a switch with routing function. A queue is bound to a switch, and a routing_key is bound to it.
  • When a message is sent, a binding_key needs to be specified, and when the message is delivered to the switch, it will be sent to the specified queue by the switch
  • The same binding_key also supports applying to multiple queues
  • In this way, when a switch is bound to multiple queues, it will be sent to the corresponding queue for processing

img

7.2 Fanout exchange: Fanout exchange

Broadcast switch, no matter what the ROUTING_KEY of the message is, the Exchange will forward the message to all bound Queues

A sector switch is the most basic type of switch, and what it can do is very simple—broadcast messages.

The sector switch will send all the messages it can receive to the queue bound to itself.

Because broadcasting does not require "thinking", sector switches are also the fastest of all switch types.

img

7.3 Topic exchange: Topic exchange

Topic exchange, works like multicast, Exchange will forward messages to all queues with the same ROUTING_KEY matching pattern

  • The message sent to the topic exchange needs to carry the routing_key of the specified rule

  • The topic exchange will send data to the corresponding (multiple) queues according to this rule.

  • The routing_key of the topic exchange needs to have certain rules, and *.#.*.....the format of the binding_key of the exchange and queue needs to be adopted, and each part is used .separately, among which:

    • *means match an arbitrary phrase

    • #Indicates matching zero or more phrases

      img

  • When the binding key of a queue is #, the queue will ignore the routing key of the message and receive all messages

7.4 Headers exchange: Headers exchange

Define a Hash data structure. When a message is sent, it will carry a set of hash data structure information.

When the content of the Hash matches, the message will be written to the queue

When binding a switch and a queue, a key "x-match" is required to be carried in the Hash structure. The value of this key can be any or all, which means whether the hash carried in the message needs to match all (all) or only one key (any) is fine. Compared with directly connected switches, the advantage of the first switch is that the matching rules are not limited to strings (string)

8 RabbitMQ's 6 core modes

8.1 Simple mode

img

8.2 Working Mode

img

8.3 Publish/Subscribe Model

img

8.4 Routing Mode

img

8.5 Theme Mode

img

8.6 Release/Confirm Mode

img

Guess you like

Origin blog.csdn.net/qq_37768368/article/details/122451323