From 5ca46dd801328727f49a6644e521bdb7fac8ab4d Mon Sep 17 00:00:00 2001 From: provenceee <83857838+provenceee@users.noreply.github.com> Date: Wed, 2 Nov 2022 18:36:50 +0800 Subject: [PATCH] =?UTF-8?q?*1.=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97?= =?UTF-8?q?=EF=BC=9B2.=E5=A2=9E=E5=8A=A0=E5=8F=8D=E5=B0=84=E7=BC=93?= =?UTF-8?q?=E5=AD=98=EF=BC=9B3.=E4=BF=AE=E6=94=B9feign=E8=8E=B7=E5=8F=96pa?= =?UTF-8?q?th=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../router/common/utils/ReflectUtils.java | 6 ++-- .../interceptor/FeignClientInterceptor.java | 28 ++++++++++--------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/sermant-plugins/sermant-router/router-common/src/main/java/com/huaweicloud/sermant/router/common/utils/ReflectUtils.java b/sermant-plugins/sermant-router/router-common/src/main/java/com/huaweicloud/sermant/router/common/utils/ReflectUtils.java index b4cc52ac6a..9839bf57ad 100644 --- a/sermant-plugins/sermant-router/router-common/src/main/java/com/huaweicloud/sermant/router/common/utils/ReflectUtils.java +++ b/sermant-plugins/sermant-router/router-common/src/main/java/com/huaweicloud/sermant/router/common/utils/ReflectUtils.java @@ -144,9 +144,8 @@ private static Optional invoke(Class invokeClass, Object obj, String try { if (parameterClass == null) { return Optional.ofNullable(method.get().invoke(obj)); - } else { - return Optional.ofNullable(method.get().invoke(obj, parameter)); } + return Optional.ofNullable(method.get().invoke(obj, parameter)); } catch (IllegalAccessException | InvocationTargetException ignored) { // 因版本的原因,有可能会找不到方法,所以可以忽略这些错误 } @@ -155,8 +154,7 @@ private static Optional invoke(Class invokeClass, Object obj, String } private static String buildMethodKey(Class clazz, String methodName, Class parameterClass) { - final String name = clazz.getName(); - final StringBuilder sb = new StringBuilder(name); + StringBuilder sb = new StringBuilder(clazz.getName()); sb.append("#").append(methodName).append("("); if (parameterClass != null) { sb.append(parameterClass.getName()); diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/FeignClientInterceptor.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/FeignClientInterceptor.java index f7c785ceb6..58f46dcf9c 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/FeignClientInterceptor.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/FeignClientInterceptor.java @@ -22,6 +22,7 @@ import com.huaweicloud.sermant.core.utils.StringUtils; import com.huaweicloud.sermant.router.common.request.RequestData; import com.huaweicloud.sermant.router.common.request.RequestHeader; +import com.huaweicloud.sermant.router.common.utils.CollectionUtils; import com.huaweicloud.sermant.router.common.utils.FlowContextUtils; import com.huaweicloud.sermant.router.common.utils.ReflectUtils; import com.huaweicloud.sermant.router.common.utils.ThreadLocalUtils; @@ -64,7 +65,8 @@ public ExecuteContext before(ExecuteContext context) { Request request = (Request) argument; Map> headers = getHeaders(request.headers()); setHeaders(request, headers); - ThreadLocalUtils.setRequestData(new RequestData(decodeTags(headers), getPath(request.url()), request.method())); + ThreadLocalUtils.setRequestData(new RequestData(decodeTags(headers), getPath(request.url()), + request.method())); } return context; } @@ -82,11 +84,13 @@ public ExecuteContext onThrow(ExecuteContext context) { } private String getPath(String url) { - String[] split = url.split("/", EXPECT_LENGTH); - if (split.length < EXPECT_LENGTH) { + // url形如:http://www.url.com/a/b?parameter=1,其中path为/a/b,?后面的参数非必须且不属于path + // 以/切分时,只需要切分为4份,即["http:", "", "www.url.com", "a/b?parameter=1"],然后去掉arr[3]中参数即可 + String[] arr = url.split("/", EXPECT_LENGTH); + if (arr.length < EXPECT_LENGTH) { return ""; } - String path = split[EXPECT_LENGTH - 1]; + String path = arr[EXPECT_LENGTH - 1]; int index = path.indexOf('?'); if (index >= 0) { path = path.substring(0, index); @@ -146,18 +150,16 @@ private Optional getRequestHeader() { } private Map> decodeTags(Map> headers) { - if (StringUtils.isBlank(FlowContextUtils.getTagName())) { + if (StringUtils.isBlank(FlowContextUtils.getTagName()) || CollectionUtils.isEmpty(headers)) { return headers; } Map> newHeaders = new HashMap<>(headers); - if (headers != null && headers.size() > 0) { - List list = headers.get(FlowContextUtils.getTagName()); - if (list != null && list.size() > 0) { - String tagStr = list.get(0); - Map> stringListMap = FlowContextUtils.decodeTags(tagStr); - newHeaders.putAll(stringListMap); - return Collections.unmodifiableMap(newHeaders); - } + List list = headers.get(FlowContextUtils.getTagName()); + if (CollectionUtils.isEmpty(list)) { + String tagStr = list.get(0); + Map> stringListMap = FlowContextUtils.decodeTags(tagStr); + newHeaders.putAll(stringListMap); + return Collections.unmodifiableMap(newHeaders); } return headers; }