feat(comment): 实现评论点赞与取消点赞功能,评论点赞、取消点赞批量写库
- 新增评论点赞布隆过滤器,提升点赞判断性能 - 实现评论点赞与取消点赞的批量操作消费者 - 添加评论点赞状态查询接口及异常处理 - 优化点赞操作合并逻辑,减少数据库访问频率 - 增加评论点赞相关 Lua 脚本支持过期时间设置 - 完善评论点赞 Mapper 层批量插入与删除方法 - 添加评论已点赞业务异常状态码 - 新增测试类用于验证评论点赞 MQ 消费逻辑 - 调整 MQ 消费者 Bean 名称避免冲突 - 更新 HTTP 测试文件中的评论 ID便于调试
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package com.hanserwei.hannote.count.biz;
|
||||
|
||||
import com.hanserwei.framework.common.utils.JsonUtils;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@SpringBootTest
|
||||
public class TestCommentLikeUnLikeConsumer {
|
||||
|
||||
@Autowired
|
||||
private RocketMQTemplate rocketMQTemplate;
|
||||
|
||||
/**
|
||||
* 测试:模拟发送评论点赞、取消点赞消息
|
||||
*/
|
||||
@Test
|
||||
void testBatchSendLikeUnlikeCommentMQ() {
|
||||
Long userId = 2001L;
|
||||
Long commentId = 4001L;
|
||||
|
||||
for (long i = 0; i < 32; i++) {
|
||||
// 构建消息体 DTO
|
||||
LikeUnlikeCommentMqDTO likeUnlikeCommentMqDTO = LikeUnlikeCommentMqDTO.builder()
|
||||
.userId(userId)
|
||||
.commentId(commentId)
|
||||
.createTime(LocalDateTime.now())
|
||||
.build();
|
||||
|
||||
// 通过冒号连接, 可让 MQ 发送给主题 Topic 时,携带上标签 Tag
|
||||
String destination = "CommentLikeUnlikeTopic:";
|
||||
|
||||
if (i % 2 == 0) { // 偶数
|
||||
likeUnlikeCommentMqDTO.setType(0); // 取消点赞
|
||||
destination = destination + "Unlike";
|
||||
} else { // 奇数
|
||||
likeUnlikeCommentMqDTO.setType(1); // 点赞
|
||||
destination = destination + "Like";
|
||||
}
|
||||
|
||||
// MQ 分区键
|
||||
String hashKey = String.valueOf(userId);
|
||||
|
||||
// 构建消息对象,并将 DTO 转成 Json 字符串设置到消息体中
|
||||
Message<String> message = MessageBuilder.withPayload(JsonUtils.toJsonString(likeUnlikeCommentMqDTO))
|
||||
.build();
|
||||
|
||||
// 同步发送 MQ 消息
|
||||
rocketMQTemplate.syncSendOrderly(destination, message, hashKey);
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
static class LikeUnlikeCommentMqDTO {
|
||||
|
||||
private Long userId;
|
||||
|
||||
private Long commentId;
|
||||
|
||||
/**
|
||||
* 0: 取消点赞, 1:点赞
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
private LocalDateTime createTime;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user