Message queue MQ (1) - Concepts and usage scenarios

 

First, what is the message queue

        Message that is the information carrier. In order to let the message sender and the message recipient can understand the information message carried (message sender needs to know how to construct a message; the message recipient need to know how to interpret the message), they need to be described in accordance with a uniform message format, kind of uniform format called message protocol (JMS) . Therefore, necessarily have a certain valid message format; without formatted message is meaningless.

       Message from the sender to the receiver, there are two ways:

      A, instant message communication , the message from the (message sender) can be issued immediately end reached the other end (message recipient), the specific implementation of this embodiment is the RPC (http simple communication of course also conform to this definition);

    Second, the delay message communication , the message sent from the one end, first into a container for temporary storage, when the particular condition, and then transmitted from the container to the other end, this particular implementation is for a container of the message queue .

 

Second, the message queue scenarios

       The following describes the message queue is commonly used in practical application scenarios. Asynchronous processing , application of decoupling , the flow rate cut front and messaging communications four scenes.

2.1 asynchronous processing

   Scene Description: After user registration, you need to send registered mail and SMS registration. 1. The conventional practice has two serial fashion ;. 2 in a parallel manner .

   (1) serial mode : After successful registration information into the database, send registered mail and then send the registration SMS. After more than three tasks completed, returned to the client.

   (2) parallel : After successful registration information into the database, and send a registered mail, send a registration SMS. After more than three tasks completed, returned to the client. The difference is that serial, parallel way of processing time can be improved.

     Each service node is assumed that the use of three 50 msec clock, without considering other network overhead and the like, the time is 150 milliseconds in a serial fashion, parallel time may be 100 milliseconds.

     Because the number of requests processed by the CPU per unit time is constant, is assumed that the CPU1 100 sec throughput.

     Serial mode:  the amount of CPU request can be processed within one second is 7 times (1000/150)

     In parallel:  the amount of CPU 1 second request can be processed is 10 (1000/100).

     Summary : As described in the above case, the performance of the traditional way of the system (amount of concurrency, throughput, response time) will be a bottleneck. How to solve this problem?

     Introducing the message queue , the service logic is not necessary, asynchronous processing .

     After transformation, the structure is as follows:

  According to the above convention:

       The user response time is the time corresponding to the registration information into the database, which is 50 ms.

       After registration messages, send SMS message queue is written directly back, so the message queue writing speed is fast, can be ignored, the user response time may be 50 ms.

       Therefore architectural changes to the system throughput to 20 per QPS. Serial 3-fold increased compared, than parallel tripled.

2.2 Application of Decoupling

 Scene Description : After the user orders, the order system needs to notify the inventory system. The traditional approach is that the interface inventory system order system call. As shown below:

The traditional model of disadvantages:

1)  If the inventory system is not accessible, orders minus inventories will fail, resulting in the order failed;

2)  the order and inventory system coupling system ;

      How to solve the above problem? After the introduction of the application program message queue, as shown below:

  • Order System : After a single user, the order processing system to complete persistence, writes messages to the message queue, the user returns the successful single order.
  • Inventory System : single subscription to the message, using the pull / push manner, acquires the order information, inventory information system according to the orders, inventory operations.
  • Given: At the next single inventory system can not work properly. Does not affect the normal order, because the order, the order system writes message queue is no longer concerned about the other follow-up operations.
  •            Decoupling order to achieve application system and inventory system. ----------- If the port is not how to do inventory, order systems have the next one?

2.3, traffic cut front

     The flow front is cut in the message queue common scenarios, groups generally used in a rush or spike activity widely.

    Scenario: spike activity, usually because of too much traffic, leading to traffic surge, application hang. To solve this problem, the general need to add the message queue in front-end applications.

  1. You can control the number of activities;
  2. A short time can ease the traffic overwhelmed applications;

  1. The user's request, the server receives, first written message queue. If the queue size exceeds the maximum number, the user request or discarded directly jump to the error page;
  2. The spike service information request message queue, do the subsequent processing.

2.4, log processing

Log processing means in the message queue with log processing, such as Kafka applications, a number of problems to solve log transfer. Architecture simplified as follows:

  • Log collection client, the log data acquisition, the timing of the writing by the write queue Kafka;
  • Kafka message queue, is responsible for receiving log data, store and forward;
  • 日志处理应用:订阅并消费kafka队列中的日志数据;

以下是新浪kafka日志处理应用案例:

