fix:fix SCG report wrong service bug when using IP routing. (#1065)

pull/1068/head
Haotian Zhang 1 year ago committed by GitHub
parent 7741cbd222
commit 86ed684cfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,3 +19,4 @@
- [fix:fix circuit breaker bean load order bug when using Nacos discovery.](https://github.com/Tencent/spring-cloud-tencent/pull/1050) - [fix:fix circuit breaker bean load order bug when using Nacos discovery.](https://github.com/Tencent/spring-cloud-tencent/pull/1050)
- [refactor:refactor Polaris registration.](https://github.com/Tencent/spring-cloud-tencent/pull/1057) - [refactor:refactor Polaris registration.](https://github.com/Tencent/spring-cloud-tencent/pull/1057)
- [feat:added automatic optimization for dynamic config refresh type.](https://github.com/Tencent/spring-cloud-tencent/pull/1058) - [feat:added automatic optimization for dynamic config refresh type.](https://github.com/Tencent/spring-cloud-tencent/pull/1058)
- [fix:fix SCG report wrong service bug when using IP routing.](https://github.com/Tencent/spring-cloud-tencent/pull/1065)

@ -73,7 +73,7 @@
<revision>1.12.0-2022.0.2-SNAPSHOT</revision> <revision>1.12.0-2022.0.2-SNAPSHOT</revision>
<!-- Dependencies --> <!-- Dependencies -->
<polaris.version>1.13.0-SNAPSHOT</polaris.version> <polaris.version>1.13.0</polaris.version>
<guava.version>32.0.1-jre</guava.version> <guava.version>32.0.1-jre</guava.version>
<mocktio.version>4.9.0</mocktio.version> <mocktio.version>4.9.0</mocktio.version>
<byte-buddy.version>1.12.19</byte-buddy.version> <byte-buddy.version>1.12.19</byte-buddy.version>

@ -17,6 +17,8 @@
package com.tencent.cloud.rpc.enhancement.scg; package com.tencent.cloud.rpc.enhancement.scg;
import java.net.URI;
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginContext;
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginRunner;
import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType; import com.tencent.cloud.rpc.enhancement.plugin.EnhancedPluginType;
@ -33,6 +35,7 @@ import org.springframework.web.server.ServerWebExchange;
import static org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter.LOAD_BALANCER_CLIENT_FILTER_ORDER; import static org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter.LOAD_BALANCER_CLIENT_FILTER_ORDER;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_LOADBALANCER_RESPONSE_ATTR; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_LOADBALANCER_RESPONSE_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
/** /**
* EnhancedGatewayGlobalFilter. * EnhancedGatewayGlobalFilter.
@ -50,11 +53,11 @@ public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered {
@Override @Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
EnhancedPluginContext enhancedPluginContext = new EnhancedPluginContext(); EnhancedPluginContext enhancedPluginContext = new EnhancedPluginContext();
URI uri = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR);
EnhancedRequestContext enhancedRequestContext = EnhancedRequestContext.builder() EnhancedRequestContext enhancedRequestContext = EnhancedRequestContext.builder()
.httpHeaders(exchange.getRequest().getHeaders()) .httpHeaders(exchange.getRequest().getHeaders())
.httpMethod(exchange.getRequest().getMethod()) .httpMethod(exchange.getRequest().getMethod())
.url(exchange.getRequest().getURI()) .url(uri)
.build(); .build();
enhancedPluginContext.setRequest(enhancedRequestContext); enhancedPluginContext.setRequest(enhancedRequestContext);
@ -65,7 +68,7 @@ public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered {
enhancedPluginContext.setTargetServiceInstance(instance, exchange.getRequest().getURI()); enhancedPluginContext.setTargetServiceInstance(instance, exchange.getRequest().getURI());
} }
else { else {
enhancedPluginContext.setTargetServiceInstance(null, exchange.getRequest().getURI()); enhancedPluginContext.setTargetServiceInstance(null, uri);
} }
// Run pre enhanced plugins. // Run pre enhanced plugins.

@ -57,16 +57,13 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_LOADBALANCER_RESPONSE_ATTR; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_LOADBALANCER_RESPONSE_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
public class EnhancedGatewayGlobalFilterTest { public class EnhancedGatewayGlobalFilterTest {
private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils; private static MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils;
@Mock @Mock
private RpcEnhancementReporterProperties reporterProperties;
@Mock
private SDKContext sdkContext;
@Mock
Registration registration; Registration registration;
@Mock @Mock
ServerWebExchange exchange; ServerWebExchange exchange;
@ -76,6 +73,10 @@ public class EnhancedGatewayGlobalFilterTest {
ServerHttpResponse response; ServerHttpResponse response;
@Mock @Mock
ServerHttpRequest request; ServerHttpRequest request;
@Mock
private RpcEnhancementReporterProperties reporterProperties;
@Mock
private SDKContext sdkContext;
@BeforeAll @BeforeAll
static void beforeAll() { static void beforeAll() {
@ -87,7 +88,8 @@ public class EnhancedGatewayGlobalFilterTest {
StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class); StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class);
doReturn(metadataLocalProperties).when(applicationContext).getBean(MetadataLocalProperties.class); doReturn(metadataLocalProperties).when(applicationContext).getBean(MetadataLocalProperties.class);
doReturn(staticMetadataManager).when(applicationContext).getBean(StaticMetadataManager.class); doReturn(staticMetadataManager).when(applicationContext).getBean(StaticMetadataManager.class);
mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext).thenReturn(applicationContext); mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext)
.thenReturn(applicationContext);
} }
@AfterAll @AfterAll
@ -123,6 +125,7 @@ public class EnhancedGatewayGlobalFilterTest {
} }
}; };
doReturn(serviceInstanceResponse).when(exchange).getAttribute(GATEWAY_LOADBALANCER_RESPONSE_ATTR); doReturn(serviceInstanceResponse).when(exchange).getAttribute(GATEWAY_LOADBALANCER_RESPONSE_ATTR);
doReturn(new URI("http://0.0.0.0/")).when(exchange).getAttribute(GATEWAY_REQUEST_URL_ATTR);
doReturn(request).when(exchange).getRequest(); doReturn(request).when(exchange).getRequest();
doReturn(response).when(exchange).getResponse(); doReturn(response).when(exchange).getResponse();

Loading…
Cancel
Save