Chat about the design and implementation logic of the message center

Tired of being interrupted by news, but also afraid of sudden silence;

1. Business Background

In the microservice architecture system, there will be many basic services that provide some capabilities that most services may need, such as file management, MQ queues, caching mechanisms, message centers, etc. These services need to provide various reusable methods. Or interface, so that other business services can be called quickly; let's take a look at the principle of message notification:

The message here is different from the MQ queue, which refers to the notification mechanism on the business side, such as SMS, email, system messages, etc. There are many needs at the business level, and a separate message center is usually packaged to provide a notification mechanism;

From the perspective of the process, message notification is a typical production-consumption model. The business side continuously produces messages, and the message center consumes them after receiving them, and pushes the notifications to the corresponding channels. Obviously, this logic has a high degree of reuse. sex.

2. Message notification

1. Process management

The process design of message notification, through the interface method provided by the message center in each business line, the content of the message in different scenarios is submitted to the message center, and the message center conducts unified maintenance and management, and adapts the corresponding message according to the source and destination of the message. Push logic:

  • Message production: There are many scenarios involved, such as activities, marketing mechanisms, system notifications, business circulation, expiration reminders, etc.;
  • Message management: Verify the structure and parameters of pre-sent messages, create message push tasks, maintain task-level push management, and track message status cycles;
  • Message consumption: Based on the structure of message tasks, construct the main content of message push, and connect multiple sending channels to achieve efficient delivery of notifications;
  • Scheduled task: The message can be pushed directly and immediately, but if it is triggered by a nighttime scheduled task, the push delay problem should be considered, and the message will be delivered at the specified time period;
  • Channel docking: Usually different channels mean different scenarios, such as monitoring push DingTalk, activities generally push WeChat, account changes send emails, marketing go through SMS, business notifications are in-app;

There are many modules involved in the whole process, and the status flow is also very complicated, but unified standard management and inflow and outflow tracking through the message center can also provide clear life cycle monitoring and maintenance;

2. Process timing

In the entire message notification link, in different flow nodes, all state changes (ie from.to state) are involved, which can form a view of the entire life cycle:

  • Initialization: The business side builds a simple message structure, and after the request is sent to the message center, a message task is initialized;
  • Task-based: verify the message sending request and convert the message into a standard push task structure;
  • 推送中:根据任务推送的时间周期类型,将任务构建成不同渠道的通知主体,从而进行渠道消息推送;
  • 已完成:根据消息在渠道推送的状态回调,更新消息中心的任务完成状态,或者失败重试;

大部分的消息通知机制都可以容忍一定的延迟性,所以消息中心完全可以解耦各个流程,引入MQ队列或者异步机制,业务方只需要将请求发送到消息中心,之后由消息中心统一调度和管理即可;

3、结构设计

这里根据系统的实现过程和经验,给出一个数据结构的设计参考,用来对业务场景做简单的维度描述:

  • 消息模板:定义通知的主体结构,基于消息的参数模型,构建推送的消息内容;
  • 消息任务:消息中心管理和维护的主体结构,以任务的模式维护消息从生产到推送完成的整个状态周期;
  • 场景记录:消息最终推送出去的内容和场景分类,也可以简单的理解为不同渠道的投递记录;
  • 交互消息:强调消息在接收方是否触达并且对消息产生了交互行为,例如会话,邮件回复,状态关联等;

三、实践总结

最后还是站在技术实现的角度,总结一下消息通知机制中的一些关键问题:

  • 生产消费:消息生产之后写入消息中心的存储容器,之后进行消费流程的管理,是业务解耦的常用手段;
  • 任务管理:以任务的模式进行消息推送的调度,通过任务状态的变化和控制,实现生命周期的管理;
  • 状态机:描述消息的流转节点和状态,在不同的事件中触发不同的状态切换和转移,并在状态变化后衔接各种业务动作;
  • 渠道对接:通常消息推送的渠道多是第三方平台,所以在消息中心会接入诸多的渠道,例如微信、钉钉、短信等;
  • 基础封装:作为分布式系统中的基础功能,在封装消息管理功能时,要考虑一定的复用性和流程的可视化呈现;

消息的本质是信息的触达和传递,但是过多的消息通知也容易让用户产生厌倦心态,所以消息内容的简洁明确,推送的间隔时段以及阅读提醒,在产品具体的实现上需要极为用心,从而让消息在业务体系中发挥更大的价值。

四、参考源码

编程文档:
https://gitee.com/cicadasmile/butte-java-note

应用仓库:
https://gitee.com/cicadasmile/butte-flyer-parent

Guess you like

Origin juejin.im/post/7118659377296310279