feat(REQ-2516): 添加微服务间调用异常信息记录
This commit is contained in:
parent
8de3080da8
commit
d61c04e123
@ -94,5 +94,11 @@
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-core</artifactId>
|
||||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
</project>
|
||||
|
||||
@ -8,6 +8,9 @@ import cn.axzo.framework.autoconfigure.web.exception.resolver.internal.RequestRe
|
||||
import cn.axzo.framework.autoconfigure.web.exception.support.GlobalErrorController;
|
||||
import cn.axzo.framework.autoconfigure.web.exception.support.GlobalExceptionHandler;
|
||||
import cn.axzo.framework.domain.web.result.Result;
|
||||
import cn.axzo.framework.web.exception.BasicRecordExceptionHandler;
|
||||
import cn.axzo.framework.web.feign.FeignRecordExceptionInterceptor;
|
||||
import feign.RequestInterceptor;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@ -184,4 +187,18 @@ public class ExceptionHandlerAutoConfiguration implements WebMvcConfigurer {
|
||||
return new RequestRejectedExceptionHttpStatusResolver();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(RequestInterceptor.class)
|
||||
public static class RecordExceptionConfiguration {
|
||||
@Bean
|
||||
public RequestInterceptor requestInterceptor(){
|
||||
return new FeignRecordExceptionInterceptor();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BasicRecordExceptionHandler basicRecordExceptionHandler() {
|
||||
return new BasicRecordExceptionHandler();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,5 +43,18 @@
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-core</artifactId>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-core</artifactId>
|
||||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
</project>
|
||||
|
||||
@ -0,0 +1,132 @@
|
||||
package cn.axzo.framework.web.exception;
|
||||
|
||||
import cn.axzo.framework.domain.web.result.ApiResult;
|
||||
import com.google.common.collect.Lists;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.PriorityOrdered;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 最高优先级的异常处理器,主要是记录应用的异常,并抛出
|
||||
*
|
||||
* @author wangli
|
||||
* @since 2024/6/24 09:43
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
@ResponseBody
|
||||
public final class BasicRecordExceptionHandler implements PriorityOrdered {
|
||||
private static final Logger log = LoggerFactory.getLogger(BasicRecordExceptionHandler.class);
|
||||
public static final String CTX_LOG_ID = "ctxLogId";
|
||||
/**
|
||||
* 多设置一个key = TraceId, value为traceId的变量到MDC. 以兼容目前的logback-spring.xml的配置
|
||||
*/
|
||||
public static final String TRACE_ID_IN_MDC = "traceId";
|
||||
public static final String MICRO_SERVER_RECORD_ERROR_HEADER_NAME = "X-Metadata-Tag";
|
||||
public static final String MICRO_SERVER_RECORD_ERROR_GET_PARAM_NAME = "pkg";
|
||||
public static final String MICRO_SERVER_RECORD_ERROR_FILTER_PACKAGE_VALUE = "cn.axzo";
|
||||
|
||||
|
||||
@Value("${axzo.framework.debugging:X-Metadata-Tag}")
|
||||
private String recordExceptionHeaderName;
|
||||
@Value("${spring.application.name:}")
|
||||
private String applicationName;
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = Throwable.class)
|
||||
public ApiResult<?> globalException(HttpServletRequest request, HttpServletResponse response, Throwable e) throws Throwable {
|
||||
String headerValue;
|
||||
if (StringUtils.hasText(recordExceptionHeaderName) && Objects.nonNull(headerValue = request.getHeader(recordExceptionHeaderName))) {
|
||||
log.debug("recordException HeaderName: {}", headerValue);
|
||||
if (Boolean.parseBoolean(headerValue)) {
|
||||
ApiResult error = ApiResult.err(500, "discovery server internal error");
|
||||
Map<String, Throwable> recordExceptionMap = new HashMap<>();
|
||||
recordExceptionMap.put(StringUtils.hasText(applicationName) ? applicationName : "not applicationName found", e);
|
||||
error.setData(rebuildThrowable(e, request));
|
||||
return error;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
// 重新抛出
|
||||
throw e;
|
||||
}
|
||||
|
||||
private ExceptionWrapper rebuildThrowable(Throwable throwable, HttpServletRequest request) {
|
||||
return new ExceptionWrapper(throwable, request);
|
||||
}
|
||||
|
||||
@Getter
|
||||
static class ExceptionWrapper implements Serializable {
|
||||
private final String traceId = getOutsideTraceId();
|
||||
private final String causeMessage;
|
||||
private final List<String> stackTrace = new ArrayList<>();
|
||||
|
||||
public ExceptionWrapper(Throwable throwable, HttpServletRequest request) {
|
||||
this.causeMessage = throwable.getMessage();
|
||||
init(throwable, request);
|
||||
}
|
||||
|
||||
private void init(Throwable throwable, HttpServletRequest request) {
|
||||
List<StackTraceElement> elements = Arrays.stream(throwable.getStackTrace()).collect(Collectors.toList());
|
||||
if (CollectionUtils.isEmpty(elements)) {
|
||||
return;
|
||||
}
|
||||
List<String> filterPackageNames = Arrays.asList(request.getParameterValues(MICRO_SERVER_RECORD_ERROR_GET_PARAM_NAME));
|
||||
filterPackageNames = CollectionUtils.isEmpty(filterPackageNames) ? Lists.newArrayList(MICRO_SERVER_RECORD_ERROR_FILTER_PACKAGE_VALUE) : filterPackageNames;
|
||||
for (StackTraceElement e : elements) {
|
||||
if (StringUtils.hasText(e.getClassName())) {
|
||||
filterPackageNames.stream().filter(i -> e.getClassName().contains(i)).findAny().ifPresent(t -> {
|
||||
StackTraceWrapper wrapper = new StackTraceWrapper();
|
||||
wrapper.setClassName(e.getClassName());
|
||||
wrapper.setMethodName(e.getMethodName());
|
||||
wrapper.setLineNumber(e.getLineNumber());
|
||||
stackTrace.add(wrapper.toString());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
static class StackTraceWrapper implements Serializable {
|
||||
private String className;
|
||||
private String methodName;
|
||||
private Integer lineNumber;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return className + "#" + methodName + ":" + lineNumber;
|
||||
}
|
||||
}
|
||||
|
||||
private static String getOutsideTraceId() {
|
||||
String res = MDC.get(TRACE_ID_IN_MDC);
|
||||
if (Objects.isNull(res)) {
|
||||
res = MDC.get(CTX_LOG_ID);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,55 @@
|
||||
package cn.axzo.framework.web.feign;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Objects;
|
||||
|
||||
import static cn.axzo.framework.web.exception.BasicRecordExceptionHandler.MICRO_SERVER_RECORD_ERROR_GET_PARAM_NAME;
|
||||
import static cn.axzo.framework.web.exception.BasicRecordExceptionHandler.MICRO_SERVER_RECORD_ERROR_HEADER_NAME;
|
||||
|
||||
/**
|
||||
* 用于 Feign 调用时,传递外部 HTTP 请求中携带的特殊 Header
|
||||
*
|
||||
* @author wangli
|
||||
* @since 2024/6/24 19:29
|
||||
*/
|
||||
public class FeignRecordExceptionInterceptor implements RequestInterceptor {
|
||||
private static final Logger log = LoggerFactory.getLogger(FeignRecordExceptionInterceptor.class);
|
||||
@Override
|
||||
public void apply(RequestTemplate template) {
|
||||
HttpServletRequest originalRequest = getOriginalRequest();
|
||||
if (Objects.isNull(originalRequest)) {
|
||||
return;
|
||||
}
|
||||
|
||||
setRequestParams(template, originalRequest);
|
||||
|
||||
setXMetaDataTag(template, originalRequest);
|
||||
}
|
||||
|
||||
private void setRequestParams(RequestTemplate template, HttpServletRequest originalRequest) {
|
||||
String[] packageNames = originalRequest.getParameterValues(MICRO_SERVER_RECORD_ERROR_GET_PARAM_NAME);
|
||||
template.query(MICRO_SERVER_RECORD_ERROR_GET_PARAM_NAME, packageNames);
|
||||
}
|
||||
|
||||
private static void setXMetaDataTag(RequestTemplate template, HttpServletRequest originalRequest) {
|
||||
// 需要传递外部传入的标识
|
||||
template.header(MICRO_SERVER_RECORD_ERROR_HEADER_NAME, originalRequest.getHeader(MICRO_SERVER_RECORD_ERROR_HEADER_NAME));
|
||||
}
|
||||
|
||||
public HttpServletRequest getOriginalRequest() {
|
||||
try {
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
return attributes.getRequest();
|
||||
} catch (Exception e) {
|
||||
log.warn("not HttpServletRequest instance bean found");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user