From 5a7564d504aea1d916a3fb2ce2d412ebd7b04856 Mon Sep 17 00:00:00 2001 From: Hanserwei <2628273921@qq.com> Date: Sun, 12 Oct 2025 20:03:33 +0800 Subject: [PATCH] =?UTF-8?q?feat(relation):=20=E5=AE=9E=E7=8E=B0=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=85=B3=E6=B3=A8=E5=8A=9F=E8=83=BD=E5=B9=B6=E9=9B=86?= =?UTF-8?q?=E6=88=90RocketMQ=E6=B6=88=E6=81=AF=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增关注用户MQ消息传输对象 FollowUserMqDTO - 定义MQ常量类 MQConstants,包含关注/取关主题与标签 - 引入RocketMQ依赖及自动配置类 RocketMQConfig - 在关注接口中构造并异步发送关注操作消息 - 使用JsonUtils将消息体序列化为JSON字符串 - 添加日志记录MQ发送状态及异常处理回调 --- .../han-note-user-relation-biz/pom.xml | 5 +++ .../relation/biz/config/RocketMQConfig.java | 10 +++++ .../relation/biz/constant/MQConstants.java | 19 +++++++++ .../biz/model/dto/FollowUserMqDTO.java | 21 ++++++++++ .../biz/service/impl/RelationServiceImpl.java | 41 ++++++++++++++++++- 5 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/config/RocketMQConfig.java create mode 100644 han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/constant/MQConstants.java create mode 100644 han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/model/dto/FollowUserMqDTO.java diff --git a/han-note-user-relation/han-note-user-relation-biz/pom.xml b/han-note-user-relation/han-note-user-relation-biz/pom.xml index 79e25a7..698d9f5 100644 --- a/han-note-user-relation/han-note-user-relation-biz/pom.xml +++ b/han-note-user-relation/han-note-user-relation-biz/pom.xml @@ -96,6 +96,11 @@ han-note-user-api + + + org.apache.rocketmq + rocketmq-spring-boot-starter + diff --git a/han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/config/RocketMQConfig.java b/han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/config/RocketMQConfig.java new file mode 100644 index 0000000..743de32 --- /dev/null +++ b/han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/config/RocketMQConfig.java @@ -0,0 +1,10 @@ +package com.hanserwei.hannote.user.relation.biz.config; + +import org.apache.rocketmq.spring.autoconfigure.RocketMQAutoConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +@Configuration +@Import(RocketMQAutoConfiguration.class) +public class RocketMQConfig { +} \ No newline at end of file diff --git a/han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/constant/MQConstants.java b/han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/constant/MQConstants.java new file mode 100644 index 0000000..3f3423d --- /dev/null +++ b/han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/constant/MQConstants.java @@ -0,0 +1,19 @@ +package com.hanserwei.hannote.user.relation.biz.constant; + +public interface MQConstants { + + /** + * Topic: 关注、取关共用一个 + */ + String TOPIC_FOLLOW_OR_UNFOLLOW = "FollowUnfollowTopic"; + + /** + * 关注标签 + */ + String TAG_FOLLOW = "Follow"; + + /** + * 取关标签 + */ + String TAG_UNFOLLOW = "Unfollow"; +} \ No newline at end of file diff --git a/han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/model/dto/FollowUserMqDTO.java b/han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/model/dto/FollowUserMqDTO.java new file mode 100644 index 0000000..bd519f9 --- /dev/null +++ b/han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/model/dto/FollowUserMqDTO.java @@ -0,0 +1,21 @@ +package com.hanserwei.hannote.user.relation.biz.model.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class FollowUserMqDTO { + + private Long userId; + + private Long followUserId; + + private LocalDateTime createTime; +} \ No newline at end of file diff --git a/han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/service/impl/RelationServiceImpl.java b/han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/service/impl/RelationServiceImpl.java index 30f2c34..87fd3d8 100644 --- a/han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/service/impl/RelationServiceImpl.java +++ b/han-note-user-relation/han-note-user-relation-biz/src/main/java/com/hanserwei/hannote/user/relation/biz/service/impl/RelationServiceImpl.java @@ -6,11 +6,14 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.hanserwei.framework.biz.context.holder.LoginUserContextHolder; import com.hanserwei.framework.common.exception.ApiException; import com.hanserwei.framework.common.response.Response; +import com.hanserwei.framework.common.utils.JsonUtils; import com.hanserwei.hannote.user.dto.resp.FindUserByIdRspDTO; +import com.hanserwei.hannote.user.relation.biz.constant.MQConstants; import com.hanserwei.hannote.user.relation.biz.constant.RedisKeyConstants; import com.hanserwei.hannote.user.relation.biz.domain.dataobject.FollowingDO; import com.hanserwei.hannote.user.relation.biz.enums.LuaResultEnum; import com.hanserwei.hannote.user.relation.biz.enums.ResponseCodeEnum; +import com.hanserwei.hannote.user.relation.biz.model.dto.FollowUserMqDTO; import com.hanserwei.hannote.user.relation.biz.model.vo.FollowUserReqVO; import com.hanserwei.hannote.user.relation.biz.rpc.UserRpcService; import com.hanserwei.hannote.user.relation.biz.service.FollowingDOService; @@ -18,9 +21,14 @@ import com.hanserwei.hannote.user.relation.biz.service.RelationService; import com.hanserwei.hannote.user.relation.biz.util.DateUtils; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; +import org.apache.rocketmq.client.producer.SendCallback; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.springframework.core.io.ClassPathResource; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.script.DefaultRedisScript; +import org.springframework.messaging.Message; +import org.springframework.messaging.support.MessageBuilder; import org.springframework.scripting.support.ResourceScriptSource; import org.springframework.stereotype.Service; @@ -39,6 +47,8 @@ public class RelationServiceImpl implements RelationService { private RedisTemplate redisTemplate; @Resource private FollowingDOService followingDOService; + @Resource + private RocketMQTemplate rocketMQTemplate; @Override public Response follow(FollowUserReqVO followUserReqVO) { @@ -118,7 +128,36 @@ public class RelationServiceImpl implements RelationService { } - // TODO: 发送 MQ + // 发送 MQ + // 构造消息体DTO + FollowUserMqDTO followUserMqDTO = FollowUserMqDTO.builder() + .userId(userId) + .followUserId(followUserId) + .createTime(now) + .build(); + // 构造消息对象,并把DTO转换为JSON字符串设置到消息体中 + Message message = MessageBuilder + .withPayload(JsonUtils.toJsonString(followUserMqDTO)) + .build(); + + // 通过冒号连接, 可让 MQ 发送给主题 Topic 时,携带上标签 Tag + String destination = MQConstants.TOPIC_FOLLOW_OR_UNFOLLOW + ":" + MQConstants.TAG_FOLLOW; + + log.info("==> 开始发送关注操作 MQ, 消息体: {}", followUserMqDTO); + + // 异步发送MQ消息,提升接口响应速度 + rocketMQTemplate.asyncSend(destination, message, new SendCallback() { + + @Override + public void onSuccess(SendResult sendResult) { + log.info("==> MQ 发送成功,SendResult: {}", sendResult); + } + + @Override + public void onException(Throwable throwable) { + log.error("==> MQ 发送异常: ", throwable); + } + }); return Response.success(); }