diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ad7836e8..7994186c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,3 +13,4 @@ - [feat:support new label expression](https://github.com/Tencent/spring-cloud-tencent/pull/641) - [feat:support read config file from local file system.](https://github.com/Tencent/spring-cloud-tencent/pull/643) - [feat:report the labels when using RestTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/644) +- [Optimize: optimize transfer metadata code](https://github.com/Tencent/spring-cloud-tencent/pull/650) diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java index baf6f7ad8..9b1d4fc7d 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataReactiveFilter.java @@ -20,12 +20,8 @@ package com.tencent.cloud.metadata.core; 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; import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.metadata.MetadataContextHolder; @@ -37,7 +33,6 @@ import reactor.core.publisher.Mono; import org.springframework.core.Ordered; import org.springframework.http.HttpHeaders; import org.springframework.http.server.reactive.ServerHttpRequest; -import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebFilter; @@ -46,8 +41,6 @@ import org.springframework.web.server.WebFilterChain; import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_DISPOSABLE_METADATA; import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_METADATA; -import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_RAW_TRANSHEADERS; -import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_RAW_TRANSHEADERS_KV; /** * Filter used for storing the metadata from upstream temporarily when web application is @@ -86,41 +79,14 @@ public class DecodeTransferMetadataReactiveFilter implements WebFilter, Ordered MetadataConstant.HeaderName.METADATA_CONTEXT, MetadataContextHolder.get()); - setCompleteTransHeaderIntoMC(serverHttpRequest); + TransHeadersTransfer.transfer(serverHttpRequest); + return webFilterChain.filter(serverWebExchange) .doOnError(throwable -> LOG.error("handle metadata[{}] error.", MetadataContextHolder.get(), throwable)) .doFinally((type) -> MetadataContextHolder.remove()); } - /** - * According to ServerHttpRequest and trans-headers(key list in string type) in metadata, build - * the complete headers(key-value list in map type) into metadata. - */ - private void setCompleteTransHeaderIntoMC(ServerHttpRequest serverHttpRequest) { - // transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}} - Map 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 headerKeys = headers.keySet(); - Iterator iterator = headerKeys.iterator(); - while (iterator.hasNext()) { - String httpHeader = iterator.next(); - Arrays.stream(transHeaderArray).forEach(transHeader -> { - if (transHeader.equals(httpHeader)) { - List list = headers.get(httpHeader); - String httpHeaderValue = JacksonUtils.serialize2Json(list); - MetadataContextHolder.get() - .putContext(FRAGMENT_RAW_TRANSHEADERS_KV, httpHeader, httpHeaderValue); - } - }); - } - } - } - private Map getIntervalMetadata(ServerHttpRequest serverHttpRequest, String headerName) { HttpHeaders httpHeaders = serverHttpRequest.getHeaders(); String customMetadataStr = httpHeaders.getFirst(headerName); diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java index b446af2e6..3bf0731cb 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/DecodeTransferMetadataServletFilter.java @@ -21,8 +21,6 @@ package com.tencent.cloud.metadata.core; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; -import java.util.Arrays; -import java.util.Enumeration; import java.util.HashMap; import java.util.Map; @@ -39,15 +37,12 @@ import org.slf4j.LoggerFactory; import org.springframework.core.annotation.Order; import org.springframework.lang.NonNull; -import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import org.springframework.web.filter.OncePerRequestFilter; import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_DISPOSABLE_METADATA; import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_METADATA; -import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_RAW_TRANSHEADERS; -import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_RAW_TRANSHEADERS_KV; /** * Filter used for storing the metadata from upstream temporarily when web application is @@ -76,33 +71,14 @@ public class DecodeTransferMetadataServletFilter extends OncePerRequestFilter { MetadataContextHolder.init(mergedTransitiveMetadata, mergedDisposableMetadata); - setCompleteTransHeaderIntoMC(httpServletRequest); - filterChain.doFilter(httpServletRequest, httpServletResponse); - } + TransHeadersTransfer.transfer(httpServletRequest); - /** - * According to HttpServletRequest and trans-headers(key list in string type) in metadata, build - * the complete headers(key-value list in map type) into metadata. - */ - private void setCompleteTransHeaderIntoMC(HttpServletRequest httpServletRequest) { - // transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}} - Map transHeaderMetadata = MetadataContextHolder.get() - .getFragmentContext(FRAGMENT_RAW_TRANSHEADERS); - if (!CollectionUtils.isEmpty(transHeaderMetadata)) { - String transHeaders = transHeaderMetadata.keySet().stream().findFirst().orElse(""); - String[] transHeaderArray = transHeaders.split(","); - Enumeration httpHeaders = httpServletRequest.getHeaderNames(); - while (httpHeaders.hasMoreElements()) { - String httpHeader = httpHeaders.nextElement(); - Arrays.stream(transHeaderArray).forEach(transHeader -> { - 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); - } - }); - } + try { + filterChain.doFilter(httpServletRequest, httpServletResponse); + } + finally { + // Clean up ThreadLocal. + MetadataContextHolder.remove(); } } diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java index b35ec02df..68d3ed000 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java @@ -19,7 +19,6 @@ package com.tencent.cloud.metadata.core; import java.io.UnsupportedEncodingException; -import java.util.HashMap; import java.util.Map; import com.tencent.cloud.common.constant.MetadataConstant; @@ -59,21 +58,14 @@ public class EncodeTransferMedataFeignInterceptor implements RequestInterceptor, public void apply(RequestTemplate requestTemplate) { // get metadata of current thread MetadataContext metadataContext = MetadataContextHolder.get(); - Map customMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - Map disposableMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE); - Map transHeaders = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_RAW_TRANSHEADERS_KV); + Map customMetadata = metadataContext.getCustomMetadata(); + Map disposableMetadata = metadataContext.getDisposableMetadata(); + Map transHeaders = metadataContext.getTransHeaders(); - // Clean up one-time metadata coming from upstream . - Map newestCustomMetadata = new HashMap<>(); - customMetadata.forEach((key, value) -> { - if (!disposableMetadata.containsKey(key)) { - newestCustomMetadata.put(key, value); - } - }); this.buildMetadataHeader(requestTemplate, disposableMetadata, CUSTOM_DISPOSABLE_METADATA); // process custom metadata - this.buildMetadataHeader(requestTemplate, newestCustomMetadata, CUSTOM_METADATA); + this.buildMetadataHeader(requestTemplate, customMetadata, CUSTOM_METADATA); // set headers that need to be transmitted from the upstream this.buildTransmittedHeader(requestTemplate, transHeaders); diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java index 89d5c5f6e..9420b6283 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java @@ -21,7 +21,6 @@ package com.tencent.cloud.metadata.core; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; -import java.util.HashMap; import java.util.Map; import com.tencent.cloud.common.constant.MetadataConstant; @@ -59,21 +58,15 @@ public class EncodeTransferMedataRestTemplateInterceptor implements ClientHttpRe @NonNull ClientHttpRequestExecution clientHttpRequestExecution) throws IOException { // get metadata of current thread MetadataContext metadataContext = MetadataContextHolder.get(); - Map customMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - Map disposableMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE); - Map transHeaders = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_RAW_TRANSHEADERS_KV); + Map customMetadata = metadataContext.getCustomMetadata(); + Map disposableMetadata = metadataContext.getDisposableMetadata(); + Map transHeaders = metadataContext.getTransHeaders(); - Map newestCustomMetadata = new HashMap<>(); - customMetadata.forEach((key, value) -> { - if (!disposableMetadata.containsKey(key)) { - newestCustomMetadata.put(key, value); - } - }); // build custom disposable metadata request header this.buildMetadataHeader(httpRequest, disposableMetadata, CUSTOM_DISPOSABLE_METADATA); // build custom metadata request header - this.buildMetadataHeader(httpRequest, newestCustomMetadata, CUSTOM_METADATA); + this.buildMetadataHeader(httpRequest, customMetadata, CUSTOM_METADATA); // set headers that need to be transmitted from the upstream this.buildTransmittedHeader(httpRequest, transHeaders); diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilter.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilter.java index a48f1d305..33bed98ca 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilter.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataScgFilter.java @@ -20,12 +20,7 @@ package com.tencent.cloud.metadata.core; 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; import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.metadata.MetadataContext; @@ -36,7 +31,6 @@ import reactor.core.publisher.Mono; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; -import org.springframework.http.HttpHeaders; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.util.CollectionUtils; import org.springframework.web.server.ServerWebExchange; @@ -44,8 +38,6 @@ import org.springframework.web.server.ServerWebExchange; import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_DISPOSABLE_METADATA; import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_METADATA; -import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_RAW_TRANSHEADERS; -import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_RAW_TRANSHEADERS_KV; import static org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter.LOAD_BALANCER_CLIENT_FILTER_ORDER; /** @@ -73,51 +65,15 @@ public class EncodeTransferMedataScgFilter implements GlobalFilter, Ordered { metadataContext = MetadataContextHolder.get(); } - Map customMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); - Map disposableMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE); + Map customMetadata = metadataContext.getCustomMetadata(); + Map disposableMetadata = metadataContext.getDisposableMetadata(); - // Clean upstream disposable metadata. - Map newestCustomMetadata = new HashMap<>(); - customMetadata.forEach((key, value) -> { - if (!disposableMetadata.containsKey(key)) { - newestCustomMetadata.put(key, value); - } - }); - - this.buildMetadataHeader(builder, newestCustomMetadata, CUSTOM_METADATA); + this.buildMetadataHeader(builder, customMetadata, CUSTOM_METADATA); this.buildMetadataHeader(builder, disposableMetadata, CUSTOM_DISPOSABLE_METADATA); - setCompleteTransHeaderIntoMC(exchange.getRequest()); - return chain.filter(exchange.mutate().request(builder.build()).build()); - } + TransHeadersTransfer.transfer(exchange.getRequest()); - /** - * According to ServerHttpRequest and trans-headers(key list in string type) in metadata, build - * the complete headers(key-value list in map type) into metadata. - */ - private void setCompleteTransHeaderIntoMC(ServerHttpRequest serverHttpRequest) { - // transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}} - Map 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 headerKeys = headers.keySet(); - Iterator iterator = headerKeys.iterator(); - while (iterator.hasNext()) { - String httpHeader = iterator.next(); - Arrays.stream(transHeaderArray).forEach(transHeader -> { - if (transHeader.equals(httpHeader)) { - List 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 chain.filter(exchange.mutate().request(builder.build()).build()); } /** diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/TransHeadersTransfer.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/TransHeadersTransfer.java new file mode 100644 index 000000000..f272d2d82 --- /dev/null +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/TransHeadersTransfer.java @@ -0,0 +1,98 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.metadata.core; + +import java.util.Arrays; +import java.util.Enumeration; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.servlet.http.HttpServletRequest; + +import com.tencent.cloud.common.metadata.MetadataContextHolder; +import com.tencent.cloud.common.util.JacksonUtils; + +import org.springframework.http.HttpHeaders; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.util.CollectionUtils; + +/** + * According to request and trans-headers(key list in string type) in metadata, build + * the complete headers(key-value list in map type) into metadata. + * + * @author lingxiao.wlx + */ +public final class TransHeadersTransfer { + + private TransHeadersTransfer() { + } + + /** + * According to {@link HttpServletRequest} and trans-headers(key list in string type) in metadata, build + * the complete headers(key-value list in map type) into metadata. + * + * @param httpServletRequest httpServletRequest + */ + public static void transfer(HttpServletRequest httpServletRequest) { + // transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}} + Map transHeaderMetadata = MetadataContextHolder.get().getTransHeaders(); + if (!CollectionUtils.isEmpty(transHeaderMetadata)) { + String transHeaders = transHeaderMetadata.keySet().stream().findFirst().orElse(""); + String[] transHeaderArray = transHeaders.split(","); + Enumeration httpHeaders = httpServletRequest.getHeaderNames(); + while (httpHeaders.hasMoreElements()) { + String httpHeader = httpHeaders.nextElement(); + Arrays.stream(transHeaderArray).forEach(transHeader -> { + if (transHeader.equals(httpHeader)) { + String httpHeaderValue = httpServletRequest.getHeader(httpHeader); + // for example, {"trans-headers-kv" : {"header1":"v1","header2":"v2"...}} + MetadataContextHolder.get().setTransHeadersKV(httpHeader, httpHeaderValue); + } + }); + } + } + } + + /** + * According to {@link ServerHttpRequest} and trans-headers(key list in string type) in metadata, build + * the complete headers(key-value list in map type) into metadata. + * + * @param serverHttpRequest serverHttpRequest + */ + public static void transfer(ServerHttpRequest serverHttpRequest) { + // transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}} + Map transHeaderMetadata = MetadataContextHolder.get().getTransHeaders(); + if (!CollectionUtils.isEmpty(transHeaderMetadata)) { + String transHeaders = transHeaderMetadata.keySet().stream().findFirst().orElse(""); + String[] transHeaderArray = transHeaders.split(","); + HttpHeaders headers = serverHttpRequest.getHeaders(); + Set headerKeys = headers.keySet(); + for (String httpHeader : headerKeys) { + Arrays.stream(transHeaderArray).forEach(transHeader -> { + if (transHeader.equals(httpHeader)) { + List list = headers.get(httpHeader); + String httpHeaderValue = JacksonUtils.serialize2Json(list); + // for example, {"trans-headers-kv" : {"header1":"v1","header2":"v2"...}} + MetadataContextHolder.get().setTransHeadersKV(httpHeader, httpHeaderValue); + } + }); + } + } + } +} diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/TransHeadersTransferTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/TransHeadersTransferTest.java new file mode 100644 index 000000000..a79ad5ce2 --- /dev/null +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/TransHeadersTransferTest.java @@ -0,0 +1,85 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.metadata.core; + +import java.util.Map; + +import com.tencent.cloud.common.metadata.MetadataContext; +import com.tencent.cloud.common.metadata.MetadataContextHolder; +import com.tencent.cloud.common.util.JacksonUtils; +import org.junit.AfterClass; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.runner.RunWith; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.mock.http.server.reactive.MockServerHttpRequest; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * Test for {@link TransHeadersTransfer}. + * + * @author lingxiao.wlx + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = RANDOM_PORT, + classes = DecodeTransferMetadataServletFilterTest.TestApplication.class, + properties = {"spring.config.location = classpath:application-test.yml"}) +public class TransHeadersTransferTest { + @AfterClass + public static void afterClass() { + MetadataContextHolder.remove(); + } + + @Test + public void transferServletTest() { + MetadataContext metadataContext = MetadataContextHolder.get(); + metadataContext.setTransHeaders("header1,header2,header3", ""); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.addHeader("header1", "1"); + request.addHeader("header2", "2"); + request.addHeader("header3", "3"); + TransHeadersTransfer.transfer(request); + Map transHeadersKV = MetadataContextHolder.get().getTransHeadersKV(); + Assertions.assertEquals(transHeadersKV.get("header1"), "1"); + Assertions.assertEquals(transHeadersKV.get("header2"), "2"); + Assertions.assertEquals(transHeadersKV.get("header3"), "3"); + } + + @Test + public void transferReactiveTest() { + MetadataContext metadataContext = MetadataContextHolder.get(); + metadataContext.setTransHeaders("header1,header2,header3", ""); + MockServerHttpRequest.BaseBuilder builder = MockServerHttpRequest.get(""); + String[] header1 = {"1"}; + String[] header2 = {"2"}; + String[] header3 = {"3"}; + builder.header("header1", header1); + builder.header("header2", header2); + builder.header("header3", header3); + MockServerHttpRequest request = builder.build(); + TransHeadersTransfer.transfer(request); + Map transHeadersKV = MetadataContextHolder.get().getTransHeadersKV(); + Assertions.assertEquals(transHeadersKV.get("header1"), JacksonUtils.serialize2Json(header1)); + Assertions.assertEquals(transHeadersKV.get("header2"), JacksonUtils.serialize2Json(header2)); + Assertions.assertEquals(transHeadersKV.get("header3"), JacksonUtils.serialize2Json(header3)); + } +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java index bd441dfd3..b12a0f81a 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java @@ -124,8 +124,7 @@ public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceI PolarisRouterContext routerContext = new PolarisRouterContext(); - routerContext.putLabels(RouterConstant.TRANSITIVE_LABELS, MetadataContextHolder.get() - .getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)); + routerContext.putLabels(RouterConstant.TRANSITIVE_LABELS, MetadataContextHolder.get().getTransitiveMetadata()); Map labelHeaderValuesMap = new HashMap<>(); try { diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptor.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptor.java index def65657b..80b3e67e2 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptor.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptor.java @@ -109,8 +109,7 @@ public class RouterLabelFeignInterceptor implements RequestInterceptor, Ordered } // labels from downstream - Map transitiveLabels = MetadataContextHolder.get() - .getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + Map transitiveLabels = MetadataContextHolder.get().getTransitiveMetadata(); labels.putAll(transitiveLabels); // pass label by header diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptor.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptor.java index d457d8729..2e39405b2 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptor.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptor.java @@ -138,8 +138,7 @@ public class RouterLabelRestTemplateInterceptor implements ClientHttpRequestInte } // labels from downstream - Map transitiveLabels = MetadataContextHolder.get() - .getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + Map transitiveLabels = MetadataContextHolder.get().getTransitiveMetadata(); labels.putAll(transitiveLabels); // pass label by header diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/scg/PolarisReactiveLoadBalancerClientFilter.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/scg/PolarisReactiveLoadBalancerClientFilter.java index 74b254a1d..1fd3e2186 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/scg/PolarisReactiveLoadBalancerClientFilter.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/scg/PolarisReactiveLoadBalancerClientFilter.java @@ -255,8 +255,7 @@ public class PolarisReactiveLoadBalancerClientFilter extends ReactiveLoadBalance } // labels from downstream - Map transitiveLabels = MetadataContextHolder.get() - .getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + Map transitiveLabels = MetadataContextHolder.get().getTransitiveMetadata(); labels.putAll(transitiveLabels); return labels; diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java index 703d1b426..644dd6c8c 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java @@ -94,7 +94,7 @@ public class RouterLabelFeignInterceptorTest { Map transitiveLabels = new HashMap<>(); transitiveLabels.put("k1", "v1"); transitiveLabels.put("k2", "v22"); - when(metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)).thenReturn(transitiveLabels); + when(metadataContext.getTransitiveMetadata()).thenReturn(transitiveLabels); // mock MetadataContextHolder#get try (MockedStatic mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class)) { diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptorTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptorTest.java index 072603bdc..7226f7e4d 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptorTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/resttemplate/RouterLabelRestTemplateInterceptorTest.java @@ -134,7 +134,7 @@ public class RouterLabelRestTemplateInterceptorTest { Map transitiveLabels = new HashMap<>(); transitiveLabels.put("k1", "v1"); transitiveLabels.put("k2", "v22"); - when(metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)).thenReturn(transitiveLabels); + when(metadataContext.getTransitiveMetadata()).thenReturn(transitiveLabels); routerLabels.putAll(transitiveLabels); mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext); diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisReactiveLoadBalancerClientFilterTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisReactiveLoadBalancerClientFilterTest.java index 5b8da5255..4c7ee91fc 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisReactiveLoadBalancerClientFilterTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisReactiveLoadBalancerClientFilterTest.java @@ -98,7 +98,7 @@ public class PolarisReactiveLoadBalancerClientFilterTest { Map transitiveLabels = new HashMap<>(); transitiveLabels.put("t1", "v1"); transitiveLabels.put("t2", "v2"); - when(metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)).thenReturn(transitiveLabels); + when(metadataContext.getTransitiveMetadata()).thenReturn(transitiveLabels); mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class); mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext); diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java index d0ccaf084..7bd08eb41 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContext.java @@ -19,6 +19,7 @@ package com.tencent.cloud.common.metadata; import java.util.Collections; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -117,6 +118,62 @@ public class MetadataContext { this.fragmentContexts = new ConcurrentHashMap<>(); } + public Map getDisposableMetadata() { + return this.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE); + } + + public Map getTransitiveMetadata() { + return this.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + } + + public Map getCustomMetadata() { + Map transitiveMetadata = this.getTransitiveMetadata(); + Map disposableMetadata = this.getDisposableMetadata(); + Map customMetadata = new HashMap<>(); + // Clean up one-time metadata coming from upstream . + transitiveMetadata.forEach((key, value) -> { + if (!disposableMetadata.containsKey(key)) { + customMetadata.put(key, value); + } + }); + return Collections.unmodifiableMap(customMetadata); + } + + public Map getTransHeaders() { + return this.getFragmentContext(MetadataContext.FRAGMENT_RAW_TRANSHEADERS); + } + + public Map getTransHeadersKV() { + return this.getFragmentContext(MetadataContext.FRAGMENT_RAW_TRANSHEADERS_KV); + } + + public Map getLoadbalancerMetadata() { + return this.getFragmentContext(FRAGMENT_LOAD_BALANCER); + } + + public void setTransitiveMetadata(Map transitiveMetadata) { + this.putFragmentContext(FRAGMENT_TRANSITIVE, Collections.unmodifiableMap(transitiveMetadata)); + } + + public void setDisposableMetadata(Map disposableMetadata) { + this.putFragmentContext(FRAGMENT_DISPOSABLE, Collections.unmodifiableMap(disposableMetadata)); + } + + public void setUpstreamDisposableMetadata(Map upstreamDisposableMetadata) { + this.putFragmentContext(FRAGMENT_UPSTREAM_DISPOSABLE, Collections.unmodifiableMap(upstreamDisposableMetadata)); + } + + public void setTransHeadersKV(String key, String value) { + this.putContext(FRAGMENT_RAW_TRANSHEADERS_KV, key, value); + } + + public void setTransHeaders(String key, String value) { + this.putContext(FRAGMENT_RAW_TRANSHEADERS, key, value); + } + + public void setLoadbalancer(String key, String value) { + this.putContext(FRAGMENT_LOAD_BALANCER, key, value); + } public Map getFragmentContext(String fragment) { Map fragmentContext = fragmentContexts.get(fragment); diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContextHolder.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContextHolder.java index 60983ef69..818fee7f7 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContextHolder.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/MetadataContextHolder.java @@ -29,8 +29,6 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_DISPOSABLE; -import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_RAW_TRANSHEADERS; -import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_TRANSITIVE; import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_UPSTREAM_DISPOSABLE; /** @@ -67,11 +65,11 @@ public final class MetadataContextHolder { // init static transitive metadata MetadataContext metadataContext = new MetadataContext(); - metadataContext.putFragmentContext(FRAGMENT_TRANSITIVE, staticMetadataManager.getMergedStaticTransitiveMetadata()); - metadataContext.putFragmentContext(FRAGMENT_DISPOSABLE, staticMetadataManager.getMergedStaticDisposableMetadata()); + metadataContext.setTransitiveMetadata(staticMetadataManager.getMergedStaticTransitiveMetadata()); + metadataContext.setDisposableMetadata(staticMetadataManager.getMergedStaticDisposableMetadata()); if (StringUtils.hasText(staticMetadataManager.getTransHeaderFromEnv())) { - metadataContext.putContext(FRAGMENT_RAW_TRANSHEADERS, staticMetadataManager.getTransHeaderFromEnv(), ""); + metadataContext.setTransHeaders(staticMetadataManager.getTransHeaderFromEnv(), ""); } METADATA_CONTEXT.set(metadataContext); @@ -132,17 +130,17 @@ public final class MetadataContextHolder { // Save transitive metadata to ThreadLocal. if (!CollectionUtils.isEmpty(dynamicTransitiveMetadata)) { - Map staticTransitiveMetadata = metadataContext.getFragmentContext(FRAGMENT_TRANSITIVE); + Map staticTransitiveMetadata = metadataContext.getTransitiveMetadata(); Map mergedTransitiveMetadata = new HashMap<>(); mergedTransitiveMetadata.putAll(staticTransitiveMetadata); mergedTransitiveMetadata.putAll(dynamicTransitiveMetadata); - metadataContext.putFragmentContext(FRAGMENT_TRANSITIVE, Collections.unmodifiableMap(mergedTransitiveMetadata)); + metadataContext.setTransitiveMetadata(Collections.unmodifiableMap(mergedTransitiveMetadata)); Map mergedDisposableMetadata = new HashMap<>(dynamicDisposableMetadata); - metadataContext.putFragmentContext(FRAGMENT_UPSTREAM_DISPOSABLE, Collections.unmodifiableMap(mergedDisposableMetadata)); + metadataContext.setUpstreamDisposableMetadata(Collections.unmodifiableMap(mergedDisposableMetadata)); Map staticDisposableMetadata = metadataContext.getFragmentContext(FRAGMENT_DISPOSABLE); - metadataContext.putFragmentContext(FRAGMENT_DISPOSABLE, Collections.unmodifiableMap(staticDisposableMetadata)); + metadataContext.setDisposableMetadata(Collections.unmodifiableMap(staticDisposableMetadata)); } MetadataContextHolder.set(metadataContext); } diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/MetadataContextHolderTest.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/MetadataContextHolderTest.java index c6b7cbcd2..c7cdf5bf8 100644 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/MetadataContextHolderTest.java +++ b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/MetadataContextHolderTest.java @@ -45,10 +45,10 @@ public class MetadataContextHolderTest { customMetadata.put("a", "1"); customMetadata.put("b", "2"); MetadataContext metadataContext = MetadataContextHolder.get(); - metadataContext.putFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE, customMetadata); + metadataContext.setTransitiveMetadata(customMetadata); MetadataContextHolder.set(metadataContext); - customMetadata = MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + metadataContext.setTransitiveMetadata(customMetadata); Assertions.assertThat(customMetadata.get("a")).isEqualTo("1"); Assertions.assertThat(customMetadata.get("b")).isEqualTo("2"); @@ -60,7 +60,7 @@ public class MetadataContextHolderTest { customMetadata.put("c", "3"); MetadataContextHolder.init(customMetadata, new HashMap<>()); metadataContext = MetadataContextHolder.get(); - customMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + customMetadata = metadataContext.getTransitiveMetadata(); Assertions.assertThat(customMetadata.get("a")).isEqualTo("1"); Assertions.assertThat(customMetadata.get("b")).isEqualTo("22"); Assertions.assertThat(customMetadata.get("c")).isEqualTo("3"); diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/ExpressionParserV1Test.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/ExpressionParserV1Test.java index 2f6e5b64e..ae55900f1 100644 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/ExpressionParserV1Test.java +++ b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/ExpressionParserV1Test.java @@ -22,7 +22,7 @@ import org.junit.Assert; import org.junit.Test; /** - * Test for {@link ExpressionParserV1} + * Test for {@link ExpressionParserV1}. * @author lepdou 2022-10-08 */ public class ExpressionParserV1Test { diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/ExpressionParserV2Test.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/ExpressionParserV2Test.java index a97b64ba5..38d4a6617 100644 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/ExpressionParserV2Test.java +++ b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/util/ExpressionParserV2Test.java @@ -22,7 +22,7 @@ import org.junit.Assert; import org.junit.Test; /** - * Test for {@link ExpressionParserV2} + * Test for {@link ExpressionParserV2}. * @author lepdou 2022-10-08 */ public class ExpressionParserV2Test { diff --git a/spring-cloud-tencent-examples/metadata-transfer-example/metadata-backend/src/main/java/com/tencent/cloud/metadata/service/backend/MetadataBackendController.java b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-backend/src/main/java/com/tencent/cloud/metadata/service/backend/MetadataBackendController.java index 7da621e81..9ee58a04a 100644 --- a/spring-cloud-tencent-examples/metadata-transfer-example/metadata-backend/src/main/java/com/tencent/cloud/metadata/service/backend/MetadataBackendController.java +++ b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-backend/src/main/java/com/tencent/cloud/metadata/service/backend/MetadataBackendController.java @@ -55,7 +55,7 @@ public class MetadataBackendController { // Get Custom Metadata From Context MetadataContext context = MetadataContextHolder.get(); - Map customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + Map customMetadataMap = context.getTransitiveMetadata(); customMetadataMap.forEach((key, value) -> { LOG.info("Metadata Backend Custom Metadata (Key-Value): {} : {}", key, value); diff --git a/spring-cloud-tencent-examples/metadata-transfer-example/metadata-frontend/src/main/java/com/tencent/cloud/metadata/service/frontend/MetadataFrontendController.java b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-frontend/src/main/java/com/tencent/cloud/metadata/service/frontend/MetadataFrontendController.java index 3d107f555..20ca8ae3a 100644 --- a/spring-cloud-tencent-examples/metadata-transfer-example/metadata-frontend/src/main/java/com/tencent/cloud/metadata/service/frontend/MetadataFrontendController.java +++ b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-frontend/src/main/java/com/tencent/cloud/metadata/service/frontend/MetadataFrontendController.java @@ -69,7 +69,7 @@ public class MetadataFrontendController { // Get Custom Metadata From Context MetadataContext context = MetadataContextHolder.get(); - Map customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + Map customMetadataMap = context.getTransitiveMetadata(); customMetadataMap.forEach((key, value) -> { LOG.info("Metadata Middle Custom Metadata (Key-Value): {} : {}", key, value); @@ -116,7 +116,7 @@ public class MetadataFrontendController { // Get Custom Metadata From Context MetadataContext context = MetadataContextHolder.get(); - Map customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + Map customMetadataMap = context.getTransitiveMetadata(); customMetadataMap.forEach((key, value) -> { LOG.info("Metadata Middle Custom Metadata (Key-Value): {} : {}", key, value); diff --git a/spring-cloud-tencent-examples/metadata-transfer-example/metadata-middle/src/main/java/com/tencent/cloud/metadata/service/middle/MetadataMiddleController.java b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-middle/src/main/java/com/tencent/cloud/metadata/service/middle/MetadataMiddleController.java index 7ea673a50..18f075e2f 100644 --- a/spring-cloud-tencent-examples/metadata-transfer-example/metadata-middle/src/main/java/com/tencent/cloud/metadata/service/middle/MetadataMiddleController.java +++ b/spring-cloud-tencent-examples/metadata-transfer-example/metadata-middle/src/main/java/com/tencent/cloud/metadata/service/middle/MetadataMiddleController.java @@ -92,7 +92,7 @@ public class MetadataMiddleController { // Get Custom Metadata From Context MetadataContext context = MetadataContextHolder.get(); - Map customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + Map customMetadataMap = context.getTransitiveMetadata(); customMetadataMap.forEach((key, value) -> { LOG.info("Metadata Middle Custom Metadata (Key-Value): {} : {}", key, value); diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/staining/TrafficStainingGatewayFilter.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/staining/TrafficStainingGatewayFilter.java index c6ddfd457..936eec417 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/staining/TrafficStainingGatewayFilter.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/staining/TrafficStainingGatewayFilter.java @@ -77,7 +77,7 @@ public class TrafficStainingGatewayFilter implements GlobalFilter, Ordered { metadataContext = MetadataContextHolder.get(); } - Map oldTransitiveMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); + Map oldTransitiveMetadata = metadataContext.getTransitiveMetadata(); // append new transitive metadata Map newTransitiveMetadata = new HashMap<>(oldTransitiveMetadata); diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporter.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporter.java index 8920530e3..021ba7268 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporter.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporter.java @@ -115,8 +115,7 @@ public class EnhancedRestTemplateReporter extends AbstractPolarisReporterAdapter private void reportResult(URI url, ClientHttpResponse response) { ServiceCallResult resultRequest = createServiceCallResult(url); try { - Map loadBalancerContext = MetadataContextHolder.get() - .getFragmentContext(MetadataContext.FRAGMENT_LOAD_BALANCER); + Map loadBalancerContext = MetadataContextHolder.get().getLoadbalancerMetadata(); String targetHost = loadBalancerContext.get("host"); String targetPort = loadBalancerContext.get("port"); diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/LoadBalancerClientAspectUtils.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/LoadBalancerClientAspectUtils.java index 0eb2dfd7f..1b9f11632 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/LoadBalancerClientAspectUtils.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/LoadBalancerClientAspectUtils.java @@ -17,7 +17,6 @@ package com.tencent.cloud.rpc.enhancement.resttemplate; -import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContextHolder; import org.aspectj.lang.ProceedingJoinPoint; @@ -37,9 +36,8 @@ public final class LoadBalancerClientAspectUtils { Object server = joinPoint.getArgs()[0]; if (server instanceof ServiceInstance) { ServiceInstance instance = (ServiceInstance) server; - MetadataContextHolder.get().putContext(MetadataContext.FRAGMENT_LOAD_BALANCER, "host", instance.getHost()); - MetadataContextHolder.get() - .putContext(MetadataContext.FRAGMENT_LOAD_BALANCER, "port", String.valueOf(instance.getPort())); + MetadataContextHolder.get().setLoadbalancer("host", instance.getHost()); + MetadataContextHolder.get().setLoadbalancer("port", String.valueOf(instance.getPort())); } } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporterTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporterTest.java index 099f266d8..d84d7d335 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporterTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateReporterTest.java @@ -82,7 +82,7 @@ public class EnhancedRestTemplateReporterTest { Map loadBalancerContext = new HashMap<>(); loadBalancerContext.put("host", "1.1.1.1"); loadBalancerContext.put("port", "8080"); - when(metadataContext.getFragmentContext(MetadataContext.FRAGMENT_LOAD_BALANCER)).thenReturn(loadBalancerContext); + when(metadataContext.getLoadbalancerMetadata()).thenReturn(loadBalancerContext); mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class); mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);