feat(count): 引入批量消费机制优化粉丝计数处理

- 添加 BufferTrigger依赖以支持消息聚合
- 实现消息批量消费逻辑,提升处理效率
- 配置批量大小为 1000,缓存队列最大容量为50000
- 设置聚合间隔为1 秒,均衡实时性与性能
- 新增测试用例验证大量消息发送与消费流程
- 日志记录聚合消息内容,便于调试与监控
This commit is contained in:
2025-10-15 22:33:20 +08:00
parent 31ab7c3d86
commit c6ac7193c1
4 changed files with 67 additions and 1 deletions

View File

@@ -2,6 +2,8 @@ package com.hanserwei.hannote.user.relation.biz;
import com.hanserwei.framework.common.utils.JsonUtils;
import com.hanserwei.hannote.user.relation.biz.constant.MQConstants;
import com.hanserwei.hannote.user.relation.biz.enums.FollowUnfollowTypeEnum;
import com.hanserwei.hannote.user.relation.biz.model.dto.CountFollowUnfollowMqDTO;
import com.hanserwei.hannote.user.relation.biz.model.dto.FollowUserMqDTO;
import com.hanserwei.hannote.user.relation.biz.model.dto.UnfollowUserMqDTO;
import jakarta.annotation.Resource;
@@ -125,4 +127,37 @@ class MQTests {
}
}
/**
* 测试:发送计数 MQ, 以统计粉丝数
*/
@Test
void testSendCountFollowUnfollowMQ() {
// 循环发送 3200 条 MQ
for (long i = 0; i < 3200; i++) {
// 构建消息体 DTO
CountFollowUnfollowMqDTO countFollowUnfollowMqDTO = CountFollowUnfollowMqDTO.builder()
.userId(i + 1) // 关注者用户 ID
.targetUserId(27L) // 目标用户
.type(FollowUnfollowTypeEnum.FOLLOW.getCode())
.build();
// 构建消息对象,并将 DTO 转成 Json 字符串设置到消息体中
org.springframework.messaging.Message<String> message = MessageBuilder.withPayload(JsonUtils.toJsonString(countFollowUnfollowMqDTO))
.build();
// 发送 MQ 通知计数服务:统计粉丝数
rocketMQTemplate.asyncSend(MQConstants.TOPIC_COUNT_FANS, message, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
log.info("==> 【计数服务粉丝数】MQ 发送成功SendResult: {}", sendResult);
}
@Override
public void onException(Throwable throwable) {
log.error("==> 【计数服务粉丝数】MQ 发送异常: ", throwable);
}
});
}
}
}