From 25b47db3cb5f99c1b266138444587c3bb026dd60 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期日, 15 八月 2021 16:59:46 +0800
Subject: [PATCH] update 移除Spring注入 改为全局缓存 并使用更新时间确保集群配置最终一致性

---
 ruoyi-oss/src/main/java/com/ruoyi/oss/properties/CloudStorageProperties.java          |    7 +++
 ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java                         |   49 +++++++++++++++---------
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssConfigServiceImpl.java |   36 +++++++++---------
 ruoyi-oss/src/main/java/com/ruoyi/oss/service/ICloudStorageStrategy.java              |    2 
 4 files changed, 57 insertions(+), 37 deletions(-)

diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java
index ea64b27..1290685 100644
--- a/ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java
+++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java
@@ -12,6 +12,7 @@
 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 @@
  */
 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 @@
 		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);
 	}
 
 }
diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/properties/CloudStorageProperties.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/properties/CloudStorageProperties.java
index d1bafde..1c62ae2 100644
--- a/ruoyi-oss/src/main/java/com/ruoyi/oss/properties/CloudStorageProperties.java
+++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/properties/CloudStorageProperties.java
@@ -2,6 +2,8 @@
 
 import lombok.Data;
 
+import java.util.Date;
+
 /**
  * OSS浜戝瓨鍌� 閰嶇疆灞炴��
  *
@@ -45,4 +47,9 @@
 	 */
 	private String isHttps;
 
+	/**
+	 * 鏇存柊鏃堕棿
+	 */
+	private Date updateTime;
+
 }
diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/service/ICloudStorageStrategy.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/service/ICloudStorageStrategy.java
index 6f8ed85..f91fc7a 100644
--- a/ruoyi-oss/src/main/java/com/ruoyi/oss/service/ICloudStorageStrategy.java
+++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/service/ICloudStorageStrategy.java
@@ -70,5 +70,5 @@
 	 */
 	UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType);
 
-    String getEndpointLink();
+	String getEndpointLink();
 }
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssConfigServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssConfigServiceImpl.java
index 49bf1fa..a202790 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssConfigServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysOssConfigServiceImpl.java
@@ -15,7 +15,6 @@
 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 @@
 			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 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 @@
     	if (flag) {
 			for (Long configId : ids) {
 				SysOssConfig config = getById(configId);
-				OssFactory.destroy(config.getConfigKey());
 				redisCache.deleteObject(getCacheKey(config.getConfigKey()));
 			}
 		}
@@ -172,4 +157,19 @@
 	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;
+	}
 }

--
Gitblit v1.9.3