diff --git a/CHANGELOG.md b/CHANGELOG.md index 77da603f3..f3827b8a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,3 +19,4 @@ - [Feature: remove location metadata](https://github.com/Tencent/spring-cloud-tencent/pull/535) - [Feature: remove location metadata](https://github.com/Tencent/spring-cloud-tencent/pull/541) - [fix:set error handler named EnhancedRestTemplateReporter for RestTemplate](https://github.com/Tencent/spring-cloud-tencent/pull/543) +- [Fix issue: report the labels in request when report the result of invocation by Feign](https://github.com/Tencent/spring-cloud-tencent/pull/546) diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/feign/PolarisFeignLoadBalancer.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/feign/PolarisFeignLoadBalancer.java index 618d6c2f5..78117eeb1 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/feign/PolarisFeignLoadBalancer.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/feign/PolarisFeignLoadBalancer.java @@ -28,11 +28,11 @@ import java.util.Optional; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.reactive.LoadBalancerCommand; +import com.tencent.cloud.common.constant.RouterConstants; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.polaris.router.PolarisRouterContext; -import com.tencent.cloud.polaris.router.RouterConstants; import org.springframework.cloud.netflix.ribbon.ServerIntrospector; import org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer; 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 df68bbfd6..c00c518f7 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 @@ -27,11 +27,11 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.tencent.cloud.common.constant.RouterConstants; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.common.util.JacksonUtils; -import com.tencent.cloud.polaris.router.RouterConstants; import com.tencent.cloud.polaris.router.RouterRuleLabelResolver; import com.tencent.cloud.polaris.router.spi.FeignRouterLabelResolver; import feign.RequestInterceptor; diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/PolarisFeignLoadBalancerTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/PolarisFeignLoadBalancerTest.java index 930abe89e..29745a1a8 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/PolarisFeignLoadBalancerTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/PolarisFeignLoadBalancerTest.java @@ -26,12 +26,12 @@ import java.util.Map; import com.netflix.client.config.DefaultClientConfigImpl; import com.netflix.loadbalancer.ILoadBalancer; +import com.tencent.cloud.common.constant.RouterConstants; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.util.ApplicationContextAwareUtils; import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.polaris.router.PolarisRouterContext; -import com.tencent.cloud.polaris.router.RouterConstants; import com.tencent.cloud.polaris.router.SimpleLoadBalancer; import org.junit.Assert; import org.junit.Test; 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 90f8b7caa..4dd56e8bb 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 @@ -27,12 +27,12 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import com.tencent.cloud.common.constant.RouterConstants; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.common.util.ApplicationContextAwareUtils; import com.tencent.cloud.common.util.JacksonUtils; -import com.tencent.cloud.polaris.router.RouterConstants; import com.tencent.cloud.polaris.router.RouterRuleLabelResolver; import com.tencent.cloud.polaris.router.spi.FeignRouterLabelResolver; import feign.RequestTemplate; diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterConstants.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/RouterConstants.java similarity index 96% rename from spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterConstants.java rename to spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/RouterConstants.java index d074c5239..bd9baad8f 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterConstants.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/RouterConstants.java @@ -16,7 +16,7 @@ * */ -package com.tencent.cloud.polaris.router; +package com.tencent.cloud.common.constant; /** * Router constants. diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/ReporterUtils.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/ReporterUtils.java index 432b911e2..fe1738fa7 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/ReporterUtils.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/ReporterUtils.java @@ -17,14 +17,24 @@ package com.tencent.cloud.rpc.enhancement.feign.plugin.reporter; +import java.io.UnsupportedEncodingException; import java.net.URI; +import java.net.URLDecoder; +import java.util.Collection; +import com.tencent.cloud.common.constant.RouterConstants; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.polaris.api.pojo.RetStatus; import com.tencent.polaris.api.pojo.ServiceKey; import com.tencent.polaris.api.rpc.ServiceCallResult; +import com.tencent.polaris.api.utils.CollectionUtils; import feign.Request; +import feign.RequestTemplate; import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; /** * Util for polaris reporter. @@ -33,6 +43,8 @@ import org.apache.commons.lang.StringUtils; */ public final class ReporterUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(ReporterUtils.class); + private ReporterUtils() { } @@ -40,7 +52,19 @@ public final class ReporterUtils { ServiceCallResult resultRequest = new ServiceCallResult(); resultRequest.setNamespace(MetadataContext.LOCAL_NAMESPACE); - String serviceName = request.requestTemplate().feignTarget().name(); + RequestTemplate requestTemplate = request.requestTemplate(); + String serviceName = requestTemplate.feignTarget().name(); + Collection labels = requestTemplate.headers().get(RouterConstants.ROUTER_LABEL_HEADER); + if (CollectionUtils.isNotEmpty(labels) && labels.iterator().hasNext()) { + String label = labels.iterator().next(); + try { + label = URLDecoder.decode(label, UTF_8); + } + catch (UnsupportedEncodingException e) { + LOGGER.error("unsupported charset exception " + UTF_8, e); + } + resultRequest.setLabels(convertLabel(label)); + } resultRequest.setService(serviceName); URI uri = URI.create(request.url()); resultRequest.setMethod(uri.getPath()); @@ -52,7 +76,12 @@ public final class ReporterUtils { } resultRequest.setHost(uri.getHost()); resultRequest.setPort(uri.getPort()); - return resultRequest; } + + private static String convertLabel(String label) { + label = label.replaceAll("\"|\\{|\\}", "") + .replaceAll(",", "|"); + return label; + } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/ReporterUtilsTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/ReporterUtilsTest.java index de60c99b8..918b04ea8 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/ReporterUtilsTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/feign/plugin/reporter/ReporterUtilsTest.java @@ -17,6 +17,10 @@ package com.tencent.cloud.rpc.enhancement.feign.plugin.reporter; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +import com.tencent.cloud.common.constant.RouterConstants; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.util.ApplicationContextAwareUtils; import com.tencent.polaris.api.pojo.RetStatus; @@ -33,6 +37,7 @@ import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST; import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER; import static org.assertj.core.api.Assertions.assertThat; @@ -77,6 +82,12 @@ public class ReporterUtilsTest { // mock RequestTemplate.class RequestTemplate requestTemplate = new RequestTemplate(); requestTemplate.feignTarget(target); + try { + requestTemplate.header(RouterConstants.ROUTER_LABEL_HEADER, URLEncoder.encode("{\"k1\":\"v1\",\"k2\":\"v2\"}", UTF_8)); + } + catch (UnsupportedEncodingException e) { + throw new RuntimeException("unsupported charset exception " + UTF_8); + } // mock request Request request = mock(Request.class); @@ -92,5 +103,6 @@ public class ReporterUtilsTest { assertThat(serviceCallResult.getMethod()).isEqualTo("/path"); assertThat(serviceCallResult.getCallerService().getNamespace()).isEqualTo(NAMESPACE_TEST); assertThat(serviceCallResult.getCallerService().getService()).isEqualTo(SERVICE_PROVIDER); + assertThat(serviceCallResult.getLabels()).isEqualTo("k1:v1|k2:v2"); } }