删除redisson

This commit is contained in:
zren25
2025-04-29 16:41:20 +08:00
parent aaffd3e8f4
commit 4be5906991
3 changed files with 56 additions and 58 deletions

View File

@@ -1,40 +0,0 @@
package com.volvo.ai.analytic.center.config;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@RefreshScope
public class RedissonConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private String port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.username}")
private String userName;
@Bean(destroyMethod = "shutdown")
public RedissonClient redissonClient() {
Config config = new Config();
config.useSingleServer()
.setAddress("redis://" + host + ":" + port)
.setPassword(password).setUsername(userName)
.setDatabase(0)
.setConnectionMinimumIdleSize(5)
.setIdleConnectionTimeout(10000)
.setConnectTimeout(50000)
.setTimeout(50000);
return Redisson.create(config);
}
}

View File

@@ -9,13 +9,12 @@ import com.volvo.ai.analytic.center.service.AiAnalysisRequestLogsService;
import com.volvo.ai.analytic.center.service.DiFyService;
import com.volvo.ai.analytic.center.utils.ConstantStr;
import com.volvo.ai.analytic.center.utils.RedisCounterRateLimiter;
import com.volvo.ai.analytic.center.utils.RedisLockService;
import com.volvo.ai.analytic.center.utils.RedisZSetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
@@ -25,7 +24,6 @@ import org.springframework.web.bind.annotation.RestController;
import java.time.Instant;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
/**
* @ClassName AnalysisDifyMqConsumer
@@ -71,16 +69,18 @@ public class AnalysisDifyMqConsumer implements RocketMQListener<MessageExt> {
@Autowired
private RedisZSetUtil redisZSetUtil;
@Autowired
private RedissonClient redissonClient;
private RedisLockService redisLockService;
@Override
public void onMessage(MessageExt messageExt) {
log.info("analysisDifyMqConsumer 当前线程: {}, 线程ID: {}", Thread.currentThread().getName(), Thread.currentThread().getId());
long startTime = System.currentTimeMillis();
RLock lock = redissonClient.getLock("LOCK_PREFIX:" + ConstantStr.DIFY_COUNTERRATELIMIT);
String lockKey = "LOCK_PREFIX:" + ConstantStr.DIFY_COUNTERRATELIMIT;
// RLock lock = redissonClient.getLock("LOCK_PREFIX:" + ConstantStr.DIFY_COUNTERRATELIMIT);
try {
// 2. 尝试加锁(参数说明:等待时间, 锁自动释放时间, 时间单位)
boolean isLocked = lock.tryLock(10, expire, TimeUnit.SECONDS); // 不等待,立即尝试获取
boolean isLocked = redisLockService.tryLock(lockKey,ConstantStr.DIFY_COUNTERRATELIMIT, expire); // 不等待,立即尝试获取
if (isLocked) {
Long count = redisZSetUtil.zCard(ConstantStr.DIFY_COUNTERRATELIMIT);
log.info("redis计数数量: {}", count);
@@ -113,26 +113,21 @@ public class AnalysisDifyMqConsumer implements RocketMQListener<MessageExt> {
log.error("异步处理asyncExecuteDifyFlow 失败aiAnalysisRequestId: {} ,{}", aiAnalysisRequestId,ex.getMessage());
return null;
});
lock.unlock();
redisLockService.releaseLock(lockKey, ConstantStr.DIFY_COUNTERRATELIMIT);
log.info("analysisDifyMqConsumer处理完成耗时{}", System.currentTimeMillis() - startTime);
}else{
log.info("redis锁未获取到 ");
throw new RuntimeException("锁超时 " );
}
} catch (InterruptedException e) {
// Thread.currentThread().interrupt(); // 保持中断状态
throw new RuntimeException("锁获取被中断", e);
} catch (Exception e){
} catch (Exception e){
log.error("analysisDifyMqConsumer 异常:{}", e.getMessage());
throw new RuntimeException("请求dify超过基数 " );
}finally {
// 4. 确保只有持有锁的线程才能解锁(关键!)
if (lock != null && lock.isHeldByCurrentThread()) {
lock.unlock();
// 建议记录解锁日志
log.info("释放锁成功锁KEY: {}", ConstantStr.DIFY_COUNTERRATELIMIT);
}
redisLockService.releaseLock(lockKey, ConstantStr.DIFY_COUNTERRATELIMIT);
// 建议记录解锁日志
log.info("释放锁成功锁KEY: {}", ConstantStr.DIFY_COUNTERRATELIMIT);
}
}

View File

@@ -0,0 +1,43 @@
package com.volvo.ai.analytic.center.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class RedisLockService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 尝试获取分布式锁
*
* @param lockKey 锁的键名
* @param requestId 请求标识(用于解锁时验证)
* @param expireTime 锁的过期时间(秒)
* @return 是否获取锁成功
*/
public boolean tryLock(String lockKey, String requestId, long expireTime) {
return stringRedisTemplate.opsForValue()
.setIfAbsent(lockKey, requestId, expireTime, TimeUnit.SECONDS);
}
/**
* 释放分布式锁
*
* @param lockKey 锁的键名
* @param requestId 请求标识(用于验证)
* @return 是否释放锁成功
*/
public boolean releaseLock(String lockKey, String requestId) {
String currentValue = stringRedisTemplate.opsForValue().get(lockKey);
if (currentValue != null && currentValue.equals(requestId)) {
stringRedisTemplate.delete(lockKey);
return true;
}
return false;
}
}