🔨 优化配置中心逻辑.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user