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

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

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

@ -75,7 +75,8 @@ public final class ReporterUtils {
resultRequest.setCallerService(new ServiceKey(sourceNamespace, sourceService));
}
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;
}

Loading…
Cancel
Save