upd: 日志统一

This commit is contained in:
骑着蜗牛追导弹 2024-12-06 21:55:11 +08:00
parent 20f3a367a2
commit e187a378d5
5 changed files with 37 additions and 43 deletions

View File

@ -121,7 +121,7 @@ public class ClientConfigLoader {
StandardCharsets.UTF_8); StandardCharsets.UTF_8);
} catch (Exception e) { } catch (Exception e) {
logger.error( logger.error(
"ClientConfigLoader -> 应用 {},环境 {},缓存配置文件 {} 失败", "应用 {},环境 {},缓存配置文件 {} 失败",
clientInfo.getDataId(), clientInfo.getDataId(),
clientInfo.getEnv(), clientInfo.getEnv(),
kve.getKey(), kve.getKey(),
@ -141,7 +141,7 @@ public class ClientConfigLoader {
throws BeansException { throws BeansException {
String defaultCacheDir = getDefaultCacheDir(); String defaultCacheDir = getDefaultCacheDir();
initClientInfo(defaultCacheDir, environment); initClientInfo(defaultCacheDir, environment);
logger.info("ClientConfigLoader -> 客户端属性: {}", clientInfo); logger.info("客户端属性: {}", clientInfo);
validateCacheDirPath(defaultCacheDir, clientInfo.getCacheDir()); validateCacheDirPath(defaultCacheDir, clientInfo.getCacheDir());
createCacheDir(clientInfo.getCacheDir()); createCacheDir(clientInfo.getCacheDir());
fixedTimeFlushConfigFileThread.start(); fixedTimeFlushConfigFileThread.start();
@ -150,7 +150,7 @@ public class ClientConfigLoader {
try { try {
ConfigClient.getInstance().start(clientInfo.getServer(), clientInfo.getPort()); ConfigClient.getInstance().start(clientInfo.getServer(), clientInfo.getPort());
} catch (InterruptedException e) { } catch (InterruptedException e) {
logger.error("ClientConfigLoader -> Netty客户端启动失败", e); logger.error("Netty客户端启动失败", e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}); });
@ -164,12 +164,12 @@ public class ClientConfigLoader {
Thread currentThread = Thread.currentThread(); Thread currentThread = Thread.currentThread();
String currentThreadName = currentThread.getName(); String currentThreadName = currentThread.getName();
currentThread.interrupt(); currentThread.interrupt();
logger.error("ClientConfigLoader -> 中断线程: {}", currentThreadName, e); logger.error("中断线程: {}", currentThreadName, e);
} }
} }
// 判断配置中心服务是否处于离线状态 // 判断配置中心服务是否处于离线状态
if (isServerOffline) { if (isServerOffline) {
logger.info("ClientConfigLoader -> 配置中心离线,尝试从本地缓存加载配置文件"); logger.info("配置中心离线,尝试从本地缓存加载配置文件");
String cacheDir = clientInfo.getCacheDir(); String cacheDir = clientInfo.getCacheDir();
FileUtil.walkFiles( FileUtil.walkFiles(
FileUtil.file(cacheDir), FileUtil.file(cacheDir),
@ -197,7 +197,7 @@ public class ClientConfigLoader {
lastConfigs.put(fileName, tempMap); lastConfigs.put(fileName, tempMap);
} }
} catch (Exception e) { } catch (Exception e) {
logger.info("ClientConfigLoader -> 配置文件转map失败", e); logger.info("配置文件转map失败", e);
} }
})); }));
} }

View File

