团课业务场景中,学员需要接收开课提醒、调课、取消、地点变更等通知。很多项目直接在团课业务代码里调用短信、小程序、站内信接口,消息推送逻辑和团课主流程强耦合。
这种实现方式存在不少问题。第一,推送渠道混杂,新增推送渠道需要修改业务代码,扩展成本高;第二,缺少统一的消息队列削峰,临近开课大量消息同时推送,容易出现接口超时、消息丢失;第三,缺少推送状态记录、失败重试、幂等控制,重复推送、漏通知问题难以排查;第四,不同通知类型缺少模板管理,开课提醒、调课通知文案分散,统一修改文案需要多处改动;第五,学员可接收渠道偏好无法配置,强制全渠道推送,容易引发打扰问题。
如果没有独立通知服务,团课变更逻辑和消息推送交织在一起,后续迭代、渠道扩容、推送规则调整都会变得困难。

独立搭建团课消息通知服务,与团课核心业务解耦,采用事件驱动模式。团课业务发生变更时,只发送事件,不直接调用推送接口。通知服务统一管理消息模板、用户渠道偏好、推送队列、重试机制。
核心业务流程:
技术分层:事件接入层接收团课业务事件;模板管理层维护通知文案模板;渠道适配层封装各个推送渠道SDK;任务调度层负责延时提醒、失败重试;日志存储层保存推送记录。

@Service
public class GroupClassNotifyService {
@Autowired
private NotifyTemplateMapper templateMapper;
@Autowired
private NotifyRecordMapper notifyRecordMapper;
@Autowired
private ChannelDispatchService channelDispatchService;
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 接收团课事件,组装消息,分发推送
*/
public void handleClassEvent(GroupClassEvent event) {
// 查询通知模板
NotifyTemplate template = templateMapper.selectByType(event.getNotifyType());
if (template == null) {
return;
}
// 渲染模板内容,填充变量
String content = renderTemplate(template.getContent(), event.getParamMap());
// 遍历学员列表,生成通知记录
for (Long userId : event.getUserIdList()) {
NotifyRecord record = new NotifyRecord();
record.setUserId(userId);
record.setClassId(event.getClassId());
record.setNotifyType(event.getNotifyType());
record.setContent(content);
record.setStatus(0);
record.setRetryCount(0);
notifyRecordMapper.insert(record);
// 投递到推送队列
NotifySendDTO sendDTO = new NotifySendDTO();
sendDTO.setRecordId(record.getId());
sendDTO.setUserId(userId);
sendDTO.setContent(content);
rocketMQTemplate.syncSend("notify_send_topic", MessageBuilder.withPayload(sendDTO).build());
}
}
/**
* 实际推送分发,根据用户渠道偏好选择推送渠道
*/
public void sendNotify(NotifySendDTO dto) {
UserChannelConfig config = userChannelConfigMapper.selectByUserId(dto.getUserId());
List<String> channelList = config.getChannelList();
boolean sendSuccess = false;
for (String channel : channelList) {
boolean result = channelDispatchService.dispatch(channel, dto.getUserId(), dto.getContent());
if (result) {
sendSuccess = true;
}
}
// 更新推送结果
NotifyRecord update = new NotifyRecord();
update.setId(dto.getRecordId());
update.setStatus(sendSuccess ? 1 : 2);
notifyRecordMapper.updateById(update);
}
private String renderTemplate(String templateStr, Map<String,Object> paramMap) {
// 简单模板变量替换
for (Map.Entry<String,Object> entry : paramMap.entrySet()) {
templateStr = templateStr.replace("${" + entry.getKey() + "}", String.valueOf(entry.getValue()));
}
return templateStr;
}
}
团课消息通知服务通过事件驱动、多渠道适配、模板化管理的方案,把开课提醒、调课通知等推送能力从团课业务中剥离出来,解决了推送逻辑耦合、渠道扩展困难、消息丢失和重复推送的问题。服务支持延时提醒、失败重试、用户渠道偏好配置,同时可以和前面训练计划相关模块打通,统一平台消息能力。落地时重点关注消息幂等、延时消息可靠性、第三方渠道限流策略,保证在团课高峰期消息稳定送达,提升学员上课体验。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。