feat: 定时写盘
This commit is contained in:
parent
c8b70718d2
commit
0d05fc479e
|
@ -1,6 +1,7 @@
|
|||
package cn.odboy.config.context;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.odboy.config.model.msgtype.ClientInfo;
|
||||
import cn.odboy.config.netty.ConfigClient;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -13,10 +14,13 @@ import org.springframework.core.env.ConfigurableEnvironment;
|
|||
import org.springframework.core.env.MapPropertySource;
|
||||
|
||||
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.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
|
@ -33,7 +37,7 @@ public class ClientConfigLoader {
|
|||
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;
|
||||
private static final Integer DEFAULT_CONFIG_PORT = 28010;
|
||||
private static final String DEFAULT_CONFIG_ENV = "default";
|
||||
private static final String DEFAULT_CONFIG_DATA_ID = "default";
|
||||
private static final String DEFAULT_PATH_WIN_SEP = ":";
|
||||
|
@ -77,6 +81,23 @@ public class ClientConfigLoader {
|
|||
log.info("客户端属性: {}", clientInfo);
|
||||
validateCacheDirPath(defaultCacheDir, clientInfo.getCacheDir());
|
||||
createCacheDir(clientInfo.getCacheDir());
|
||||
ThreadUtil.execAsync(() -> {
|
||||
// 定时刷盘
|
||||
while (true) {
|
||||
try {
|
||||
if (!lastConfigs.isEmpty()) {
|
||||
List<String> fileContent = new ArrayList<>();
|
||||
for (Map.Entry<String, Object> kve : lastConfigs.entrySet()) {
|
||||
fileContent.add(kve.getKey() + "=" + kve.getValue());
|
||||
}
|
||||
FileUtil.writeLines(fileContent, FileUtil.file(clientInfo.getCacheDir(), "config"), StandardCharsets.UTF_8);
|
||||
}
|
||||
Thread.sleep(5 * 1000);
|
||||
} catch (Exception e) {
|
||||
// 忽略
|
||||
}
|
||||
}
|
||||
});
|
||||
try {
|
||||
ConfigClient client = new ConfigClient();
|
||||
client.start(clientInfo.getServer(), clientInfo.getPort());
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package cn.odboy.config.netty;
|
||||
|
||||
import cn.odboy.config.context.ClientConfigLoader;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
|
@ -50,7 +51,7 @@ public class ConfigClient {
|
|||
@Override
|
||||
protected void initChannel(SocketChannel ch) throws Exception {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
pipeline.addLast(new ConfigClientHandler(ConfigClient.this));
|
||||
pipeline.addLast(new ConfigClientHandler());
|
||||
}
|
||||
});
|
||||
doConnect(server, port);
|
||||
|
@ -101,6 +102,7 @@ public class ConfigClient {
|
|||
protected void reConnect() {
|
||||
try {
|
||||
System.err.println("ConfigClient -> Start reconnect to server." + this.connectServerIp + ":" + this.connectServerPort);
|
||||
ClientConfigLoader.isConfigLoaded = false;
|
||||
if (channel != null && channel.isOpen()) {
|
||||
System.err.println("ConfigClient -> Server [" + this.connectServerIp + "] channel is active, close it and reconnect");
|
||||
channel.close();
|
||||
|
|
|
@ -19,11 +19,6 @@ import java.util.Map;
|
|||
import java.util.Properties;
|
||||
|
||||
public class ConfigClientHandler extends ChannelInboundHandlerAdapter {
|
||||
private final ConfigClient configClient;
|
||||
|
||||
public ConfigClientHandler(ConfigClient configClient) {
|
||||
this.configClient = configClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
kenaito:
|
||||
config-center:
|
||||
server: 127.0.0.1
|
||||
port: 28002
|
||||
port: 28011
|
||||
data-id: kenaito-config-demo
|
||||
cache-dir: c:\\data
|
||||
env: daily
|
|
@ -1,7 +1,7 @@
|
|||
kenaito:
|
||||
config-center:
|
||||
server: 127.0.0.1
|
||||
port: 28002
|
||||
port: 28011
|
||||
data-id: kenaito-config-demo
|
||||
cache-dir: /home/admin/data
|
||||
env: production
|
|
@ -1,7 +1,7 @@
|
|||
kenaito:
|
||||
config-center:
|
||||
server: 127.0.0.1
|
||||
port: 28002
|
||||
port: 28011
|
||||
data-id: kenaito-config-demo
|
||||
cache-dir: /home/admin/data
|
||||
env: stage
|
|
@ -50,7 +50,7 @@ public class ConfigNettyServer implements InitializingBean {
|
|||
pipeline.addLast(new ConfigServerHandler(configFileService));
|
||||
}
|
||||
});
|
||||
log.info("Netty Server Start...");
|
||||
log.info("Netty Server Start..., Port=" + configCenterPort);
|
||||
ChannelFuture channelFuture = serverBootstrap.bind(configCenterPort).sync();
|
||||
channelFuture.channel().closeFuture().sync();
|
||||
} finally {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
server:
|
||||
port: 28000
|
||||
port: 28010
|
||||
http2:
|
||||
# 提高网络传输效率, 默认为false
|
||||
enabled: true
|
||||
|
@ -219,8 +219,9 @@ file:
|
|||
path: /home/kenaito/file/
|
||||
avatar: /home/kenaito/avatar/
|
||||
windows:
|
||||
path: C:\kenaito\file\
|
||||
avatar: C:\kenaito\avatar\
|
||||
# 一定不要用’\‘符号, 会导致一系列的BUG
|
||||
path: C:/kenaito/file/
|
||||
avatar: C:/kenaito/avatar/
|
||||
# 文件大小 /M
|
||||
maxSize: 100
|
||||
avatarMaxSize: 5
|
||||
|
@ -248,4 +249,4 @@ task:
|
|||
queue-capacity: 50
|
||||
kenaito:
|
||||
config-center:
|
||||
port: 28002
|
||||
port: 28011
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue