Commit 2fe650782a7f4e3b8768877578eb1d373ece123a
1 parent
36d196a4
优化,代码健壮性检查
Showing
4 changed files
with
20 additions
and
19 deletions
gateway-core/src/main/java/com/diligrp/xtrade/core/filters/factory/DispatchGatewayFilterFactory.java
... | ... | @@ -14,8 +14,6 @@ import org.springframework.stereotype.Component; |
14 | 14 | import org.springframework.web.server.ServerWebExchange; |
15 | 15 | import reactor.core.publisher.Mono; |
16 | 16 | |
17 | -import java.util.concurrent.ConcurrentLinkedQueue; | |
18 | - | |
19 | 17 | import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR; |
20 | 18 | |
21 | 19 | /** | ... | ... |
gateway-core/src/main/java/com/diligrp/xtrade/core/filters/web/ReactiveContextWebFilter.java renamed to gateway-core/src/main/java/com/diligrp/xtrade/core/filters/web/FacadeWebFilter.java
1 | 1 | package com.diligrp.xtrade.core.filters.web; |
2 | 2 | |
3 | -import com.diligrp.xtrade.core.common.constant.GatewayConst; | |
4 | 3 | import com.diligrp.xtrade.core.support.ApiMetricsCollector; |
5 | 4 | import com.diligrp.xtrade.core.support.ApiMetricsInfo; |
6 | 5 | import com.diligrp.xtrade.core.support.context.ReactiveExchangeContextHolder; |
6 | +import com.diligrp.xtrade.shared.type.ErrorCode; | |
7 | 7 | import org.slf4j.Logger; |
8 | 8 | import org.slf4j.LoggerFactory; |
9 | 9 | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
... | ... | @@ -18,12 +18,12 @@ import reactor.core.publisher.Mono; |
18 | 18 | /** |
19 | 19 | * @Auther: miaoguoxin |
20 | 20 | * @Date: 2020/4/16 09:17 |
21 | - * @Description: 操作reactive request和response上下文的过滤器 | |
21 | + * @Description: 入口过滤器(前置和后置处理) | |
22 | 22 | */ |
23 | 23 | @Component |
24 | 24 | @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) |
25 | -public class ReactiveContextWebFilter implements WebFilter { | |
26 | - private static final Logger log = LoggerFactory.getLogger(ReactiveContextWebFilter.class); | |
25 | +public class FacadeWebFilter implements WebFilter { | |
26 | + private static final Logger log = LoggerFactory.getLogger(FacadeWebFilter.class); | |
27 | 27 | |
28 | 28 | @Override |
29 | 29 | public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { |
... | ... | @@ -40,10 +40,11 @@ public class ReactiveContextWebFilter implements WebFilter { |
40 | 40 | log.info("消耗时间:{}", endTime); |
41 | 41 | ApiMetricsInfo apiMetricsInfo = ApiMetricsInfo.build(exchange, endTime); |
42 | 42 | ApiMetricsCollector.addMetricsInfo(apiMetricsInfo); |
43 | - Throwable throwable = (Throwable) exchange.getAttributes().get(GatewayConst.GATEWAY_EXCEPTION_ATTR); | |
44 | - if (throwable != null) { | |
43 | + if (apiMetricsInfo != null | |
44 | + && apiMetricsInfo.getCode() == ErrorCode.UNKNOWN_ERROR.getCode() | |
45 | + && apiMetricsInfo.getThrowable() != null) { | |
45 | 46 | //log.error("[全局异常处理]异常请求路径:{},记录异常信息:{}", errorAttrs.get("path"),errorAttributes.getError(request)); |
46 | - log.error("error:{}", throwable); | |
47 | + log.error("error:{}", apiMetricsInfo.getThrowable()); | |
47 | 48 | } |
48 | 49 | }); |
49 | 50 | } | ... | ... |
gateway-core/src/main/java/com/diligrp/xtrade/core/support/ApiMetricsInfo.java
... | ... | @@ -3,6 +3,7 @@ package com.diligrp.xtrade.core.support; |
3 | 3 | import com.diligrp.xtrade.core.common.constant.GatewayConst; |
4 | 4 | import com.diligrp.xtrade.shared.domain.Message; |
5 | 5 | import com.diligrp.xtrade.shared.util.JsonUtils; |
6 | +import com.fasterxml.jackson.annotation.JsonIgnore; | |
6 | 7 | import com.fasterxml.jackson.core.type.TypeReference; |
7 | 8 | import org.apache.logging.log4j.util.Strings; |
8 | 9 | import org.springframework.cloud.gateway.route.Route; |
... | ... | @@ -10,7 +11,6 @@ import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; |
10 | 11 | import org.springframework.web.server.ServerWebExchange; |
11 | 12 | |
12 | 13 | import java.net.URI; |
13 | -import java.net.URISyntaxException; | |
14 | 14 | import java.time.LocalDateTime; |
15 | 15 | |
16 | 16 | /** |
... | ... | @@ -29,8 +29,9 @@ public class ApiMetricsInfo { |
29 | 29 | private String url; |
30 | 30 | /**请求体*/ |
31 | 31 | private String requestBody; |
32 | - /**异常栈跟踪Json信息*/ | |
33 | - private String stacktrace; | |
32 | + /**异常信息*/ | |
33 | + @JsonIgnore | |
34 | + private Throwable throwable; | |
34 | 35 | /**方法执行时间,单位:ms*/ |
35 | 36 | private Long executeTime; |
36 | 37 | /**创建时间*/ |
... | ... | @@ -56,7 +57,7 @@ public class ApiMetricsInfo { |
56 | 57 | info.setUrl(uri != null ? uri.toString() : "unknown"); |
57 | 58 | info.setServiceId(route != null ? route.getId() : "unknown"); |
58 | 59 | info.setRequestBody(requestBody); |
59 | - info.setStacktrace(throwable != null ? JsonUtils.toJsonString(throwable.getStackTrace()) : null); | |
60 | + info.setThrowable(throwable); | |
60 | 61 | info.setExecuteTime(time); |
61 | 62 | return info; |
62 | 63 | } |
... | ... | @@ -122,12 +123,11 @@ public class ApiMetricsInfo { |
122 | 123 | this.requestBody = requestBody; |
123 | 124 | } |
124 | 125 | |
125 | - public String getStacktrace() { | |
126 | - return stacktrace; | |
126 | + public Throwable getThrowable() { | |
127 | + return throwable; | |
127 | 128 | } |
128 | 129 | |
129 | - public void setStacktrace(String stacktrace) { | |
130 | - this.stacktrace = stacktrace; | |
130 | + public void setThrowable(Throwable throwable) { | |
131 | + this.throwable = throwable; | |
131 | 132 | } |
132 | - | |
133 | 133 | } | ... | ... |
gateway-core/src/main/java/com/diligrp/xtrade/core/support/dispatch/RequestDispatcher.java
... | ... | @@ -58,7 +58,9 @@ public class RequestDispatcher { |
58 | 58 | try { |
59 | 59 | if (HAS_INIT.compareAndSet(false, true)) { |
60 | 60 | String[] paths = MAPPING_INFO_MAP.keySet().toArray(new String[0]); |
61 | - PATH_PATTERNS.addAll(PathUtils.assemblePath(paths, PATH_PATTERNS)); | |
61 | + if (paths.length>0){ | |
62 | + PATH_PATTERNS.addAll(PathUtils.assemblePath(paths, PATH_PATTERNS)); | |
63 | + } | |
62 | 64 | } |
63 | 65 | String matchUri = PathUtils.getMatchUri(uri, PATH_PATTERNS); |
64 | 66 | if (Strings.isNullOrEmpty(matchUri)) { | ... | ... |