mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2026-01-03 20:35:29 +08:00
3.2.0-beta,重构很大:升级springboot2.6.6、spring-cloud-alibaba 2021.1、mybatisplus3.5.1、代码规范部分重构
This commit is contained in:
@ -4,7 +4,7 @@
|
||||
<parent>
|
||||
<groupId>org.jeecgframework.boot</groupId>
|
||||
<artifactId>jeecg-boot-base</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<version>3.2.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<description>公共模块</description>
|
||||
|
||||
@ -13,8 +13,11 @@ import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 自定义Map
|
||||
*/
|
||||
* BaseMap
|
||||
*
|
||||
* @author: scott
|
||||
* @date: 2020/01/01 16:17
|
||||
*/
|
||||
public class BaseMap extends HashMap<String, Object> {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@ -4,7 +4,12 @@ import org.jeecg.common.util.SpringContextHolder;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* SpringContextHolder注册用
|
||||
*
|
||||
* @author: scott
|
||||
* @date: 2020/01/01 16:00
|
||||
*/
|
||||
@Configuration
|
||||
public class CommonConfig {
|
||||
|
||||
|
||||
@ -6,7 +6,9 @@ import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 存放token到上下文供队列调用feign使用
|
||||
* 存放临时令牌Token到线程上下文
|
||||
*
|
||||
* 供队列、定时任务 feign调用使用(解决无会话Token问题)
|
||||
* @author zyf
|
||||
*/
|
||||
public class TransmitUserTokenFilter implements Filter {
|
||||
@ -30,10 +32,10 @@ public class TransmitUserTokenFilter implements Filter {
|
||||
String token = request.getHeader(X_ACCESS_TOKEN);
|
||||
if (token!=null) {
|
||||
try {
|
||||
//将token放入上下文中
|
||||
//将Token放入当前线程上下文中
|
||||
UserTokenContext.setToken(token);
|
||||
} catch (Exception e) {
|
||||
|
||||
//e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,11 +2,16 @@ package org.jeecg.common.config.mqtoken;
|
||||
|
||||
|
||||
/**
|
||||
* 用户token上下文
|
||||
* 用户Token线程上下文
|
||||
*
|
||||
* 供队列、定时任务 feign调用使用(解决无会话Token问题)
|
||||
* @author zyf
|
||||
*/
|
||||
public class UserTokenContext {
|
||||
|
||||
/**
|
||||
* 当前线程的TOKEN副本
|
||||
*/
|
||||
private static ThreadLocal<String> userToken = new ThreadLocal<String>();
|
||||
|
||||
public UserTokenContext() {
|
||||
@ -19,4 +24,8 @@ public class UserTokenContext {
|
||||
public static void setToken(String token){
|
||||
userToken.set(token);
|
||||
}
|
||||
|
||||
public static void remove() {
|
||||
userToken.remove();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,11 +1,20 @@
|
||||
package org.jeecg.common.constant;
|
||||
|
||||
/**
|
||||
* @Description: GlobalConstants
|
||||
* @author: scott
|
||||
* @date: 2020/01/01 16:01
|
||||
*/
|
||||
public class GlobalConstants {
|
||||
|
||||
/**
|
||||
* 业务处理器beanName传递参数
|
||||
*/
|
||||
public static final String HANDLER_NAME = "handlerName";
|
||||
/**
|
||||
* 路由刷新触发器
|
||||
*/
|
||||
public static final String LODER_ROUDER_HANDLER = "loderRouderHandler";
|
||||
|
||||
/**
|
||||
* redis消息通道名称
|
||||
|
||||
@ -0,0 +1,108 @@
|
||||
package org.jeecg.common.enums;
|
||||
|
||||
/**
|
||||
* @Description: 异常错误信息定义
|
||||
* @author: zyf
|
||||
* @date: 2022/4/14 10:05
|
||||
*/
|
||||
public enum SentinelErrorInfoEnum {
|
||||
|
||||
/**
|
||||
* 流控异常
|
||||
*/
|
||||
FlowException("访问频繁,请稍候再试"),
|
||||
|
||||
/**
|
||||
* 热点参数异常
|
||||
*/
|
||||
ParamFlowException("热点参数限流"),
|
||||
|
||||
/**
|
||||
* 系统规则限流或降级
|
||||
*/
|
||||
SystemBlockException("系统规则限流或降级"),
|
||||
|
||||
/**
|
||||
* 授权规则不通过
|
||||
*/
|
||||
AuthorityException("授权规则不通过"),
|
||||
|
||||
/**
|
||||
* 授权规则不通过
|
||||
*/
|
||||
UnknownError("未知异常"),
|
||||
|
||||
/**
|
||||
* 服务降级
|
||||
*/
|
||||
DegradeException("服务降级");
|
||||
|
||||
|
||||
/**
|
||||
* 错误信息
|
||||
*/
|
||||
String error;
|
||||
|
||||
/**
|
||||
* 错误代码
|
||||
*/
|
||||
Integer code;
|
||||
|
||||
|
||||
public String getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
public void setError(String error) {
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param error 错误信息
|
||||
* @param code 错误代码
|
||||
*/
|
||||
SentinelErrorInfoEnum(String error, Integer code) {
|
||||
this.error = error;
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造器
|
||||
*
|
||||
* @param error 错误信息
|
||||
*/
|
||||
SentinelErrorInfoEnum(String error) {
|
||||
this.error = error;
|
||||
this.code = 500;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据异常名称匹配
|
||||
*
|
||||
* @param throwable 异常
|
||||
* @return String 错误信息
|
||||
*/
|
||||
public static SentinelErrorInfoEnum getErrorByException(Throwable throwable) {
|
||||
if(throwable==null){
|
||||
return null;
|
||||
}
|
||||
|
||||
String exceptionClass=throwable.getClass().getSimpleName();
|
||||
for (SentinelErrorInfoEnum e : SentinelErrorInfoEnum.values()) {
|
||||
if (exceptionClass.equals(e.name())) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -8,8 +8,10 @@ import org.springframework.data.redis.core.RedisTemplate;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* redis客户端
|
||||
*/
|
||||
* @Description: redis客户端
|
||||
* @author: scott
|
||||
* @date: 2020/01/01 16:01
|
||||
*/
|
||||
@Configuration
|
||||
public class JeecgRedisClient {
|
||||
|
||||
|
||||
@ -46,26 +46,6 @@ public class RedisConfig extends CachingConfigurerSupport {
|
||||
@Resource
|
||||
private LettuceConnectionFactory lettuceConnectionFactory;
|
||||
|
||||
// /**
|
||||
// * @description 自定义的缓存key的生成策略 若想使用这个key
|
||||
// * 只需要讲注解上keyGenerator的值设置为keyGenerator即可</br>
|
||||
// * @return 自定义策略生成的key
|
||||
// */
|
||||
// @Override
|
||||
// @Bean
|
||||
// public KeyGenerator keyGenerator() {
|
||||
// return new KeyGenerator() {
|
||||
// @Override
|
||||
// public Object generate(Object target, Method method, Object... params) {
|
||||
// StringBuilder sb = new StringBuilder();
|
||||
// sb.append(target.getClass().getName());
|
||||
// sb.append(method.getDeclaringClass().getName());
|
||||
// Arrays.stream(params).map(Object::toString).forEach(sb::append);
|
||||
// return sb.toString();
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
|
||||
/**
|
||||
* RedisTemplate配置
|
||||
* @param lettuceConnectionFactory
|
||||
@ -113,7 +93,7 @@ public class RedisConfig extends CachingConfigurerSupport {
|
||||
// 创建默认缓存配置对象
|
||||
/* 默认配置,设置缓存有效期 1小时*/
|
||||
//RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(1));
|
||||
/* 自定义配置test:demo 的超时时间为 5分钟*/
|
||||
// 自定义配置test:demo 的超时时间为 5分钟
|
||||
RedisCacheManager cacheManager = RedisCacheManager.builder(writer).cacheDefaults(redisCacheConfiguration)
|
||||
.withInitialCacheConfigurations(singletonMap(CacheConstant.SYS_DICT_TABLE_CACHE,
|
||||
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10)).disableCachingNullValues()
|
||||
|
||||
@ -0,0 +1,18 @@
|
||||
package org.jeecg.common.modules.redis.listener;
|
||||
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
|
||||
/**
|
||||
* @Description: 自定义消息监听
|
||||
* @author: scott
|
||||
* @date: 2020/01/01 16:02
|
||||
*/
|
||||
public interface JeecgRedisListener {
|
||||
/**
|
||||
* 接受消息
|
||||
*
|
||||
* @param message
|
||||
*/
|
||||
void onMessage(BaseMap message);
|
||||
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
package org.jeecg.common.modules.redis.listener;
|
||||
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
|
||||
/**
|
||||
* 自定义消息监听
|
||||
*/
|
||||
public interface JeecgRedisListerer {
|
||||
|
||||
void onMessage(BaseMap message);
|
||||
|
||||
}
|
||||
@ -5,7 +5,7 @@ import cn.hutool.core.util.ObjectUtil;
|
||||
import lombok.Data;
|
||||
import org.jeecg.common.base.BaseMap;
|
||||
import org.jeecg.common.constant.GlobalConstants;
|
||||
import org.jeecg.common.modules.redis.listener.JeecgRedisListerer;
|
||||
import org.jeecg.common.modules.redis.listener.JeecgRedisListener;
|
||||
import org.jeecg.common.util.SpringContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@ -24,7 +24,7 @@ public class RedisReceiver {
|
||||
*/
|
||||
public void onMessage(BaseMap params) {
|
||||
Object handlerName = params.get(GlobalConstants.HANDLER_NAME);
|
||||
JeecgRedisListerer messageListener = SpringContextHolder.getHandler(handlerName.toString(), JeecgRedisListerer.class);
|
||||
JeecgRedisListener messageListener = SpringContextHolder.getHandler(handlerName.toString(), JeecgRedisListener.class);
|
||||
if (ObjectUtil.isNotEmpty(messageListener)) {
|
||||
messageListener.onMessage(params);
|
||||
}
|
||||
|
||||
@ -11,6 +11,8 @@ import java.util.function.Function;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.PessimisticLockingFailureException;
|
||||
import org.springframework.data.redis.cache.CacheStatistics;
|
||||
import org.springframework.data.redis.cache.CacheStatisticsCollector;
|
||||
import org.springframework.data.redis.cache.RedisCacheWriter;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
@ -20,8 +22,11 @@ import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* 该类参照 DefaultRedisCacheWriter 重写了 remove 方法实现通配符*删除
|
||||
*/
|
||||
* 该类参照 DefaultRedisCacheWriter 重写了 remove 方法实现通配符*删除
|
||||
*
|
||||
* @author: scott
|
||||
* @date: 2020/01/01 16:18
|
||||
*/
|
||||
@Slf4j
|
||||
public class JeecgRedisCacheWriter implements RedisCacheWriter {
|
||||
|
||||
@ -39,6 +44,7 @@ public class JeecgRedisCacheWriter implements RedisCacheWriter {
|
||||
this.sleepTime = sleepTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(String name, byte[] key, byte[] value, @Nullable Duration ttl) {
|
||||
Assert.notNull(name, "Name must not be null!");
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
@ -54,6 +60,7 @@ public class JeecgRedisCacheWriter implements RedisCacheWriter {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] get(String name, byte[] key) {
|
||||
Assert.notNull(name, "Name must not be null!");
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
@ -62,6 +69,7 @@ public class JeecgRedisCacheWriter implements RedisCacheWriter {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Duration ttl) {
|
||||
Assert.notNull(name, "Name must not be null!");
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
@ -97,12 +105,14 @@ public class JeecgRedisCacheWriter implements RedisCacheWriter {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(String name, byte[] key) {
|
||||
Assert.notNull(name, "Name must not be null!");
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
String keyString = new String(key);
|
||||
log.info("redis remove key:" + keyString);
|
||||
if(keyString!=null && keyString.endsWith("*")){
|
||||
String keyIsAll = "*";
|
||||
if(keyString!=null && keyString.endsWith(keyIsAll)){
|
||||
execute(name, connection -> {
|
||||
// 获取某个前缀所拥有的所有的键,某个前缀开头,后面肯定是*
|
||||
Set<byte[]> keys = connection.keys(key);
|
||||
@ -119,6 +129,7 @@ public class JeecgRedisCacheWriter implements RedisCacheWriter {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clean(String name, byte[] pattern) {
|
||||
Assert.notNull(name, "Name must not be null!");
|
||||
Assert.notNull(pattern, "Pattern must not be null!");
|
||||
@ -218,4 +229,22 @@ public class JeecgRedisCacheWriter implements RedisCacheWriter {
|
||||
private static byte[] createCacheLockKey(String name) {
|
||||
return (name + "~lock").getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
//update-begin-author:zyf date:20220216 for:升级springboot版本到2.4.0+以后需要实现的方法*
|
||||
private final CacheStatisticsCollector statistics = CacheStatisticsCollector.create();
|
||||
@Override
|
||||
public CacheStatistics getCacheStatistics(String cacheName) {
|
||||
return statistics.getCacheStatistics(cacheName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearStatistics(String name) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public RedisCacheWriter withStatisticsCollector(CacheStatisticsCollector cacheStatisticsCollector) {
|
||||
return null;
|
||||
}
|
||||
//update-begin-author:zyf date:20220216 for:升级springboot版本到2.4.0+以后需要实现的方法*
|
||||
}
|
||||
|
||||
@ -1,15 +1,14 @@
|
||||
package org.jeecg.common.util;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.*;
|
||||
import org.springframework.data.redis.core.Cursor;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ScanOptions;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* redis 工具类
|
||||
@ -21,8 +20,6 @@ public class RedisUtil {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
/**
|
||||
* 指定缓存失效时间
|
||||
@ -79,7 +76,8 @@ public class RedisUtil {
|
||||
if (key.length == 1) {
|
||||
redisTemplate.delete(key[0]);
|
||||
} else {
|
||||
redisTemplate.delete(CollectionUtils.arrayToList(key));
|
||||
//springboot2.4后用法
|
||||
redisTemplate.delete(Arrays.asList(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -585,7 +583,8 @@ public class RedisUtil {
|
||||
try {
|
||||
return redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
|
||||
Set<String> binaryKeys = new HashSet<>();
|
||||
Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder().match(realKey).count(Integer.MAX_VALUE).build());
|
||||
//springboot2.4后用法
|
||||
Cursor<byte[]> cursor = connection.scan(ScanOptions.scanOptions().match(realKey).count(Integer.MAX_VALUE).build());
|
||||
while (cursor.hasNext()) {
|
||||
binaryKeys.add(new String(cursor.next()));
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ public class SpringContextHolder implements ApplicationContextAware {
|
||||
try {
|
||||
t = applicationContext.getBean(name, cls);
|
||||
} catch (Exception e) {
|
||||
log.error("####################" + name + "未定义");
|
||||
log.warn("Customize redis listener handle [ " + name + " ], does not exist!");
|
||||
}
|
||||
}
|
||||
return t;
|
||||
|
||||
Reference in New Issue
Block a user