fix:fix reporting bug when port is -1.

pull/1080/head
Haotian Zhang 1 year ago
parent 8ecb4b07a0
commit 78e0a6da7b

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

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

@ -61,7 +61,7 @@ spring:
basedOnPreviousValue: false
routes:
- id: GatewayCalleeService
uri: lb://GatewayCalleeService
uri: http://www.baidu.com
predicates:
- Path=/GatewayCalleeService/**
filters:

@ -17,5 +17,6 @@ spring:
zuul:
routes:
GatewayCalleeService:
serviceId: GatewayCalleeService
# serviceId: GatewayCalleeService
url: http://www.baidu.com
path: /GatewayCalleeService/**

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

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

@ -61,30 +61,27 @@ public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered {
.build();
enhancedPluginContext.setRequest(enhancedRequestContext);
// enhancedPluginContext.setLocalServiceInstance(pluginRunner.getLocalServiceInstance());
// Response<ServiceInstance> 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);

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

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

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