From f5fec381cc785bd02f62f979334faa449e340d59 Mon Sep 17 00:00:00 2001 From: lepdou Date: Tue, 29 Nov 2022 10:46:21 +0800 Subject: [PATCH] Adapt polaris-java 1.10.1 version (#748) --- CHANGELOG.md | 1 + .../discovery/PolarisDiscoveryHandler.java | 1 + .../MetadataRouterRequestInterceptor.java | 12 ++++- .../NearbyRouterRequestInterceptor.java | 11 +++-- .../RuleBasedRouterRequestInterceptor.java | 19 ++++++-- ...RouterServiceInstanceListSupplierTest.java | 46 +++++++++++-------- spring-cloud-tencent-dependencies/pom.xml | 2 +- .../FeatureEnvRouterRequestInterceptor.java | 12 +++-- 8 files changed, 69 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb97594b8..832a2e245 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,3 +12,4 @@ - [Code optimization & correct comment](https://github.com/Tencent/spring-cloud-tencent/pull/723) - [fix:fix javadoc.](https://github.com/Tencent/spring-cloud-tencent/pull/726) - [fix:fix discovery junit.](https://github.com/Tencent/spring-cloud-tencent/pull/728) +- [adapt polaris-java 1.10.1 version](https://github.com/Tencent/spring-cloud-tencent/pull/748) diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryHandler.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryHandler.java index 51e16e34d..688562d76 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryHandler.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryHandler.java @@ -61,6 +61,7 @@ public class PolarisDiscoveryHandler { GetHealthyInstancesRequest getHealthyInstancesRequest = new GetHealthyInstancesRequest(); getHealthyInstancesRequest.setNamespace(namespace); getHealthyInstancesRequest.setService(service); + getHealthyInstancesRequest.setIncludeCircuitBreakInstances(false); return polarisConsumer.getHealthyInstances(getHealthyInstancesRequest); } diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/MetadataRouterRequestInterceptor.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/MetadataRouterRequestInterceptor.java index 54f912442..5a29a1ff9 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/MetadataRouterRequestInterceptor.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/MetadataRouterRequestInterceptor.java @@ -18,6 +18,7 @@ package com.tencent.cloud.polaris.router.interceptor; +import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -25,9 +26,12 @@ import com.tencent.cloud.common.constant.RouterConstant; import com.tencent.cloud.polaris.router.PolarisRouterContext; import com.tencent.cloud.polaris.router.config.properties.PolarisMetadataRouterProperties; import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor; +import com.tencent.polaris.api.pojo.RouteArgument; import com.tencent.polaris.plugins.router.metadata.MetadataRouter; import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest; +import org.springframework.util.CollectionUtils; + /** * Router request interceptor for metadata router. * @author lepdou, Hoatian Zhang @@ -53,6 +57,12 @@ public class MetadataRouterRequestInterceptor implements RouterRequestIntercepto Map metadataRouterLabels = routerContext.getLabels(RouterConstant.ROUTER_LABELS, metadataRouterKeys); // 3. set metadata router labels to request - request.addRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA, metadataRouterLabels); + Set routeArguments = new HashSet<>(); + if (!CollectionUtils.isEmpty(metadataRouterKeys)) { + for (Map.Entry entry : metadataRouterLabels.entrySet()) { + routeArguments.add(RouteArgument.buildCustom(entry.getKey(), entry.getValue())); + } + } + request.putRouterArgument(MetadataRouter.ROUTER_TYPE_METADATA, routeArguments); } } diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/NearbyRouterRequestInterceptor.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/NearbyRouterRequestInterceptor.java index 86efcd62e..ca59f7788 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/NearbyRouterRequestInterceptor.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/NearbyRouterRequestInterceptor.java @@ -18,12 +18,13 @@ package com.tencent.cloud.polaris.router.interceptor; -import java.util.HashMap; -import java.util.Map; +import java.util.HashSet; +import java.util.Set; import com.tencent.cloud.polaris.router.PolarisRouterContext; import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties; import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor; +import com.tencent.polaris.api.pojo.RouteArgument; import com.tencent.polaris.plugins.router.nearby.NearbyRouter; import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest; @@ -45,9 +46,9 @@ public class NearbyRouterRequestInterceptor implements RouterRequestInterceptor return; } - Map nearbyRouterMetadata = new HashMap<>(); - nearbyRouterMetadata.put(NearbyRouter.ROUTER_ENABLED, "true"); + Set routeArguments = new HashSet<>(1); + routeArguments.add(RouteArgument.buildCustom(NearbyRouter.ROUTER_ENABLED, "true")); - request.addRouterMetadata(NearbyRouter.ROUTER_TYPE_NEAR_BY, nearbyRouterMetadata); + request.putRouterArgument(NearbyRouter.ROUTER_TYPE_NEAR_BY, routeArguments); } } diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/RuleBasedRouterRequestInterceptor.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/RuleBasedRouterRequestInterceptor.java index 00aee3590..34814f650 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/RuleBasedRouterRequestInterceptor.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/RuleBasedRouterRequestInterceptor.java @@ -17,16 +17,20 @@ package com.tencent.cloud.polaris.router.interceptor; -import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import com.tencent.cloud.common.constant.RouterConstant; import com.tencent.cloud.polaris.router.PolarisRouterContext; import com.tencent.cloud.polaris.router.config.properties.PolarisRuleBasedRouterProperties; import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor; +import com.tencent.polaris.api.pojo.RouteArgument; import com.tencent.polaris.plugins.router.rule.RuleBasedRouter; import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest; +import org.springframework.util.CollectionUtils; + /** * Router request interceptor for rule based router. * @author lepdou, Hoatian Zhang @@ -44,16 +48,21 @@ public class RuleBasedRouterRequestInterceptor implements RouterRequestIntercept boolean ruleBasedRouterEnabled = polarisRuleBasedRouterProperties.isEnabled(); // set dynamic switch for rule based router - Map metadata = new HashMap<>(); - metadata.put(RuleBasedRouter.ROUTER_ENABLED, String.valueOf(ruleBasedRouterEnabled)); - request.addRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED, metadata); + Set routeArguments = new HashSet<>(); + routeArguments.add(RouteArgument.buildCustom(RuleBasedRouter.ROUTER_ENABLED, String.valueOf(ruleBasedRouterEnabled))); // The label information that the rule based routing depends on // is placed in the metadata of the source service for transmission. // Later, can consider putting it in routerMetadata like other routers. if (ruleBasedRouterEnabled) { Map ruleRouterLabels = routerContext.getLabels(RouterConstant.ROUTER_LABELS); - request.getSourceService().setMetadata(ruleRouterLabels); + if (!CollectionUtils.isEmpty(ruleRouterLabels)) { + for (Map.Entry label : ruleRouterLabels.entrySet()) { + routeArguments.add(RouteArgument.buildCustom(label.getKey(), label.getValue())); + } + } } + + request.putRouterArgument(RuleBasedRouter.ROUTER_TYPE_RULE_BASED, routeArguments); } } diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplierTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplierTest.java index 063997570..67896c05a 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplierTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplierTest.java @@ -24,6 +24,7 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import com.tencent.cloud.common.constant.RouterConstant; @@ -42,6 +43,7 @@ import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor; import com.tencent.polaris.api.pojo.DefaultInstance; import com.tencent.polaris.api.pojo.DefaultServiceInstances; import com.tencent.polaris.api.pojo.Instance; +import com.tencent.polaris.api.pojo.RouteArgument; import com.tencent.polaris.api.pojo.ServiceInstances; import com.tencent.polaris.api.pojo.ServiceKey; import com.tencent.polaris.plugins.router.metadata.MetadataRouter; @@ -124,13 +126,16 @@ public class PolarisRouterServiceInstanceListSupplierTest { ProcessRoutersRequest request = polarisSupplier.buildProcessRoutersRequest(serviceInstances, routerContext); polarisSupplier.processRouterRequestInterceptors(request, routerContext); - Map routerMetadata = request.getRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA); + Set routerMetadata = request.getRouterArguments(MetadataRouter.ROUTER_TYPE_METADATA); Assert.assertEquals(1, routerMetadata.size()); - Assert.assertEquals(0, request.getRouterMetadata(NearbyRouter.ROUTER_TYPE_NEAR_BY).size()); - Assert.assertEquals(1, request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED).size()); - Assert.assertEquals("false", request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED) - .get(RuleBasedRouter.ROUTER_ENABLED)); + Assert.assertEquals(0, request.getRouterArguments(NearbyRouter.ROUTER_TYPE_NEAR_BY).size()); + Assert.assertEquals(1, request.getRouterArguments(RuleBasedRouter.ROUTER_TYPE_RULE_BASED).size()); + + for (RouteArgument routeArgument : request.getRouterArguments(RuleBasedRouter.ROUTER_TYPE_RULE_BASED)) { + Assert.assertEquals(RuleBasedRouter.ROUTER_ENABLED, routeArgument.getKey()); + Assert.assertEquals("false", routeArgument.getValue()); + } } } @@ -153,14 +158,22 @@ public class PolarisRouterServiceInstanceListSupplierTest { ProcessRoutersRequest request = polarisSupplier.buildProcessRoutersRequest(serviceInstances, routerContext); polarisSupplier.processRouterRequestInterceptors(request, routerContext); - Map routerMetadata = request.getRouterMetadata(NearbyRouter.ROUTER_TYPE_NEAR_BY); + Set routerMetadata = request.getRouterArguments(NearbyRouter.ROUTER_TYPE_NEAR_BY); - Assert.assertEquals(0, request.getRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA).size()); + Assert.assertEquals(0, request.getRouterArguments(MetadataRouter.ROUTER_TYPE_METADATA).size()); Assert.assertEquals(1, routerMetadata.size()); - Assert.assertEquals("true", routerMetadata.get(NearbyRouter.ROUTER_ENABLED)); - Assert.assertEquals(1, request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED).size()); - Assert.assertEquals("false", request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED) - .get(RuleBasedRouter.ROUTER_ENABLED)); + + for (RouteArgument routeArgument : routerMetadata) { + Assert.assertEquals(NearbyRouter.ROUTER_ENABLED, routeArgument.getKey()); + Assert.assertEquals("true", routeArgument.getValue()); + } + + Assert.assertEquals(1, request.getRouterArguments(RuleBasedRouter.ROUTER_TYPE_RULE_BASED).size()); + + for (RouteArgument routeArgument : request.getRouterArguments(RuleBasedRouter.ROUTER_TYPE_RULE_BASED)) { + Assert.assertEquals(RuleBasedRouter.ROUTER_ENABLED, routeArgument.getKey()); + Assert.assertEquals("false", routeArgument.getValue()); + } } } @@ -183,14 +196,11 @@ public class PolarisRouterServiceInstanceListSupplierTest { ProcessRoutersRequest request = polarisSupplier.buildProcessRoutersRequest(serviceInstances, routerContext); polarisSupplier.processRouterRequestInterceptors(request, routerContext); - Map routerMetadata = request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED); + Set routerMetadata = request.getRouterArguments(RuleBasedRouter.ROUTER_TYPE_RULE_BASED); - Assert.assertEquals(1, routerMetadata.size()); - Assert.assertEquals(0, request.getRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA).size()); - Assert.assertEquals(0, request.getRouterMetadata(NearbyRouter.ROUTER_TYPE_NEAR_BY).size()); - Assert.assertEquals(1, request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED).size()); - Assert.assertEquals("true", request.getRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED) - .get(RuleBasedRouter.ROUTER_ENABLED)); + Assert.assertEquals(3, routerMetadata.size()); + Assert.assertEquals(0, request.getRouterArguments(MetadataRouter.ROUTER_TYPE_METADATA).size()); + Assert.assertEquals(0, request.getRouterArguments(NearbyRouter.ROUTER_TYPE_NEAR_BY).size()); } } diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index f34100407..f746931c4 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -73,7 +73,7 @@ 1.9.0-2020.0.5-SNAPSHOT - 1.10.0-SNAPSHOT + 1.10.1-SNAPSHOT 1.2.11 4.5.1 1.12.10 diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-featureenv-plugin/src/main/java/com/tencent/cloud/plugin/featureenv/FeatureEnvRouterRequestInterceptor.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-featureenv-plugin/src/main/java/com/tencent/cloud/plugin/featureenv/FeatureEnvRouterRequestInterceptor.java index 05bbdaf40..2a135f3ad 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-featureenv-plugin/src/main/java/com/tencent/cloud/plugin/featureenv/FeatureEnvRouterRequestInterceptor.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-featureenv-plugin/src/main/java/com/tencent/cloud/plugin/featureenv/FeatureEnvRouterRequestInterceptor.java @@ -18,11 +18,12 @@ package com.tencent.cloud.plugin.featureenv; -import java.util.HashMap; -import java.util.Map; +import java.util.HashSet; +import java.util.Set; import com.tencent.cloud.polaris.router.PolarisRouterContext; import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor; +import com.tencent.polaris.api.pojo.RouteArgument; import com.tencent.polaris.api.rpc.MetadataFailoverType; import com.tencent.polaris.plugins.router.metadata.MetadataRouter; import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest; @@ -54,10 +55,11 @@ public class FeatureEnvRouterRequestInterceptor implements RouterRequestIntercep } //3. set env metadata to router request - Map envMetadata = new HashMap<>(); - envMetadata.put(envLabelKey, envLabelValue); + Set routeArguments = new HashSet<>(1); + routeArguments.add(RouteArgument.buildCustom(envLabelKey, envLabelValue)); + + request.putRouterArgument(MetadataRouter.ROUTER_TYPE_METADATA, routeArguments); - request.addRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA, envMetadata); //4. set failover type to others request.setMetadataFailoverType(MetadataFailoverType.METADATAFAILOVERNOTKEY);