mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-14 05:42:06 +08:00
1、RedisLockUtil加锁解锁修复sonar.LocksNotUnlockedCheck;2、api、文件导入生产单数据加解锁基于多租户问题修复;
This commit is contained in:
+2
-3
@@ -3,7 +3,6 @@ package com.cf.imes.framework.redis.config;
|
|||||||
import cn.hutool.core.text.StrPool;
|
import cn.hutool.core.text.StrPool;
|
||||||
import com.cf.imes.framework.redis.core.TimeoutRedisCacheManager;
|
import com.cf.imes.framework.redis.core.TimeoutRedisCacheManager;
|
||||||
import com.cf.imes.framework.redis.util.RedisLockUtil;
|
import com.cf.imes.framework.redis.util.RedisLockUtil;
|
||||||
import org.redisson.api.RedissonClient;
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.cache.CacheProperties;
|
import org.springframework.boot.autoconfigure.cache.CacheProperties;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
@@ -82,7 +81,7 @@ public class ChenfengCacheAutoConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public RedisLockUtil lockUtil(ChenfengCacheProperties chenfengCacheProperties, RedissonClient client, RedisTemplate redisTemplate) {
|
public RedisLockUtil lockUtil(RedisTemplate redisTemplate) {
|
||||||
return new RedisLockUtil(chenfengCacheProperties, client, redisTemplate);
|
return new RedisLockUtil(redisTemplate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-60
@@ -1,11 +1,8 @@
|
|||||||
package com.cf.imes.framework.redis.util;
|
package com.cf.imes.framework.redis.util;
|
||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import com.cf.imes.framework.redis.config.ChenfengCacheProperties;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.redisson.api.RLock;
|
|
||||||
import org.redisson.api.RedissonClient;
|
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -20,65 +17,8 @@ import java.util.concurrent.TimeUnit;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class RedisLockUtil {
|
public class RedisLockUtil {
|
||||||
|
|
||||||
private ChenfengCacheProperties chenfengCacheProperties;
|
|
||||||
|
|
||||||
private RedissonClient redissonClient;
|
|
||||||
|
|
||||||
private RedisTemplate redisTemplate;
|
private RedisTemplate redisTemplate;
|
||||||
|
|
||||||
/**
|
|
||||||
* 加锁
|
|
||||||
*
|
|
||||||
* @param key rediskey
|
|
||||||
* @return true拿到锁,反之没有做对应的业务提醒
|
|
||||||
*/
|
|
||||||
public boolean acquireLock(String key) {
|
|
||||||
boolean getLock = false;
|
|
||||||
try {
|
|
||||||
RLock rLock = redissonClient.getLock(key);
|
|
||||||
return rLock.tryLock(chenfengCacheProperties.getLockWaitTime(), chenfengCacheProperties.getLockTimeout(), TimeUnit.MILLISECONDS);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("[RedisLockUtil][lock]加锁失败", e);
|
|
||||||
return getLock;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 加锁
|
|
||||||
*
|
|
||||||
* @param key rediskey
|
|
||||||
* @param timeout 锁的超时时间,传空则按照默认配置
|
|
||||||
* @param waitTime 等待获取锁的时间,传空则按照默认配置
|
|
||||||
* @return true拿到锁,反之没有做对应的业务提醒
|
|
||||||
*/
|
|
||||||
public boolean acquireLock(String key, Integer waitTime, Integer timeout) {
|
|
||||||
boolean getLock = false;
|
|
||||||
try {
|
|
||||||
RLock rLock = redissonClient.getLock(key);
|
|
||||||
int wt = ObjectUtil.isNull(waitTime) ? chenfengCacheProperties.getLockWaitTime() : waitTime;
|
|
||||||
int to = ObjectUtil.isNull(timeout) ? chenfengCacheProperties.getLockTimeout() : timeout;
|
|
||||||
return rLock.tryLock(wt, to, TimeUnit.MILLISECONDS);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("[RedisLockUtil][lock]加锁失败", e);
|
|
||||||
return getLock;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 解锁
|
|
||||||
* 使用注意:配合以上两个redisson的lock只能在当前线程中unlock,否则失败!!!
|
|
||||||
*
|
|
||||||
* @param key rediskey
|
|
||||||
*/
|
|
||||||
public void releaseLock(String key) {
|
|
||||||
try {
|
|
||||||
RLock rLock = redissonClient.getLock(key);
|
|
||||||
rLock.unlock();
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("[RedisLockUtil][unlock]解锁失败", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 手动set nx ex加锁
|
* 手动set nx ex加锁
|
||||||
* 使用注意:可用于跨服务
|
* 使用注意:可用于跨服务
|
||||||
|
|||||||
+21
-13
@@ -10,6 +10,8 @@ import com.cf.imes.framework.common.pojo.PageResult;
|
|||||||
import com.cf.imes.framework.common.util.object.BeanUtils;
|
import com.cf.imes.framework.common.util.object.BeanUtils;
|
||||||
import com.cf.imes.framework.mybatis.core.generator.SnowFlakeGenerator;
|
import com.cf.imes.framework.mybatis.core.generator.SnowFlakeGenerator;
|
||||||
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
|
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
|
||||||
|
import com.cf.imes.framework.redis.config.ChenfengCacheProperties;
|
||||||
|
import com.cf.imes.framework.redis.constants.RedisKeyConstants;
|
||||||
import com.cf.imes.framework.redis.util.RedisLockUtil;
|
import com.cf.imes.framework.redis.util.RedisLockUtil;
|
||||||
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
|
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
|
||||||
import com.cf.imes.module.executor.controller.admin.order.vo.order.*;
|
import com.cf.imes.module.executor.controller.admin.order.vo.order.*;
|
||||||
@@ -107,7 +109,9 @@ public class OrderController {
|
|||||||
@Resource
|
@Resource
|
||||||
private OrderInputProcessor orderInputProcessor;
|
private OrderInputProcessor orderInputProcessor;
|
||||||
|
|
||||||
private String lockKey = "order:1631:lock:";
|
@Resource
|
||||||
|
private ChenfengCacheProperties chenfengCacheProperties;
|
||||||
|
|
||||||
private String errKey = "生产数据导入异常,{}";
|
private String errKey = "生产数据导入异常,{}";
|
||||||
|
|
||||||
public static final String ORDER_PLATE_MODEL = "imes_order_plate_model";
|
public static final String ORDER_PLATE_MODEL = "imes_order_plate_model";
|
||||||
@@ -229,13 +233,15 @@ public class OrderController {
|
|||||||
@OperateLog(module="管理后台 - 生产单管理",name = "生产单api数据导入新增",type = IMPORT)
|
@OperateLog(module="管理后台 - 生产单管理",name = "生产单api数据导入新增",type = IMPORT)
|
||||||
@Parameter(name = "orderNo", description = "原订单号", required = true, example = "20230809027818")
|
@Parameter(name = "orderNo", description = "原订单号", required = true, example = "20230809027818")
|
||||||
@PreAuthorize("@ss.hasPermission('production:manager-list:create')")
|
@PreAuthorize("@ss.hasPermission('production:manager-list:create')")
|
||||||
public CommonResult<String> getApiData(@RequestParam(value = "orderNo") String orderNo){
|
public CommonResult<String> getApiData(@RequestParam(value = "orderNo") String orderNo) throws InterruptedException {
|
||||||
|
// 加导入锁
|
||||||
boolean isLocked = redisLockUtil.acquireLock(lockKey);
|
String importLockValue = "syncApiImport";
|
||||||
|
String importLockKey = String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, getUserOrganId());
|
||||||
|
boolean isLocked = redisLockUtil.acquireLock(importLockKey, importLockValue, chenfengCacheProperties.getLockTimeout());
|
||||||
if (!isLocked) {
|
if (!isLocked) {
|
||||||
log.error(errKey, LOCK_ERR);
|
throw new ServiceException(ORDER_IMPORT_ORGAN_LOCK_ERROR);
|
||||||
return error(LOCK_ERR);
|
|
||||||
}
|
}
|
||||||
|
Thread.sleep(20000);
|
||||||
Long orderId = null;
|
Long orderId = null;
|
||||||
try {
|
try {
|
||||||
orderId = (Long) identifierGenerator.nextId(null);
|
orderId = (Long) identifierGenerator.nextId(null);
|
||||||
@@ -257,7 +263,8 @@ public class OrderController {
|
|||||||
return error((ServiceException) cause);
|
return error((ServiceException) cause);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
redisLockUtil.releaseLock(lockKey);
|
// 导入锁解锁
|
||||||
|
redisLockUtil.releaseLock(importLockKey, importLockValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -285,11 +292,12 @@ public class OrderController {
|
|||||||
Map<String, String> mapOrder = null;
|
Map<String, String> mapOrder = null;
|
||||||
|
|
||||||
Boolean flag = true;
|
Boolean flag = true;
|
||||||
|
// 加导入锁
|
||||||
boolean isLocked = redisLockUtil.acquireLock(lockKey);
|
String importLockValue = "syncFileImport";
|
||||||
|
String importLockKey = String.format(RedisKeyConstants.ORDER_IMPORT_LOCK_KEY, getUserOrganId());
|
||||||
|
boolean isLocked = redisLockUtil.acquireLock(importLockKey, importLockValue, chenfengCacheProperties.getLockTimeout());
|
||||||
if (!isLocked) {
|
if (!isLocked) {
|
||||||
log.error(errKey,LOCK_ERR);
|
throw new ServiceException(ORDER_IMPORT_ORGAN_LOCK_ERROR);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
if (type == 0) { //excel
|
if (type == 0) { //excel
|
||||||
@@ -404,8 +412,8 @@ public class OrderController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
|
// 导入锁解锁
|
||||||
redisLockUtil.releaseLock(lockKey);
|
redisLockUtil.releaseLock(importLockKey, importLockValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user