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.odboy.config.constant.ClientConfigConsts;
|
||||
import cn.odboy.config.constant.ClientConfigVars;
|
||||
import cn.odboy.config.model.msgtype.ClientInfo;
|
||||
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.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
|
@ -18,16 +26,6 @@ import org.springframework.core.env.ConfigurableEnvironment;
|
|||
import org.springframework.core.env.MapPropertySource;
|
||||
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 {
|
||||
private static final Logger logger = LoggerFactory.getLogger(ClientConfigLoader.class);
|
||||
|
||||
/**
|
||||
* 定时将配置写盘,缓存配置信息
|
||||
*/
|
||||
/** 定时将配置写盘,缓存配置信息 */
|
||||
private final Thread fixedTimeFlushConfigFileThread =
|
||||
ThreadUtil.newThread(
|
||||
() -> {
|
||||
|
@ -104,7 +100,10 @@ public class ClientConfigLoader {
|
|||
ThreadUtil.execAsync(
|
||||
() -> {
|
||||
try {
|
||||
ConfigClient.getInstance().start(ClientConfigConsts.clientInfo.getServer(), ClientConfigConsts.clientInfo.getPort());
|
||||
ConfigClient.getInstance()
|
||||
.start(
|
||||
ClientConfigConsts.clientInfo.getServer(),
|
||||
ClientConfigConsts.clientInfo.getPort());
|
||||
} catch (InterruptedException e) {
|
||||
logger.error("Netty客户端启动失败", e);
|
||||
throw new RuntimeException(e);
|
||||
|
@ -134,17 +133,20 @@ public class ClientConfigLoader {
|
|||
String fileName = file.getName();
|
||||
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();
|
||||
String suffix = FileUtil.getSuffix(file);
|
||||
boolean isYml = "yml".equals(suffix) || "yaml".equals(suffix);
|
||||
if (isYml) {
|
||||
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 {
|
||||
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);
|
||||
for (Map.Entry<Object, Object> kv : properties.entrySet()) {
|
||||
String key = (String) kv.getKey();
|
||||
|
@ -159,12 +161,14 @@ public class ClientConfigLoader {
|
|||
}
|
||||
// 合并配置项
|
||||
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) {
|
||||
ClientConfigVars.cacheConfigs.putAll(filename2Config.getValue());
|
||||
}
|
||||
MapPropertySource propertySource =
|
||||
new MapPropertySource(ClientConfigConsts.PROPERTY_SOURCE_NAME, ClientConfigVars.cacheConfigs);
|
||||
new MapPropertySource(
|
||||
ClientConfigConsts.PROPERTY_SOURCE_NAME, ClientConfigVars.cacheConfigs);
|
||||
environment.getPropertySources().addFirst(propertySource);
|
||||
}
|
||||
}
|
||||
|
@ -173,15 +177,28 @@ public class ClientConfigLoader {
|
|||
|
||||
private static void initClientInfo(String defaultCacheDir, ConfigurableEnvironment environment) {
|
||||
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(
|
||||
environment.getProperty(ClientConfigConsts.DEFAULT_CONFIG_NAME_PORT, Integer.class, ClientConfigConsts.DEFAULT_CONFIG_PORT));
|
||||
ClientConfigConsts. clientInfo.setEnv(
|
||||
environment.getProperty(ClientConfigConsts.DEFAULT_CONFIG_NAME_ENV, String.class, ClientConfigConsts.DEFAULT_CONFIG_ENV));
|
||||
environment.getProperty(
|
||||
ClientConfigConsts.DEFAULT_CONFIG_NAME_PORT,
|
||||
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(
|
||||
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(
|
||||
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() {
|
||||
|
@ -202,15 +219,16 @@ public class ClientConfigLoader {
|
|||
&& !cacheDir.contains(ClientConfigConsts.DEFAULT_PATH_WIN_SEP)) {
|
||||
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(
|
||||
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) {
|
||||
Path path = Paths.get(cacheDir);
|
||||
if (!Files.exists(path)) {
|
||||
|
|
Loading…
Reference in New Issue