update 优化 移除ThreadLocalHolder(不可控问题太多)
This commit is contained in:
parent
57318cc55d
commit
e11b1bb2ec
@ -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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -4,8 +4,14 @@ import cn.dev33.satoken.SaManager;
|
|||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.crypto.SecureUtil;
|
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.constant.GlobalConstants;
|
||||||
import org.dromara.common.core.context.ThreadLocalHolder;
|
|
||||||
import org.dromara.common.core.domain.R;
|
import org.dromara.common.core.domain.R;
|
||||||
import org.dromara.common.core.exception.ServiceException;
|
import org.dromara.common.core.exception.ServiceException;
|
||||||
import org.dromara.common.core.utils.MessageUtils;
|
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.idempotent.annotation.RepeatSubmit;
|
||||||
import org.dromara.common.json.utils.JsonUtils;
|
import org.dromara.common.json.utils.JsonUtils;
|
||||||
import org.dromara.common.redis.utils.RedisUtils;
|
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.validation.BindingResult;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@ -37,7 +36,7 @@ import java.util.StringJoiner;
|
|||||||
@Aspect
|
@Aspect
|
||||||
public class RepeatSubmitAspect {
|
public class RepeatSubmitAspect {
|
||||||
|
|
||||||
private static final String KEY_CACHE = "keyCache";
|
private static final ThreadLocal<String> KEY_CACHE = new ThreadLocal<>();
|
||||||
|
|
||||||
@Before("@annotation(repeatSubmit)")
|
@Before("@annotation(repeatSubmit)")
|
||||||
public void doBefore(JoinPoint point, RepeatSubmit repeatSubmit) throws Throwable {
|
public void doBefore(JoinPoint point, RepeatSubmit repeatSubmit) throws Throwable {
|
||||||
@ -60,7 +59,7 @@ public class RepeatSubmitAspect {
|
|||||||
// 唯一标识(指定key + url + 消息头)
|
// 唯一标识(指定key + url + 消息头)
|
||||||
String cacheRepeatKey = GlobalConstants.REPEAT_SUBMIT_KEY + url + submitKey;
|
String cacheRepeatKey = GlobalConstants.REPEAT_SUBMIT_KEY + url + submitKey;
|
||||||
if (RedisUtils.setObjectIfAbsent(cacheRepeatKey, "", Duration.ofMillis(interval))) {
|
if (RedisUtils.setObjectIfAbsent(cacheRepeatKey, "", Duration.ofMillis(interval))) {
|
||||||
ThreadLocalHolder.set(KEY_CACHE, cacheRepeatKey);
|
KEY_CACHE.set(cacheRepeatKey);
|
||||||
} else {
|
} else {
|
||||||
String message = repeatSubmit.message();
|
String message = repeatSubmit.message();
|
||||||
if (StringUtils.startsWith(message, "{") && StringUtils.endsWith(message, "}")) {
|
if (StringUtils.startsWith(message, "{") && StringUtils.endsWith(message, "}")) {
|
||||||
@ -83,10 +82,9 @@ public class RepeatSubmitAspect {
|
|||||||
if (r.getCode() == R.SUCCESS) {
|
if (r.getCode() == R.SUCCESS) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String cacheKey = ThreadLocalHolder.get(KEY_CACHE);
|
RedisUtils.deleteObject(KEY_CACHE.get());
|
||||||
RedisUtils.deleteObject(cacheKey);
|
|
||||||
} finally {
|
} finally {
|
||||||
ThreadLocalHolder.remove(KEY_CACHE);
|
KEY_CACHE.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -99,9 +97,8 @@ public class RepeatSubmitAspect {
|
|||||||
*/
|
*/
|
||||||
@AfterThrowing(value = "@annotation(repeatSubmit)", throwing = "e")
|
@AfterThrowing(value = "@annotation(repeatSubmit)", throwing = "e")
|
||||||
public void doAfterThrowing(JoinPoint joinPoint, RepeatSubmit repeatSubmit, Exception e) {
|
public void doAfterThrowing(JoinPoint joinPoint, RepeatSubmit repeatSubmit, Exception e) {
|
||||||
String cacheKey = ThreadLocalHolder.get(KEY_CACHE);
|
RedisUtils.deleteObject(KEY_CACHE.get());
|
||||||
RedisUtils.deleteObject(cacheKey);
|
KEY_CACHE.remove();
|
||||||
ThreadLocalHolder.remove(KEY_CACHE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4,16 +4,6 @@ import cn.hutool.core.lang.Dict;
|
|||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import org.dromara.common.core.context.ThreadLocalHolder;
|
|
||||||
import org.dromara.common.core.domain.model.LoginUser;
|
|
||||||
import org.dromara.common.core.utils.ServletUtils;
|
|
||||||
import org.dromara.common.core.utils.SpringUtils;
|
|
||||||
import org.dromara.common.core.utils.StringUtils;
|
|
||||||
import org.dromara.common.json.utils.JsonUtils;
|
|
||||||
import org.dromara.common.log.annotation.Log;
|
|
||||||
import org.dromara.common.log.enums.BusinessStatus;
|
|
||||||
import org.dromara.common.log.event.OperLogEvent;
|
|
||||||
import org.dromara.common.satoken.utils.LoginHelper;
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -23,6 +13,15 @@ import org.aspectj.lang.annotation.AfterReturning;
|
|||||||
import org.aspectj.lang.annotation.AfterThrowing;
|
import org.aspectj.lang.annotation.AfterThrowing;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.aspectj.lang.annotation.Before;
|
import org.aspectj.lang.annotation.Before;
|
||||||
|
import org.dromara.common.core.domain.model.LoginUser;
|
||||||
|
import org.dromara.common.core.utils.ServletUtils;
|
||||||
|
import org.dromara.common.core.utils.SpringUtils;
|
||||||
|
import org.dromara.common.core.utils.StringUtils;
|
||||||
|
import org.dromara.common.json.utils.JsonUtils;
|
||||||
|
import org.dromara.common.log.annotation.Log;
|
||||||
|
import org.dromara.common.log.enums.BusinessStatus;
|
||||||
|
import org.dromara.common.log.event.OperLogEvent;
|
||||||
|
import org.dromara.common.satoken.utils.LoginHelper;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
@ -51,7 +50,7 @@ public class LogAspect {
|
|||||||
/**
|
/**
|
||||||
* 计时 key
|
* 计时 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)")
|
@Before(value = "@annotation(controllerLog)")
|
||||||
public void boBefore(JoinPoint joinPoint, Log controllerLog) {
|
public void boBefore(JoinPoint joinPoint, Log controllerLog) {
|
||||||
StopWatch stopWatch = new StopWatch();
|
StopWatch stopWatch = new StopWatch();
|
||||||
ThreadLocalHolder.set(LOG_STOP_WATCH_KEY, stopWatch);
|
KEY_CACHE.set(stopWatch);
|
||||||
stopWatch.start();
|
stopWatch.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +111,7 @@ public class LogAspect {
|
|||||||
// 处理设置注解上的参数
|
// 处理设置注解上的参数
|
||||||
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
|
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
|
||||||
// 设置消耗时间
|
// 设置消耗时间
|
||||||
StopWatch stopWatch = ThreadLocalHolder.get(LOG_STOP_WATCH_KEY);
|
StopWatch stopWatch = KEY_CACHE.get();
|
||||||
stopWatch.stop();
|
stopWatch.stop();
|
||||||
operLog.setCostTime(stopWatch.getTime());
|
operLog.setCostTime(stopWatch.getTime());
|
||||||
// 发布事件保存数据库
|
// 发布事件保存数据库
|
||||||
@ -122,7 +121,7 @@ public class LogAspect {
|
|||||||
log.error("异常信息:{}", exp.getMessage());
|
log.error("异常信息:{}", exp.getMessage());
|
||||||
exp.printStackTrace();
|
exp.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
ThreadLocalHolder.remove(LOG_STOP_WATCH_KEY);
|
KEY_CACHE.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
package org.dromara.common.tenant.helper;
|
package org.dromara.common.tenant.helper;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.context.SaHolder;
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import com.alibaba.ttl.TransmittableThreadLocal;
|
||||||
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
|
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
|
||||||
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
|
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
|
||||||
import lombok.AccessLevel;
|
import lombok.AccessLevel;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.dromara.common.core.constant.GlobalConstants;
|
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.SpringUtils;
|
||||||
import org.dromara.common.core.utils.StringUtils;
|
import org.dromara.common.core.utils.StringUtils;
|
||||||
import org.dromara.common.redis.utils.RedisUtils;
|
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 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;
|
return;
|
||||||
}
|
}
|
||||||
if (!isLogin()) {
|
if (!isLogin()) {
|
||||||
ThreadLocalHolder.set(TENANT_ID_KEY, tenantId);
|
TEMP_DYNAMIC_TENANT.set(tenantId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
|
String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
|
||||||
RedisUtils.setCacheObject(cacheKey, tenantId);
|
RedisUtils.setCacheObject(cacheKey, tenantId);
|
||||||
ThreadLocalHolder.set(cacheKey, tenantId);
|
SaHolder.getStorage().set(cacheKey, tenantId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -106,15 +107,15 @@ public class TenantHelper {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (!isLogin()) {
|
if (!isLogin()) {
|
||||||
return ThreadLocalHolder.get(TENANT_ID_KEY);
|
return TEMP_DYNAMIC_TENANT.get();
|
||||||
}
|
}
|
||||||
String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
|
String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
|
||||||
String tenantId = ThreadLocalHolder.get(cacheKey);
|
String tenantId = (String) SaHolder.getStorage().get(cacheKey);
|
||||||
if (StringUtils.isNotBlank(tenantId)) {
|
if (StringUtils.isNotBlank(tenantId)) {
|
||||||
return tenantId;
|
return tenantId;
|
||||||
}
|
}
|
||||||
tenantId = RedisUtils.getCacheObject(cacheKey);
|
tenantId = RedisUtils.getCacheObject(cacheKey);
|
||||||
ThreadLocalHolder.set(cacheKey, tenantId);
|
SaHolder.getStorage().set(cacheKey, tenantId);
|
||||||
return tenantId;
|
return tenantId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,12 +127,12 @@ public class TenantHelper {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!isLogin()) {
|
if (!isLogin()) {
|
||||||
ThreadLocalHolder.remove(TENANT_ID_KEY);
|
TEMP_DYNAMIC_TENANT.remove();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
|
String cacheKey = DYNAMIC_TENANT_KEY + ":" + LoginHelper.getUserId();
|
||||||
RedisUtils.deleteObject(cacheKey);
|
RedisUtils.deleteObject(cacheKey);
|
||||||
ThreadLocalHolder.remove(cacheKey);
|
SaHolder.getStorage().delete(cacheKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2,15 +2,14 @@ package org.dromara.common.web.interceptor;
|
|||||||
|
|
||||||
import cn.hutool.core.io.IoUtil;
|
import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
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.json.utils.JsonUtils;
|
|
||||||
import org.dromara.common.web.filter.RepeatedlyRequestWrapper;
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.time.StopWatch;
|
import org.apache.commons.lang3.time.StopWatch;
|
||||||
|
import org.dromara.common.core.utils.SpringUtils;
|
||||||
|
import org.dromara.common.core.utils.StringUtils;
|
||||||
|
import org.dromara.common.json.utils.JsonUtils;
|
||||||
|
import org.dromara.common.web.filter.RepeatedlyRequestWrapper;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
@ -30,7 +29,7 @@ public class PlusWebInvokeTimeInterceptor implements HandlerInterceptor {
|
|||||||
|
|
||||||
private final String prodProfile = "prod";
|
private final String prodProfile = "prod";
|
||||||
|
|
||||||
private final String STOP_WATCH_KEY = "stopwatch";
|
private final static ThreadLocal<StopWatch> KEY_CACHE = new ThreadLocal<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
@ -56,7 +55,7 @@ public class PlusWebInvokeTimeInterceptor implements HandlerInterceptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
StopWatch stopWatch = new StopWatch();
|
StopWatch stopWatch = new StopWatch();
|
||||||
ThreadLocalHolder.set(STOP_WATCH_KEY, stopWatch);
|
KEY_CACHE.set(stopWatch);
|
||||||
stopWatch.start();
|
stopWatch.start();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -70,10 +69,10 @@ public class PlusWebInvokeTimeInterceptor implements HandlerInterceptor {
|
|||||||
@Override
|
@Override
|
||||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||||
if (!prodProfile.equals(SpringUtils.getActiveProfile())) {
|
if (!prodProfile.equals(SpringUtils.getActiveProfile())) {
|
||||||
StopWatch stopWatch = ThreadLocalHolder.get(STOP_WATCH_KEY);
|
StopWatch stopWatch = KEY_CACHE.get();
|
||||||
stopWatch.stop();
|
stopWatch.stop();
|
||||||
log.info("[PLUS]结束请求 => URL[{}],耗时:[{}]毫秒", request.getMethod() + " " + request.getRequestURI(), stopWatch.getTime());
|
log.info("[PLUS]结束请求 => URL[{}],耗时:[{}]毫秒", request.getMethod() + " " + request.getRequestURI(), stopWatch.getTime());
|
||||||
ThreadLocalHolder.clear();
|
KEY_CACHE.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user