refactor some code.

pull/1496/head^2
Haotian Zhang 7 months ago
parent 015b154b72
commit c3d4baca8c

@ -57,7 +57,7 @@
<dependency> <dependency>
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-gateway-plugin</artifactId> <artifactId>spring-cloud-starter-tencent-gateway-plugin</artifactId>
</dependency> </dependency>
<dependency> <dependency>

@ -52,7 +52,7 @@ public class PolarisDiscoveryHandler {
* @return list of healthy instances * @return list of healthy instances
*/ */
public InstancesResponse getHealthyInstances(String service) { public InstancesResponse getHealthyInstances(String service) {
String namespace = MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE, String namespace = MetadataContextHolder.get().getContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, polarisDiscoveryProperties.getNamespace()); MetadataConstant.POLARIS_TARGET_NAMESPACE, polarisDiscoveryProperties.getNamespace());
GetHealthyInstancesRequest getHealthyInstancesRequest = new GetHealthyInstancesRequest(); GetHealthyInstancesRequest getHealthyInstancesRequest = new GetHealthyInstancesRequest();

@ -74,7 +74,7 @@ public final class RouterUtils {
serviceMetadata = instanceList.get(0).getServiceMetadata(); serviceMetadata = instanceList.get(0).getServiceMetadata();
} }
String namespace = MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE, String namespace = MetadataContextHolder.get().getContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, MetadataContext.LOCAL_NAMESPACE); MetadataConstant.POLARIS_TARGET_NAMESPACE, MetadataContext.LOCAL_NAMESPACE);
ServiceKey serviceKey = new ServiceKey(namespace, serviceName); ServiceKey serviceKey = new ServiceKey(namespace, serviceName);

@ -74,7 +74,8 @@ public class RouterUtilsTest {
.thenReturn(testNamespaceAndService); .thenReturn(testNamespaceAndService);
MetadataContext metadataContext = Mockito.mock(MetadataContext.class); MetadataContext metadataContext = Mockito.mock(MetadataContext.class);
mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext); mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);
Mockito.when(metadataContext.getFragmentContext(anyString(), anyString(), anyString())).thenReturn(testNamespaceAndService); Mockito.when(metadataContext.getContext(anyString(), anyString(), anyString()))
.thenReturn(testNamespaceAndService);
int instanceSize = 100; int instanceSize = 100;
int weight = 50; int weight = 50;

@ -129,6 +129,10 @@ public class MetadataContext extends com.tencent.polaris.metadata.core.manager.M
super(MetadataConstant.POLARIS_TRANSITIVE_HEADER_PREFIX); super(MetadataConstant.POLARIS_TRANSITIVE_HEADER_PREFIX);
} }
public static void setLocalService(String service) {
LOCAL_SERVICE = service;
}
private Map<String, String> getMetadataAsMap(MetadataType metadataType, TransitiveType transitiveType, boolean caller) { private Map<String, String> getMetadataAsMap(MetadataType metadataType, TransitiveType transitiveType, boolean caller) {
MetadataContainer metadataContainer = getMetadataContainer(metadataType, caller); MetadataContainer metadataContainer = getMetadataContainer(metadataType, caller);
Map<String, String> values = new HashMap<>(); Map<String, String> values = new HashMap<>();
@ -283,7 +287,7 @@ public class MetadataContext extends com.tencent.polaris.metadata.core.manager.M
} }
} }
public String getFragmentContext(String fragment, String key, String defaultValue) { public String getContext(String fragment, String key, String defaultValue) {
return getFragmentContext(fragment).getOrDefault(key, defaultValue); return getFragmentContext(fragment).getOrDefault(key, defaultValue);
} }
@ -338,8 +342,4 @@ public class MetadataContext extends com.tencent.polaris.metadata.core.manager.M
context.put(key, value); context.put(key, value);
putFragmentContext(fragment, context); putFragmentContext(fragment, context);
} }
public static void setLocalService(String service) {
LOCAL_SERVICE = service;
}
} }

@ -86,7 +86,7 @@
<dependency> <dependency>
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-gateway-plugin</artifactId> <artifactId>spring-cloud-starter-tencent-gateway-plugin</artifactId>
</dependency> </dependency>
<dependency> <dependency>

@ -194,7 +194,7 @@
<dependency> <dependency>
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-gateway-plugin</artifactId> <artifactId>spring-cloud-starter-tencent-gateway-plugin</artifactId>
<version>${revision}</version> <version>${revision}</version>
</dependency> </dependency>

@ -20,7 +20,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-gateway-plugin</artifactId> <artifactId>spring-cloud-starter-tencent-gateway-plugin</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>

