update 移除Spring注入 改为全局缓存 并使用更新时间确保集群配置最终一致性
This commit is contained in:
parent
881edb3e62
commit
25b47db3cb
@ -12,6 +12,7 @@ import com.ruoyi.oss.exception.OssException;
|
||||
import com.ruoyi.oss.properties.CloudStorageProperties;
|
||||
import com.ruoyi.oss.service.ICloudStorageStrategy;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ -22,15 +23,27 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
*/
|
||||
public class OssFactory {
|
||||
|
||||
private static RedisCache redisCache;
|
||||
private static RedisCache redisCache;
|
||||
|
||||
static {
|
||||
OssFactory.redisCache = SpringUtils.getBean(RedisCache.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务实例缓存
|
||||
*/
|
||||
private static final Map<String, ICloudStorageStrategy> SERVICES = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 服务配置更新时间缓存
|
||||
*/
|
||||
private static final Map<String, Date> SERVICES_UPDATE_TIME = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 获取默认实例
|
||||
*/
|
||||
public static ICloudStorageStrategy instance() {
|
||||
// 获取redis 默认类型
|
||||
String type = Convert.toStr(redisCache.getCacheObject(CloudConstant.CACHE_CONFIG_KEY));
|
||||
if (StringUtils.isEmpty(type)) {
|
||||
throw new OssException("文件存储服务类型无法找到!");
|
||||
@ -38,27 +51,27 @@ public class OssFactory {
|
||||
return instance(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据类型获取实例
|
||||
*/
|
||||
public static ICloudStorageStrategy instance(String type) {
|
||||
ICloudStorageStrategy service = SERVICES.get(type);
|
||||
if (service == null) {
|
||||
Object json = redisCache.getCacheObject(CloudConstant.SYS_OSS_KEY + type);
|
||||
CloudStorageProperties properties = JsonUtils.parseObject(json.toString(), CloudStorageProperties.class);
|
||||
String beanName = CloudServiceEnumd.getServiceName(type);
|
||||
ICloudStorageStrategy bean = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type), properties);
|
||||
SpringUtils.registerBean(beanName, bean);
|
||||
service = SpringUtils.getBean(beanName);
|
||||
SERVICES.put(type, bean);
|
||||
Date oldDate = SERVICES_UPDATE_TIME.get(type);
|
||||
Object json = redisCache.getCacheObject(CloudConstant.SYS_OSS_KEY + type);
|
||||
CloudStorageProperties properties = JsonUtils.parseObject(json.toString(), CloudStorageProperties.class);
|
||||
if (properties == null) {
|
||||
throw new OssException("系统异常, '" + type + "'配置信息不存在!");
|
||||
}
|
||||
Date nowDate = properties.getUpdateTime();
|
||||
// 服务存在并更新时间相同则返回(使用更新时间确保配置最终一致性)
|
||||
if (service != null && oldDate.equals(nowDate)) {
|
||||
return service;
|
||||
}
|
||||
// 获取redis配置信息 创建对象 并缓存
|
||||
service = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type), properties);
|
||||
SERVICES.put(type, service);
|
||||
SERVICES_UPDATE_TIME.put(type, nowDate);
|
||||
return service;
|
||||
}
|
||||
|
||||
public static void destroy(String type) {
|
||||
ICloudStorageStrategy service = SERVICES.get(type);
|
||||
if (service == null) {
|
||||
return;
|
||||
}
|
||||
SpringUtils.unregisterBean(CloudServiceEnumd.getServiceName(type));
|
||||
SERVICES.remove(type);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ package com.ruoyi.oss.properties;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* OSS云存储 配置属性
|
||||
*
|
||||
@ -45,4 +47,9 @@ public class CloudStorageProperties {
|
||||
*/
|
||||
private String isHttps;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
}
|
||||
|
@ -70,5 +70,5 @@ public interface ICloudStorageStrategy {
|
||||
*/
|
||||
UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType);
|
||||
|
||||
String getEndpointLink();
|
||||
String getEndpointLink();
|
||||
}
|
||||
|
@ -15,7 +15,6 @@ import com.ruoyi.common.utils.JsonUtils;
|
||||
import com.ruoyi.common.utils.PageUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.oss.constant.CloudConstant;
|
||||
import com.ruoyi.oss.factory.OssFactory;
|
||||
import com.ruoyi.system.domain.SysOssConfig;
|
||||
import com.ruoyi.system.domain.bo.SysOssConfigBo;
|
||||
import com.ruoyi.system.domain.vo.SysOssConfigVo;
|
||||
@ -54,7 +53,7 @@ public class SysOssConfigServiceImpl extends ServicePlusImpl<SysOssConfigMapper,
|
||||
if ("0".equals(config.getStatus())) {
|
||||
redisCache.setCacheObject(CloudConstant.CACHE_CONFIG_KEY, configKey);
|
||||
}
|
||||
redisCache.setCacheObject(getCacheKey(configKey), JsonUtils.toJsonString(config));
|
||||
setConfigCache(true, config);
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,27 +81,14 @@ public class SysOssConfigServiceImpl extends ServicePlusImpl<SysOssConfigMapper,
|
||||
public Boolean insertByBo(SysOssConfigBo bo) {
|
||||
SysOssConfig config = BeanUtil.toBean(bo, SysOssConfig.class);
|
||||
validEntityBeforeSave(config);
|
||||
boolean flag = save(config);
|
||||
if (flag) {
|
||||
redisCache.setCacheObject(
|
||||
getCacheKey(config.getConfigKey()),
|
||||
JsonUtils.toJsonString(config));
|
||||
}
|
||||
return flag;
|
||||
return setConfigCache(save(config), config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean updateByBo(SysOssConfigBo bo) {
|
||||
SysOssConfig config = BeanUtil.toBean(bo, SysOssConfig.class);
|
||||
validEntityBeforeSave(config);
|
||||
boolean flag = updateById(config);
|
||||
if (flag) {
|
||||
OssFactory.destroy(config.getConfigKey());
|
||||
redisCache.setCacheObject(
|
||||
getCacheKey(config.getConfigKey()),
|
||||
JsonUtils.toJsonString(config));
|
||||
}
|
||||
return flag;
|
||||
return setConfigCache(updateById(config), config);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -126,7 +112,6 @@ public class SysOssConfigServiceImpl extends ServicePlusImpl<SysOssConfigMapper,
|
||||
if (flag) {
|
||||
for (Long configId : ids) {
|
||||
SysOssConfig config = getById(configId);
|
||||
OssFactory.destroy(config.getConfigKey());
|
||||
redisCache.deleteObject(getCacheKey(config.getConfigKey()));
|
||||
}
|
||||
}
|
||||
@ -172,4 +157,19 @@ public class SysOssConfigServiceImpl extends ServicePlusImpl<SysOssConfigMapper,
|
||||
private String getCacheKey(String configKey) {
|
||||
return CloudConstant.SYS_OSS_KEY + configKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果操作成功 则更新缓存
|
||||
* @param flag 操作状态
|
||||
* @param config 配置
|
||||
* @return 返回操作状态
|
||||
*/
|
||||
private boolean setConfigCache(boolean flag, SysOssConfig config) {
|
||||
if (flag) {
|
||||
redisCache.setCacheObject(
|
||||
getCacheKey(config.getConfigKey()),
|
||||
JsonUtils.toJsonString(config));
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user