Skip to content

Commit

Permalink
*1.优化日志;2.增加反射缓存;3.修改feign获取path的方式
Browse files Browse the repository at this point in the history
  • Loading branch information
provenceee committed Nov 3, 2022
1 parent cdc5215 commit 366ff55
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ private static Optional<Object> 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) {
// 因版本的原因,有可能会找不到方法,所以可以忽略这些错误
}
Expand All @@ -155,8 +154,7 @@ private static Optional<Object> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +65,8 @@ public ExecuteContext before(ExecuteContext context) {
Request request = (Request) argument;
Map<String, List<String>> 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;
}
Expand All @@ -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
// 以/切分时,只需要切分为4份,即["http:", "", "www.url.com", "a/b?parameter=1"]
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);
Expand Down Expand Up @@ -146,18 +150,16 @@ private Optional<RequestHeader> getRequestHeader() {
}

private Map<String, List<String>> decodeTags(Map<String, List<String>> headers) {
if (StringUtils.isBlank(FlowContextUtils.getTagName())) {
if (StringUtils.isBlank(FlowContextUtils.getTagName()) || CollectionUtils.isEmpty(headers)) {
return headers;
}
Map<String, List<String>> newHeaders = new HashMap<>(headers);
if (headers != null && headers.size() > 0) {
List<String> list = headers.get(FlowContextUtils.getTagName());
if (list != null && list.size() > 0) {
String tagStr = list.get(0);
Map<String, List<String>> stringListMap = FlowContextUtils.decodeTags(tagStr);
newHeaders.putAll(stringListMap);
return Collections.unmodifiableMap(newHeaders);
}
List<String> list = headers.get(FlowContextUtils.getTagName());
if (CollectionUtils.isEmpty(list)) {
String tagStr = list.get(0);
Map<String, List<String>> stringListMap = FlowContextUtils.decodeTags(tagStr);
newHeaders.putAll(stringListMap);
return Collections.unmodifiableMap(newHeaders);
}
return headers;
}
Expand Down

0 comments on commit 366ff55

Please sign in to comment.