From b63d50dc6a9df9cadf9914d3d404d85b868353db Mon Sep 17 00:00:00 2001
From: Haotian Zhang <928016560@qq.com>
Date: Tue, 25 Jul 2023 10:00:02 +0800
Subject: [PATCH] fix:fix SCG report wrong service bug when using IP routing.
(#1064)
---
CHANGELOG.md | 1 +
spring-cloud-tencent-dependencies/pom.xml | 2 +-
.../scg/EnhancedGatewayGlobalFilter.java | 9 ++++++---
.../scg/EnhancedGatewayGlobalFilterTest.java | 13 ++++++++-----
4 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8c47f725e..e4eb2719e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,3 +19,4 @@
- [fix:fix circuit breaker bean load order bug when using Nacos discovery.](https://github.com/Tencent/spring-cloud-tencent/pull/1049)
- [refactor:refactor Polaris registration.](https://github.com/Tencent/spring-cloud-tencent/pull/1056)
- [feat:added automatic optimization for dynamic config refresh type.](https://github.com/Tencent/spring-cloud-tencent/pull/1059)
+- [fix:fix SCG report wrong service bug when using IP routing.](https://github.com/Tencent/spring-cloud-tencent/pull/1064)
diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml
index bfbd52313..8b1baf09f 100644
--- a/spring-cloud-tencent-dependencies/pom.xml
+++ b/spring-cloud-tencent-dependencies/pom.xml
@@ -73,7 +73,7 @@
1.12.0-2020.0.6-SNAPSHOT
- 1.13.0-SNAPSHOT
+ 1.13.0
32.0.1-jre
1.2.11
4.5.1
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 9d4806a2a..e403d8d4d 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
@@ -17,6 +17,8 @@
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.EnhancedPluginRunner;
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.support.ServerWebExchangeUtils.GATEWAY_LOADBALANCER_RESPONSE_ATTR;
+import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
/**
* EnhancedGatewayGlobalFilter.
@@ -50,11 +53,11 @@ public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered {
@Override
public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
EnhancedPluginContext enhancedPluginContext = new EnhancedPluginContext();
-
+ URI uri = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR);
EnhancedRequestContext enhancedRequestContext = EnhancedRequestContext.builder()
.httpHeaders(exchange.getRequest().getHeaders())
.httpMethod(exchange.getRequest().getMethod())
- .url(exchange.getRequest().getURI())
+ .url(uri)
.build();
enhancedPluginContext.setRequest(enhancedRequestContext);
@@ -65,7 +68,7 @@ public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered {
enhancedPluginContext.setTargetServiceInstance(instance, exchange.getRequest().getURI());
}
else {
- enhancedPluginContext.setTargetServiceInstance(null, exchange.getRequest().getURI());
+ enhancedPluginContext.setTargetServiceInstance(null, uri);
}
// Run pre enhanced plugins.
diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/scg/EnhancedGatewayGlobalFilterTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/scg/EnhancedGatewayGlobalFilterTest.java
index d50adce6f..d25ea12ec 100644
--- a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/scg/EnhancedGatewayGlobalFilterTest.java
+++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/scg/EnhancedGatewayGlobalFilterTest.java
@@ -57,16 +57,13 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
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_REQUEST_URL_ATTR;
@ExtendWith(MockitoExtension.class)
public class EnhancedGatewayGlobalFilterTest {
private static MockedStatic mockedApplicationContextAwareUtils;
@Mock
- private RpcEnhancementReporterProperties reporterProperties;
- @Mock
- private SDKContext sdkContext;
- @Mock
Registration registration;
@Mock
ServerWebExchange exchange;
@@ -76,6 +73,10 @@ public class EnhancedGatewayGlobalFilterTest {
ServerHttpResponse response;
@Mock
ServerHttpRequest request;
+ @Mock
+ private RpcEnhancementReporterProperties reporterProperties;
+ @Mock
+ private SDKContext sdkContext;
@BeforeAll
static void beforeAll() {
@@ -87,7 +88,8 @@ public class EnhancedGatewayGlobalFilterTest {
StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class);
doReturn(metadataLocalProperties).when(applicationContext).getBean(MetadataLocalProperties.class);
doReturn(staticMetadataManager).when(applicationContext).getBean(StaticMetadataManager.class);
- mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext).thenReturn(applicationContext);
+ mockedApplicationContextAwareUtils.when(ApplicationContextAwareUtils::getApplicationContext)
+ .thenReturn(applicationContext);
}
@AfterAll
@@ -123,6 +125,7 @@ public class EnhancedGatewayGlobalFilterTest {
}
};
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(response).when(exchange).getResponse();