🔨 优化配置中心逻辑.
This commit is contained in:
@@ -24,6 +24,7 @@ package org.dromara.visor.common.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
@@ -34,14 +35,14 @@ import java.util.function.Function;
|
||||
* @since 2025/1/6 18:01
|
||||
*/
|
||||
@Slf4j
|
||||
public class ConfigRef<T> {
|
||||
public abstract class ConfigRef<T> {
|
||||
|
||||
public final String key;
|
||||
|
||||
private final Function<String, T> convert;
|
||||
|
||||
public T value;
|
||||
|
||||
protected final Function<String, T> convert;
|
||||
|
||||
public ConfigRef(String key, Function<String, T> convert) {
|
||||
this.key = key;
|
||||
this.convert = convert;
|
||||
@@ -52,21 +53,28 @@ public class ConfigRef<T> {
|
||||
*
|
||||
* @param value value
|
||||
*/
|
||||
public void override(String value) {
|
||||
try {
|
||||
this.value = convert.apply(value);
|
||||
} catch (Exception e) {
|
||||
log.error("ConfigRef trigger override error key: {}, value: {}", key, value, e);
|
||||
}
|
||||
}
|
||||
public abstract void override(String value);
|
||||
|
||||
/**
|
||||
* 设置值
|
||||
* 修改配置
|
||||
*
|
||||
* @param value value
|
||||
*/
|
||||
public void set(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
public abstract void set(T value);
|
||||
|
||||
/**
|
||||
* 获取配置
|
||||
*
|
||||
* @return value
|
||||
*/
|
||||
public abstract T get();
|
||||
|
||||
/**
|
||||
* 修改回调
|
||||
*
|
||||
* @param changeEvent changeEvent
|
||||
* @return this
|
||||
*/
|
||||
public abstract ConfigRef<T> onChange(BiConsumer<T, T> changeEvent);
|
||||
|
||||
}
|
||||
|
||||
@@ -33,6 +33,91 @@ import java.util.function.Function;
|
||||
*/
|
||||
public interface ConfigStore {
|
||||
|
||||
/**
|
||||
* 获取 string 配置
|
||||
*
|
||||
* @param key key
|
||||
* @return config
|
||||
*/
|
||||
String getString(String key);
|
||||
|
||||
/**
|
||||
* 获取 string 配置
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultValue defaultValue
|
||||
* @return config
|
||||
*/
|
||||
String getString(String key, String defaultValue);
|
||||
|
||||
/**
|
||||
* 获取 int 配置
|
||||
*
|
||||
* @param key key
|
||||
* @return config
|
||||
*/
|
||||
Integer getInteger(String key);
|
||||
|
||||
/**
|
||||
* 获取 int 配置
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultValue defaultValue
|
||||
* @return config
|
||||
*/
|
||||
Integer getInteger(String key, Integer defaultValue);
|
||||
|
||||
/**
|
||||
* 获取 long 配置
|
||||
*
|
||||
* @param key key
|
||||
* @return config
|
||||
*/
|
||||
Long getLong(String key);
|
||||
|
||||
/**
|
||||
* 获取 long 配置
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultValue defaultValue
|
||||
* @return config
|
||||
*/
|
||||
Long getLong(String key, Long defaultValue);
|
||||
|
||||
/**
|
||||
* 获取 double 配置
|
||||
*
|
||||
* @param key key
|
||||
* @return config
|
||||
*/
|
||||
Double getDouble(String key);
|
||||
|
||||
/**
|
||||
* 获取 double 配置
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultValue defaultValue
|
||||
* @return config
|
||||
*/
|
||||
Double getDouble(String key, Double defaultValue);
|
||||
|
||||
/**
|
||||
* 获取 boolean 配置
|
||||
*
|
||||
* @param key key
|
||||
* @return config
|
||||
*/
|
||||
Boolean getBoolean(String key);
|
||||
|
||||
/**
|
||||
* 获取 boolean 配置
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultValue defaultValue
|
||||
* @return config
|
||||
*/
|
||||
Boolean getBoolean(String key, Boolean defaultValue);
|
||||
|
||||
/**
|
||||
* 获取配置
|
||||
*
|
||||
@@ -64,6 +149,7 @@ public interface ConfigStore {
|
||||
* 获取配置
|
||||
*
|
||||
* @param key key
|
||||
* @param convert convert
|
||||
* @param defaultValue defaultValue
|
||||
* @param <T> T
|
||||
* @return conf
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.common.constant;
|
||||
|
||||
/**
|
||||
* 配置项常量
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2025/1/14 16:15
|
||||
*/
|
||||
public interface ConfigKeys {
|
||||
|
||||
/**
|
||||
* 加密公钥
|
||||
*/
|
||||
String ENCRYPT_PUBLIC_KEY = "encrypt.publicKey";
|
||||
|
||||
/**
|
||||
* 加密私钥
|
||||
*/
|
||||
String ENCRYPT_PRIVATE_KEY = "encrypt.privateKey";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (c) 2023 - present Dromara, All rights reserved.
|
||||
*
|
||||
* https://visor.dromara.org
|
||||
* https://visor.dromara.org.cn
|
||||
* https://visor.orionsec.cn
|
||||
*
|
||||
* Members:
|
||||
* Jiahang Li - ljh1553488six@139.com - author
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.dromara.visor.framework.config.core.ref;
|
||||
|
||||
import cn.orionsec.kit.lang.utils.Objects1;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.visor.common.config.ConfigRef;
|
||||
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 配置引用实现类
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2025/1/14 16:10
|
||||
*/
|
||||
@Slf4j
|
||||
public class ConfigRefImpl<T> extends ConfigRef<T> {
|
||||
|
||||
protected BiConsumer<T, T> changeEvent;
|
||||
|
||||
public ConfigRefImpl(String key, Function<String, T> convert) {
|
||||
super(key, convert);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void override(String value) {
|
||||
try {
|
||||
this.set(convert.apply(value));
|
||||
} catch (Exception e) {
|
||||
log.error("ConfigRef trigger override error key: {}, value: {}", key, value, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(T value) {
|
||||
T before = this.value;
|
||||
this.value = value;
|
||||
// 被修改
|
||||
if (!Objects1.eq(before, value)) {
|
||||
log.info("ConfigRef changed key: {}, value: {}", key, value);
|
||||
// 触发事件
|
||||
if (changeEvent != null) {
|
||||
changeEvent.accept(value, before);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigRef<T> onChange(BiConsumer<T, T> changeEvent) {
|
||||
this.changeEvent = changeEvent;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,10 +22,11 @@
|
||||
*/
|
||||
package org.dromara.visor.framework.config.core.store;
|
||||
|
||||
import org.dromara.visor.common.config.ConfigRef;
|
||||
import org.dromara.visor.framework.config.core.service.ConfigFrameworkService;
|
||||
import cn.orionsec.kit.lang.utils.collect.Lists;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dromara.visor.common.config.ConfigRef;
|
||||
import org.dromara.visor.framework.config.core.ref.ConfigRefImpl;
|
||||
import org.dromara.visor.framework.config.core.service.ConfigFrameworkService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -59,7 +60,7 @@ public class ManagementConfigStoreImpl implements ManagementConfigStore {
|
||||
|
||||
@Override
|
||||
public void override(String key, String value) {
|
||||
log.info("ManagementConfigStore.override key: {}, value: {}", key, value);
|
||||
log.info("ConfigStore.override key: {}, value: {}", key, value);
|
||||
// 修改配置
|
||||
configMap.put(key, value);
|
||||
// 修改引用
|
||||
@@ -72,11 +73,61 @@ public class ManagementConfigStoreImpl implements ManagementConfigStore {
|
||||
@Override
|
||||
public void register(ConfigRef<?> ref) {
|
||||
String key = ref.key;
|
||||
log.info("ManagementConfigStore.register ref key: {}", key);
|
||||
log.info("ConfigStore.register ref key: {}", key);
|
||||
// 注册引用
|
||||
configRefs.computeIfAbsent(key, k -> new ArrayList<>()).add(ref);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String key) {
|
||||
return this.getConfig(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String key, String defaultValue) {
|
||||
return this.getConfig(key, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getInteger(String key) {
|
||||
return this.getConfig(key, Integer::valueOf, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getInteger(String key, Integer defaultValue) {
|
||||
return this.getConfig(key, Integer::valueOf, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getLong(String key) {
|
||||
return this.getConfig(key, Long::valueOf, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getLong(String key, Long defaultValue) {
|
||||
return this.getConfig(key, Long::valueOf, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getDouble(String key) {
|
||||
return this.getConfig(key, Double::valueOf, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getDouble(String key, Double defaultValue) {
|
||||
return this.getConfig(key, Double::valueOf, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getBoolean(String key) {
|
||||
return this.getConfig(key, Boolean::valueOf, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getBoolean(String key, Boolean defaultValue) {
|
||||
return this.getConfig(key, Boolean::valueOf, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getConfig(String key) {
|
||||
return this.getConfig(key, Function.identity(), null);
|
||||
@@ -162,7 +213,7 @@ public class ManagementConfigStoreImpl implements ManagementConfigStore {
|
||||
@Override
|
||||
public <T> ConfigRef<T> ref(String key, Function<String, T> convert, T defaultValue) {
|
||||
// 创建引用
|
||||
ConfigRef<T> ref = new ConfigRef<>(key, convert);
|
||||
ConfigRef<T> ref = new ConfigRefImpl<>(key, convert);
|
||||
// 设置值
|
||||
String value = configMap.get(key);
|
||||
if (value != null) {
|
||||
|
||||
@@ -42,6 +42,111 @@ public class ConfigStores {
|
||||
private ConfigStores() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 string 配置
|
||||
*
|
||||
* @param key key
|
||||
* @return config
|
||||
*/
|
||||
public String getString(String key) {
|
||||
return delegate.getString(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 string 配置
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultValue defaultValue
|
||||
* @return config
|
||||
*/
|
||||
public String getString(String key, String defaultValue) {
|
||||
return delegate.getString(key, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 int 配置
|
||||
*
|
||||
* @param key key
|
||||
* @return config
|
||||
*/
|
||||
public Integer getInteger(String key) {
|
||||
return delegate.getInteger(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 int 配置
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultValue defaultValue
|
||||
* @return config
|
||||
*/
|
||||
public Integer getInteger(String key, Integer defaultValue) {
|
||||
return delegate.getInteger(key, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 long 配置
|
||||
*
|
||||
* @param key key
|
||||
* @return config
|
||||
*/
|
||||
public Long getLong(String key) {
|
||||
return delegate.getLong(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 long 配置
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultValue defaultValue
|
||||
* @return config
|
||||
*/
|
||||
public Long getLong(String key, Long defaultValue) {
|
||||
return delegate.getLong(key, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 double 配置
|
||||
*
|
||||
* @param key key
|
||||
* @return config
|
||||
*/
|
||||
public Double getDouble(String key) {
|
||||
return delegate.getDouble(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 double 配置
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultValue defaultValue
|
||||
* @return config
|
||||
*/
|
||||
public Double getDouble(String key, Double defaultValue) {
|
||||
return delegate.getDouble(key, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 boolean 配置
|
||||
*
|
||||
* @param key key
|
||||
* @return config
|
||||
*/
|
||||
public Boolean getBoolean(String key) {
|
||||
return delegate.getBoolean(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 boolean 配置
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultValue defaultValue
|
||||
* @return config
|
||||
*/
|
||||
public Boolean getBoolean(String key, Boolean defaultValue) {
|
||||
return delegate.getBoolean(key, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置
|
||||
*
|
||||
|
||||
@@ -25,6 +25,7 @@ package org.dromara.visor.framework.encrypt.core.impl;
|
||||
import cn.orionsec.kit.lang.utils.crypto.RSA;
|
||||
import org.dromara.visor.common.config.ConfigRef;
|
||||
import org.dromara.visor.common.config.ConfigStore;
|
||||
import org.dromara.visor.common.constant.ConfigKeys;
|
||||
import org.dromara.visor.common.interfaces.RsaEncryptor;
|
||||
|
||||
import java.security.interfaces.RSAPrivateKey;
|
||||
@@ -44,8 +45,8 @@ public class RsaEncryptorImpl implements RsaEncryptor {
|
||||
private final ConfigRef<RSAPrivateKey> privateKey;
|
||||
|
||||
public RsaEncryptorImpl(ConfigStore configStore) {
|
||||
this.publicKey = configStore.ref("encrypt.publicKey", RSA::getPublicKey);
|
||||
this.privateKey = configStore.ref("encrypt.privateKey", RSA::getPrivateKey);
|
||||
this.publicKey = configStore.ref(ConfigKeys.ENCRYPT_PUBLIC_KEY, RSA::getPublicKey);
|
||||
this.privateKey = configStore.ref(ConfigKeys.ENCRYPT_PRIVATE_KEY, RSA::getPrivateKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -22,10 +22,10 @@
|
||||
*/
|
||||
package org.dromara.visor.module.asset.handler.host.transfer.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import cn.orionsec.kit.lang.utils.time.Dates;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* sftp 文件备份参数
|
||||
@@ -35,9 +35,6 @@ import lombok.NoArgsConstructor;
|
||||
* @since 2024/4/15 23:13
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SftpFileBackupParams {
|
||||
|
||||
/**
|
||||
@@ -50,4 +47,16 @@ public class SftpFileBackupParams {
|
||||
*/
|
||||
private Long timestamp;
|
||||
|
||||
/**
|
||||
* 当前时间
|
||||
*/
|
||||
private String time;
|
||||
|
||||
public SftpFileBackupParams(String fileName) {
|
||||
this.fileName = fileName;
|
||||
Date date = new Date();
|
||||
this.timestamp = date.getTime();
|
||||
this.time = Dates.format(date, Dates.YMD_HMS2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ public class SftpUtils {
|
||||
SftpFile file = executor.getFile(path);
|
||||
if (file != null) {
|
||||
// 文件存在则备份
|
||||
SftpFileBackupParams backupParams = new SftpFileBackupParams(file.getName(), System.currentTimeMillis());
|
||||
SftpFileBackupParams backupParams = new SftpFileBackupParams(file.getName());
|
||||
String target = Strings.format(config.getBackupFileName(), JSON.parseObject(JSON.toJSONString(backupParams)));
|
||||
// 移动
|
||||
executor.move(path, target);
|
||||
|
||||
@@ -30,7 +30,7 @@ import org.dromara.visor.module.infra.entity.vo.SystemSettingAggregateVO;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 系统配置缓存 key
|
||||
* 系统设置缓存 key
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
|
||||
@@ -32,7 +32,7 @@ import org.dromara.visor.module.infra.handler.setting.model.SftpSystemSettingMod
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 系统配置类型枚举
|
||||
* 系统设置类型枚举
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
|
||||
@@ -28,7 +28,7 @@ import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 加密系统配置模型
|
||||
* 加密系统设置模型
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
|
||||
@@ -28,7 +28,7 @@ import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* SFTP 系统配置模型
|
||||
* SFTP 系统设置模型
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
|
||||
Reference in New Issue
Block a user