From 4a5107653f2e4e2927f24bee57794b7fa8954c1b Mon Sep 17 00:00:00 2001 From: odboy Date: Tue, 3 Dec 2024 22:22:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=9B=E5=BB=BA=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../context/ConfigCenterConfigLoader.java | 65 +++++++++++++++---- .../config/netty/ConfigCenterClient.java | 4 +- .../resources/config/application-daily.yml | 2 +- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/kenaito-config-core/src/main/java/cn/odboy/config/context/ConfigCenterConfigLoader.java b/kenaito-config-core/src/main/java/cn/odboy/config/context/ConfigCenterConfigLoader.java index 4be32e3..e25bcd4 100644 --- a/kenaito-config-core/src/main/java/cn/odboy/config/context/ConfigCenterConfigLoader.java +++ b/kenaito-config-core/src/main/java/cn/odboy/config/context/ConfigCenterConfigLoader.java @@ -1,5 +1,6 @@ package cn.odboy.config.context; +import cn.hutool.core.io.FileUtil; import cn.odboy.config.ConfigCenterProperties; import cn.odboy.config.netty.ConfigCenterClient; import lombok.extern.slf4j.Slf4j; @@ -11,6 +12,11 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.ConfigurableEnvironment; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + /** * 配置加载器 * @@ -22,7 +28,7 @@ import org.springframework.core.env.ConfigurableEnvironment; public class ConfigCenterConfigLoader { private static final String OS_TYPE_WIN = "win"; private static final String OS_TYPE_MAC = "mac"; - private static final String DEFAULT_PATH_WIN = "c:/data"; + private static final String DEFAULT_PATH_WIN = "c:\\data"; private static final String DEFAULT_PATH_MAC = "/home/admin/data"; private static final String DEFAULT_CONFIG_SERVER = "127.0.0.1"; private static final Integer DEFAULT_CONFIG_PORT = 28002; @@ -37,6 +43,11 @@ public class ConfigCenterConfigLoader { private static final String DEFAULT_CONFIG_NAME_ENV = "kenaito.config-center.env"; private static final String DEFAULT_CONFIG_NAME_DATA_ID = "kenaito.config-center.data-id"; private static final String DEFAULT_CONFIG_NAME_CACHE_DIR = "kenaito.config-center.cache-dir"; + /** + * 路径分割符 + */ + private static final String DEFAULT_PATH_SEP_WIN = "\\"; + private static final String DEFAULT_PATH_SEP_MAC = "/"; @Autowired private ConfigCenterProperties properties; @@ -45,26 +56,17 @@ public class ConfigCenterConfigLoader { return new BeanFactoryPostProcessor() { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { - String defaultCacheDir; - String os = System.getProperty("os.name"); - if (os.toLowerCase().startsWith(OS_TYPE_WIN)) { - defaultCacheDir = DEFAULT_PATH_WIN; - } else if (os.toLowerCase().startsWith(OS_TYPE_MAC)) { - defaultCacheDir = DEFAULT_PATH_MAC; - } else { - defaultCacheDir = DEFAULT_PATH_MAC; - } + String defaultCacheDir = getDefaultCacheDir(); String server = environment.getProperty(DEFAULT_CONFIG_NAME_SERVER, String.class, DEFAULT_CONFIG_SERVER); Integer port = environment.getProperty(DEFAULT_CONFIG_NAME_PORT, Integer.class, DEFAULT_CONFIG_PORT); String env = environment.getProperty(DEFAULT_CONFIG_NAME_ENV, String.class, DEFAULT_CONFIG_ENV); String dataId = environment.getProperty(DEFAULT_CONFIG_NAME_DATA_ID, String.class, DEFAULT_CONFIG_DATA_ID); String cacheDir = environment.getProperty(DEFAULT_CONFIG_NAME_CACHE_DIR, String.class, defaultCacheDir); - if (defaultCacheDir.contains(DEFAULT_PATH_WIN_SEP) && !cacheDir.contains(DEFAULT_PATH_WIN_SEP)) { - throw new RuntimeException(DEFAULT_CONFIG_NAME_CACHE_DIR + " 配置的路径不正确"); - } + validateCacheDirPath(defaultCacheDir, cacheDir); + createCacheDir(cacheDir); ConfigCenterClient client = new ConfigCenterClient(); try { - client.start(properties); + client.start(server, port); } catch (InterruptedException e) { log.error("Netty Client Start Error", e); throw new RuntimeException(e); @@ -76,4 +78,39 @@ public class ConfigCenterConfigLoader { } }; } + + private static String getDefaultCacheDir() { + String defaultCacheDir; + String os = System.getProperty("os.name"); + if (os.toLowerCase().startsWith(OS_TYPE_WIN)) { + defaultCacheDir = DEFAULT_PATH_WIN; + } else if (os.toLowerCase().startsWith(OS_TYPE_MAC)) { + defaultCacheDir = DEFAULT_PATH_MAC; + } else { + defaultCacheDir = DEFAULT_PATH_MAC; + } + return defaultCacheDir; + } + + private static void validateCacheDirPath(String defaultCacheDir, String cacheDir) { + if (defaultCacheDir.contains(DEFAULT_PATH_WIN_SEP) && !cacheDir.contains(DEFAULT_PATH_WIN_SEP)) { + throw new RuntimeException(DEFAULT_CONFIG_NAME_CACHE_DIR + " 配置的路径不正确"); + } + if (cacheDir.contains(DEFAULT_PATH_WIN_SEP) && !cacheDir.contains(DEFAULT_PATH_SEP_WIN)) { + throw new RuntimeException(DEFAULT_CONFIG_NAME_CACHE_DIR + " 配置的路径不正确, 正确的路径示范, " + DEFAULT_PATH_WIN); + } + } + + /** + * 创建缓存文件夹 + */ + private static void createCacheDir(String cacheDir) { + Path path = Paths.get(cacheDir); + if (!Files.exists(path)) { + File mkdir = FileUtil.mkdir(cacheDir); + if (!mkdir.canWrite()) { + throw new RuntimeException("缓存文件夹创建失败, 无读写权限"); + } + } + } } diff --git a/kenaito-config-core/src/main/java/cn/odboy/config/netty/ConfigCenterClient.java b/kenaito-config-core/src/main/java/cn/odboy/config/netty/ConfigCenterClient.java index 15d7f4e..8dac3a3 100644 --- a/kenaito-config-core/src/main/java/cn/odboy/config/netty/ConfigCenterClient.java +++ b/kenaito-config-core/src/main/java/cn/odboy/config/netty/ConfigCenterClient.java @@ -12,7 +12,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class ConfigCenterClient { - public void start(ConfigCenterProperties properties) throws InterruptedException { + public void start(String server, Integer port) throws InterruptedException { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap bootstrap = new Bootstrap(); @@ -25,7 +25,7 @@ public class ConfigCenterClient { } }); log.info("Netty Client Start..."); - ChannelFuture channelFuture = bootstrap.connect(properties.getServer(), properties.getPort()).sync(); + ChannelFuture channelFuture = bootstrap.connect(server, port).sync(); channelFuture.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); diff --git a/kenaito-config-service/src/main/resources/config/application-daily.yml b/kenaito-config-service/src/main/resources/config/application-daily.yml index 4df9266..0116afc 100644 --- a/kenaito-config-service/src/main/resources/config/application-daily.yml +++ b/kenaito-config-service/src/main/resources/config/application-daily.yml @@ -3,5 +3,5 @@ kenaito: server: 127.0.0.1 port: 28002 data-id: kenaito-config-service - cache-dir: /home/admin/data + cache-dir: c:\\data env: daily \ No newline at end of file