feat: 日志配置文件
This commit is contained in:
@@ -41,6 +41,11 @@ public final class SystemConstants {
|
|||||||
*/
|
*/
|
||||||
public static final String PROPERTIES_FILE = "settings.properties";
|
public static final String PROPERTIES_FILE = "settings.properties";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志配置文件名称
|
||||||
|
*/
|
||||||
|
public static final String LOG_CONFIG_FILE = "logback.xml";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sqlite
|
* sqlite
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -17,8 +17,11 @@ import java.io.OutputStream;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@@ -39,14 +42,14 @@ public final class SystemInitializer implements Refreshable {
|
|||||||
|
|
||||||
private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;
|
private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;
|
||||||
private Set<Instance> instances;
|
private Set<Instance> instances;
|
||||||
private Map<String, Instance> runningInstances;
|
private final Map<String, ScheduledFuture<?>> runningInstances = new HashMap<>(64);
|
||||||
|
|
||||||
SystemInitializer(Configuration configuration, MultipleSourceInstanceContext instanceContext) {
|
SystemInitializer(Configuration configuration, MultipleSourceInstanceContext instanceContext) {
|
||||||
this.configuration = configuration;
|
this.configuration = configuration;
|
||||||
this.instanceContext = instanceContext;
|
this.instanceContext = instanceContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Configurer configurer(){
|
public static Configurer configurer() {
|
||||||
return new Configurer();
|
return new Configurer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,6 +59,8 @@ public final class SystemInitializer implements Refreshable {
|
|||||||
|
|
||||||
// 释放配置文件
|
// 释放配置文件
|
||||||
releaseResource(SystemConstants.PROPERTIES_FILE);
|
releaseResource(SystemConstants.PROPERTIES_FILE);
|
||||||
|
// 释放日志文件
|
||||||
|
releaseResource(SystemConstants.LOG_CONFIG_FILE);
|
||||||
|
|
||||||
// 刷新配置信息
|
// 刷新配置信息
|
||||||
configuration.refresh();
|
configuration.refresh();
|
||||||
@@ -66,6 +71,9 @@ public final class SystemInitializer implements Refreshable {
|
|||||||
// 初始化线程池
|
// 初始化线程池
|
||||||
initThreadPool(coreSize);
|
initThreadPool(coreSize);
|
||||||
|
|
||||||
|
// 尝试链接dashboard
|
||||||
|
registerToDashboard();
|
||||||
|
|
||||||
// 加载实例(不同的容器加载时机不同)
|
// 加载实例(不同的容器加载时机不同)
|
||||||
loadInstances();
|
loadInstances();
|
||||||
|
|
||||||
@@ -92,7 +100,7 @@ public final class SystemInitializer implements Refreshable {
|
|||||||
log.info("载入 {} 个实例.", instances.size());
|
log.info("载入 {} 个实例.", instances.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void releaseResource(String resourceName){
|
private void releaseResource(String resourceName) {
|
||||||
ClassLoader classLoader = SystemConstants.class.getClassLoader();
|
ClassLoader classLoader = SystemConstants.class.getClassLoader();
|
||||||
Path path = Paths.get(SystemConstants.USER_DIR + File.separator + resourceName);
|
Path path = Paths.get(SystemConstants.USER_DIR + File.separator + resourceName);
|
||||||
// 检查文件是否已存在
|
// 检查文件是否已存在
|
||||||
@@ -106,13 +114,13 @@ public final class SystemInitializer implements Refreshable {
|
|||||||
OutputStream outputStream = Files.newOutputStream(path);
|
OutputStream outputStream = Files.newOutputStream(path);
|
||||||
byte[] buffer = new byte[1024];
|
byte[] buffer = new byte[1024];
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
if(inputStream != null){
|
if(inputStream != null) {
|
||||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||||
outputStream.write(buffer, 0, bytesRead);
|
outputStream.write(buffer, 0, bytesRead);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
}catch (Exception e){
|
} catch (Exception e) {
|
||||||
log.error("文件 {} 解压失败!, 原因: {}", resourceName, e.getMessage());
|
log.error("文件 {} 解压失败!, 原因: {}", resourceName, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -122,18 +130,20 @@ public final class SystemInitializer implements Refreshable {
|
|||||||
Assert.notNull(instances);
|
Assert.notNull(instances);
|
||||||
|
|
||||||
for (Instance i : instances) {
|
for (Instance i : instances) {
|
||||||
if(!i.validate()){
|
if (!i.validate()) {
|
||||||
log.error("实例{}({})参数校验不通过, 将不会被运行.", i.getName(), i.getType());
|
log.error("实例{}({})参数校验不通过, 将不会被运行.", i.getName(), i.getType());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// 初始化实例
|
// 初始化实例
|
||||||
i.refresh();
|
i.refresh();
|
||||||
scheduledThreadPoolExecutor.scheduleWithFixedDelay(i, 0, i.getInterval(), TimeUnit.SECONDS);
|
ScheduledFuture<?> future = scheduledThreadPoolExecutor.scheduleWithFixedDelay(i, 0,
|
||||||
|
i.getInterval(), TimeUnit.SECONDS);
|
||||||
|
runningInstances.put(i.getName(), future);
|
||||||
log.info("{}({})已启动, 运行周期 {} 秒.", i.getName(), i.getType(), i.getInterval());
|
log.info("{}({})已启动, 运行周期 {} 秒.", i.getName(), i.getType(), i.getInterval());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initThreadPool(int coreSize){
|
private void initThreadPool(int coreSize) {
|
||||||
Assert.isLargerThan(coreSize, 1);
|
Assert.isLargerThan(coreSize, 1);
|
||||||
scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(coreSize, new TaskThreadFactory());
|
scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(coreSize, new TaskThreadFactory());
|
||||||
|
|
||||||
@@ -146,7 +156,7 @@ public final class SystemInitializer implements Refreshable {
|
|||||||
IPAddressResponse response = IPAddressClient.instance.getIPAddress();
|
IPAddressResponse response = IPAddressClient.instance.getIPAddress();
|
||||||
String ip;
|
String ip;
|
||||||
if(response != null
|
if(response != null
|
||||||
&& (ip = response.getQuery()) != null){
|
&& (ip = response.getQuery()) != null) {
|
||||||
NetworkContextHolder.setIpAddress(ip);
|
NetworkContextHolder.setIpAddress(ip);
|
||||||
log.info("本机最新公网IP地址 => {}", ip);
|
log.info("本机最新公网IP地址 => {}", ip);
|
||||||
}
|
}
|
||||||
@@ -161,4 +171,12 @@ public final class SystemInitializer implements Refreshable {
|
|||||||
log.info("已关闭.");
|
log.info("已关闭.");
|
||||||
}, "DDNS-ShutDownHook"));
|
}, "DDNS-ShutDownHook"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void registerToDashboard() {
|
||||||
|
CompletableFuture.runAsync(() -> {
|
||||||
|
|
||||||
|
}, scheduledThreadPoolExecutor).whenComplete((r, t) -> {
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,10 @@ public class TaskThreadFactory implements ThreadFactory {
|
|||||||
MDC.put("pid", SystemSupport.getPid());
|
MDC.put("pid", SystemSupport.getPid());
|
||||||
r.run();
|
r.run();
|
||||||
};
|
};
|
||||||
return new Thread(runnable, String.format("ddns-task-%s", count.getAndIncrement()));
|
return new Thread(runnable, String.format(getNamePattern(), count.getAndIncrement()));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getNamePattern() {
|
||||||
|
return "ddns-task-%s";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.serliunx.ddns.thread;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author SerLiunx
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
|
public class UtilThreadFactory extends TaskThreadFactory {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getNamePattern() {
|
||||||
|
return "ddns-util-%s";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
package com.serliunx.ddns.test;
|
package com.serliunx.ddns.test;
|
||||||
|
|
||||||
import com.serliunx.ddns.constant.SystemConstants;
|
import com.serliunx.ddns.constant.SystemConstants;
|
||||||
|
import com.serliunx.ddns.core.context.FileInstanceContext;
|
||||||
import com.serliunx.ddns.core.context.GenericInstanceContext;
|
import com.serliunx.ddns.core.context.GenericInstanceContext;
|
||||||
|
import com.serliunx.ddns.core.context.MultipleSourceInstanceContext;
|
||||||
import com.serliunx.ddns.core.factory.JsonFileInstanceFactory;
|
import com.serliunx.ddns.core.factory.JsonFileInstanceFactory;
|
||||||
import com.serliunx.ddns.core.factory.XmlFileInstanceFactory;
|
import com.serliunx.ddns.core.factory.XmlFileInstanceFactory;
|
||||||
import com.serliunx.ddns.core.factory.YamlFileInstanceFactory;
|
import com.serliunx.ddns.core.factory.YamlFileInstanceFactory;
|
||||||
@@ -14,7 +16,7 @@ import org.junit.Test;
|
|||||||
public class ContextTest {
|
public class ContextTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testContext() {
|
public void testGenericContext() {
|
||||||
GenericInstanceContext genericInstanceContext = new GenericInstanceContext();
|
GenericInstanceContext genericInstanceContext = new GenericInstanceContext();
|
||||||
|
|
||||||
genericInstanceContext.addListableInstanceFactory(new XmlFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR));
|
genericInstanceContext.addListableInstanceFactory(new XmlFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR));
|
||||||
@@ -24,4 +26,11 @@ public class ContextTest {
|
|||||||
genericInstanceContext.refresh();
|
genericInstanceContext.refresh();
|
||||||
genericInstanceContext.getInstances().forEach(System.out::println);
|
genericInstanceContext.getInstances().forEach(System.out::println);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFileContext(){
|
||||||
|
MultipleSourceInstanceContext context = new FileInstanceContext();
|
||||||
|
|
||||||
|
context.getInstances().forEach(System.out::println);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user