feat: 创建缓存文件夹
This commit is contained in:
parent
d12bb238cf
commit
4a5107653f
|
@ -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("缓存文件夹创建失败, 无读写权限");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue