From 2d6c306ae113a7dd549a8e415ca075b754470dfc Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期五, 03 九月 2021 17:42:58 +0800
Subject: [PATCH] update 优化 oss配置 使用发布订阅工具 刷新配置

---
 ruoyi-oss/src/main/java/com/ruoyi/oss/factory/OssFactory.java |   43 +++++++++++++++++++++++++++++--------------
 1 files changed, 29 insertions(+), 14 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..7a22c50 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
@@ -11,6 +11,8 @@
 import com.ruoyi.oss.exception.OssException;
 import com.ruoyi.oss.properties.CloudStorageProperties;
 import com.ruoyi.oss.service.ICloudStorageStrategy;
+import com.ruoyi.oss.service.abstractd.AbstractCloudStorageStrategy;
+import lombok.extern.slf4j.Slf4j;
 
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -20,17 +22,29 @@
  *
  * @author Lion Li
  */
+@Slf4j
 public class OssFactory {
 
-    private static RedisCache redisCache;
+	private static RedisCache redisCache;
 
 	static {
 		OssFactory.redisCache = SpringUtils.getBean(RedisCache.class);
+		redisCache.subscribe(CloudConstant.CACHE_CONFIG_KEY, String.class, msg -> {
+			refreshService(msg);
+			log.info("璁㈤槄鍒锋柊OSS閰嶇疆 => " + msg);
+		});
 	}
 
+	/**
+	 * 鏈嶅姟瀹炰緥缂撳瓨
+	 */
 	private static final Map<String, ICloudStorageStrategy> SERVICES = 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 +52,28 @@
 		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);
+			refreshService(type);
+			service = SERVICES.get(type);
 		}
 		return service;
 	}
 
-	public static void destroy(String type) {
-		ICloudStorageStrategy service = SERVICES.get(type);
-		if (service == null) {
-			return;
+	private static void refreshService(String 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 + "'閰嶇疆淇℃伅涓嶅瓨鍦�!");
 		}
-		SpringUtils.unregisterBean(CloudServiceEnumd.getServiceName(type));
-		SERVICES.remove(type);
+		// 鑾峰彇redis閰嶇疆淇℃伅 鍒涘缓瀵硅薄 骞剁紦瀛�
+		ICloudStorageStrategy service = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type));
+		((AbstractCloudStorageStrategy)service).init(properties);
+		SERVICES.put(type, service);
 	}
 
 }

--
Gitblit v1.9.3