mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2025-09-09 05:39:13 +00:00
update 优化 移除ThreadLocalHolder(不可控问题太多)
This commit is contained in:
@@ -1,57 +0,0 @@
|
||||
package org.dromara.common.core.context;
|
||||
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 线程持有类
|
||||
*
|
||||
* @author Michelle.Chung
|
||||
*/
|
||||
public class ThreadLocalHolder {
|
||||
|
||||
/**
|
||||
* 初始化 (支持异步)
|
||||
*/
|
||||
private static final ThreadLocal<Map<String, Object>> THREAD_LOCAL = TransmittableThreadLocal.withInitial(HashMap::new);
|
||||
|
||||
/**
|
||||
* 设置值
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
*/
|
||||
public static <T> void set(String key, T value) {
|
||||
THREAD_LOCAL.get().put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取值
|
||||
*
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T get(String key) {
|
||||
return (T) THREAD_LOCAL.get().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除值
|
||||
*
|
||||
* @param key 键
|
||||
*/
|
||||
public static void remove(String key) {
|
||||
THREAD_LOCAL.get().remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空值
|
||||
*/
|
||||
public static void clear() {
|
||||
THREAD_LOCAL.remove();
|
||||
}
|
||||
|
||||
}
|
@@ -1,9 +1,9 @@
|
||||
package org.dromara.common.dict.service.impl;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
import org.dromara.common.core.constant.CacheConstants;
|
||||
import org.dromara.common.core.context.ThreadLocalHolder;
|
||||
import org.dromara.common.core.service.DictService;
|
||||
import org.dromara.common.core.utils.StreamUtils;
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
@@ -38,10 +38,10 @@ public class DictServiceImpl implements DictService {
|
||||
@Override
|
||||
public String getDictLabel(String dictType, String dictValue, String separator) {
|
||||
// 优先从本地缓存获取
|
||||
List<RemoteDictDataVo> datas = ThreadLocalHolder.get(CacheConstants.SYS_DICT_KEY + dictType);
|
||||
List<RemoteDictDataVo> datas = (List<RemoteDictDataVo>) SaHolder.getStorage().get(CacheConstants.SYS_DICT_KEY + dictType);
|
||||
if (ObjectUtil.isNull(datas)) {
|
||||
datas = remoteDictService.selectDictDataByType(dictType);
|
||||
ThreadLocalHolder.set(CacheConstants.SYS_DICT_KEY + dictType, datas);
|
||||
SaHolder.getStorage().set(CacheConstants.SYS_DICT_KEY + dictType, datas);
|
||||
}
|
||||
|
||||
Map<String, String> map = StreamUtils.toMap(datas, RemoteDictDataVo::getDictValue, RemoteDictDataVo::getDictLabel);
|
||||
@@ -65,10 +65,10 @@ public class DictServiceImpl implements DictService {
|
||||
@Override
|
||||
public String getDictValue(String dictType, String dictLabel, String separator) {
|
||||
// 优先从本地缓存获取
|
||||
List<RemoteDictDataVo> datas = ThreadLocalHolder.get(CacheConstants.SYS_DICT_KEY + dictType);
|
||||
List<RemoteDictDataVo> datas = (List<RemoteDictDataVo>) SaHolder.getStorage().get(CacheConstants.SYS_DICT_KEY + dictType);
|
||||
if (ObjectUtil.isNull(datas)) {
|
||||
datas = remoteDictService.selectDictDataByType(dictType);
|
||||
ThreadLocalHolder.set(CacheConstants.SYS_DICT_KEY + dictType, datas);
|
||||
SaHolder.getStorage().set(CacheConstants.SYS_DICT_KEY + dictType, datas);
|
||||
}
|
||||
|
||||
Map<String, String> map = StreamUtils.toMap(datas, RemoteDictDataVo::getDictLabel, RemoteDictDataVo::getDictValue);
|
||||
|
@@ -4,8 +4,14 @@ import cn.dev33.satoken.SaManager;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.AfterThrowing;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.dromara.common.core.constant.GlobalConstants;
|
||||
import org.dromara.common.core.context.ThreadLocalHolder;
|
||||
import org.dromara.common.core.domain.R;
|
||||
import org.dromara.common.core.exception.ServiceException;
|
||||
import org.dromara.common.core.utils.MessageUtils;
|
||||
@@ -14,13 +20,6 @@ import org.dromara.common.core.utils.StringUtils;
|
||||
import org.dromara.common.idempotent.annotation.RepeatSubmit;
|
||||
import org.dromara.common.json.utils.JsonUtils;
|
||||
import org.dromara.common.redis.utils.RedisUtils;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.AfterThrowing;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -37,7 +36,7 @@ import java.util.StringJoiner;
|
||||
@Aspect
|
||||
public class RepeatSubmitAspect {
|
||||
|
||||
private static final String KEY_CACHE = "keyCache";
|
||||
private static final ThreadLocal<String> KEY_CACHE = new ThreadLocal<>();
|
||||
|
||||
@Before("@annotation(repeatSubmit)")
|
||||
public void doBefore(JoinPoint point, RepeatSubmit repeatSubmit) throws Throwable {
|
||||
@@ -60,7 +59,7 @@ public class RepeatSubmitAspect {
|
||||
// 唯一标识(指定key + url + 消息头)
|
||||
String cacheRepeatKey = GlobalConstants.REPEAT_SUBMIT_KEY + url + submitKey;
|
||||
if (RedisUtils.setObjectIfAbsent(cacheRepeatKey, "", Duration.ofMillis(interval))) {
|
||||
ThreadLocalHolder.set(KEY_CACHE, cacheRepeatKey);
|
||||
KEY_CACHE.set(cacheRepeatKey);
|
||||
} else {
|
||||
String message = repeatSubmit.message();
|
||||
if (StringUtils.startsWith(message, "{") && StringUtils.endsWith(message, "}")) {
|
||||
@@ -83,10 +82,9 @@ public class RepeatSubmitAspect {
|
||||
if (r.getCode() == R.SUCCESS) {
|
||||
return;
|
||||
}
|
||||
String cacheKey = ThreadLocalHolder.get(KEY_CACHE);
|
||||
RedisUtils.deleteObject(cacheKey);
|
||||
RedisUtils.deleteObject(KEY_CACHE.get());
|
||||
} finally {
|
||||
ThreadLocalHolder.remove(KEY_CACHE);
|
||||
KEY_CACHE.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -99,9 +97,8 @@ public class RepeatSubmitAspect {
|
||||
*/
|
||||
@AfterThrowing(value = "@annotation(repeatSubmit)", throwing = "e")
|
||||
public void doAfterThrowing(JoinPoint joinPoint, RepeatSubmit repeatSubmit, Exception e) {
|
||||
String cacheKey = ThreadLocalHolder.get(KEY_CACHE);
|
||||
RedisUtils.deleteObject(cacheKey);
|
||||
ThreadLocalHolder.remove(KEY_CACHE);
|
||||
RedisUtils.deleteObject(KEY_CACHE.get());
|
||||
KEY_CACHE.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -13,7 +13,6 @@ import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.AfterThrowing;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.dromara.common.core.context.ThreadLocalHolder;
|
||||
import org.dromara.common.core.utils.ServletUtils;
|
||||
import org.dromara.common.core.utils.SpringUtils;
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
@@ -51,7 +50,7 @@ public class LogAspect {
|
||||
/**
|
||||
* 计时 key
|
||||
*/
|
||||
private static final String LOG_STOP_WATCH_KEY = "logStopwatch";
|
||||
private static final ThreadLocal<StopWatch> KEY_CACHE = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 处理请求前执行
|
||||
@@ -59,7 +58,7 @@ public class LogAspect {
|
||||
@Before(value = "@annotation(controllerLog)")
|
||||
public void boBefore(JoinPoint joinPoint, Log controllerLog) {
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
ThreadLocalHolder.set(LOG_STOP_WATCH_KEY, stopWatch);
|
||||
KEY_CACHE.set(stopWatch);
|
||||
stopWatch.start();
|
||||
}
|
||||
|
||||
@@ -112,7 +111,7 @@ public class LogAspect {
|
||||
// 处理设置注解上的参数
|
||||
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
|
||||
// 设置消耗时间
|
||||
StopWatch stopWatch = ThreadLocalHolder.get(LOG_STOP_WATCH_KEY);
|
||||
StopWatch stopWatch = KEY_CACHE.get();
|
||||
stopWatch.stop();
|
||||
operLog.setCostTime(stopWatch.getTime());
|
||||
// 发布事件保存数据库
|
||||
@@ -122,7 +121,7 @@ public class LogAspect {
|
||||
log.error("异常信息:{}", exp.getMessage());
|
||||
exp.printStackTrace();
|
||||
} finally {
|
||||
ThreadLocalHolder.remove(LOG_STOP_WATCH_KEY);
|
||||
KEY_CACHE.remove();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,14 +1,15 @@
|
||||
package org.dromara.common.tenant.helper;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
|
||||
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.common.core.constant.GlobalConstants;
|
||||
import org.dromara.common.core.context.ThreadLocalHolder;
|
||||
import org.dromara.common.core.utils.SpringUtils;
|
||||
import org.dromara.common.core.utils.StringUtils;
|
||||
import org.dromara.common.redis.utils.RedisUtils;
|
||||
@@ -27,7 +28,7 @@ public class TenantHelper {
|
||||
|
||||
private static final String DYNAMIC_TENANT_KEY = GlobalConstants.GLOBAL_REDIS_KEY + "dynamicTenant";
|
||||
|
||||
private static final String TENANT_ID_KEY = "tempDynamicTenant";
|
||||
private static final ThreadLocal<String> TEMP_DYNAMIC_TENANT = new TransmittableThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 租户功能是否启用
|
||||
@@ -88,12 +89,12 @@ public class TenantHelper {
|
||||
return;
|
||||
}
|
||||
if (!isLogin()) {
|
||||
ThreadLocalHolder.set(TENANT_ID_KEY, tenantId);
|
||||
TEMP_DYNAMIC_TENANT.set(tenantId);
|
||||
return;
|
||||
}
|
||||
String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
|
||||
RedisUtils.setCacheObject(cacheKey, tenantId);
|
||||
ThreadLocalHolder.set(cacheKey, tenantId);
|
||||
SaHolder.getStorage().set(cacheKey, tenantId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,15 +107,15 @@ public class TenantHelper {
|
||||
return null;
|
||||
}
|
||||
if (!isLogin()) {
|
||||
return ThreadLocalHolder.get(TENANT_ID_KEY);
|
||||
return TEMP_DYNAMIC_TENANT.get();
|
||||
}
|
||||
String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
|
||||
String tenantId = ThreadLocalHolder.get(cacheKey);
|
||||
String tenantId = (String) SaHolder.getStorage().get(cacheKey);
|
||||
if (StringUtils.isNotBlank(tenantId)) {
|
||||
return tenantId;
|
||||
}
|
||||
tenantId = RedisUtils.getCacheObject(cacheKey);
|
||||
ThreadLocalHolder.set(cacheKey, tenantId);
|
||||
SaHolder.getStorage().set(cacheKey, tenantId);
|
||||
return tenantId;
|
||||
}
|
||||
|
||||
@@ -126,12 +127,12 @@ public class TenantHelper {
|
||||
return;
|
||||
}
|
||||
if (!isLogin()) {
|
||||
ThreadLocalHolder.remove(TENANT_ID_KEY);
|
||||
TEMP_DYNAMIC_TENANT.remove();
|
||||
return;
|
||||
}
|
||||
String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
|
||||
RedisUtils.deleteObject(cacheKey);
|
||||
ThreadLocalHolder.remove(cacheKey);
|
||||
SaHolder.getStorage().delete(cacheKey);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user