🔨 优化配置中心逻辑.

This commit is contained in:
lijiahang
2025-01-15 10:16:36 +08:00
parent 51392e09e2
commit e2a645d1e1
13 changed files with 418 additions and 32 deletions

View File

@@ -24,6 +24,7 @@ package org.dromara.visor.common.config;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.function.BiConsumer;
import java.util.function.Function; import java.util.function.Function;
/** /**
@@ -34,14 +35,14 @@ import java.util.function.Function;
* @since 2025/1/6 18:01 * @since 2025/1/6 18:01
*/ */
@Slf4j @Slf4j
public class ConfigRef<T> { public abstract class ConfigRef<T> {
public final String key; public final String key;
private final Function<String, T> convert;
public T value; public T value;
protected final Function<String, T> convert;
public ConfigRef(String key, Function<String, T> convert) { public ConfigRef(String key, Function<String, T> convert) {
this.key = key; this.key = key;
this.convert = convert; this.convert = convert;
@@ -52,21 +53,28 @@ public class ConfigRef<T> {
* *
* @param value value * @param value value
*/ */
public void override(String value) { public abstract void override(String value);
try {
this.value = convert.apply(value);
} catch (Exception e) {
log.error("ConfigRef trigger override error key: {}, value: {}", key, value, e);
}
}
/** /**
* 设置值 * 修改配置
* *
* @param value value * @param value value
*/ */
public void set(T value) { public abstract void set(T value);
this.value = value;
} /**
* 获取配置
*
* @return value
*/
public abstract T get();
/**
* 修改回调
*
* @param changeEvent changeEvent
* @return this
*/
public abstract ConfigRef<T> onChange(BiConsumer<T, T> changeEvent);
} }

View File

@@ -33,6 +33,91 @@ import java.util.function.Function;
*/ */
public interface ConfigStore { 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 key key
* @param convert convert
* @param defaultValue defaultValue * @param defaultValue defaultValue
* @param <T> T * @param <T> T
* @return conf * @return conf

View File

@@ -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";
}

View File

@@ -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;
}
}

View File

@@ -22,10 +22,11 @@
*/ */
package org.dromara.visor.framework.config.core.store; 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 cn.orionsec.kit.lang.utils.collect.Lists;
import lombok.extern.slf4j.Slf4j; 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.ArrayList;
import java.util.List; import java.util.List;
@@ -59,7 +60,7 @@ public class ManagementConfigStoreImpl implements ManagementConfigStore {
@Override @Override
public void override(String key, String value) { 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); configMap.put(key, value);
// 修改引用 // 修改引用
@@ -72,11 +73,61 @@ public class ManagementConfigStoreImpl implements ManagementConfigStore {
@Override @Override
public void register(ConfigRef<?> ref) { public void register(ConfigRef<?> ref) {
String key = ref.key; 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); 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 @Override
public String getConfig(String key) { public String getConfig(String key) {
return this.getConfig(key, Function.identity(), null); return this.getConfig(key, Function.identity(), null);
@@ -162,7 +213,7 @@ public class ManagementConfigStoreImpl implements ManagementConfigStore {
@Override @Override
public <T> ConfigRef<T> ref(String key, Function<String, T> convert, T defaultValue) { 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); String value = configMap.get(key);
if (value != null) { if (value != null) {

View File

@@ -42,6 +42,111 @@ public class ConfigStores {
private 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);
}
/** /**
* 获取配置 * 获取配置
* *

View File

@@ -25,6 +25,7 @@ package org.dromara.visor.framework.encrypt.core.impl;
import cn.orionsec.kit.lang.utils.crypto.RSA; import cn.orionsec.kit.lang.utils.crypto.RSA;
import org.dromara.visor.common.config.ConfigRef; import org.dromara.visor.common.config.ConfigRef;
import org.dromara.visor.common.config.ConfigStore; import org.dromara.visor.common.config.ConfigStore;
import org.dromara.visor.common.constant.ConfigKeys;
import org.dromara.visor.common.interfaces.RsaEncryptor; import org.dromara.visor.common.interfaces.RsaEncryptor;
import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPrivateKey;
@@ -44,8 +45,8 @@ public class RsaEncryptorImpl implements RsaEncryptor {
private final ConfigRef<RSAPrivateKey> privateKey; private final ConfigRef<RSAPrivateKey> privateKey;
public RsaEncryptorImpl(ConfigStore configStore) { public RsaEncryptorImpl(ConfigStore configStore) {
this.publicKey = configStore.ref("encrypt.publicKey", RSA::getPublicKey); this.publicKey = configStore.ref(ConfigKeys.ENCRYPT_PUBLIC_KEY, RSA::getPublicKey);
this.privateKey = configStore.ref("encrypt.privateKey", RSA::getPrivateKey); this.privateKey = configStore.ref(ConfigKeys.ENCRYPT_PRIVATE_KEY, RSA::getPrivateKey);
} }
@Override @Override

View File

@@ -22,10 +22,10 @@
*/ */
package org.dromara.visor.module.asset.handler.host.transfer.model; package org.dromara.visor.module.asset.handler.host.transfer.model;
import lombok.AllArgsConstructor; import cn.orionsec.kit.lang.utils.time.Dates;
import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/** /**
* sftp 文件备份参数 * sftp 文件备份参数
@@ -35,9 +35,6 @@ import lombok.NoArgsConstructor;
* @since 2024/4/15 23:13 * @since 2024/4/15 23:13
*/ */
@Data @Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SftpFileBackupParams { public class SftpFileBackupParams {
/** /**
@@ -50,4 +47,16 @@ public class SftpFileBackupParams {
*/ */
private Long timestamp; 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);
}
} }

View File

@@ -58,7 +58,7 @@ public class SftpUtils {
SftpFile file = executor.getFile(path); SftpFile file = executor.getFile(path);
if (file != null) { 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))); String target = Strings.format(config.getBackupFileName(), JSON.parseObject(JSON.toJSONString(backupParams)));
// 移动 // 移动
executor.move(path, target); executor.move(path, target);

View File

@@ -30,7 +30,7 @@ import org.dromara.visor.module.infra.entity.vo.SystemSettingAggregateVO;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
* 系统置缓存 key * 系统置缓存 key
* *
* @author Jiahang Li * @author Jiahang Li
* @version 1.0.0 * @version 1.0.0

View File

@@ -32,7 +32,7 @@ import org.dromara.visor.module.infra.handler.setting.model.SftpSystemSettingMod
import java.util.Map; import java.util.Map;
/** /**
* 系统置类型枚举 * 系统置类型枚举
* *
* @author Jiahang Li * @author Jiahang Li
* @version 1.0.0 * @version 1.0.0

View File

@@ -28,7 +28,7 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
/** /**
* 加密系统置模型 * 加密系统置模型
* *
* @author Jiahang Li * @author Jiahang Li
* @version 1.0.0 * @version 1.0.0

View File

@@ -28,7 +28,7 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
/** /**
* SFTP 系统置模型 * SFTP 系统置模型
* *
* @author Jiahang Li * @author Jiahang Li
* @version 1.0.0 * @version 1.0.0