diff --git a/CHANGELOG.md b/CHANGELOG.md index bc8684c0e..6301c6b37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,4 +27,5 @@ - [fix: move ConditionalOnTsfEnabled to spring-cloud-tencent-commons and fix PolarisInetUtilsAutoConfiguration.](https://github.com/Tencent/spring-cloud-tencent/pull/1354) - [fix: memory cost too many when using wildcard feign calls](https://github.com/Tencent/spring-cloud-tencent/pull/1356) - [feat: support otel trace](https://github.com/Tencent/spring-cloud-tencent/pull/1363) -- [feat:support TSF router.](https://github.com/Tencent/spring-cloud-tencent/pull/1368) \ No newline at end of file +- [feat:support TSF router.](https://github.com/Tencent/spring-cloud-tencent/pull/1368) +- [feat:upgrade nearby router and add namespace nearby router.](https://github.com/Tencent/spring-cloud-tencent/pull/1371) diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml b/spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml index 230e50a82..aa627b37c 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml @@ -57,6 +57,10 @@ com.tencent.polaris router-nearby + + com.tencent.polaris + router-namespace + com.tencent.polaris router-metadata diff --git a/spring-cloud-starter-tencent-polaris-config/pom.xml b/spring-cloud-starter-tencent-polaris-config/pom.xml index e7262c29f..dde98dbae 100644 --- a/spring-cloud-starter-tencent-polaris-config/pom.xml +++ b/spring-cloud-starter-tencent-polaris-config/pom.xml @@ -1,6 +1,6 @@ - spring-cloud-tencent @@ -34,6 +34,10 @@ com.tencent.polaris router-nearby + + com.tencent.polaris + router-namespace + com.tencent.polaris router-metadata diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index 7c3bf8889..a1588af44 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -146,8 +146,9 @@ public class PolarisServiceRegistry implements ServiceRegistrycom.tencent.polaris router-nearby + + com.tencent.polaris + router-namespace + com.tencent.polaris router-canary diff --git a/spring-cloud-starter-tencent-polaris-router/pom.xml b/spring-cloud-starter-tencent-polaris-router/pom.xml index 2e8dc7cb4..e97f4d864 100644 --- a/spring-cloud-starter-tencent-polaris-router/pom.xml +++ b/spring-cloud-starter-tencent-polaris-router/pom.xml @@ -44,6 +44,10 @@ com.tencent.polaris router-nearby + + com.tencent.polaris + router-namespace + diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterConfigModifier.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterConfigModifier.java index 455578050..67376a187 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterConfigModifier.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/RouterConfigModifier.java @@ -22,10 +22,10 @@ import com.tencent.cloud.common.constant.OrderConstant; import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties; import com.tencent.polaris.api.config.consumer.ServiceRouterConfig; -import com.tencent.polaris.api.plugin.route.LocationLevel; import com.tencent.polaris.factory.config.ConfigurationImpl; import com.tencent.polaris.plugins.router.healthy.RecoverRouterConfig; import com.tencent.polaris.plugins.router.nearby.NearbyRouterConfig; +import com.tencent.polaris.specification.api.v1.traffic.manage.RoutingProto; import org.apache.commons.lang.StringUtils; /** @@ -53,7 +53,8 @@ public class RouterConfigModifier implements PolarisConfigModifier { .setPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_RECOVER, recoverRouterConfig); if (StringUtils.isNotBlank(polarisNearByRouterProperties.getMatchLevel())) { - LocationLevel locationLevel = LocationLevel.valueOf(polarisNearByRouterProperties.getMatchLevel()); + RoutingProto.NearbyRoutingConfig.LocationLevel locationLevel = + RoutingProto.NearbyRoutingConfig.LocationLevel.valueOf(StringUtils.upperCase(polarisNearByRouterProperties.getMatchLevel())); NearbyRouterConfig nearbyRouterConfig = configuration.getConsumer().getServiceRouter().getPluginConfig( ServiceRouterConfig.DEFAULT_ROUTER_NEARBY, NearbyRouterConfig.class); nearbyRouterConfig.setMatchLevel(locationLevel); diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterAutoConfiguration.java index b052aab22..ec4fbf282 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterAutoConfiguration.java @@ -23,9 +23,11 @@ import java.util.Collections; import java.util.List; import com.tencent.cloud.polaris.router.config.properties.PolarisMetadataRouterProperties; +import com.tencent.cloud.polaris.router.config.properties.PolarisNamespaceRouterProperties; import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties; import com.tencent.cloud.polaris.router.config.properties.PolarisRuleBasedRouterProperties; import com.tencent.cloud.polaris.router.interceptor.MetadataRouterRequestInterceptor; +import com.tencent.cloud.polaris.router.interceptor.NamespaceRouterRequestInterceptor; import com.tencent.cloud.polaris.router.interceptor.NearbyRouterRequestInterceptor; import com.tencent.cloud.polaris.router.interceptor.RuleBasedRouterRequestInterceptor; import com.tencent.cloud.polaris.router.resttemplate.RouterLabelRestTemplateInterceptor; @@ -70,6 +72,11 @@ public class RouterAutoConfiguration { return new RuleBasedRouterRequestInterceptor(polarisRuleBasedRouterProperties); } + @Bean + @ConditionalOnProperty(value = "spring.cloud.polaris.router.namespace-router.enabled", matchIfMissing = true) + public NamespaceRouterRequestInterceptor namespaceRouterRequestInterceptor(PolarisNamespaceRouterProperties polarisNamespaceRouterProperties) { + return new NamespaceRouterRequestInterceptor(polarisNamespaceRouterProperties); + } /** * Create when gateway application is SCG. diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterConfigModifierAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterConfigModifierAutoConfiguration.java index e1de4a37f..c3669ab80 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterConfigModifierAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/RouterConfigModifierAutoConfiguration.java @@ -20,6 +20,7 @@ package com.tencent.cloud.polaris.router.config; import com.tencent.cloud.polaris.router.RouterConfigModifier; import com.tencent.cloud.polaris.router.config.properties.PolarisMetadataRouterProperties; +import com.tencent.cloud.polaris.router.config.properties.PolarisNamespaceRouterProperties; import com.tencent.cloud.polaris.router.config.properties.PolarisNearByRouterProperties; import com.tencent.cloud.polaris.router.config.properties.PolarisRuleBasedRouterProperties; @@ -35,7 +36,8 @@ import org.springframework.context.annotation.Import; */ @Configuration(proxyBeanMethods = false) @ConditionalOnPolarisRouterEnabled -@Import({PolarisNearByRouterProperties.class, PolarisMetadataRouterProperties.class, PolarisRuleBasedRouterProperties.class}) +@Import({PolarisNearByRouterProperties.class, PolarisMetadataRouterProperties.class, PolarisRuleBasedRouterProperties.class, + PolarisNamespaceRouterProperties.class}) public class RouterConfigModifierAutoConfiguration { @Bean diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/properties/PolarisNamespaceRouterProperties.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/properties/PolarisNamespaceRouterProperties.java new file mode 100644 index 000000000..16250b722 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/properties/PolarisNamespaceRouterProperties.java @@ -0,0 +1,59 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.router.config.properties; + +import com.tencent.polaris.api.rpc.NamespaceRouterFailoverType; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * the configuration for namespace router. + * + * @author lepdou 2022-05-23 + */ +@ConfigurationProperties(prefix = "spring.cloud.polaris.router.namespace-router") +public class PolarisNamespaceRouterProperties { + + private boolean enabled = false; + + private NamespaceRouterFailoverType failOver = NamespaceRouterFailoverType.all; + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public NamespaceRouterFailoverType getFailOver() { + return failOver; + } + + public void setFailOver(NamespaceRouterFailoverType failOver) { + this.failOver = failOver; + } + + @Override + public String toString() { + return "PolarisNamespaceRouterProperties{" + + "enabled=" + enabled + + ", failOver=" + failOver + + '}'; + } +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterProperties.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterProperties.java index df861e19a..226195a13 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterProperties.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterProperties.java @@ -27,7 +27,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "spring.cloud.polaris.router.nearby-router") public class PolarisNearByRouterProperties { - private boolean enabled = true; + private boolean enabled = false; private String matchLevel; diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/NamespaceRouterRequestInterceptor.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/NamespaceRouterRequestInterceptor.java new file mode 100644 index 000000000..e9dd95fe3 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/interceptor/NamespaceRouterRequestInterceptor.java @@ -0,0 +1,55 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * + */ + +package com.tencent.cloud.polaris.router.interceptor; + +import com.tencent.cloud.common.metadata.MetadataContextHolder; +import com.tencent.cloud.polaris.router.PolarisRouterContext; +import com.tencent.cloud.polaris.router.config.properties.PolarisNamespaceRouterProperties; +import com.tencent.cloud.polaris.router.spi.RouterRequestInterceptor; +import com.tencent.polaris.metadata.core.MetadataContainer; +import com.tencent.polaris.metadata.core.MetadataType; +import com.tencent.polaris.metadata.core.TransitiveType; +import com.tencent.polaris.plugins.router.namespace.NamespaceRouter; +import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest; + +/** + * Router request interceptor for namespace router. + * + * @author Hoatian Zhang + */ +public class NamespaceRouterRequestInterceptor implements RouterRequestInterceptor { + + private final PolarisNamespaceRouterProperties polarisNamespaceRouterProperties; + + public NamespaceRouterRequestInterceptor(PolarisNamespaceRouterProperties polarisNamespaceRouterProperties) { + this.polarisNamespaceRouterProperties = polarisNamespaceRouterProperties; + } + + @Override + public void apply(ProcessRoutersRequest request, PolarisRouterContext routerContext) { + // set namespace router enable + MetadataContainer metadataContainer = MetadataContextHolder.get() + .getMetadataContainer(MetadataType.CUSTOM, false); + metadataContainer.putMetadataMapValue(NamespaceRouter.ROUTER_TYPE_NAMESPACE, NamespaceRouter.ROUTER_ENABLED, + String.valueOf(polarisNamespaceRouterProperties.isEnabled()), TransitiveType.NONE); + + // set namespace router fail over type. + request.setNamespaceRouterFailoverType(polarisNamespaceRouterProperties.getFailOver()); + } +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/additional-spring-configuration-metadata.json index f628ed620..92251c916 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -9,14 +9,14 @@ { "name": "spring.cloud.polaris.router.nearby-router.enabled", "type": "java.lang.Boolean", - "defaultValue": true, + "defaultValue": false, "description": "the switch for nearby router." }, { "name": "spring.cloud.polaris.router.nearby-router.matchLevel", "type": "java.lang.String", - "defaultValue": "zone", - "description": "the match level for nearby router, options can be region/zone/campus." + "defaultValue": "ZONE", + "description": "the match level for nearby router, options can be REGION/ZONE/CAMPUS." }, { "name": "spring.cloud.polaris.router.rule-router.enabled", @@ -30,11 +30,42 @@ "defaultValue": "all", "description": "the fail over type for rule based router." }, + { + "name": "spring.cloud.polaris.router.namespace-router.enabled", + "type": "java.lang.Boolean", + "defaultValue": false, + "description": "the switch for namespace router." + }, + { + "name": "spring.cloud.polaris.router.namespace-router.failOver", + "type": "java.lang.String", + "defaultValue": "all", + "description": "the fail over type for namespace router." + }, { "name": "spring.cloud.polaris.router.enabled", "type": "java.lang.Boolean", "defaultValue": true, "description": "the switch for router module." } + ], + "hints": [ + { + "name": "spring.cloud.polaris.router.nearby-router.matchLevel", + "values": [ + { + "value": "CAMPUS" + }, + { + "value": "ZONE" + }, + { + "value": "REGION" + }, + { + "value": "ALL" + } + ] + } ] } diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/RouterBootstrapAutoConfigurationTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/RouterBootstrapAutoConfigurationTest.java index f10d9ada4..4c0048bcd 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/RouterBootstrapAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/RouterBootstrapAutoConfigurationTest.java @@ -49,7 +49,7 @@ public class RouterBootstrapAutoConfigurationTest { RpcEnhancementAutoConfiguration.class, RouterBootstrapAutoConfiguration.class)) .withPropertyValues("spring.cloud.polaris.enabled=true") - .withPropertyValues("spring.cloud.polaris.router.nearby-router.matchLevel=campus") + .withPropertyValues("spring.cloud.polaris.router.nearby-router.matchLevel=CAMPUS") .withPropertyValues("spring.cloud.polaris.circuitbreaker.enabled=true"); @Test @@ -61,7 +61,7 @@ public class RouterBootstrapAutoConfigurationTest { routerConfigModifier.modify((ConfigurationImpl) configuration); NearbyRouterConfig nearbyRouterConfig = configuration.getConsumer().getServiceRouter().getPluginConfig( ServiceRouterConfig.DEFAULT_ROUTER_NEARBY, NearbyRouterConfig.class); - Assertions.assertEquals("campus", nearbyRouterConfig.getMatchLevel().name()); + Assertions.assertEquals("CAMPUS", nearbyRouterConfig.getMatchLevel().name()); }); } } diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java index 379fbf7e9..7b2598f33 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java @@ -37,7 +37,7 @@ public class PolarisNearByRouterPropertiesTest { @Test public void isEnabled() { - assertThat(properties.isEnabled()).isEqualTo(true); + assertThat(properties.isEnabled()).isEqualTo(false); } @Test @@ -49,6 +49,6 @@ public class PolarisNearByRouterPropertiesTest { @Test public void testToString() { assertThat(properties.toString()) - .isEqualTo("PolarisNearByRouterProperties{enabled=true, matchLevel='null'}"); + .isEqualTo("PolarisNearByRouterProperties{enabled=false, matchLevel='null'}"); } } diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java index bdd60f8ef..4690489b2 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java @@ -39,8 +39,8 @@ public class CustomMetadata implements InstanceMetadataProvider { return metadata; } - @Override - public String getZone() { - return "shenzhen-zone-1"; - } +// @Override +// public String getZone() { +// return "shenzhen-zone-1"; +// } } diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/application.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/application.yml index 787baac99..f87641bc1 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/application.yml +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/application.yml @@ -37,7 +37,7 @@ spring: metadata: content: label1: value1 - region: huanan +# region: huanan management: endpoints: web: diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java index 8257f2cd5..4cbacb2d6 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/java/com/tencent/cloud/quickstart/callee/CustomMetadata.java @@ -39,8 +39,8 @@ public class CustomMetadata implements InstanceMetadataProvider { return metadata; } - @Override - public String getZone() { - return "shenzhen-zone-2"; - } +// @Override +// public String getZone() { +// return "shenzhen-zone-2"; +// } } diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/application.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/application.yml index b74bef5a1..ef1c3cf9b 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/application.yml +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/application.yml @@ -36,7 +36,7 @@ spring: metadata: content: label1: value2 - region: huanan +# region: huanan management: endpoints: web: diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/CustomMetadataProvider.java b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/CustomMetadataProvider.java index 002ad99a0..f49315220 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/CustomMetadataProvider.java +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/java/com/tencent/cloud/quickstart/caller/CustomMetadataProvider.java @@ -28,8 +28,8 @@ import org.springframework.stereotype.Component; @Component public class CustomMetadataProvider implements InstanceMetadataProvider { - @Override - public String getRegion() { - return "huadong"; - } +// @Override +// public String getRegion() { +// return "huadong"; +// } } diff --git a/spring-cloud-tencent-polaris-context/pom.xml b/spring-cloud-tencent-polaris-context/pom.xml index a1d62e918..c05cc4ab9 100644 --- a/spring-cloud-tencent-polaris-context/pom.xml +++ b/spring-cloud-tencent-polaris-context/pom.xml @@ -34,6 +34,10 @@ com.tencent.polaris router-nearby + + com.tencent.polaris + router-namespace + com.tencent.polaris router-metadata @@ -108,6 +112,10 @@ com.tencent.polaris router-nearby + + com.tencent.polaris + router-namespace + com.tencent.polaris router-metadata diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PostInitPolarisSDKContext.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PostInitPolarisSDKContext.java index bf41c50bd..132d9e618 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PostInitPolarisSDKContext.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PostInitPolarisSDKContext.java @@ -20,8 +20,8 @@ package com.tencent.cloud.polaris.context; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.polaris.api.plugin.common.ValueContext; -import com.tencent.polaris.api.plugin.route.LocationLevel; import com.tencent.polaris.client.api.SDKContext; +import com.tencent.polaris.specification.api.v1.traffic.manage.RoutingProto; import org.apache.commons.lang.StringUtils; /** @@ -39,13 +39,13 @@ public class PostInitPolarisSDKContext { ValueContext valueContext = sdkContext.getValueContext(); if (StringUtils.isNotBlank(region)) { - valueContext.setValue(LocationLevel.region.name(), region); + valueContext.setValue(RoutingProto.NearbyRoutingConfig.LocationLevel.REGION.name(), region); } if (StringUtils.isNotBlank(zone)) { - valueContext.setValue(LocationLevel.zone.name(), zone); + valueContext.setValue(RoutingProto.NearbyRoutingConfig.LocationLevel.ZONE.name(), zone); } if (StringUtils.isNotBlank(campus)) { - valueContext.setValue(LocationLevel.campus.name(), campus); + valueContext.setValue(RoutingProto.NearbyRoutingConfig.LocationLevel.CAMPUS.name(), campus); } } } diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/env/TsfCoreEnvironmentPostProcessor.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/env/TsfCoreEnvironmentPostProcessor.java index 6aa9d6248..fd39beb9b 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/env/TsfCoreEnvironmentPostProcessor.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/env/TsfCoreEnvironmentPostProcessor.java @@ -120,12 +120,15 @@ public final class TsfCoreEnvironmentPostProcessor implements EnvironmentPostPro // tse_polaris_ip defaultProperties.put("spring.cloud.polaris.address", "grpc://" + environment.getProperty("tse_polaris_ip", "") + ":8091"); - // tse_polaris_ip + // tsf_sctt_extensions_port defaultProperties.put("spring.cloud.polaris.stat.port", environment.getProperty("tsf_sctt_extensions_port", "11134")); // rule based router fail over type defaultProperties.put("spring.cloud.polaris.router.rule-router.fail-over", "none"); + // namespace affinity router + defaultProperties.put("spring.cloud.polaris.router.namespace-router.enabled", "true"); + MapPropertySource propertySource = new MapPropertySource("tsf-polaris-properties", defaultProperties); environment.getPropertySources().addFirst(propertySource); } diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/metadata/TsfInstanceMetadataProvider.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/metadata/TsfInstanceMetadataProvider.java index 0c490020f..9ee28a9ba 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/metadata/TsfInstanceMetadataProvider.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/metadata/TsfInstanceMetadataProvider.java @@ -74,4 +74,14 @@ public class TsfInstanceMetadataProvider implements InstanceMetadataProvider { } }}; } + + @Override + public String getRegion() { + return tsfCoreProperties.getTsfRegion(); + } + + @Override + public String getZone() { + return tsfCoreProperties.getTsfZone(); + } } diff --git a/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PostInitPolarisSDKContextTest.java b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PostInitPolarisSDKContextTest.java index a5567c764..c6d06f8a5 100644 --- a/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PostInitPolarisSDKContextTest.java +++ b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PostInitPolarisSDKContextTest.java @@ -19,8 +19,8 @@ package com.tencent.cloud.polaris.context; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.polaris.api.plugin.common.ValueContext; -import com.tencent.polaris.api.plugin.route.LocationLevel; import com.tencent.polaris.client.api.SDKContext; +import com.tencent.polaris.specification.api.v1.traffic.manage.RoutingProto; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -55,9 +55,9 @@ public final class PostInitPolarisSDKContextTest { when(staticMetadataManager.getCampus()).thenReturn(CAMPUS); new PostInitPolarisSDKContext(sdkContext, staticMetadataManager); - String regionName = valueContext.getValue(LocationLevel.region.name()); - String zoneName = valueContext.getValue(LocationLevel.zone.name()); - String campusName = valueContext.getValue(LocationLevel.campus.name()); + String regionName = valueContext.getValue(RoutingProto.NearbyRoutingConfig.LocationLevel.REGION.name()); + String zoneName = valueContext.getValue(RoutingProto.NearbyRoutingConfig.LocationLevel.ZONE.name()); + String campusName = valueContext.getValue(RoutingProto.NearbyRoutingConfig.LocationLevel.CAMPUS.name()); Assertions.assertThat(regionName).isEqualTo(REGION); Assertions.assertThat(zoneName).isEqualTo(ZONE); diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/PolarisInstanceTransformer.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/PolarisInstanceTransformer.java index 73f6db5cc..81e8beb23 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/PolarisInstanceTransformer.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/transformer/PolarisInstanceTransformer.java @@ -40,7 +40,7 @@ public class PolarisInstanceTransformer implements InstanceTransformer { instance.setCampus(polarisServiceInstance.getPolarisInstance().getCampus()); instance.setWeight(polarisServiceInstance.getPolarisInstance().getWeight()); if (CollectionUtils.isNotEmpty(polarisServiceInstance.getServiceMetadata())) { - instance.getServiceMetadata().putAll(polarisServiceInstance.getServiceMetadata()); + instance.setServiceMetadata(polarisServiceInstance.getServiceMetadata()); } } }