feat: 新增配置信息上下文.
This commit is contained in:
@@ -24,4 +24,9 @@ public final class ConfigurationKeys {
|
|||||||
* 定时任务周期: 获取最新IP
|
* 定时任务周期: 获取最新IP
|
||||||
*/
|
*/
|
||||||
public static final String KEY_TASK_REFRESH_INTERVAL_IP = "system.task.refresh.interval.ip";
|
public static final String KEY_TASK_REFRESH_INTERVAL_IP = "system.task.refresh.interval.ip";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阿里云解析线路
|
||||||
|
*/
|
||||||
|
public static final String KEY_ALIYUN_ENDPOINT = "instance.aliyun.endpoint.url";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,15 +8,15 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
|||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.json.JsonMapper;
|
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
|
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
|
||||||
|
import com.serliunx.ddns.support.ConfigurationContextHolder;
|
||||||
import darabonba.core.client.ClientOverrideConfiguration;
|
import darabonba.core.client.ClientOverrideConfiguration;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
import static com.serliunx.ddns.config.ConfigurationKeys.KEY_ALIYUN_ENDPOINT;
|
||||||
import static com.serliunx.ddns.constant.SystemConstants.XML_ROOT_INSTANCE_NAME;
|
import static com.serliunx.ddns.constant.SystemConstants.XML_ROOT_INSTANCE_NAME;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,7 +25,6 @@ import static com.serliunx.ddns.constant.SystemConstants.XML_ROOT_INSTANCE_NAME;
|
|||||||
* @version 1.0.0
|
* @version 1.0.0
|
||||||
* @since 2024/5/15
|
* @since 2024/5/15
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("all")
|
|
||||||
@JacksonXmlRootElement(localName = XML_ROOT_INSTANCE_NAME)
|
@JacksonXmlRootElement(localName = XML_ROOT_INSTANCE_NAME)
|
||||||
public class AliyunInstance extends AbstractInstance {
|
public class AliyunInstance extends AbstractInstance {
|
||||||
|
|
||||||
@@ -96,10 +95,10 @@ public class AliyunInstance extends AbstractInstance {
|
|||||||
.credentialsProvider(provider)
|
.credentialsProvider(provider)
|
||||||
.overrideConfiguration(
|
.overrideConfiguration(
|
||||||
ClientOverrideConfiguration.create()
|
ClientOverrideConfiguration.create()
|
||||||
.setEndpointOverride("alidns.cn-hangzhou.aliyuncs.com")
|
.setEndpointOverride(ConfigurationContextHolder.getConfiguration().getString(KEY_ALIYUN_ENDPOINT))
|
||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
log.debug(getName() + ": 初始化完成.");
|
log.debug("{}: 初始化完成.", getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -115,7 +114,7 @@ public class AliyunInstance extends AbstractInstance {
|
|||||||
try {
|
try {
|
||||||
requestResponse.whenComplete((v, t) -> {
|
requestResponse.whenComplete((v, t) -> {
|
||||||
if (t != null) { //出现异常
|
if (t != null) { //出现异常
|
||||||
log.error("出现异常 {}:", t.getCause(), t.getMessage());
|
log.error("出现异常 {} : {}", t.getCause(), t.getMessage());
|
||||||
} else {
|
} else {
|
||||||
String result = null;
|
String result = null;
|
||||||
try {
|
try {
|
||||||
@@ -145,7 +144,7 @@ public class AliyunInstance extends AbstractInstance {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
} catch (InterruptedException | ExecutionException e) {
|
||||||
log.error("出现了不应该出现的异常 => {}", e);
|
log.error("出现了不应该出现的异常 => {}", e.getMessage());
|
||||||
return null;
|
return null;
|
||||||
} catch (TimeoutException e) {
|
} catch (TimeoutException e) {
|
||||||
log.error("记录查询超时!");
|
log.error("记录查询超时!");
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.serliunx.ddns.support;
|
||||||
|
|
||||||
|
import com.serliunx.ddns.config.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置信息上下文
|
||||||
|
* <li> {@link Configuration}
|
||||||
|
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||||
|
* @version 1.0.1
|
||||||
|
* @since 2024/7/8
|
||||||
|
*/
|
||||||
|
public final class ConfigurationContextHolder {
|
||||||
|
|
||||||
|
private static final ThreadLocal<Configuration> CONFIGURATION_HOLDER = new ThreadLocal<>();
|
||||||
|
|
||||||
|
private ConfigurationContextHolder() {throw new UnsupportedOperationException();}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取配置信息
|
||||||
|
*
|
||||||
|
* @return 配置信息
|
||||||
|
*/
|
||||||
|
public static Configuration getConfiguration() {
|
||||||
|
return CONFIGURATION_HOLDER.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置配置信息
|
||||||
|
* <li> 一般在程序初始化时调用
|
||||||
|
*
|
||||||
|
* @param configuration 配置信息
|
||||||
|
*/
|
||||||
|
public static void setConfiguration(Configuration configuration) {
|
||||||
|
CONFIGURATION_HOLDER.set(configuration);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -73,6 +73,7 @@ public final class SystemInitializer implements Refreshable, Clearable {
|
|||||||
|
|
||||||
// 刷新配置信息
|
// 刷新配置信息
|
||||||
configuration.refresh();
|
configuration.refresh();
|
||||||
|
ConfigurationContextHolder.setConfiguration(configuration);
|
||||||
|
|
||||||
// 获取核心线程数量, 默认为CPU核心数量
|
// 获取核心线程数量, 默认为CPU核心数量
|
||||||
int coreSize = configuration.getInteger(KEY_THREAD_POOL_CORE_SIZE, Runtime.getRuntime().availableProcessors());
|
int coreSize = configuration.getInteger(KEY_THREAD_POOL_CORE_SIZE, Runtime.getRuntime().availableProcessors());
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
system.cfg.log.onstart=true
|
system.cfg.log.onstart=true
|
||||||
system.pool.core.size=4
|
system.pool.core.size=4
|
||||||
system.task.refresh.interval.ip=300
|
system.task.refresh.interval.ip=300
|
||||||
|
instance.aliyun.endpoint.url=alidns.cn-hangzhou.aliyuncs.com
|
||||||
Reference in New Issue
Block a user