feat: 3.2.0

实时日志添加 ip和端口
This commit is contained in:
byteblogs168 2024-03-22 16:10:43 +08:00
parent 87b128193f
commit 9d7b6ad72e
7 changed files with 62 additions and 13 deletions

View File

@ -2,6 +2,7 @@ package com.aizuda.easy.retry.client.common.appender;
import com.aizuda.easy.retry.client.common.log.report.LogReportFactory; import com.aizuda.easy.retry.client.common.log.report.LogReportFactory;
import com.aizuda.easy.retry.client.common.log.support.EasyRetryLogManager; import com.aizuda.easy.retry.client.common.log.support.EasyRetryLogManager;
import com.aizuda.easy.retry.client.common.netty.NettyChannel;
import com.aizuda.easy.retry.common.log.dto.LogContentDTO; import com.aizuda.easy.retry.common.log.dto.LogContentDTO;
import com.aizuda.easy.retry.common.log.constant.LogFieldConstants; import com.aizuda.easy.retry.common.log.constant.LogFieldConstants;
import org.apache.log4j.MDC; import org.apache.log4j.MDC;
@ -44,6 +45,8 @@ public class EasyRetryLog4j2Appender extends AbstractAppender {
logContentDTO.addLocationField(getLocationField(event)); logContentDTO.addLocationField(getLocationField(event));
logContentDTO.addThrowableField(getThrowableField(event)); logContentDTO.addThrowableField(getThrowableField(event));
logContentDTO.addMessageField(event.getMessage().getFormattedMessage()); logContentDTO.addMessageField(event.getMessage().getFormattedMessage());
logContentDTO.addHostField(NettyChannel.getClientHost());
logContentDTO.addPortField(NettyChannel.getClientPort());
// slidingWindow syncReportLog // slidingWindow syncReportLog
Optional.ofNullable(LogReportFactory.get()).ifPresent(logReport -> logReport.report(logContentDTO)); Optional.ofNullable(LogReportFactory.get()).ifPresent(logReport -> logReport.report(logContentDTO));

View File

@ -2,6 +2,7 @@ package com.aizuda.easy.retry.client.common.appender;
import com.aizuda.easy.retry.client.common.log.report.LogReportFactory; import com.aizuda.easy.retry.client.common.log.report.LogReportFactory;
import com.aizuda.easy.retry.client.common.log.support.EasyRetryLogManager; import com.aizuda.easy.retry.client.common.log.support.EasyRetryLogManager;
import com.aizuda.easy.retry.client.common.netty.NettyChannel;
import com.aizuda.easy.retry.common.log.dto.LogContentDTO; import com.aizuda.easy.retry.common.log.dto.LogContentDTO;
import com.aizuda.easy.retry.common.log.constant.LogFieldConstants; import com.aizuda.easy.retry.common.log.constant.LogFieldConstants;
import org.apache.log4j.AppenderSkeleton; import org.apache.log4j.AppenderSkeleton;
@ -40,6 +41,8 @@ public class EasyRetryLog4jAppender extends AppenderSkeleton {
logContentDTO.addMessageField(event.getMessage().toString()); logContentDTO.addMessageField(event.getMessage().toString());
logContentDTO.addLocationField(event.getLocationInformation().fullInfo); logContentDTO.addLocationField(event.getLocationInformation().fullInfo);
logContentDTO.addThrowableField(getThrowableField(event)); logContentDTO.addThrowableField(getThrowableField(event));
logContentDTO.addHostField(NettyChannel.getClientHost());
logContentDTO.addPortField(NettyChannel.getClientPort());
// slidingWindow syncReportLog // slidingWindow syncReportLog
Optional.ofNullable(LogReportFactory.get()).ifPresent(logReport -> logReport.report(logContentDTO)); Optional.ofNullable(LogReportFactory.get()).ifPresent(logReport -> logReport.report(logContentDTO));

View File

@ -8,6 +8,7 @@ import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.UnsynchronizedAppenderBase; import ch.qos.logback.core.UnsynchronizedAppenderBase;
import com.aizuda.easy.retry.client.common.log.report.LogReportFactory; import com.aizuda.easy.retry.client.common.log.report.LogReportFactory;
import com.aizuda.easy.retry.client.common.log.support.EasyRetryLogManager; import com.aizuda.easy.retry.client.common.log.support.EasyRetryLogManager;
import com.aizuda.easy.retry.client.common.netty.NettyChannel;
import com.aizuda.easy.retry.common.log.dto.LogContentDTO; import com.aizuda.easy.retry.common.log.dto.LogContentDTO;
import com.aizuda.easy.retry.common.log.constant.LogFieldConstants; import com.aizuda.easy.retry.common.log.constant.LogFieldConstants;
import org.slf4j.MDC; import org.slf4j.MDC;
@ -50,6 +51,8 @@ public class EasyRetryLogbackAppender<E> extends UnsynchronizedAppenderBase<E> {
logContentDTO.addMessageField(event.getFormattedMessage()); logContentDTO.addMessageField(event.getFormattedMessage());
logContentDTO.addLocationField(getLocationField(event)); logContentDTO.addLocationField(getLocationField(event));
logContentDTO.addThrowableField(getThrowableField(event)); logContentDTO.addThrowableField(getThrowableField(event));
logContentDTO.addHostField(NettyChannel.getClientHost());
logContentDTO.addPortField(NettyChannel.getClientPort());
// slidingWindow syncReportLog // slidingWindow syncReportLog
Optional.ofNullable(LogReportFactory.get()).ifPresent(logReport -> logReport.report(logContentDTO)); Optional.ofNullable(LogReportFactory.get()).ifPresent(logReport -> logReport.report(logContentDTO));

View File

@ -97,6 +97,42 @@ public class NettyChannel {
return System.getProperty(EASY_RETRY_SERVER_HOST); return System.getProperty(EASY_RETRY_SERVER_HOST);
} }
/**
* 获取指定的客户IP
*
* @return 客户端IP
*/
public static String getClientHost() {
EasyRetryProperties easyRetryProperties = SpringContext.CONTEXT.getBean(EasyRetryProperties.class);
String host = easyRetryProperties.getHost();
// 获取客户端指定的IP地址
if (StrUtil.isBlank(host)) {
host = HOST;
}
return host;
}
/**
* 获取客户端端口
*
* @return port 端口
*/
public static Integer getClientPort() {
EasyRetryProperties easyRetryProperties = SpringContext.CONTEXT.getBean(EasyRetryProperties.class);
ServerProperties serverProperties = SpringContext.CONTEXT.getBean(ServerProperties.class);
Integer port = easyRetryProperties.getPort();
// 获取客户端指定的端口
if (Objects.isNull(port)) {
port = Optional.ofNullable(serverProperties.getPort()).orElse(PORT);
}
return port;
}
public static void setChannel(Channel channel) { public static void setChannel(Channel channel) {
NettyChannel.CHANNEL = channel; NettyChannel.CHANNEL = channel;
} }
@ -130,17 +166,6 @@ public class NettyChannel {
return; return;
} }
Integer port = easyRetryProperties.getPort();
// 获取客户端指定的端口
if (Objects.isNull(port)) {
port = Optional.ofNullable(serverProperties.getPort()).orElse(PORT);
}
String host = easyRetryProperties.getHost();
// 获取客户端指定的IP地址
if (StrUtil.isBlank(host)) {
host = HOST;
}
request.headers() request.headers()
.set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON) .set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON)
@ -149,10 +174,10 @@ public class NettyChannel {
//设置传递请求内容的长度 //设置传递请求内容的长度
.set(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes()) .set(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes())
.set(HeadersEnum.HOST_ID.getKey(), HOST_ID) .set(HeadersEnum.HOST_ID.getKey(), HOST_ID)
.set(HeadersEnum.HOST_IP.getKey(), host) .set(HeadersEnum.HOST_IP.getKey(), getClientHost())
.set(HeadersEnum.GROUP_NAME.getKey(), EasyRetryProperties.getGroup()) .set(HeadersEnum.GROUP_NAME.getKey(), EasyRetryProperties.getGroup())
.set(HeadersEnum.CONTEXT_PATH.getKey(), Optional.ofNullable(serverProperties.getServlet().getContextPath()).orElse("/")) .set(HeadersEnum.CONTEXT_PATH.getKey(), Optional.ofNullable(serverProperties.getServlet().getContextPath()).orElse("/"))
.set(HeadersEnum.HOST_PORT.getKey(), port) .set(HeadersEnum.HOST_PORT.getKey(), getClientPort())
.set(HeadersEnum.VERSION.getKey(), GroupVersionCache.getVersion()) .set(HeadersEnum.VERSION.getKey(), GroupVersionCache.getVersion())
.set(HeadersEnum.HOST.getKey(), serverConfig.getHost()) .set(HeadersEnum.HOST.getKey(), serverConfig.getHost())
.set(HeadersEnum.NAMESPACE.getKey(), Optional.ofNullable(easyRetryProperties.getNamespace()).orElse( .set(HeadersEnum.NAMESPACE.getKey(), Optional.ofNullable(easyRetryProperties.getNamespace()).orElse(

View File

@ -15,5 +15,7 @@ public interface LogFieldConstants {
String LEVEL = "level"; String LEVEL = "level";
String LOCATION = "location"; String LOCATION = "location";
String THROWABLE = "throwable"; String THROWABLE = "throwable";
String HOST = "host";
String PORT = "port";
} }

View File

@ -66,4 +66,12 @@ public class LogContentDTO {
this.addField(LogFieldConstants.THROWABLE, throwable); this.addField(LogFieldConstants.THROWABLE, throwable);
} }
public void addHostField(String host) {
this.addField(LogFieldConstants.HOST, host);
}
public void addPortField(Integer port) {
this.addField(LogFieldConstants.PORT, String.valueOf(port));
}
} }

View File

@ -7,11 +7,14 @@ import ch.qos.logback.classic.spi.ThrowableProxyUtil;
import ch.qos.logback.core.CoreConstants; import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.UnsynchronizedAppenderBase; import ch.qos.logback.core.UnsynchronizedAppenderBase;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.aizuda.easy.retry.common.core.context.SpringContext;
import com.aizuda.easy.retry.common.core.util.JsonUtil; import com.aizuda.easy.retry.common.core.util.JsonUtil;
import com.aizuda.easy.retry.common.core.util.NetUtil;
import com.aizuda.easy.retry.common.log.EasyRetryLog; import com.aizuda.easy.retry.common.log.EasyRetryLog;
import com.aizuda.easy.retry.common.log.constant.LogFieldConstants; import com.aizuda.easy.retry.common.log.constant.LogFieldConstants;
import com.aizuda.easy.retry.common.log.dto.LogContentDTO; import com.aizuda.easy.retry.common.log.dto.LogContentDTO;
import com.aizuda.easy.retry.server.common.LogStorage; import com.aizuda.easy.retry.server.common.LogStorage;
import com.aizuda.easy.retry.server.common.config.SystemProperties;
import com.aizuda.easy.retry.server.common.dto.LogMetaDTO; import com.aizuda.easy.retry.server.common.dto.LogMetaDTO;
import com.aizuda.easy.retry.server.common.log.LogStorageFactory; import com.aizuda.easy.retry.server.common.log.LogStorageFactory;
import org.slf4j.MDC; import org.slf4j.MDC;
@ -47,6 +50,8 @@ public class EasyRetryServerLogbackAppender<E> extends UnsynchronizedAppenderBas
logContentDTO.addThreadField(event.getThreadName()); logContentDTO.addThreadField(event.getThreadName());
logContentDTO.addLocationField(getLocationField(event)); logContentDTO.addLocationField(getLocationField(event));
logContentDTO.addThrowableField(getThrowableField(event)); logContentDTO.addThrowableField(getThrowableField(event));
logContentDTO.addHostField(NetUtil.getLocalIpStr());
logContentDTO.addPortField(SpringContext.getBean(SystemProperties.class).getNettyPort());
LogMetaDTO logMetaDTO = null; LogMetaDTO logMetaDTO = null;
try { try {