@ -43,7 +43,7 @@ public class ConfigClient {
private static volatile ConfigClient instance; private static volatile ConfigClient instance;
/** 最大重试次数 */ /** 最大重试次数 */
private static final int MAX_RETRY_COUNT = 2; private static final int MAX_RETRY_COUNT = 5;
/** 当前重试次数 */ /** 当前重试次数 */
private static int retryCount = 0; private static int retryCount = 0;
@ -90,7 +90,7 @@ public class ConfigClient {
} }
private void doConnect(String server, Integer port) throws InterruptedException { private void doConnect(String server, Integer port) throws InterruptedException {
logger.info("ConfigClient -> Start"); logger.info("开始连接配置中心服务...");
if (channel != null && channel.isActive()) { if (channel != null && channel.isActive()) {
return; return;
} }
@ -107,11 +107,11 @@ public class ConfigClient {
channel = channelFuture.channel(); channel = channelFuture.channel();
// 重置重试次数 // 重置重试次数
retryCount = 0; retryCount = 0;
logger.info("ConfigClient -> Connect success {}:{}", serverIp, serverPort); logger.info("连接成功 {}:{}", serverIp, serverPort);
} else { } else {
// 失败后delaySecond秒默认是5秒重连周期性delaySecond秒的重连 // 失败后delaySecond秒默认是5秒重连周期性delaySecond秒的重连
retryCount++; retryCount++;
logger.info("ConfigClient -> 当前重试次数: {}", retryCount); logger.info("当前重试次数: {}", retryCount);
if (retryCount >= MAX_RETRY_COUNT) { if (retryCount >= MAX_RETRY_COUNT) {
ClientConfigLoader.isConfigLoaded = true; ClientConfigLoader.isConfigLoaded = true;
ClientConfigLoader.isServerOffline = true; ClientConfigLoader.isServerOffline = true;
@ -133,13 +133,11 @@ public class ConfigClient {
/** 重新连接 */ /** 重新连接 */
protected void reConnect() { protected void reConnect() {
try { try {
logger.info( logger.info("重连配置中心服务 {}:{}", this.serverIp, this.serverPort);
"ConfigClient -> Start reconnect to server.{}:{}", this.serverIp, this.serverPort);
ClientConfigLoader.isConfigLoaded = false; ClientConfigLoader.isConfigLoaded = false;
ClientConfigLoader.isServerOffline = false; ClientConfigLoader.isServerOffline = false;
if (channel != null && channel.isOpen()) { if (channel != null && channel.isOpen()) {
logger.info( logger.info("Channel已激活, 关闭且重启中...");
"ConfigClient -> Server [{}] channel is active, close it and reconnect", this.serverIp);
channel.close(); channel.close();
} }
bootstrap bootstrap
@ -148,11 +146,7 @@ public class ConfigClient {
.sync() .sync()
.channel(); .channel();
} catch (Exception e) { } catch (Exception e) {
logger.error( logger.error("重连配置中心服务失败 {}:{}", this.serverIp, this.serverPort, e);
"ConfigClient -> ReConnect to server failure. server={}:{}:{}",
this.serverIp,
this.serverPort,
e.getMessage());
} }
} }
} }

View File

@ -29,17 +29,17 @@ public class ConfigClientHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception { public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
logger.info("ConfigClientHandler -> 当Channel已经注册到它的EventLoop并且能够处理I/O时被调用"); logger.info("当Channel已经注册到它的EventLoop并且能够处理I/O时被调用");
} }
@Override @Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
logger.info("ConfigClientHandler -> 当Channel从它的EventLoop注销并且无法处理任何I/O时被调用"); logger.info("当Channel从它的EventLoop注销并且无法处理任何I/O时被调用");
} }
@Override @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception { public void channelActive(ChannelHandlerContext ctx) throws Exception {
logger.info("ConfigClientHandler -> 当Channel处于活动状态已经连接到它的远程节点时被调用, 注册客户端"); logger.info("当Channel处于活动状态已经连接到它的远程节点时被调用, 注册客户端");
SmallMessage smallMessage = new SmallMessage(); SmallMessage smallMessage = new SmallMessage();
smallMessage.setType(TransferMessageType.REGISTER); smallMessage.setType(TransferMessageType.REGISTER);
smallMessage.setResp(SmallMessage.Response.ok(ClientConfigLoader.clientInfo)); smallMessage.setResp(SmallMessage.Response.ok(ClientConfigLoader.clientInfo));
@ -48,22 +48,22 @@ public class ConfigClientHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) throws Exception {
logger.info("ConfigClientHandler -> 当Channel离开活动状态并且不再连接到它的远程节点时被调用"); logger.info("当Channel离开活动状态并且不再连接到它的远程节点时被调用");
/// this.configClient.reConnect(); /// this.configClient.reConnect();
} }
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
/// logger.info("ConfigClientHandler -> 当从Channel读取数据时被调用, 收到服务器消息"); /// logger.info("当从Channel读取数据时被调用, 收到服务器消息");
logger.info("ConfigClientHandler -> 从服务端读取到Object"); logger.info("从服务端读取到Object");
SmallMessage smallMessage = MessageUtil.getMessage(msg); SmallMessage smallMessage = MessageUtil.getMessage(msg);
SmallMessage.Response resp = smallMessage.getResp(); SmallMessage.Response resp = smallMessage.getResp();
switch (smallMessage.getType()) { switch (smallMessage.getType()) {
case REGISTER: case REGISTER:
if (!resp.getSuccess()) { if (!resp.getSuccess()) {
logger.info("ConfigClientHandler -> 注册失败, {}", resp.getErrorMessage()); logger.info("注册失败, {}", resp.getErrorMessage());
} else { } else {
logger.info("ConfigClientHandler -> 注册成功, 给服务端发信号, 表明准备好可以拉取配置了"); logger.info("注册成功, 给服务端发信号, 表明准备好可以拉取配置了");
// 准备拉取配置给服务端发信号 // 准备拉取配置给服务端发信号
SmallMessage pullConfigMessage = new SmallMessage(); SmallMessage pullConfigMessage = new SmallMessage();
pullConfigMessage.setType(TransferMessageType.PULL_CONFIG); pullConfigMessage.setType(TransferMessageType.PULL_CONFIG);
@ -76,7 +76,7 @@ public class ConfigClientHandler extends ChannelInboundHandlerAdapter {
throw new RuntimeException(resp.getErrorMessage()); throw new RuntimeException(resp.getErrorMessage());
} }
List<ConfigFileInfo> configFileInfos = MessageUtil.toConfigFileInfoList(resp.getData()); List<ConfigFileInfo> configFileInfos = MessageUtil.toConfigFileInfoList(resp.getData());
logger.info("ConfigClientHandler -> 收到来自服务端推送的配置信息"); logger.info("收到来自服务端推送的配置信息");
Map<String, String> originConfigs = new HashMap<>(1); Map<String, String> originConfigs = new HashMap<>(1);
Map<String, Map<String, Object>> lastConfigs = new HashMap<>(1); Map<String, Map<String, Object>> lastConfigs = new HashMap<>(1);
try { try {
@ -101,7 +101,7 @@ public class ConfigClientHandler extends ChannelInboundHandlerAdapter {
lastConfigs.put(fileName, tempMap); lastConfigs.put(fileName, tempMap);
} }
} }
logger.info("ConfigClientHandler -> 配置文件转map成功"); logger.info("配置文件转map成功");
ClientConfigLoader.originConfigs = originConfigs; ClientConfigLoader.originConfigs = originConfigs;
ClientConfigLoader.lastConfigs = lastConfigs; ClientConfigLoader.lastConfigs = lastConfigs;
ClientConfigLoader.isConfigLoaded = true; ClientConfigLoader.isConfigLoaded = true;
@ -110,7 +110,7 @@ public class ConfigClientHandler extends ChannelInboundHandlerAdapter {
ClientConfigLoader.clientInfo.notifyAll(); ClientConfigLoader.clientInfo.notifyAll();
} }
} catch (IOException e) { } catch (IOException e) {
logger.info("ConfigClientHandler -> 配置文件转map失败", e); logger.info("配置文件转map失败", e);
} }
break; break;
default: default:

View File

@ -56,7 +56,7 @@ public class ConfigClientManage {
public static void register(String env, String dataId, ChannelHandlerContext ctx) { public static void register(String env, String dataId, ChannelHandlerContext ctx) {
String envClientKey = String.format("%s_%s_%s", env, dataId, ChannelUtil.getId(ctx)); String envClientKey = String.format("%s_%s_%s", env, dataId, ChannelUtil.getId(ctx));
CLIENT.put(envClientKey, ctx.channel()); CLIENT.put(envClientKey, ctx.channel());
log.info("ConfigClientManage -> 客户端 {} 注册成功", envClientKey); log.info("客户端 {} 注册成功", envClientKey);
} }
/** /**
@ -76,7 +76,7 @@ public class ConfigClientManage {
channel.closeFuture(); channel.closeFuture();
} }
CLIENT.remove(envClientKey); CLIENT.remove(envClientKey);
log.info("ConfigClientManage -> 客户端 {} 注销成功", envClientKey); log.info("客户端 {} 注销成功", envClientKey);
} }
} }
} }

View File

@ -29,9 +29,9 @@ public class ConfigServerHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
/// log.info("ServerHandler -> 当从Channel读取数据时被调用"); /// log.info("当从Channel读取数据时被调用");
SmallMessage smallMessage = MessageUtil.getMessage(msg); SmallMessage smallMessage = MessageUtil.getMessage(msg);
log.info("ServerHandler -> 从客户端读取到Object{}", smallMessage); log.info("从客户端读取到Object{}", smallMessage);
SmallMessage.Response resp = smallMessage.getResp(); SmallMessage.Response resp = smallMessage.getResp();
switch (smallMessage.getType()) { switch (smallMessage.getType()) {
case REGISTER: case REGISTER:
@ -53,16 +53,16 @@ public class ConfigServerHandler extends ChannelInboundHandlerAdapter {
break; break;
case PULL_CONFIG: case PULL_CONFIG:
if (!resp.getSuccess()) { if (!resp.getSuccess()) {
log.info("ServerHandler -> 客户端说它没有准备好拉取配置"); log.info("客户端说它没有准备好拉取配置");
break; break;
} }
log.info("ServerHandler -> 客户端说它准备好拉取配置了"); log.info("客户端说它准备好拉取配置了");
String[] envDataId = ConfigClientManage.getEnvDataId(ctx.channel().id()); String[] envDataId = ConfigClientManage.getEnvDataId(ctx.channel().id());
String env = envDataId[0]; String env = envDataId[0];
String dataId = envDataId[1]; String dataId = envDataId[1];
List<Channel> channels = ConfigClientManage.queryChannels(env, dataId); List<Channel> channels = ConfigClientManage.queryChannels(env, dataId);
if (channels.isEmpty()) { if (channels.isEmpty()) {
log.info("ServerHandler -> 没有在线的客户端"); log.info("没有在线的客户端");
break; break;
} }
List<ConfigFileInfo> fileList = configFileService.getFileList(env, dataId); List<ConfigFileInfo> fileList = configFileService.getFileList(env, dataId);
@ -74,7 +74,7 @@ public class ConfigServerHandler extends ChannelInboundHandlerAdapter {
MessageUtil.toPushConfigBad(String.format("应用 %s 没有环境编码为 %s 的配置", dataId, env))); MessageUtil.toPushConfigBad(String.format("应用 %s 没有环境编码为 %s 的配置", dataId, env)));
} catch (Exception e) { } catch (Exception e) {
log.error( log.error(
"ServerHandler -> 推送无法获取配置的消息到客户端失败, env: {}, dataId={}, channelId={}", "推送无法获取配置的消息到客户端失败, env: {}, dataId={}, channelId={}",
env, env,
dataId, dataId,
ChannelUtil.getId(channel.id()), ChannelUtil.getId(channel.id()),
@ -87,13 +87,13 @@ public class ConfigServerHandler extends ChannelInboundHandlerAdapter {
try { try {
ctx.writeAndFlush(MessageUtil.toPushConfigOk(fileList)); ctx.writeAndFlush(MessageUtil.toPushConfigOk(fileList));
log.info( log.info(
"ServerHandler -> 推送配置到客户端成功, env: {}, dataId={}, channelId={}", "推送配置到客户端成功, env: {}, dataId={}, channelId={}",
env, env,
dataId, dataId,
ChannelUtil.getId(channel.id())); ChannelUtil.getId(channel.id()));
} catch (Exception e) { } catch (Exception e) {
log.error( log.error(
"ServerHandler -> 推送配置到客户端失败, env: {}, dataId={}, channelId={}", "推送配置到客户端失败, env: {}, dataId={}, channelId={}",
env, env,
dataId, dataId,
ChannelUtil.getId(channel.id()), ChannelUtil.getId(channel.id()),
@ -109,7 +109,7 @@ public class ConfigServerHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error("ServerHandler -> 当Channel发生异常被调用", cause); log.error("当Channel发生异常被调用", cause);
/// cause.printStackTrace(); /// cause.printStackTrace();
ConfigClientManage.unregister(ctx.channel().id()); ConfigClientManage.unregister(ctx.channel().id());
ctx.close(); ctx.close();
@ -117,13 +117,13 @@ public class ConfigServerHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.info("ServerHandler -> 当Channel处于非活动状态已经连接到它的远程节点时被调用"); log.info("当Channel处于非活动状态已经连接到它的远程节点时被调用");
ConfigClientManage.unregister(ctx.channel().id()); ConfigClientManage.unregister(ctx.channel().id());
} }
@Override @Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
log.info("ServerHandler -> 当Channel从它的EventLoop注销并且不能够处理I/O时被调用"); log.info("当Channel从它的EventLoop注销并且不能够处理I/O时被调用");
ConfigClientManage.unregister(ctx.channel().id()); ConfigClientManage.unregister(ctx.channel().id());
} }
} }