(1)Kafka:接收用户日志的消息队列。

(2)Logstash:做日志解析,统一成JSON输出给Elasticsearch。

(3)Elasticsearch:实时日志分析服务的核心技术,一个schemaless,实时的数据存储服务,通过index组织数据,兼具强大的搜索和统计功能。

(4)Kibana:基于Elasticsearch的数据可视化组件,超强的数据可视化能力是众多公司选择ELK stack的重要原因。

 

三、消息模式

    它有两种消息模式:点对点模式发布订阅模式

3.1、点对点模式

  点对点模式包含三个角色:消息队列(Queue)发送者(Sender)接收者(Receiver)

  每个消息都被发送到一个特定的队列,接收者从队列中获取消息。队列保留着消息,直到他们被消费或超时。

点对点的特点

  1. 每个消息只有一个消费者(Consumer)(即一旦被消费,消息就不再在消息队列中)
  2. 发送者和接收者之间在时间上没有依赖性,也就是说当发送者发送了消息之后,不管接收者有没有正在运行,它不会影响到消息被发送到队列
  3. 接收者在成功接收消息之后需向队列应答成功

         如果希望发送的每个消息都会被成功处理的话,那么需要P2P模式。

3.2、发布订阅模式

  包含三个角色:主题(Topic)发布者(Publisher)订阅者(Subscriber) 。

  多个发布者将消息发送到Topic,系统将这些消息传递给多个订阅者。

Pub/Sub的特点

  1. 每个消息可以有多个消费者
  2. 发布者和订阅者之间有时间上的依赖性。针对某个主题(Topic)的订阅者,它必须创建一个订阅者之后,才能消费发布者的消息。
  3. 为了消费消息,订阅者必须保持运行的状态。

       为了缓和这样严格的时间相关性,JMS允许订阅者创建一个可持久化的订阅。这样,即使订阅者没有被激活(运行),它也能接收到发布者的消息。  

  如果希望发送的消息可以不被做任何处理、或者只被一个消息者处理、或者可以被多个消费者处理的话,那么可以采用Pub/Sub模型。

 

本文转自:https://blog.csdn.net/kingcat666/article/details/78660535

 

一、什么是消息队列

        消息即是信息的载体。为了让消息发送者和消息接收者都能够明白消息所承载的信息(消息发送者需要知道如何构造消息;消息接收者需要知道如何解析消息),它们就需要按照一种统一的格式描述消息,这种统一的格式称之为消息协议(JMS)。所以,有效的消息一定具有某一种格式;而没有格式的消息是没有意义的。

       消息从发送者到接收者的方式也有两种:

      一、即时消息通讯,消息从一端发出后(消息发送者)立即就可以达到另一端(消息接收者),这种方式的具体实现就是RPC(当然单纯的http通讯也满足这个定义);

    二、延迟消息通讯,消息从某一端发出后,首先进入一个容器进行临时存储,当达到某种条件后,再由这个容器发送给另一端,这个容器的一种具体实现就是消息队列

 

二、消息队列的应用场景

       以下介绍消息队列在实际应用中常用的使用场景。异步处理应用解耦流量削锋消息通讯四个场景。

2.1、异步处理

   场景说明:用户注册后,需要发注册邮件和注册短信。传统的做法有两种1.串行的方式;2.并行方式

   (1)串行方式:将注册信息写入数据库成功后,发送注册邮件,再发送注册短信。以上三个任务全部完成后,返回给客户端。

   (2)并行方式:将注册信息写入数据库成功后,发送注册邮件的同时,发送注册短信。以上三个任务完成后,返回给客户端。与串行的差别是,并行的方式可以提高处理的时间。

     假设三个业务节点每个使用50毫秒钟,不考虑网络等其他开销,则串行方式的时间是150毫秒,并行的时间可能是100毫秒。

     因为CPU在单位时间内处理的请求数是一定的,假设CPU1秒内吞吐量是100次。

     串行方式: 1秒内CPU可处理的请求量是7次(1000/150)

     并行方式: 1秒内CPU可处理的请求量是10次(1000/100)。

     小结如以上案例描述,传统的方式系统的性能(并发量,吞吐量,响应时间)会有瓶颈。如何解决这个问题呢?

     引入消息队列将不是必须的业务逻辑,异步处理

     改造后的架构如下:

  按照以上约定:

       用户的响应时间相当于是注册信息写入数据库的时间,也就是50毫秒。

       注册邮件,发送短信写入消息队列后,直接返回,因此写入消息队列的速度很快,基本可以忽略,因此用户的响应时间可能是50毫秒。

       因此架构改变后,系统的吞吐量提高到每秒20 QPS。比串行提高了3倍,比并行提高了两倍。

