From 6264476485b6608724e4c598ca610e999e85ff90 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Sun, 25 Jun 2023 10:13:41 +0800 Subject: [PATCH] fix:fix reporting bug when port is -1. (#1038) --- CHANGELOG.md | 1 + .../ExceptionCircuitBreakerReporterTest.java | 2 +- .../SuccessCircuitBreakerReporterTest.java | 2 +- .../feign/EnhancedFeignClient.java | 4 +-- .../plugin/EnhancedPluginContext.java | 30 +++++++++++++++++-- .../EnhancedRestTemplateInterceptor.java | 4 +-- .../scg/EnhancedGatewayGlobalFilter.java | 5 +++- ...hancedWebClientExchangeFilterFunction.java | 4 +-- .../AssemblyClientExceptionHookTest.java | 2 +- .../plugin/AssemblyClientPostHookTest.java | 2 +- .../plugin/AssemblyClientPreHookTest.java | 2 +- .../AssemblyServerExceptionHookTest.java | 2 +- .../plugin/AssemblyServerPostHookTest.java | 2 +- .../plugin/AssemblyServerPreHookTest.java | 2 +- .../plugin/EnhancedPluginContextTest.java | 25 ++++++++++++++-- .../plugin/ExceptionPolarisReporterTest.java | 2 +- .../plugin/SuccessPolarisReporterTest.java | 2 +- 17 files changed, 72 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cef4aa98e..06ab32290 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,3 +14,4 @@ - [feat: update sct-all package method by maven shaded plugin.](https://github.com/Tencent/spring-cloud-tencent/pull/1024) - [fix:use path parameter in `@FeignClient` for circuit-breaker.](https://github.com/Tencent/spring-cloud-tencent/pull/1029) - [build(deps): bump guava in /spring-cloud-tencent-dependencies](https://github.com/Tencent/spring-cloud-tencent/pull/1034) +- [fix:fix reporting bug when port is -1.](https://github.com/Tencent/spring-cloud-tencent/pull/1038) diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/reporter/ExceptionCircuitBreakerReporterTest.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/reporter/ExceptionCircuitBreakerReporterTest.java index 7be53b2fa..ec882022e 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/reporter/ExceptionCircuitBreakerReporterTest.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/reporter/ExceptionCircuitBreakerReporterTest.java @@ -121,7 +121,7 @@ public class ExceptionCircuitBreakerReporterTest { pluginContext.setRequest(request); pluginContext.setResponse(response); - pluginContext.setTargetServiceInstance(serviceInstance); + pluginContext.setTargetServiceInstance(serviceInstance, null); pluginContext.setThrowable(new RuntimeException()); exceptionCircuitBreakerReporter.run(pluginContext); diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/reporter/SuccessCircuitBreakerReporterTest.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/reporter/SuccessCircuitBreakerReporterTest.java index 5d50a24c1..86707c237 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/reporter/SuccessCircuitBreakerReporterTest.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/reporter/SuccessCircuitBreakerReporterTest.java @@ -127,7 +127,7 @@ public class SuccessCircuitBreakerReporterTest { pluginContext.setRequest(request); pluginContext.setResponse(response); - pluginContext.setTargetServiceInstance(serviceInstance); + pluginContext.setTargetServiceInstance(serviceInstance, null); successCircuitBreakerReporter.run(pluginContext); successCircuitBreakerReporter.getOrder(); diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignClient.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignClient.java index 1af26f5b6..b7ee6d11b 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignClient.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/feign/EnhancedFeignClient.java @@ -71,8 +71,8 @@ public class EnhancedFeignClient implements Client { enhancedPluginContext.setRequest(enhancedRequestContext); enhancedPluginContext.setLocalServiceInstance(pluginRunner.getLocalServiceInstance()); - enhancedPluginContext.setTargetServiceInstance( - (ServiceInstance) MetadataContextHolder.get().getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE)); + enhancedPluginContext.setTargetServiceInstance((ServiceInstance) MetadataContextHolder.get() + .getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE), url); // Run pre enhanced plugins. pluginRunner.run(EnhancedPluginType.Client.PRE, enhancedPluginContext); diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/plugin/EnhancedPluginContext.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/plugin/EnhancedPluginContext.java index 4676c51ad..7eb14ae89 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/plugin/EnhancedPluginContext.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/plugin/EnhancedPluginContext.java @@ -18,6 +18,13 @@ package com.tencent.cloud.rpc.enhancement.plugin; +import java.net.URI; +import java.util.Objects; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; /** @@ -27,6 +34,8 @@ import org.springframework.cloud.client.ServiceInstance; */ public class EnhancedPluginContext { + private static final Logger LOGGER = LoggerFactory.getLogger(EnhancedPluginContext.class); + private EnhancedRequestContext request; private EnhancedResponseContext response; @@ -86,8 +95,25 @@ public class EnhancedPluginContext { return targetServiceInstance; } - public void setTargetServiceInstance(ServiceInstance targetServiceInstance) { - this.targetServiceInstance = targetServiceInstance; + public void setTargetServiceInstance(ServiceInstance targetServiceInstance, URI url) { + if (Objects.nonNull(targetServiceInstance)) { + this.targetServiceInstance = targetServiceInstance; + } + else if (Objects.nonNull(url)) { + DefaultServiceInstance defaultServiceInstance = new DefaultServiceInstance(); + defaultServiceInstance.setUri(url); + if (defaultServiceInstance.isSecure()) { + defaultServiceInstance.setPort(443); + } + else { + defaultServiceInstance.setPort(80); + } + this.targetServiceInstance = defaultServiceInstance; + } + else { + this.targetServiceInstance = new DefaultServiceInstance(); + LOGGER.warn("TargetServiceInstance is empty."); + } } @Override diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateInterceptor.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateInterceptor.java index 2a4e0f6fc..c50cbb907 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateInterceptor.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/resttemplate/EnhancedRestTemplateInterceptor.java @@ -60,8 +60,8 @@ public class EnhancedRestTemplateInterceptor implements ClientHttpRequestInterce enhancedPluginContext.setRequest(enhancedRequestContext); enhancedPluginContext.setLocalServiceInstance(pluginRunner.getLocalServiceInstance()); - enhancedPluginContext.setTargetServiceInstance( - (ServiceInstance) MetadataContextHolder.get().getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE)); + enhancedPluginContext.setTargetServiceInstance((ServiceInstance) MetadataContextHolder.get() + .getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE), request.getURI()); // Run pre enhanced plugins. pluginRunner.run(EnhancedPluginType.Client.PRE, enhancedPluginContext); diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/scg/EnhancedGatewayGlobalFilter.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/scg/EnhancedGatewayGlobalFilter.java index 39cd0a608..9d4806a2a 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/scg/EnhancedGatewayGlobalFilter.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/scg/EnhancedGatewayGlobalFilter.java @@ -62,7 +62,10 @@ public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered { Response serviceInstanceResponse = exchange.getAttribute(GATEWAY_LOADBALANCER_RESPONSE_ATTR); if (serviceInstanceResponse != null && serviceInstanceResponse.hasServer()) { ServiceInstance instance = serviceInstanceResponse.getServer(); - enhancedPluginContext.setTargetServiceInstance(instance); + enhancedPluginContext.setTargetServiceInstance(instance, exchange.getRequest().getURI()); + } + else { + enhancedPluginContext.setTargetServiceInstance(null, exchange.getRequest().getURI()); } // Run pre enhanced plugins. diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/webclient/EnhancedWebClientExchangeFilterFunction.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/webclient/EnhancedWebClientExchangeFilterFunction.java index 2d5577fc2..673cac3da 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/webclient/EnhancedWebClientExchangeFilterFunction.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/webclient/EnhancedWebClientExchangeFilterFunction.java @@ -57,8 +57,8 @@ public class EnhancedWebClientExchangeFilterFunction implements ExchangeFilterFu enhancedPluginContext.setRequest(enhancedRequestContext); enhancedPluginContext.setLocalServiceInstance(pluginRunner.getLocalServiceInstance()); - enhancedPluginContext.setTargetServiceInstance( - (ServiceInstance) MetadataContextHolder.get().getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE)); + enhancedPluginContext.setTargetServiceInstance((ServiceInstance) MetadataContextHolder.get() + .getLoadbalancerMetadata().get(LOAD_BALANCER_SERVICE_INSTANCE), request.url()); // Run post enhanced plugins. pluginRunner.run(EnhancedPluginType.Client.PRE, enhancedPluginContext); diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyClientExceptionHookTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyClientExceptionHookTest.java index a3f959c69..d89006ccd 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyClientExceptionHookTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyClientExceptionHookTest.java @@ -120,7 +120,7 @@ public class AssemblyClientExceptionHookTest { pluginContext.setRequest(request); pluginContext.setResponse(response); - pluginContext.setTargetServiceInstance(targetServiceInstance); + pluginContext.setTargetServiceInstance(targetServiceInstance, null); pluginContext.setLocalServiceInstance(localServiceInstance); pluginContext.setThrowable(new RuntimeException()); diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyClientPostHookTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyClientPostHookTest.java index 22b11cc32..9aa3f0d8e 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyClientPostHookTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyClientPostHookTest.java @@ -120,7 +120,7 @@ public class AssemblyClientPostHookTest { pluginContext.setRequest(request); pluginContext.setResponse(response); - pluginContext.setTargetServiceInstance(targetServiceInstance); + pluginContext.setTargetServiceInstance(targetServiceInstance, null); pluginContext.setLocalServiceInstance(localServiceInstance); pluginContext.setThrowable(new RuntimeException()); diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyClientPreHookTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyClientPreHookTest.java index 08d8d7a2b..f597c1026 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyClientPreHookTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyClientPreHookTest.java @@ -117,7 +117,7 @@ public class AssemblyClientPreHookTest { pluginContext.setRequest(request); pluginContext.setResponse(response); - pluginContext.setTargetServiceInstance(targetServiceInstance); + pluginContext.setTargetServiceInstance(targetServiceInstance, null); pluginContext.setLocalServiceInstance(localServiceInstance); pluginContext.setThrowable(new RuntimeException()); diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyServerExceptionHookTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyServerExceptionHookTest.java index 644e82bb2..ff04c578a 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyServerExceptionHookTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyServerExceptionHookTest.java @@ -117,7 +117,7 @@ public class AssemblyServerExceptionHookTest { pluginContext.setRequest(request); pluginContext.setResponse(response); - pluginContext.setTargetServiceInstance(targetServiceInstance); + pluginContext.setTargetServiceInstance(targetServiceInstance, null); pluginContext.setLocalServiceInstance(localServiceInstance); pluginContext.setThrowable(new RuntimeException()); diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyServerPostHookTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyServerPostHookTest.java index 2cf1f0400..a15cd5796 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyServerPostHookTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyServerPostHookTest.java @@ -117,7 +117,7 @@ public class AssemblyServerPostHookTest { pluginContext.setRequest(request); pluginContext.setResponse(response); - pluginContext.setTargetServiceInstance(targetServiceInstance); + pluginContext.setTargetServiceInstance(targetServiceInstance, null); pluginContext.setLocalServiceInstance(localServiceInstance); pluginContext.setThrowable(new RuntimeException()); diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyServerPreHookTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyServerPreHookTest.java index d3d324806..8bfe4c9a0 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyServerPreHookTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/AssemblyServerPreHookTest.java @@ -117,7 +117,7 @@ public class AssemblyServerPreHookTest { pluginContext.setRequest(request); pluginContext.setResponse(response); - pluginContext.setTargetServiceInstance(targetServiceInstance); + pluginContext.setTargetServiceInstance(targetServiceInstance, null); pluginContext.setLocalServiceInstance(localServiceInstance); pluginContext.setThrowable(new RuntimeException()); diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/EnhancedPluginContextTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/EnhancedPluginContextTest.java index 0e9824911..646ee0263 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/EnhancedPluginContextTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/EnhancedPluginContextTest.java @@ -18,7 +18,9 @@ package com.tencent.cloud.rpc.enhancement.plugin; import java.net.URI; +import java.net.URISyntaxException; import java.util.Arrays; +import java.util.Collections; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.util.ApplicationContextAwareUtils; @@ -117,7 +119,7 @@ public class EnhancedPluginContextTest { EnhancedPluginContext enhancedPluginContext = new EnhancedPluginContext(); enhancedPluginContext.setRequest(requestContext); enhancedPluginContext.setResponse(responseContext); - enhancedPluginContext.setTargetServiceInstance(new DefaultServiceInstance()); + enhancedPluginContext.setTargetServiceInstance(new DefaultServiceInstance(), null); enhancedPluginContext.setThrowable(mock(Exception.class)); enhancedPluginContext.setDelay(0); assertThat(enhancedPluginContext.getRequest()).isNotNull(); @@ -148,7 +150,26 @@ public class EnhancedPluginContextTest { doReturn(configuration).when(sdkContext).getConfig(); - enhancedPluginRunner = new DefaultEnhancedPluginRunner(Arrays.asList(enhancedPlugin2), null, sdkContext); + enhancedPluginRunner = new DefaultEnhancedPluginRunner(Collections.singletonList(enhancedPlugin2), null, sdkContext); enhancedPluginRunner.run(EnhancedPluginType.Client.POST, enhancedPluginContext); } + + @Test + public void testSetTargetServiceInstance() throws URISyntaxException { + EnhancedPluginContext enhancedPluginContext = new EnhancedPluginContext(); + + // targetServiceInstance != null + DefaultServiceInstance testDefaultServiceInstance = new DefaultServiceInstance(); + testDefaultServiceInstance.setPort(1); + enhancedPluginContext.setTargetServiceInstance(testDefaultServiceInstance, null); + assertThat(enhancedPluginContext.getTargetServiceInstance().getPort()).isEqualTo(1); + + // targetServiceInstance == null && url != null + enhancedPluginContext.setTargetServiceInstance(null, new URI("https://www.qq.com")); + assertThat(enhancedPluginContext.getTargetServiceInstance().getPort()).isEqualTo(443); + + // targetServiceInstance == null && url == null + enhancedPluginContext.setTargetServiceInstance(null, null); + assertThat(enhancedPluginContext.getTargetServiceInstance().getPort()).isEqualTo(0); + } } diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/ExceptionPolarisReporterTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/ExceptionPolarisReporterTest.java index 840c5d498..5288b35ce 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/ExceptionPolarisReporterTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/ExceptionPolarisReporterTest.java @@ -124,7 +124,7 @@ public class ExceptionPolarisReporterTest { pluginContext.setRequest(request); pluginContext.setResponse(response); - pluginContext.setTargetServiceInstance(serviceInstance); + pluginContext.setTargetServiceInstance(serviceInstance, null); pluginContext.setThrowable(new RuntimeException()); exceptionPolarisReporter.run(pluginContext); diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/SuccessPolarisReporterTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/SuccessPolarisReporterTest.java index 5af25a89b..bb82ce8c3 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/SuccessPolarisReporterTest.java +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/plugin/SuccessPolarisReporterTest.java @@ -122,7 +122,7 @@ public class SuccessPolarisReporterTest { pluginContext.setRequest(request); pluginContext.setResponse(response); - pluginContext.setTargetServiceInstance(serviceInstance); + pluginContext.setTargetServiceInstance(serviceInstance, null); successPolarisReporter.run(pluginContext); successPolarisReporter.getOrder();