fix fegin report call result error when using fegin direct call (#621)

pull/627/head
lepdou 2 years ago committed by GitHub
parent 994f87a522
commit 008551b15f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,5 +8,6 @@
- [Automatically transmit some headers specified by a environment varaible that directly defines header keys.](https://github.com/Tencent/spring-cloud-tencent/pull/606) - [Automatically transmit some headers specified by a environment varaible that directly defines header keys.](https://github.com/Tencent/spring-cloud-tencent/pull/606)
- [Optimize: Maybe remove Chinese characters](https://github.com/Tencent/spring-cloud-tencent/pull/607) - [Optimize: Maybe remove Chinese characters](https://github.com/Tencent/spring-cloud-tencent/pull/607)
- [ fix pr #606 cause ci fail](https://github.com/Tencent/spring-cloud-tencent/pull/613) - [ fix pr #606 cause ci fail](https://github.com/Tencent/spring-cloud-tencent/pull/613)
- [Bugfix: fix feign report call result error when using feign direct call](https://github.com/Tencent/spring-cloud-tencent/pull/621)
- [fix pr 606: modify a part of changes requested by review.](https://github.com/Tencent/spring-cloud-tencent/pull/620) - [fix pr 606: modify a part of changes requested by review.](https://github.com/Tencent/spring-cloud-tencent/pull/620)
- [fix pr 613: modify a judgment logic](https://github.com/Tencent/spring-cloud-tencent/pull/618) - [fix pr 613: modify a judgment logic](https://github.com/Tencent/spring-cloud-tencent/pull/618)

@ -22,7 +22,6 @@ import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -99,22 +98,20 @@ public class DecodeTransferMetadataReactiveFilter implements WebFilter, Ordered
*/ */
private void setCompleteTransHeaderIntoMC(ServerHttpRequest serverHttpRequest) { private void setCompleteTransHeaderIntoMC(ServerHttpRequest serverHttpRequest) {
// transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}} // transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}}
Map<String, String> transHeaderMetadata = MetadataContextHolder.get() Map<String, String> transHeaderMetadata = MetadataContextHolder.get()
.getFragmentContext(FRAGMENT_RAW_TRANSHEADERS); .getFragmentContext(FRAGMENT_RAW_TRANSHEADERS);
if (!CollectionUtils.isEmpty(transHeaderMetadata)) { if (!CollectionUtils.isEmpty(transHeaderMetadata)) {
String transHeaders = transHeaderMetadata.keySet().stream().findFirst().orElse(""); String transHeaders = transHeaderMetadata.keySet().stream().findFirst().orElse("");
String[] transHeaderArray = transHeaders.split(","); String[] transHeaderArray = transHeaders.split(",");
HttpHeaders headers = serverHttpRequest.getHeaders(); HttpHeaders headers = serverHttpRequest.getHeaders();
Set<String> headerKeys = headers.keySet(); Set<String> headerKeys = headers.keySet();
Iterator<String> iterator = headerKeys.iterator(); for (String httpHeader : headerKeys) {
while (iterator.hasNext()) {
String httpHeader = iterator.next();
Arrays.stream(transHeaderArray).forEach(transHeader -> { Arrays.stream(transHeaderArray).forEach(transHeader -> {
if (transHeader.equals(httpHeader)) { if (transHeader.equals(httpHeader)) {
List<String> list = headers.get(httpHeader); List<String> list = headers.get(httpHeader);
String httpHeaderValue = JacksonUtils.serialize2Json(list); String httpHeaderValue = JacksonUtils.serialize2Json(list);
MetadataContextHolder.get().putContext(FRAGMENT_RAW_TRANSHEADERS_KV, httpHeader, httpHeaderValue); MetadataContextHolder.get()
return; .putContext(FRAGMENT_RAW_TRANSHEADERS_KV, httpHeader, httpHeaderValue);
} }
}); });
} }

@ -86,7 +86,7 @@ public class DecodeTransferMetadataServletFilter extends OncePerRequestFilter {
*/ */
private void setCompleteTransHeaderIntoMC(HttpServletRequest httpServletRequest) { private void setCompleteTransHeaderIntoMC(HttpServletRequest httpServletRequest) {
// transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}} // transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}}
Map<String, String> transHeaderMetadata = MetadataContextHolder.get() Map<String, String> transHeaderMetadata = MetadataContextHolder.get()
.getFragmentContext(FRAGMENT_RAW_TRANSHEADERS); .getFragmentContext(FRAGMENT_RAW_TRANSHEADERS);
if (!CollectionUtils.isEmpty(transHeaderMetadata)) { if (!CollectionUtils.isEmpty(transHeaderMetadata)) {
String transHeaders = transHeaderMetadata.keySet().stream().findFirst().orElse(""); String transHeaders = transHeaderMetadata.keySet().stream().findFirst().orElse("");
@ -98,8 +98,8 @@ public class DecodeTransferMetadataServletFilter extends OncePerRequestFilter {
if (transHeader.equals(httpHeader)) { if (transHeader.equals(httpHeader)) {
String httpHeaderValue = httpServletRequest.getHeader(httpHeader); String httpHeaderValue = httpServletRequest.getHeader(httpHeader);
// for example, {"trans-headers-kv" : {"header1":"v1","header2":"v2"...}} // for example, {"trans-headers-kv" : {"header1":"v1","header2":"v2"...}}
MetadataContextHolder.get().putContext(FRAGMENT_RAW_TRANSHEADERS_KV, httpHeader, httpHeaderValue); MetadataContextHolder.get()
return; .putContext(FRAGMENT_RAW_TRANSHEADERS_KV, httpHeader, httpHeaderValue);
} }
}); });
} }

@ -22,7 +22,6 @@ import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -97,23 +96,21 @@ public class EncodeTransferMedataScgFilter implements GlobalFilter, Ordered {
*/ */
private void setCompleteTransHeaderIntoMC(ServerHttpRequest serverHttpRequest) { private void setCompleteTransHeaderIntoMC(ServerHttpRequest serverHttpRequest) {
// transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}} // transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}}
Map<String, String> transHeaderMetadata = MetadataContextHolder.get() Map<String, String> transHeaderMetadata = MetadataContextHolder.get()
.getFragmentContext(FRAGMENT_RAW_TRANSHEADERS); .getFragmentContext(FRAGMENT_RAW_TRANSHEADERS);
if (!CollectionUtils.isEmpty(transHeaderMetadata)) { if (!CollectionUtils.isEmpty(transHeaderMetadata)) {
String transHeaders = transHeaderMetadata.keySet().stream().findFirst().orElse(""); String transHeaders = transHeaderMetadata.keySet().stream().findFirst().orElse("");
String[] transHeaderArray = transHeaders.split(","); String[] transHeaderArray = transHeaders.split(",");
HttpHeaders headers = serverHttpRequest.getHeaders(); HttpHeaders headers = serverHttpRequest.getHeaders();
Set<String> headerKeys = headers.keySet(); Set<String> headerKeys = headers.keySet();
Iterator<String> iterator = headerKeys.iterator(); for (String httpHeader : headerKeys) {
while (iterator.hasNext()) {
String httpHeader = iterator.next();
Arrays.stream(transHeaderArray).forEach(transHeader -> { Arrays.stream(transHeaderArray).forEach(transHeader -> {
if (transHeader.equals(httpHeader)) { if (transHeader.equals(httpHeader)) {
List<String> list = headers.get(httpHeader); List<String> list = headers.get(httpHeader);
String httpHeaderValue = JacksonUtils.serialize2Json(list); String httpHeaderValue = JacksonUtils.serialize2Json(list);
// for example, {"trans-headers-kv" : {"header1":"v1","header2":"v2"...}} // for example, {"trans-headers-kv" : {"header1":"v1","header2":"v2"...}}
MetadataContextHolder.get().putContext(FRAGMENT_RAW_TRANSHEADERS_KV, httpHeader, httpHeaderValue); MetadataContextHolder.get()
return; .putContext(FRAGMENT_RAW_TRANSHEADERS_KV, httpHeader, httpHeaderValue);
} }
}); });
} }

@ -75,7 +75,8 @@ public final class ReporterUtils {
resultRequest.setCallerService(new ServiceKey(sourceNamespace, sourceService)); resultRequest.setCallerService(new ServiceKey(sourceNamespace, sourceService));
} }
resultRequest.setHost(uri.getHost()); resultRequest.setHost(uri.getHost());
resultRequest.setPort(uri.getPort()); // -1 means access directly by url, and use http default port number 80
resultRequest.setPort(uri.getPort() == -1 ? 80 : uri.getPort());
return resultRequest; return resultRequest;
} }

Loading…
Cancel
Save