upd: format
This commit is contained in:
parent
549294190a
commit
f65f995cc5
|
@ -5,8 +5,16 @@ import cn.hutool.core.thread.ThreadUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.odboy.config.constant.ClientConfigConsts;
|
import cn.odboy.config.constant.ClientConfigConsts;
|
||||||
import cn.odboy.config.constant.ClientConfigVars;
|
import cn.odboy.config.constant.ClientConfigVars;
|
||||||
import cn.odboy.config.model.msgtype.ClientInfo;
|
|
||||||
import cn.odboy.config.netty.ConfigClient;
|
import cn.odboy.config.netty.ConfigClient;
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
|
@ -18,16 +26,6 @@ import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.core.env.MapPropertySource;
|
import org.springframework.core.env.MapPropertySource;
|
||||||
import org.yaml.snakeyaml.Yaml;
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Properties;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置加载器
|
* 配置加载器
|
||||||
*
|
*
|
||||||
|
@ -38,9 +36,7 @@ import java.util.Set;
|
||||||
public class ClientConfigLoader {
|
public class ClientConfigLoader {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ClientConfigLoader.class);
|
private static final Logger logger = LoggerFactory.getLogger(ClientConfigLoader.class);
|
||||||
|
|
||||||
/**
|
/** 定时将配置写盘,缓存配置信息 */
|
||||||
* 定时将配置写盘,缓存配置信息
|
|
||||||
*/
|
|
||||||
private final Thread fixedTimeFlushConfigFileThread =
|
private final Thread fixedTimeFlushConfigFileThread =
|
||||||
ThreadUtil.newThread(
|
ThreadUtil.newThread(
|
||||||
() -> {
|
() -> {
|
||||||
|
@ -104,7 +100,10 @@ public class ClientConfigLoader {
|
||||||
ThreadUtil.execAsync(
|
ThreadUtil.execAsync(
|
||||||
() -> {
|
() -> {
|
||||||
try {
|
try {
|
||||||
ConfigClient.getInstance().start(ClientConfigConsts.clientInfo.getServer(), ClientConfigConsts.clientInfo.getPort());
|
ConfigClient.getInstance()
|
||||||
|
.start(
|
||||||
|
ClientConfigConsts.clientInfo.getServer(),
|
||||||
|
ClientConfigConsts.clientInfo.getPort());
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
logger.error("Netty客户端启动失败", e);
|
logger.error("Netty客户端启动失败", e);
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
@ -134,17 +133,20 @@ public class ClientConfigLoader {
|
||||||
String fileName = file.getName();
|
String fileName = file.getName();
|
||||||
ClientConfigVars.originConfigs.clear();
|
ClientConfigVars.originConfigs.clear();
|
||||||
// 配置原生内容
|
// 配置原生内容
|
||||||
ClientConfigVars.originConfigs.put(fileName, FileUtil.readString(file, StandardCharsets.UTF_8));
|
ClientConfigVars.originConfigs.put(
|
||||||
|
fileName, FileUtil.readString(file, StandardCharsets.UTF_8));
|
||||||
// 转换为应用能识别的配置项
|
// 转换为应用能识别的配置项
|
||||||
ClientConfigVars.lastConfigs.clear();
|
ClientConfigVars.lastConfigs.clear();
|
||||||
String suffix = FileUtil.getSuffix(file);
|
String suffix = FileUtil.getSuffix(file);
|
||||||
boolean isYml = "yml".equals(suffix) || "yaml".equals(suffix);
|
boolean isYml = "yml".equals(suffix) || "yaml".equals(suffix);
|
||||||
if (isYml) {
|
if (isYml) {
|
||||||
Yaml yaml = new Yaml();
|
Yaml yaml = new Yaml();
|
||||||
ClientConfigVars.lastConfigs.put(fileName, yaml.load(ClientConfigVars.originConfigs.get(fileName)));
|
ClientConfigVars.lastConfigs.put(
|
||||||
|
fileName, yaml.load(ClientConfigVars.originConfigs.get(fileName)));
|
||||||
} else {
|
} else {
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
properties.load(StrUtil.getReader(ClientConfigVars.originConfigs.get(fileName)));
|
properties.load(
|
||||||
|
StrUtil.getReader(ClientConfigVars.originConfigs.get(fileName)));
|
||||||
Map<String, Object> tempMap = new HashMap<>(1);
|
Map<String, Object> tempMap = new HashMap<>(1);
|
||||||
for (Map.Entry<Object, Object> kv : properties.entrySet()) {
|
for (Map.Entry<Object, Object> kv : properties.entrySet()) {
|
||||||
String key = (String) kv.getKey();
|
String key = (String) kv.getKey();
|
||||||
|
@ -159,12 +161,14 @@ public class ClientConfigLoader {
|
||||||
}
|
}
|
||||||
// 合并配置项
|
// 合并配置项
|
||||||
ClientConfigVars.cacheConfigs.clear();
|
ClientConfigVars.cacheConfigs.clear();
|
||||||
Set<Map.Entry<String, Map<String, Object>>> filename2ConfigMap = ClientConfigVars.lastConfigs.entrySet();
|
Set<Map.Entry<String, Map<String, Object>>> filename2ConfigMap =
|
||||||
|
ClientConfigVars.lastConfigs.entrySet();
|
||||||
for (Map.Entry<String, Map<String, Object>> filename2Config : filename2ConfigMap) {
|
for (Map.Entry<String, Map<String, Object>> filename2Config : filename2ConfigMap) {
|
||||||
ClientConfigVars.cacheConfigs.putAll(filename2Config.getValue());
|
ClientConfigVars.cacheConfigs.putAll(filename2Config.getValue());
|
||||||
}
|
}
|
||||||
MapPropertySource propertySource =
|
MapPropertySource propertySource =
|
||||||
new MapPropertySource(ClientConfigConsts.PROPERTY_SOURCE_NAME, ClientConfigVars.cacheConfigs);
|
new MapPropertySource(
|
||||||
|
ClientConfigConsts.PROPERTY_SOURCE_NAME, ClientConfigVars.cacheConfigs);
|
||||||
environment.getPropertySources().addFirst(propertySource);
|
environment.getPropertySources().addFirst(propertySource);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -173,15 +177,28 @@ public class ClientConfigLoader {
|
||||||
|
|
||||||
private static void initClientInfo(String defaultCacheDir, ConfigurableEnvironment environment) {
|
private static void initClientInfo(String defaultCacheDir, ConfigurableEnvironment environment) {
|
||||||
ClientConfigConsts.clientInfo.setServer(
|
ClientConfigConsts.clientInfo.setServer(
|
||||||
environment.getProperty(ClientConfigConsts.DEFAULT_CONFIG_NAME_SERVER, String.class, ClientConfigConsts.DEFAULT_CONFIG_SERVER));
|
environment.getProperty(
|
||||||
|
ClientConfigConsts.DEFAULT_CONFIG_NAME_SERVER,
|
||||||
|
String.class,
|
||||||
|
ClientConfigConsts.DEFAULT_CONFIG_SERVER));
|
||||||
ClientConfigConsts.clientInfo.setPort(
|
ClientConfigConsts.clientInfo.setPort(
|
||||||
environment.getProperty(ClientConfigConsts.DEFAULT_CONFIG_NAME_PORT, Integer.class, ClientConfigConsts.DEFAULT_CONFIG_PORT));
|
environment.getProperty(
|
||||||
ClientConfigConsts. clientInfo.setEnv(
|
ClientConfigConsts.DEFAULT_CONFIG_NAME_PORT,
|
||||||
environment.getProperty(ClientConfigConsts.DEFAULT_CONFIG_NAME_ENV, String.class, ClientConfigConsts.DEFAULT_CONFIG_ENV));
|
Integer.class,
|
||||||
|
ClientConfigConsts.DEFAULT_CONFIG_PORT));
|
||||||
|
ClientConfigConsts.clientInfo.setEnv(
|
||||||
|
environment.getProperty(
|
||||||
|
ClientConfigConsts.DEFAULT_CONFIG_NAME_ENV,
|
||||||
|
String.class,
|
||||||
|
ClientConfigConsts.DEFAULT_CONFIG_ENV));
|
||||||
ClientConfigConsts.clientInfo.setDataId(
|
ClientConfigConsts.clientInfo.setDataId(
|
||||||
environment.getProperty(ClientConfigConsts.DEFAULT_CONFIG_NAME_DATA_ID, String.class, ClientConfigConsts.DEFAULT_CONFIG_DATA_ID));
|
environment.getProperty(
|
||||||
|
ClientConfigConsts.DEFAULT_CONFIG_NAME_DATA_ID,
|
||||||
|
String.class,
|
||||||
|
ClientConfigConsts.DEFAULT_CONFIG_DATA_ID));
|
||||||
ClientConfigConsts.clientInfo.setCacheDir(
|
ClientConfigConsts.clientInfo.setCacheDir(
|
||||||
environment.getProperty(ClientConfigConsts.DEFAULT_CONFIG_NAME_CACHE_DIR, String.class, defaultCacheDir));
|
environment.getProperty(
|
||||||
|
ClientConfigConsts.DEFAULT_CONFIG_NAME_CACHE_DIR, String.class, defaultCacheDir));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getDefaultCacheDir() {
|
private static String getDefaultCacheDir() {
|
||||||
|
@ -202,15 +219,16 @@ public class ClientConfigLoader {
|
||||||
&& !cacheDir.contains(ClientConfigConsts.DEFAULT_PATH_WIN_SEP)) {
|
&& !cacheDir.contains(ClientConfigConsts.DEFAULT_PATH_WIN_SEP)) {
|
||||||
throw new RuntimeException(ClientConfigConsts.DEFAULT_CONFIG_NAME_CACHE_DIR + " 配置的路径不正确");
|
throw new RuntimeException(ClientConfigConsts.DEFAULT_CONFIG_NAME_CACHE_DIR + " 配置的路径不正确");
|
||||||
}
|
}
|
||||||
if (cacheDir.contains(ClientConfigConsts.DEFAULT_PATH_WIN_SEP) && !cacheDir.contains(ClientConfigConsts.DEFAULT_PATH_SEP_WIN)) {
|
if (cacheDir.contains(ClientConfigConsts.DEFAULT_PATH_WIN_SEP)
|
||||||
|
&& !cacheDir.contains(ClientConfigConsts.DEFAULT_PATH_SEP_WIN)) {
|
||||||
throw new RuntimeException(
|
throw new RuntimeException(
|
||||||
ClientConfigConsts.DEFAULT_CONFIG_NAME_CACHE_DIR + " 配置的路径不正确, 正确的路径示范, " + ClientConfigConsts.DEFAULT_PATH_WIN);
|
ClientConfigConsts.DEFAULT_CONFIG_NAME_CACHE_DIR
|
||||||
|
+ " 配置的路径不正确, 正确的路径示范, "
|
||||||
|
+ ClientConfigConsts.DEFAULT_PATH_WIN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** 创建缓存文件夹 */
|
||||||
* 创建缓存文件夹
|
|
||||||
*/
|
|
||||||
private static void createCacheDir(String cacheDir) {
|
private static void createCacheDir(String cacheDir) {
|
||||||
Path path = Paths.get(cacheDir);
|
Path path = Paths.get(cacheDir);
|
||||||
if (!Files.exists(path)) {
|
if (!Files.exists(path)) {
|
||||||
|
|
Loading…
Reference in New Issue