fix:fix reporting bug when port is -1. (#1039)

pull/1042/head
Haotian Zhang 1 year ago committed by GitHub
parent 0759a8f9dc
commit d53824bea8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -14,3 +14,4 @@
- [feat:sct-all package is now available as a shaded uber-jar.](https://github.com/Tencent/spring-cloud-tencent/pull/1026)
- [fix:use path parameter in `@FeignClient` for circuit-breaker.](https://github.com/Tencent/spring-cloud-tencent/pull/1030)
- [build(deps): bump guava in /spring-cloud-tencent-dependencies](https://github.com/Tencent/spring-cloud-tencent/pull/1036)
- [fix:fix reporting bug when port is -1.](https://github.com/Tencent/spring-cloud-tencent/pull/1039)

@ -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);

@ -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();

@ -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);

@ -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

@ -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);

@ -62,7 +62,10 @@ public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered {
Response<ServiceInstance> 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.

@ -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);

@ -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());

@ -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());

@ -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());

@ -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());

@ -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());

@ -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());

@ -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);
}
}

@ -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);

@ -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();

Loading…
Cancel
Save