The hand and take you through the messaging middleware (1)

Before introducing any news middleware, introduce a few concepts:

1. What is the message

  It refers to message one way communication and interaction between software objects utilized.

2. What is the middleware

  Non-underlying operating system software, non-business applications, not directly to end users, and can not bring direct value to customers collectively referred to as middleware software

3. What is the message queue

  Message Queue message is an implementation of the middleware.

4. After the introduction to the concepts of the above, I believe you already have some idea of ​​the message, middleware, message queues, so then we have to introduce what is the message middleware

  Focuses on the sending and receiving of data, highly efficient and reliable use of asynchronous messaging platform-independent mechanism for data exchange and data communications on a distributed system integration

  which may be more abstract, since the message queue is a way messaging middleware implemented, as we from its practical application scenarios to further understand it, the message queue scenarios are mainly the following aspects:

4.1 asynchronous processing:

  场景说明:提供新用户注册后,发送短信或邮件的功能
传统的做法有有两种 1.串行方式;2.并行方式
 2、串行方式:将注册信息写入数据库成功后,发送注册邮件,再发送注册短信。以上三个任务全部完成后,返回给客户端。

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

  假设三个业务节点每个使用50毫秒钟,不考虑网络等其他开销,则串行方式的时间是150毫秒,并行的时间可能是100毫秒。
  因为CPU在单位时间内处理的请求数是一定的,假设CPU1秒内吞吐量是100次。则串行方式1秒内CPU可处理的请求量是7次(1000/150)。并行方式处理的请求量是10次(1000/100)
小结:如以上案例描述,传统的方式系统的性能(并发量,吞吐量,响应时间)会有瓶颈。
  引入消息队列,将不是必须的业务逻辑,异步处理。改造后的架构如下:

  按照以上约定,用户的响应时间相当于是注册信息写入数据库的时间,也就是50毫秒。注册邮件,发送短信写入消息队列后,直接返回,因此写入消息队列的速度很快,基本可以忽略,因此用户的响应时间可能是50毫秒。因此架构改变后,系统的吞吐量提高到每秒20 QPS。比串行提高了3倍,比并行提高了两倍。

4.2 应用解耦:

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

  传统模式的缺点:假如库存系统无法访问,则订单减库存将失败,从而导致订单失败,订单系统与库存系统耦合
  引入应用消息队列后的方案:

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

4.3 流量削峰:

  场景说明:秒杀活动,一般会因为流量过大,导致流量暴增,应用挂掉。为解决这个问题,一般需要在应用前端加入消息队列。通过加入消息队列完成如下功能:
 a、可以控制活动的人数
 b、可以缓解短时间内高流量压垮应用

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

Guess you like

Origin www.cnblogs.com/kaischoolmate/p/12104076.html