@ -21,7 +21,7 @@
<dependency> <dependency>
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-gateway-plugin</artifactId> <artifactId>spring-cloud-starter-tencent-gateway-plugin</artifactId>
</dependency> </dependency>
<dependency> <dependency>

@ -16,7 +16,7 @@
<modules> <modules>
<module>spring-cloud-tencent-featureenv-plugin</module> <module>spring-cloud-tencent-featureenv-plugin</module>
<module>spring-cloud-tencent-gateway-plugin</module> <module>spring-cloud-starter-tencent-gateway-plugin</module>
<module>spring-cloud-starter-tencent-discovery-adapter-plugin</module> <module>spring-cloud-starter-tencent-discovery-adapter-plugin</module>
<module>spring-cloud-tencent-lossless-plugin</module> <module>spring-cloud-tencent-lossless-plugin</module>
<module>spring-cloud-starter-tencent-threadlocal-plugin</module> <module>spring-cloud-starter-tencent-threadlocal-plugin</module>

@ -10,8 +10,8 @@
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-tencent-gateway-plugin</artifactId> <artifactId>spring-cloud-starter-tencent-gateway-plugin</artifactId>
<name>Spring Cloud Tencent Gateway Plugin</name> <name>Spring Cloud Starter Tencent Gateway Plugin</name>
<dependencies> <dependencies>
<dependency> <dependency>
@ -22,6 +22,7 @@
<dependency> <dependency>
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId> <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
<optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>

@ -79,7 +79,7 @@ public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered {
metadataContext = MetadataContextHolder.get(); metadataContext = MetadataContextHolder.get();
} }
String governanceNamespace = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE, String governanceNamespace = metadataContext.getContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, MetadataContext.LOCAL_NAMESPACE); MetadataConstant.POLARIS_TARGET_NAMESPACE, MetadataContext.LOCAL_NAMESPACE);
@ -125,7 +125,8 @@ public class EnhancedGatewayGlobalFilter implements GlobalFilter, Ordered {
URI uri = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR); URI uri = exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR);
enhancedPluginContext.getRequest().setUrl(uri); enhancedPluginContext.getRequest().setUrl(uri);
if (uri != null) { if (uri != null) {
if (route != null && route.getUri().getScheme().contains("lb") && StringUtils.isNotEmpty(serviceId)) { if (route != null && route.getUri().getScheme()
.contains("lb") && StringUtils.isNotEmpty(serviceId)) {
DefaultServiceInstance serviceInstance = new DefaultServiceInstance(); DefaultServiceInstance serviceInstance = new DefaultServiceInstance();
serviceInstance.setServiceId(serviceId); serviceInstance.setServiceId(serviceId);
serviceInstance.setHost(uri.getHost()); serviceInstance.setHost(uri.getHost());

@ -136,7 +136,7 @@ public final class PolarisEnhancedPluginUtils {
@Nullable String calleeServiceName, @Nullable String calleeHost, @Nullable Integer calleePort, @Nullable String calleeServiceName, @Nullable String calleeHost, @Nullable Integer calleePort,
URI uri, @Nullable Integer statusCode, long delay, @Nullable Throwable exception) { URI uri, @Nullable Integer statusCode, long delay, @Nullable Throwable exception) {
String governanceNamespace = MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE, String governanceNamespace = MetadataContextHolder.get().getContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, MetadataContext.LOCAL_NAMESPACE); MetadataConstant.POLARIS_TARGET_NAMESPACE, MetadataContext.LOCAL_NAMESPACE);
ServiceKey calleeServiceKey = new ServiceKey(governanceNamespace, StringUtils.isBlank(calleeServiceName) ? uri.getHost() : calleeServiceName); ServiceKey calleeServiceKey = new ServiceKey(governanceNamespace, StringUtils.isBlank(calleeServiceName) ? uri.getHost() : calleeServiceName);

@ -46,7 +46,7 @@ public class PolarisInstanceTransformer implements InstanceTransformer {
instance.setServiceMetadata(polarisServiceInstance.getServiceMetadata()); instance.setServiceMetadata(polarisServiceInstance.getServiceMetadata());
} }
String namespace = MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_APPLICATION_NONE, String namespace = MetadataContextHolder.get().getContext(MetadataContext.FRAGMENT_APPLICATION_NONE,
MetadataConstant.POLARIS_TARGET_NAMESPACE, instance.getNamespace()); MetadataConstant.POLARIS_TARGET_NAMESPACE, instance.getNamespace());
instance.setNamespace(namespace); instance.setNamespace(namespace);

Loading…
Cancel
Save