From 78e0a6da7bef2d1cf7bf0979bd00170de1e2b2cd Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Sun, 25 Jun 2023 14:36:11 +0800 Subject: [PATCH] fix:fix reporting bug when port is -1. --- CHANGELOG.md | 1 + .../ExceptionCircuitBreakerReporterTest.java | 2 +- .../SuccessCircuitBreakerReporterTest.java | 2 +- .../src/main/resources/bootstrap.yml | 2 +- .../src/main/resources/bootstrap.yml | 3 +- .../feign/EnhancedFeignClient.java | 2 +- .../plugin/EnhancedPluginContext.java | 30 +++++++++++++++++-- .../EnhancedRestTemplateInterceptor.java | 4 +-- .../scg/EnhancedGatewayGlobalFilter.java | 25 +++++++--------- ...hancedWebClientExchangeFilterFunction.java | 4 +-- .../zuul/EnhancedRouteZuulFilter.java | 5 +++- .../plugin/EnhancedPluginContextTest.java | 25 ++++++++++++++-- .../plugin/ExceptionPolarisReporterTest.java | 2 +- .../plugin/SuccessPolarisReporterTest.java | 2 +- 14 files changed, 79 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8758d040..5381972f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,3 +13,4 @@ - feat:sct-all package is now available as a shaded uber-jar. - fix:use path parameter in `@FeignClient` for circuit-breaker. - build(deps): bump guava in /spring-cloud-tencent-dependencies +- fix:fix reporting bug when port is -1. 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-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/bootstrap.yml index 7f1aa5671..e915a92b2 100644 --- a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/bootstrap.yml @@ -61,7 +61,7 @@ spring: basedOnPreviousValue: false routes: - id: GatewayCalleeService - uri: lb://GatewayCalleeService + uri: http://www.baidu.com predicates: - Path=/GatewayCalleeService/** filters: diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/resources/bootstrap.yml index 3c494e3ff..c80785ecf 100644 --- a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/resources/bootstrap.yml @@ -17,5 +17,6 @@ spring: zuul: routes: GatewayCalleeService: - serviceId: GatewayCalleeService + # serviceId: GatewayCalleeService + url: http://www.baidu.com path: /GatewayCalleeService/** 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 37ae78e5b..d09f77ae2 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,7 +71,7 @@ public class EnhancedFeignClient implements Client { enhancedPluginContext.setLocalServiceInstance(pluginRunner.getLocalServiceInstance()); DefaultServiceInstance serviceInstance = new DefaultServiceInstance(request.requestTemplate().feignTarget() .name(), url.getHost(), url.getPort(), url.getScheme().equals("https")); - enhancedPluginContext.setTargetServiceInstance(serviceInstance); + enhancedPluginContext.setTargetServiceInstance(serviceInstance, 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 a76b5507f..7afcc114b 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 @@ -61,30 +61,27 @@ public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered { .build(); enhancedPluginContext.setRequest(enhancedRequestContext); -// enhancedPluginContext.setLocalServiceInstance(pluginRunner.getLocalServiceInstance()); -// Response serviceInstanceResponse = exchange.getAttribute(GATEWAY_LOADBALANCER_RESPONSE_ATTR); -// if (serviceInstanceResponse != null && serviceInstanceResponse.hasServer()) { -// ServiceInstance instance = serviceInstanceResponse.getServer(); -// enhancedPluginContext.setTargetServiceInstance(instance); -// } - // Run pre enhanced plugins. pluginRunner.run(EnhancedPluginType.Client.PRE, enhancedPluginContext); long startTime = System.currentTimeMillis(); return chain.filter(exchange) .doOnSubscribe(v -> { - DefaultServiceInstance serviceInstance = new DefaultServiceInstance(); Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR); - if (route != null) { - serviceInstance.setServiceId(route.getUri().getHost()); - } URI uri = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR); + enhancedPluginContext.getRequest().setUrl(uri); if (uri != null) { - serviceInstance.setHost(uri.getHost()); - serviceInstance.setPort(uri.getPort()); + if (route != null && route.getUri().getScheme().contains("lb")) { + DefaultServiceInstance serviceInstance = new DefaultServiceInstance(); + serviceInstance.setServiceId(route.getUri().getHost()); + serviceInstance.setHost(uri.getHost()); + serviceInstance.setPort(uri.getPort()); + enhancedPluginContext.setTargetServiceInstance(serviceInstance, null); + } + else { + enhancedPluginContext.setTargetServiceInstance(null, uri); + } } - enhancedPluginContext.setTargetServiceInstance(serviceInstance); }) .doOnSuccess(v -> { enhancedPluginContext.setDelay(System.currentTimeMillis() - startTime); 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/main/java/com/tencent/cloud/rpc/enhancement/zuul/EnhancedRouteZuulFilter.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/zuul/EnhancedRouteZuulFilter.java index e849e4c8c..8d3372335 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/zuul/EnhancedRouteZuulFilter.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/zuul/EnhancedRouteZuulFilter.java @@ -112,7 +112,10 @@ public class EnhancedRouteZuulFilter extends ZuulFilter { serviceInstance.setServiceId(ZuulFilterUtils.getServiceId(context)); serviceInstance.setHost(ribbonResponse.getRequestedURI().getHost()); serviceInstance.setPort(ribbonResponse.getRequestedURI().getPort()); - enhancedPluginContext.setTargetServiceInstance(serviceInstance); + enhancedPluginContext.setTargetServiceInstance(serviceInstance, null); + } + else { + enhancedPluginContext.setTargetServiceInstance(null, uri); } // Run pre enhanced plugins. 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();