mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	update 优化 oss配置 使用发布订阅工具 刷新配置
This commit is contained in:
		@@ -31,7 +31,7 @@ public class RedisCache {
 | 
				
			|||||||
	 * @param msg 发送数据
 | 
						 * @param msg 发送数据
 | 
				
			||||||
	 * @param consumer 自定义处理
 | 
						 * @param consumer 自定义处理
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public <T> void publish(String channelKey, T msg, Consumer consumer) {
 | 
						public <T> void publish(String channelKey, T msg, Consumer<T> consumer) {
 | 
				
			||||||
		RTopic topic = redissonClient.getTopic(channelKey);
 | 
							RTopic topic = redissonClient.getTopic(channelKey);
 | 
				
			||||||
		topic.publish(msg);
 | 
							topic.publish(msg);
 | 
				
			||||||
		consumer.accept(msg);
 | 
							consumer.accept(msg);
 | 
				
			||||||
@@ -49,7 +49,7 @@ public class RedisCache {
 | 
				
			|||||||
	 * @param clazz 消息类型
 | 
						 * @param clazz 消息类型
 | 
				
			||||||
	 * @param consumer 自定义处理
 | 
						 * @param consumer 自定义处理
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public <T> void subscribe(String channelKey, Class<T> clazz, Consumer consumer) {
 | 
						public <T> void subscribe(String channelKey, Class<T> clazz, Consumer<T> consumer) {
 | 
				
			||||||
		RTopic topic = redissonClient.getTopic(channelKey);
 | 
							RTopic topic = redissonClient.getTopic(channelKey);
 | 
				
			||||||
		topic.addListener(clazz, (channel, msg) -> consumer.accept(msg));
 | 
							topic.addListener(clazz, (channel, msg) -> consumer.accept(msg));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -36,7 +36,7 @@ public class RedisPubSubController {
 | 
				
			|||||||
	@GetMapping("/sub")
 | 
						@GetMapping("/sub")
 | 
				
			||||||
	public AjaxResult<Void> sub(String key){
 | 
						public AjaxResult<Void> sub(String key){
 | 
				
			||||||
		redisCache.subscribe(key, String.class, msg -> {
 | 
							redisCache.subscribe(key, String.class, msg -> {
 | 
				
			||||||
			System.out.println("订阅通道 => " + key + ", 接收值 => " + msg.toString());
 | 
								System.out.println("订阅通道 => " + key + ", 接收值 => " + msg);
 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
		return AjaxResult.success("操作成功");
 | 
							return AjaxResult.success("操作成功");
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,8 +12,8 @@ import com.ruoyi.oss.exception.OssException;
 | 
				
			|||||||
import com.ruoyi.oss.properties.CloudStorageProperties;
 | 
					import com.ruoyi.oss.properties.CloudStorageProperties;
 | 
				
			||||||
import com.ruoyi.oss.service.ICloudStorageStrategy;
 | 
					import com.ruoyi.oss.service.ICloudStorageStrategy;
 | 
				
			||||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageStrategy;
 | 
					import com.ruoyi.oss.service.abstractd.AbstractCloudStorageStrategy;
 | 
				
			||||||
 | 
					import lombok.extern.slf4j.Slf4j;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.Date;
 | 
					 | 
				
			||||||
import java.util.Map;
 | 
					import java.util.Map;
 | 
				
			||||||
import java.util.concurrent.ConcurrentHashMap;
 | 
					import java.util.concurrent.ConcurrentHashMap;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -22,12 +22,17 @@ import java.util.concurrent.ConcurrentHashMap;
 | 
				
			|||||||
 *
 | 
					 *
 | 
				
			||||||
 * @author Lion Li
 | 
					 * @author Lion Li
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					@Slf4j
 | 
				
			||||||
public class OssFactory {
 | 
					public class OssFactory {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static RedisCache redisCache;
 | 
						private static RedisCache redisCache;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	static {
 | 
						static {
 | 
				
			||||||
		OssFactory.redisCache = SpringUtils.getBean(RedisCache.class);
 | 
							OssFactory.redisCache = SpringUtils.getBean(RedisCache.class);
 | 
				
			||||||
 | 
							redisCache.subscribe(CloudConstant.CACHE_CONFIG_KEY, String.class, msg -> {
 | 
				
			||||||
 | 
								refreshService(msg);
 | 
				
			||||||
 | 
								log.info("订阅刷新OSS配置 => " + msg);
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
@@ -35,11 +40,6 @@ public class OssFactory {
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	private static final Map<String, ICloudStorageStrategy> SERVICES = new ConcurrentHashMap<>();
 | 
						private static final Map<String, ICloudStorageStrategy> SERVICES = new ConcurrentHashMap<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * 服务配置更新时间缓存
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	private static final Map<String, Date> SERVICES_UPDATE_TIME = new ConcurrentHashMap<>();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * 获取默认实例
 | 
						 * 获取默认实例
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
@@ -57,23 +57,23 @@ public class OssFactory {
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	public static ICloudStorageStrategy instance(String type) {
 | 
						public static ICloudStorageStrategy instance(String type) {
 | 
				
			||||||
		ICloudStorageStrategy service = SERVICES.get(type);
 | 
							ICloudStorageStrategy service = SERVICES.get(type);
 | 
				
			||||||
		Date oldDate = SERVICES_UPDATE_TIME.get(type);
 | 
							if (service == null) {
 | 
				
			||||||
 | 
								refreshService(type);
 | 
				
			||||||
 | 
								service = SERVICES.get(type);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return service;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						private static void refreshService(String type) {
 | 
				
			||||||
		Object json = redisCache.getCacheObject(CloudConstant.SYS_OSS_KEY + type);
 | 
							Object json = redisCache.getCacheObject(CloudConstant.SYS_OSS_KEY + type);
 | 
				
			||||||
		CloudStorageProperties properties = JsonUtils.parseObject(json.toString(), CloudStorageProperties.class);
 | 
							CloudStorageProperties properties = JsonUtils.parseObject(json.toString(), CloudStorageProperties.class);
 | 
				
			||||||
		if (properties == null) {
 | 
							if (properties == null) {
 | 
				
			||||||
			throw new OssException("系统异常, '" + type + "'配置信息不存在!");
 | 
								throw new OssException("系统异常, '" + type + "'配置信息不存在!");
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		Date nowDate = properties.getUpdateTime();
 | 
					 | 
				
			||||||
		// 服务存在并更新时间相同则返回(使用更新时间确保配置最终一致性)
 | 
					 | 
				
			||||||
		if (service != null && oldDate.equals(nowDate)) {
 | 
					 | 
				
			||||||
			return service;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		// 获取redis配置信息 创建对象 并缓存
 | 
							// 获取redis配置信息 创建对象 并缓存
 | 
				
			||||||
		service = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type));
 | 
							ICloudStorageStrategy service = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type));
 | 
				
			||||||
		((AbstractCloudStorageStrategy)service).init(properties);
 | 
							((AbstractCloudStorageStrategy)service).init(properties);
 | 
				
			||||||
		SERVICES.put(type, service);
 | 
							SERVICES.put(type, service);
 | 
				
			||||||
		SERVICES_UPDATE_TIME.put(type, nowDate);
 | 
					 | 
				
			||||||
		return service;
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,6 +21,7 @@ import com.ruoyi.system.domain.vo.SysOssConfigVo;
 | 
				
			|||||||
import com.ruoyi.system.mapper.SysOssConfigMapper;
 | 
					import com.ruoyi.system.mapper.SysOssConfigMapper;
 | 
				
			||||||
import com.ruoyi.system.service.ISysOssConfigService;
 | 
					import com.ruoyi.system.service.ISysOssConfigService;
 | 
				
			||||||
import lombok.RequiredArgsConstructor;
 | 
					import lombok.RequiredArgsConstructor;
 | 
				
			||||||
 | 
					import lombok.extern.slf4j.Slf4j;
 | 
				
			||||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
					import org.springframework.beans.factory.annotation.Autowired;
 | 
				
			||||||
import org.springframework.stereotype.Service;
 | 
					import org.springframework.stereotype.Service;
 | 
				
			||||||
import org.springframework.transaction.annotation.Transactional;
 | 
					import org.springframework.transaction.annotation.Transactional;
 | 
				
			||||||
@@ -36,6 +37,7 @@ import java.util.List;
 | 
				
			|||||||
 * @author 孤舟烟雨
 | 
					 * @author 孤舟烟雨
 | 
				
			||||||
 * @date 2021-08-13
 | 
					 * @date 2021-08-13
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					@Slf4j
 | 
				
			||||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
 | 
					@RequiredArgsConstructor(onConstructor_ = @Autowired)
 | 
				
			||||||
@Service
 | 
					@Service
 | 
				
			||||||
public class SysOssConfigServiceImpl extends ServicePlusImpl<SysOssConfigMapper, SysOssConfig, SysOssConfigVo> implements ISysOssConfigService {
 | 
					public class SysOssConfigServiceImpl extends ServicePlusImpl<SysOssConfigMapper, SysOssConfig, SysOssConfigVo> implements ISysOssConfigService {
 | 
				
			||||||
@@ -169,6 +171,9 @@ public class SysOssConfigServiceImpl extends ServicePlusImpl<SysOssConfigMapper,
 | 
				
			|||||||
			redisCache.setCacheObject(
 | 
								redisCache.setCacheObject(
 | 
				
			||||||
				getCacheKey(config.getConfigKey()),
 | 
									getCacheKey(config.getConfigKey()),
 | 
				
			||||||
				JsonUtils.toJsonString(config));
 | 
									JsonUtils.toJsonString(config));
 | 
				
			||||||
 | 
								redisCache.publish(CloudConstant.CACHE_CONFIG_KEY, config.getConfigKey(), msg -> {
 | 
				
			||||||
 | 
									log.info("发布刷新OSS配置 => " + msg);
 | 
				
			||||||
 | 
								});
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return flag;
 | 
							return flag;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user