Commit 2fe650782a7f4e3b8768877578eb1d373ece123a

Authored by miaoguoxin
1 parent 36d196a4

优化,代码健壮性检查

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