From 94a73090ba889525dc8d18a4bb579f87c859eb5f Mon Sep 17 00:00:00 2001 From: pikaqiudeshujia Date: Tue, 26 Nov 2024 15:38:05 +0800 Subject: [PATCH] =?UTF-8?q?release-=20=E7=A7=AF=E5=88=86=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E5=8D=95=E7=BA=BF=E7=A8=8B=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/luoo/user/config/RabbitMQConfig.java | 8 ----- .../luoo/user/listener/PointLogListener.java | 29 +++++++++++++++++-- .../com/luoo/user/util/DistributedLock.java | 21 ++++++++++++++ 3 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 luoo_user/src/main/java/com/luoo/user/util/DistributedLock.java diff --git a/luoo_user/src/main/java/com/luoo/user/config/RabbitMQConfig.java b/luoo_user/src/main/java/com/luoo/user/config/RabbitMQConfig.java index 978db06..5cbc2d0 100644 --- a/luoo_user/src/main/java/com/luoo/user/config/RabbitMQConfig.java +++ b/luoo_user/src/main/java/com/luoo/user/config/RabbitMQConfig.java @@ -25,12 +25,4 @@ public class RabbitMQConfig { return factory; } - @Bean - public SimpleRabbitListenerContainerFactory pointLogFactory(ConnectionFactory connectionFactory) { - SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); - factory.setConnectionFactory(connectionFactory); - factory.setMessageConverter(new Jackson2JsonMessageConverter()); - factory.setPrefetchCount(1); - return factory; - } } diff --git a/luoo_user/src/main/java/com/luoo/user/listener/PointLogListener.java b/luoo_user/src/main/java/com/luoo/user/listener/PointLogListener.java index 0d0cf77..77bdb15 100644 --- a/luoo_user/src/main/java/com/luoo/user/listener/PointLogListener.java +++ b/luoo_user/src/main/java/com/luoo/user/listener/PointLogListener.java @@ -3,9 +3,11 @@ package com.luoo.user.listener; import com.fasterxml.jackson.databind.ObjectMapper; import com.luoo.user.pojo.UserPointLog; import com.luoo.user.service.UserPointLogService; +import com.luoo.user.util.DistributedLock; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -18,13 +20,19 @@ import java.io.IOException; * @create: 2024-07-24 13:05 **/ @Component -@RabbitListener(queues = "pointLog", containerFactory = "pointLogFactory") +@RabbitListener(queues = "pointLog") @Slf4j public class PointLogListener { @Autowired private UserPointLogService userPointLogService; + @Autowired + private DistributedLock distributedLock; + + @Autowired + private RabbitTemplate rabbitTemplate; + @RabbitHandler public void executePointLog(String json) { log.info("userPointLog:{}", json); @@ -33,7 +41,24 @@ public class PointLogListener { try { UserPointLog userPointLog = objectMapper.readValue(json, UserPointLog.class); - userPointLogService.add(userPointLog); + String userId = userPointLog.getUserId(); + // 对userId进行redis分布式锁 + // springboot 写一段setnx的分布式锁 + + String key = "pointLog:" + userId; + + if (distributedLock.tryLock(key, "1")) { + try { + // 执行需要加锁的业务逻辑 + userPointLogService.add(userPointLog); + } finally { + // 释放锁 + distributedLock.unlock(key); + } + } else { + // 获取锁失败,处理相应逻辑 + rabbitTemplate.convertAndSend("pointLog", json); + } } catch (IOException e) { e.printStackTrace(); } diff --git a/luoo_user/src/main/java/com/luoo/user/util/DistributedLock.java b/luoo_user/src/main/java/com/luoo/user/util/DistributedLock.java new file mode 100644 index 0000000..084e543 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/util/DistributedLock.java @@ -0,0 +1,21 @@ +package com.luoo.user.util; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +@Component +public class DistributedLock { + + @Autowired + private RedisTemplate redisTemplate; + + public boolean tryLock(String key, String value) { + Boolean success = redisTemplate.opsForValue().setIfAbsent(key, value); + return success != null && success; + } + + public void unlock(String key) { + redisTemplate.delete(key); + } +} \ No newline at end of file