2.2、应用解耦

 场景说明用户下单后,订单系统需要通知库存系统。传统的做法是,订单系统调用库存系统的接口。如下图:

传统模式的缺点:

1)  假如库存系统无法访问,则订单减库存将失败,从而导致订单失败;

2)  订单系统与库存系统耦合

      如何解决以上问题呢?引入应用消息队列后的方案,如下图:

  • 订单系统:用户下单后,订单系统完成持久化处理,将消息写入消息队列,返回用户订单下单成功。
  • 库存系统:订阅下单的消息,采用拉/推的方式,获取下单信息,库存系统根据下单信息,进行库存操作。
  • 假如:在下单时库存系统不能正常使用。也不影响正常下单,因为下单后,订单系统写入消息队列就不再关心其他的后续操作了。
  •            实现订单系统与库存系统的应用解耦。 -----------假如库存端口不够怎么办,订单系统一直在下单?

2.3、流量削锋

     流量削锋也是消息队列中的常用场景,一般在秒杀或团抢活动中使用广泛。

    应用场景:秒杀活动,一般会因为流量过大,导致流量暴增,应用挂掉。为解决这个问题,一般需要在应用前端加入消息队列。

  1. 可以控制活动的人数;
  2. 可以缓解短时间内高流量压垮应用;

  1. 用户的请求,服务器接收后,首先写入消息队列。假如消息队列长度超过最大数量,则直接抛弃用户请求或跳转到错误页面;
  2. 秒杀业务根据消息队列中的请求信息,再做后续处理。

2.4、日志处理

日志处理是指将消息队列用在日志处理中,比如Kafka的应用,解决大量日志传输的问题。架构简化如下:

  • 日志采集客户端,负责日志数据采集,定时写受写入Kafka队列;
  • Kafka消息队列,负责日志数据的接收,存储和转发;
  • 日志处理应用:订阅并消费kafka队列中的日志数据;

以下是新浪kafka日志处理应用案例:

(1)Kafka:接收用户日志的消息队列。

(2)Logstash:做日志解析,统一成JSON输出给Elasticsearch。

(3)Elasticsearch:实时日志分析服务的核心技术,一个schemaless,实时的数据存储服务,通过index组织数据,兼具强大的搜索和统计功能。

(4)Kibana:基于Elasticsearch的数据可视化组件,超强的数据可视化能力是众多公司选择ELK stack的重要原因。

 

三、消息模式

    它有两种消息模式:点对点模式发布订阅模式

3.1、点对点模式

  点对点模式包含三个角色:消息队列(Queue)发送者(Sender)接收者(Receiver)

  每个消息都被发送到一个特定的队列,接收者从队列中获取消息。队列保留着消息,直到他们被消费或超时。

点对点的特点

  1. 每个消息只有一个消费者(Consumer)(即一旦被消费,消息就不再在消息队列中)
  2. 发送者和接收者之间在时间上没有依赖性,也就是说当发送者发送了消息之后,不管接收者有没有正在运行,它不会影响到消息被发送到队列
  3. 接收者在成功接收消息之后需向队列应答成功

         如果希望发送的每个消息都会被成功处理的话,那么需要P2P模式。

3.2、发布订阅模式

  包含三个角色:主题(Topic)发布者(Publisher)订阅者(Subscriber) 。

  多个发布者将消息发送到Topic,系统将这些消息传递给多个订阅者。

Pub/Sub的特点

  1. 每个消息可以有多个消费者
  2. 发布者和订阅者之间有时间上的依赖性。针对某个主题(Topic)的订阅者,它必须创建一个订阅者之后,才能消费发布者的消息。
  3. 为了消费消息,订阅者必须保持运行的状态。

       为了缓和这样严格的时间相关性,JMS允许订阅者创建一个可持久化的订阅。这样,即使订阅者没有被激活(运行),它也能接收到发布者的消息。  

  如果希望发送的消息可以不被做任何处理、或者只被一个消息者处理、或者可以被多个消费者处理的话,那么可以采用Pub/Sub模型。

 

本文转自:https://blog.csdn.net/kingcat666/article/details/78660535

 

Guess you like

Origin www.cnblogs.com/651434092qq/p/11081764.html