Detailed explanation of the basic concept of message queue of message queue (2)

foreword

The previous article explained the usefulness and usage scenarios of message queues. This article will give you a brief introduction to some of its basic concepts.

First, the basic concept of message queue

1.1 Broker

The concept of Broker comes from Apache ActiveMQ, which is the MQ server in layman's terms.

1.2 Message producers and consumers

  • Message producer Producer: Send messages to the message queue.
  • Message consumer Consumer: Receive messages from the message queue.

1.3 Message Model

  • Point-to-point message queue model
    Message producers send messages to a specific queue, and message consumers receive messages from the queue.
    The producer and consumer of the message may not be running at the same time.
    Each successfully processed message is acknowledged by the message consumer. As shown in the figure:

  • Publish-subscribe message model - In
    the Topic publish-subscribe message model, it supports publishing messages to a specific topic topic, and 0 or more subscribers receive messages from
    this message topic. Under this model, publishers and subscribers are unaware of each other. In the actual operation process,
    you must subscribe first, then send the message, and then receive the subscribed message . This order must be guaranteed.

1.4 Message Ordering Guarantee

Based on the Queue message model, the sequence of messages can be guaranteed by using the first-in-first-out feature of FIFO.

1.5 ACK confirmation mechanism of message

That is, the Acknowledgement mechanism of the message:
In order to ensure that the message is not lost, the message queue provides a message Acknowledge mechanism, that is, the ACK mechanism. When the Consumer
confirms that the message has been consumed and processed, it sends an ACK to the message queue. At this time, the message queue can delete the message. .
If the Consumer is down/shutdown and no ACK is sent, the message queue will think that the message has not been processed and will resend the
message to other Consumers for re-consumption processing.

1.6 Informal endurance

Message persistence is very important for some key core businesses. After enabling message persistence, after the message queue is
down and restarted, messages can be recovered from persistent storage, messages are not lost, and consumption and processing can continue.

1.7 Synchronous and asynchronous sending and receiving of messages

  • Synchronization The sending and receiving of
    messages supports synchronous sending and receiving. In the
    synchronous sending and receiving scenario, the message producer and the consumer respond in a two-way mode. For example, Zhang San writes a letter to the post office transfer station,
    and then Li Si gets the letter from the transfer station, and then writes a letter. A copy of the return receipt will be put at the transfer station, and then Zhang San will pick it up. Of course, when Zhang San
    writes the letter, he must write down the return address.
    If the message is received in a synchronous way (Pull), if the queue is empty, the receiving will be in a synchronous
    blocking state and will wait until the message arrives.
  • Asynchronous sending and receiving of
    messages also supports asynchronous methods: sending messages asynchronously, without waiting for the receipt confirmation from the message queue.
    Receive messages asynchronously, and trigger message consumers to receive messages in a push manner.

1.8 Transaction Support for Messages

The sending and receiving of messages supports transactions. For example, in the task center scenario, one processing may involve the receiving and
processing of multiple messages, which are within the scope of the same transaction. If a message fails to be processed, the transaction is rolled back and the message is returned to the queue. middle.

2. JMS consumer service

The Java Message Service (JMS) API is an API for message-oriented middleware (MOM) in the Java platform, which is used to send messages between two applications, or in a distributed system, for asynchronous communication . Point-to-point and publish-subscribe were originally defined by JMS. The main difference or problem between these two modes is whether the messages sent to the queue can be repeatedly consumed (multi-subscription) The JMS specification currently supports two message models:



  • point to point (queue)
  • publish/subscribe (publish/subscribe, topic)

2.1 Point-to-point: Queue, non-repeatable consumption

The message producer produces the message and sends it to the queue, and then the message consumer takes it out of the queue and consumes the message.
After the message is consumed, there is no more storage in the queue, so it is impossible for the message consumer to consume the message that has been consumed.
Queue supports the existence of multiple consumers, but for a message, only one consumer can consume it.

The P2P mode consists of three roles:

  • Message queue (Queue)
  • Sender
  • Receiver

Each message is sent to a specific queue, and the receiver gets the message from the queue. Queues hold messages
until they are consumed or time out.

2.2 Publish/Subscribe: Topic, which can be consumed repeatedly

A message producer (publishing) publishes a message to a topic, and multiple message consumers (subscribing) consume the message at the same time.
Unlike the peer-to-peer approach, messages published to a topic are consumed by all subscribers.

Publish-subscribe mode that supports subscription groups: In the
publish-subscribe mode, when the publisher has a large amount of messages, it is obvious that the processing capacity of a single subscriber is insufficient. In fact, in the real
scenario , multiple subscriber nodes form a subscription group to load balance consumption topic messages, that is, group subscriptions, so that subscribers can
easily achieve linear expansion of consumption capacity. It can be seen that there are multiple Queues under a topic, each Queue is a point-to-point
method, and the Queues are published and subscribed .

2.3 Differences

  • Point-to-point mode The
    producer sends a message to the queue. A queue can have many consumers, but a message can only be
    accepted by one consumer . When no consumer is available, the message will be saved until there is an available consumer, so Queue implements
    a reliable load balancing.

  • Publish -subscribe mode The message sent by the publisher to the topic, only the subscribers who subscribe to the topic will receive the message. Topic implements publish and subscribe.
    When you publish a message, all services that subscribe to this topic can get the message, so from 1 to N subscribers can get
    a copy of the message.

3. Comparison of popular models

The traditional enterprise message queue ActiveMQ follows the JMS specification and implements the point-to-point and publish-subscribe model, but other popular
message queues, RabbitMQ and Kafka, do not follow the JMS specification.

3.1 RabbitMQ

RabbitMQ implements the AQMP protocol, which defines the message routing rules and methods. The producer sends messages
to , and the consumer consumes messages according to the queue name.
RabbitMQ supports both memory queues and persistent queues. The consumer is a push model, and the consumption status and subscription relationship are
maintained . Messages are deleted immediately after consumption, and historical messages are not retained.

  • Point-to-point The
    producer sends a message to the Queue through routing, and only one consumer can consume it.

  • Multiple subscriptions
    When RabbitMQ needs to support multiple subscriptions, the messages sent by the publisher are written to multiple Queues at the same time through routing, and different subscription groups consume
    different Queues. Therefore, when multiple subscriptions are supported, the message will be copied multiple times.

3.2 Kafka

Kafka only supports message persistence. The consumer is a pull model. The client is responsible for maintaining the consumption status and subscription relationship.
After the message is consumed, it will not be deleted immediately, and historical messages will be retained. Therefore, when multiple subscriptions are supported, only one copy of the message
can . But there may be duplication of consumption.

Reference article:
https://blog.csdn.net/heyutao007/article/details/50131089
https://www.cnblogs.com/tianqing/p/7110468.html

Guess you like

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