From d1b8f7dadea1870e42c82df5a2a7e12aa07dc469 Mon Sep 17 00:00:00 2001 From: lepdou Date: Wed, 29 Jun 2022 16:51:43 +0800 Subject: [PATCH] optimize static metadata manager (#341) --- CHANGELOG.md | 1 + .../PolarisFeignClientAutoConfiguration.java | 2 +- .../PolarisRestTemplateAutoConfiguration.java | 2 +- ...larisFeignClientAutoConfigurationTest.java | 2 +- .../polaris/config/ConfigurationModifier.java | 2 +- ...larisConfigBootstrapAutoConfiguration.java | 4 +- .../adapter/PolarisConfigFileLocator.java | 2 +- .../adapter/PolarisConfigFileLocatorTest.java | 2 +- .../polaris/registry/PolarisRegistration.java | 24 +-- ...larisServiceRegistryAutoConfiguration.java | 6 +- ...coveryPropertiesAutoConfigurationTest.java | 2 +- ...pertiesBootstrapAutoConfigurationTest.java | 2 +- ...PolarisDiscoveryAutoConfigurationTest.java | 2 +- ...larisDiscoveryClientConfigurationTest.java | 2 +- .../PolarisServiceDiscoveryTest.java | 2 +- ...ctiveDiscoveryClientConfigurationTest.java | 2 +- .../registry/PolarisRegistrationTest.java | 2 +- ...sServiceRegistryAutoConfigurationTest.java | 2 +- .../registry/PolarisServiceRegistryTest.java | 2 +- .../PolarisRateLimitAutoConfiguration.java | 2 +- ...PolarisRateLimitAutoConfigurationTest.java | 2 +- .../router/spi/RouterLabelResolver.java | 9 +- .../metadata/StaticMetadataManager.java | 102 +++++++++- .../config/MetadataAutoConfiguration.java | 7 +- .../common}/spi/InstanceMetadataProvider.java | 27 ++- .../metadata/StaticMetadataManagerTest.java | 186 ++++++++++++++++++ .../service/callee/CustomMetadata.java | 14 +- .../src/main/resources/bootstrap.yml | 4 + .../src/main/resources/bootstrap.yml | 4 + .../example/CustomRouterLabelResolver.java | 1 + .../cloud/polaris/context/ModifyAddress.java | 1 + .../context/PostInitPolarisSDKContext.java | 50 +++++ .../PolarisContextAutoConfiguration.java | 35 +--- ...arisContextBootstrapAutoConfiguration.java | 2 +- .../PolarisContextPostConfiguration.java | 39 ++++ .../PolarisContextProperties.java | 3 +- ...itional-spring-configuration-metadata.json | 12 +- .../main/resources/META-INF/spring.factories | 5 +- .../PolarisContextAutoConfigurationTest.java | 1 + .../context/PolarisContextGetHostTest.java | 2 + ...arisLoadBalancerAutoConfigurationTest.java | 2 +- .../PolarisRouterAutoConfigurationTest.java | 2 +- 42 files changed, 462 insertions(+), 115 deletions(-) rename {spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context => spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common}/spi/InstanceMetadataProvider.java (73%) create mode 100644 spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java create mode 100644 spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PostInitPolarisSDKContext.java rename spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/{ => config}/PolarisContextAutoConfiguration.java (58%) rename spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/{ => config}/PolarisContextBootstrapAutoConfiguration.java (95%) create mode 100644 spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextPostConfiguration.java rename spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/{ => config}/PolarisContextProperties.java (96%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05b9f7c7b..758c67e26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,3 +18,4 @@ - [Feature: add spring cloud tencent logo](https://github.com/Tencent/spring-cloud-tencent/pull/331) - [UT: add metadata-transfer unit test](https://github.com/Tencent/spring-cloud-tencent/pull/334) - [test:update junit of metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/338) +- [Feature: Optimize static metadata manager](https://github.com/Tencent/spring-cloud-tencent/pull/341) diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisFeignClientAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisFeignClientAutoConfiguration.java index ae699dead..a1f50a3df 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisFeignClientAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisFeignClientAutoConfiguration.java @@ -18,7 +18,7 @@ package com.tencent.cloud.polaris.circuitbreaker.config; import com.tencent.cloud.polaris.circuitbreaker.feign.PolarisFeignBeanPostProcessor; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.factory.api.DiscoveryAPIFactory; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisRestTemplateAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisRestTemplateAutoConfiguration.java index ea99fce36..d537de32e 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisRestTemplateAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/config/PolarisRestTemplateAutoConfiguration.java @@ -20,7 +20,7 @@ package com.tencent.cloud.polaris.circuitbreaker.config; import com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisResponseErrorHandler; import com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisRestTemplateModifier; import com.tencent.cloud.polaris.circuitbreaker.resttemplate.PolarisRestTemplateResponseErrorHandler; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.polaris.api.core.ConsumerAPI; import org.springframework.beans.factory.annotation.Autowired; diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfigurationTest.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfigurationTest.java index 2443949c9..682b99522 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfigurationTest.java @@ -19,7 +19,7 @@ package com.tencent.cloud.polaris.circuitbreaker; import com.tencent.cloud.polaris.circuitbreaker.config.PolarisFeignClientAutoConfiguration; import com.tencent.cloud.polaris.circuitbreaker.feign.PolarisFeignBeanPostProcessor; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.polaris.api.core.ConsumerAPI; import org.junit.Test; diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java index 495d6ec3a..9953b3c67 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java @@ -25,7 +25,7 @@ import com.tencent.cloud.common.constant.ContextConstant; import com.tencent.cloud.common.util.AddressUtils; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.context.PolarisConfigModifier; -import com.tencent.cloud.polaris.context.PolarisContextProperties; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.polaris.factory.config.ConfigurationImpl; import org.apache.commons.lang.StringUtils; diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java index 0a87c0e65..20f719108 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java @@ -21,8 +21,8 @@ import com.tencent.cloud.polaris.config.adapter.PolarisConfigFileLocator; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; -import com.tencent.cloud.polaris.context.PolarisContextProperties; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory; diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java index b23d6a132..0b42e8dc4 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java @@ -25,7 +25,7 @@ import java.util.concurrent.ConcurrentHashMap; import com.tencent.cloud.polaris.config.config.ConfigFileGroup; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.enums.ConfigFileFormat; -import com.tencent.cloud.polaris.context.PolarisContextProperties; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.polaris.configuration.api.core.ConfigFileMetadata; import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.api.core.ConfigKVFile; diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java index 6fe0bfa2a..8c072ef9d 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java @@ -26,7 +26,7 @@ import java.util.Map; import com.google.common.collect.Lists; import com.tencent.cloud.polaris.config.config.ConfigFileGroup; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; -import com.tencent.cloud.polaris.context.PolarisContextProperties; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.api.core.ConfigKVFile; import org.junit.Assert; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java index ba018a658..e0a57e9f7 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java @@ -26,7 +26,6 @@ import java.util.Map; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; -import com.tencent.cloud.polaris.context.spi.InstanceMetadataProvider; import com.tencent.polaris.client.api.SDKContext; import org.apache.commons.lang.StringUtils; @@ -53,8 +52,6 @@ public class PolarisRegistration implements Registration, ServiceInstance { private final StaticMetadataManager staticMetadataManager; - private final InstanceMetadataProvider instanceMetadataProvider; - private Map metadata; private final String host; @@ -62,13 +59,11 @@ public class PolarisRegistration implements Registration, ServiceInstance { public PolarisRegistration( DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration, PolarisDiscoveryProperties polarisDiscoveryProperties, SDKContext context, - StaticMetadataManager staticMetadataManager, - InstanceMetadataProvider instanceMetadataProvider) { + StaticMetadataManager staticMetadataManager) { this.discoveryPropertiesAutoConfiguration = discoveryPropertiesAutoConfiguration; this.polarisDiscoveryProperties = polarisDiscoveryProperties; this.polarisContext = context; this.staticMetadataManager = staticMetadataManager; - this.instanceMetadataProvider = instanceMetadataProvider; host = polarisContext.getConfig().getGlobal().getAPI().getBindIP(); } @@ -116,23 +111,6 @@ public class PolarisRegistration implements Registration, ServiceInstance { // location info will be putted both in metadata and instance's field instanceMetadata.putAll(staticMetadataManager.getLocationMetadata()); - // custom metadata from spi - if (instanceMetadataProvider != null) { - if (StringUtils.isNotBlank(instanceMetadataProvider.getRegion())) { - instanceMetadata.put(StaticMetadataManager.LOCATION_KEY_ZONE, instanceMetadataProvider.getRegion()); - } - if (StringUtils.isNotBlank(instanceMetadataProvider.getZone())) { - instanceMetadata.put(StaticMetadataManager.LOCATION_KEY_ZONE, instanceMetadataProvider.getZone()); - } - if (StringUtils.isNotBlank(instanceMetadataProvider.getCampus())) { - instanceMetadata.put(StaticMetadataManager.LOCATION_KEY_ZONE, instanceMetadataProvider.getCampus()); - } - - if (!CollectionUtils.isEmpty(instanceMetadataProvider.getMetadata())) { - instanceMetadata.putAll(instanceMetadataProvider.getMetadata()); - } - } - this.metadata = Collections.unmodifiableMap(instanceMetadata); } return metadata; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java index 01e4f2910..f6a689a57 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfiguration.java @@ -21,7 +21,6 @@ package com.tencent.cloud.polaris.registry; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; -import com.tencent.cloud.polaris.context.spi.InstanceMetadataProvider; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import com.tencent.polaris.client.api.SDKContext; @@ -35,7 +34,6 @@ import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationC import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.lang.Nullable; /** * Autoconfiguration of service registry of Polaris. @@ -62,9 +60,9 @@ public class PolarisServiceRegistryAutoConfiguration { public PolarisRegistration polarisRegistration( DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration, PolarisDiscoveryProperties polarisDiscoveryProperties, SDKContext context, - StaticMetadataManager staticMetadataManager, @Nullable InstanceMetadataProvider instanceMetadataProvider) { + StaticMetadataManager staticMetadataManager) { return new PolarisRegistration(discoveryPropertiesAutoConfiguration, - polarisDiscoveryProperties, context, staticMetadataManager, instanceMetadataProvider); + polarisDiscoveryProperties, context, staticMetadataManager); } @Bean diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfigurationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfigurationTest.java index b75d16d93..8f89a8313 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfigurationTest.java @@ -17,7 +17,7 @@ package com.tencent.cloud.polaris; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import com.tencent.cloud.polaris.extend.consul.ConsulContextProperties; import com.tencent.polaris.api.core.ConsumerAPI; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/DiscoveryPropertiesBootstrapAutoConfigurationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/DiscoveryPropertiesBootstrapAutoConfigurationTest.java index 729c0a30a..3ef145d0e 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/DiscoveryPropertiesBootstrapAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/DiscoveryPropertiesBootstrapAutoConfigurationTest.java @@ -17,7 +17,7 @@ package com.tencent.cloud.polaris; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfigurationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfigurationTest.java index 970466391..388c1bbaa 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryAutoConfigurationTest.java @@ -19,7 +19,7 @@ package com.tencent.cloud.polaris.discovery; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.core.ProviderAPI; import com.tencent.polaris.test.mock.discovery.NamingServer; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfigurationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfigurationTest.java index 3e7dc0378..9f50c0bef 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryClientConfigurationTest.java @@ -17,7 +17,7 @@ package com.tencent.cloud.polaris.discovery; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.polaris.test.mock.discovery.NamingServer; import org.junit.AfterClass; import org.junit.BeforeClass; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscoveryTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscoveryTest.java index 78fe10ee6..ed8289b68 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscoveryTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscoveryTest.java @@ -19,7 +19,7 @@ package com.tencent.cloud.polaris.discovery; import java.util.List; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.polaris.api.exception.PolarisException; import com.tencent.polaris.api.pojo.ServiceKey; import com.tencent.polaris.test.mock.discovery.NamingServer; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfigurationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfigurationTest.java index 3bca80f06..ca64a22e7 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/discovery/reactive/PolarisReactiveDiscoveryClientConfigurationTest.java @@ -17,7 +17,7 @@ package com.tencent.cloud.polaris.discovery.reactive; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration; import com.tencent.polaris.test.mock.discovery.NamingServer; import org.junit.AfterClass; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java index 11d380a75..cd32ec090 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisRegistrationTest.java @@ -78,7 +78,7 @@ public class PolarisRegistrationTest { doReturn(Collections.singletonMap("key2", "value2")).when(staticMetadataManager).getLocationMetadata(); polarisRegistration = new PolarisRegistration( - discoveryPropertiesAutoConfiguration, polarisDiscoveryProperties, polarisContext, staticMetadataManager, null); + discoveryPropertiesAutoConfiguration, polarisDiscoveryProperties, polarisContext, staticMetadataManager); } @Test diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfigurationTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfigurationTest.java index 7a59e405f..4230cdebc 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryAutoConfigurationTest.java @@ -17,7 +17,7 @@ package com.tencent.cloud.polaris.registry; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration; import com.tencent.polaris.test.mock.discovery.NamingServer; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryTest.java index 02fff38f4..246bc64b9 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistryTest.java @@ -17,7 +17,7 @@ package com.tencent.cloud.polaris.registry; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration; import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration; import com.tencent.polaris.api.pojo.ServiceKey; diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/config/PolarisRateLimitAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/config/PolarisRateLimitAutoConfiguration.java index 01dd591dc..83cc00c91 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/config/PolarisRateLimitAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/config/PolarisRateLimitAutoConfiguration.java @@ -19,8 +19,8 @@ package com.tencent.cloud.polaris.ratelimit.config; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.context.ServiceRuleManager; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.ratelimit.RateLimitRuleLabelResolver; import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant; import com.tencent.cloud.polaris.ratelimit.filter.QuotaCheckReactiveFilter; diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/config/PolarisRateLimitAutoConfigurationTest.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/config/PolarisRateLimitAutoConfigurationTest.java index a531d81a8..387fe510d 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/config/PolarisRateLimitAutoConfigurationTest.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/config/PolarisRateLimitAutoConfigurationTest.java @@ -17,7 +17,7 @@ package com.tencent.cloud.polaris.ratelimit.config; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.ratelimit.RateLimitRuleLabelResolver; import com.tencent.cloud.polaris.ratelimit.filter.QuotaCheckReactiveFilter; import com.tencent.cloud.polaris.ratelimit.filter.QuotaCheckServletFilter; diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/spi/RouterLabelResolver.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/spi/RouterLabelResolver.java index 49b9ccf4c..99a6b5bf3 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/spi/RouterLabelResolver.java +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/spi/RouterLabelResolver.java @@ -18,6 +18,7 @@ package com.tencent.cloud.polaris.router.spi; +import java.util.Collections; import java.util.Map; import feign.RequestTemplate; @@ -37,7 +38,9 @@ public interface RouterLabelResolver extends Ordered { * @param requestTemplate the feign request. * @return resolved labels */ - Map resolve(RequestTemplate requestTemplate); + default Map resolve(RequestTemplate requestTemplate) { + return Collections.emptyMap(); + } /** * resolve labels from rest template request. @@ -45,5 +48,7 @@ public interface RouterLabelResolver extends Ordered { * @param body the rest template request body. * @return resolved labels */ - Map resolve(HttpRequest request, byte[] body); + default Map resolve(HttpRequest request, byte[] body) { + return Collections.emptyMap(); + } } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java index 575775bf7..5d4143b73 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java @@ -22,14 +22,18 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; +import com.tencent.cloud.common.spi.InstanceMetadataProvider; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.util.CollectionUtils; + /** - * manage metadata from env/config file. + * manage metadata from env/config file/custom spi. * *@author lepdou 2022-05-20 */ @@ -60,17 +64,26 @@ public class StaticMetadataManager { private Map envTransitiveMetadata; private Map configMetadata; private Map configTransitiveMetadata; + private Map customSPIMetadata; + private Map customSPITransitiveMetadata; + private Map mergedStaticMetadata; private Map mergedStaticTransitiveMetadata; private String zone; private String region; private String campus; - public StaticMetadataManager(MetadataLocalProperties metadataLocalProperties) { + public StaticMetadataManager(MetadataLocalProperties metadataLocalProperties, + InstanceMetadataProvider instanceMetadataProvider) { parseConfigMetadata(metadataLocalProperties); + parseEnvMetadata(); + + parseCustomMetadata(instanceMetadataProvider); + + parseLocationMetadata(metadataLocalProperties, instanceMetadataProvider); + merge(); - parseLocationMetadata(); LOGGER.info("[SCT] Loaded static metadata info. {}", this); } @@ -125,26 +138,89 @@ public class StaticMetadataManager { configMetadata = Collections.unmodifiableMap(allMetadata); } + private void parseCustomMetadata(InstanceMetadataProvider instanceMetadataProvider) { + if (instanceMetadataProvider == null) { + customSPIMetadata = Collections.emptyMap(); + customSPITransitiveMetadata = Collections.emptyMap(); + return; + } + + // resolve all metadata + Map allMetadata = instanceMetadataProvider.getMetadata(); + if (allMetadata == null) { + customSPIMetadata = Collections.emptyMap(); + } + else { + customSPIMetadata = Collections.unmodifiableMap(allMetadata); + } + + // resolve transitive metadata + Set transitiveKeys = instanceMetadataProvider.getTransitiveMetadataKeys(); + Map transitiveMetadata = new HashMap<>(); + if (!CollectionUtils.isEmpty(transitiveKeys)) { + for (String key : transitiveKeys) { + if (customSPIMetadata.containsKey(key)) { + transitiveMetadata.put(key, customSPIMetadata.get(key)); + } + } + } + customSPITransitiveMetadata = Collections.unmodifiableMap(transitiveMetadata); + } + private void merge() { - // env priority is bigger than config + // the priority is : custom > env > config Map mergedMetadataResult = new HashMap<>(); mergedMetadataResult.putAll(configMetadata); mergedMetadataResult.putAll(envMetadata); + mergedMetadataResult.putAll(customSPIMetadata); + // set location info as metadata + mergedMetadataResult.putAll(getLocationMetadata()); this.mergedStaticMetadata = Collections.unmodifiableMap(mergedMetadataResult); Map mergedTransitiveMetadataResult = new HashMap<>(); mergedTransitiveMetadataResult.putAll(configTransitiveMetadata); mergedTransitiveMetadataResult.putAll(envTransitiveMetadata); + mergedTransitiveMetadataResult.putAll(customSPITransitiveMetadata); this.mergedStaticTransitiveMetadata = Collections.unmodifiableMap(mergedTransitiveMetadataResult); } - private void parseLocationMetadata() { - zone = System.getenv(ENV_METADATA_ZONE); - region = System.getenv(ENV_METADATA_REGION); - campus = System.getenv(ENV_METADATA_CAMPUS); + private void parseLocationMetadata(MetadataLocalProperties metadataLocalProperties, + InstanceMetadataProvider instanceMetadataProvider) { + // resolve zone info + if (instanceMetadataProvider != null) { + zone = instanceMetadataProvider.getZone(); + } + if (StringUtils.isBlank(zone)) { + zone = System.getenv(ENV_METADATA_ZONE); + } + if (StringUtils.isBlank(zone)) { + zone = metadataLocalProperties.getContent().get(LOCATION_KEY_ZONE); + } + + // resolve region info + if (instanceMetadataProvider != null) { + region = instanceMetadataProvider.getRegion(); + } + if (StringUtils.isBlank(region)) { + region = System.getenv(ENV_METADATA_REGION); + } + if (StringUtils.isBlank(region)) { + region = metadataLocalProperties.getContent().get(LOCATION_KEY_REGION); + } + + // resolve campus info + if (instanceMetadataProvider != null) { + campus = instanceMetadataProvider.getCampus(); + } + if (StringUtils.isBlank(campus)) { + campus = System.getenv(ENV_METADATA_CAMPUS); + } + if (StringUtils.isBlank(campus)) { + campus = metadataLocalProperties.getContent().get(LOCATION_KEY_CAMPUS); + } } public Map getAllEnvMetadata() { @@ -163,6 +239,14 @@ public class StaticMetadataManager { return configTransitiveMetadata; } + public Map getAllCustomMetadata() { + return customSPIMetadata; + } + + public Map getCustomSPITransitiveMetadata() { + return customSPITransitiveMetadata; + } + public Map getMergedStaticMetadata() { return mergedStaticMetadata; } @@ -204,6 +288,8 @@ public class StaticMetadataManager { ", envTransitiveMetadata=" + envTransitiveMetadata + ", configMetadata=" + configMetadata + ", configTransitiveMetadata=" + configTransitiveMetadata + + ", customSPIMetadata=" + customSPIMetadata + + ", customSPITransitiveMetadata=" + customSPITransitiveMetadata + ", mergedStaticMetadata=" + mergedStaticMetadata + ", mergedStaticTransitiveMetadata=" + mergedStaticTransitiveMetadata + ", zone='" + zone + '\'' + diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfiguration.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfiguration.java index 5bcc0fd34..0271297ea 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfiguration.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/config/MetadataAutoConfiguration.java @@ -20,11 +20,13 @@ package com.tencent.cloud.common.metadata.config; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.common.metadata.filter.gateway.MetadataFirstScgFilter; +import com.tencent.cloud.common.spi.InstanceMetadataProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.lang.Nullable; /** * Metadata auto configuration. @@ -44,8 +46,9 @@ public class MetadataAutoConfiguration { } @Bean - public StaticMetadataManager metadataManager(MetadataLocalProperties metadataLocalProperties) { - return new StaticMetadataManager(metadataLocalProperties); + public StaticMetadataManager metadataManager(MetadataLocalProperties metadataLocalProperties, + @Nullable InstanceMetadataProvider instanceMetadataProvider) { + return new StaticMetadataManager(metadataLocalProperties, instanceMetadataProvider); } /** diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/spi/InstanceMetadataProvider.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/spi/InstanceMetadataProvider.java similarity index 73% rename from spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/spi/InstanceMetadataProvider.java rename to spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/spi/InstanceMetadataProvider.java index 6b2b73a6b..dd5f03e16 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/spi/InstanceMetadataProvider.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/spi/InstanceMetadataProvider.java @@ -16,9 +16,11 @@ * */ -package com.tencent.cloud.polaris.context.spi; +package com.tencent.cloud.common.spi; +import java.util.Collections; import java.util.Map; +import java.util.Set; /** * @@ -30,26 +32,41 @@ public interface InstanceMetadataProvider { /** * @return the metadata of instance. */ - Map getMetadata(); + default Map getMetadata() { + return Collections.emptyMap(); + } + + /** + * @return the keys of transitive metadata. + */ + default Set getTransitiveMetadataKeys() { + return Collections.emptySet(); + } /** * The region of current instance. * * @return the region info. */ - String getRegion(); + default String getRegion() { + return ""; + } /** * The zone of current instance. * * @return the zone info. */ - String getZone(); + default String getZone() { + return ""; + } /** * The campus/datacenter of current instance. * * @return the campus or datacenter info. */ - String getCampus(); + default String getCampus() { + return ""; + } } diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java new file mode 100644 index 000000000..3c8ae438d --- /dev/null +++ b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java @@ -0,0 +1,186 @@ +/* + * 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.common.metadata; + +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; +import com.tencent.cloud.common.spi.InstanceMetadataProvider; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import org.springframework.util.CollectionUtils; + +import static org.mockito.Mockito.when; + + +/** + * test for {@link StaticMetadataManager} + *@author lepdou 2022-06-27 + */ +@RunWith(MockitoJUnitRunner.class) +public class StaticMetadataManagerTest { + + @Mock + private MetadataLocalProperties metadataLocalProperties; + + @Test + public void testParseConfigMetadata() { + Map content = new HashMap<>(); + content.put("k1", "v1"); + content.put("k2", "v22"); + content.put("zone", "zone1"); + content.put("region", "region1"); + + when(metadataLocalProperties.getContent()).thenReturn(content); + when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1")); + + StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, null); + + Map metadata = metadataManager.getAllConfigMetadata(); + Assert.assertEquals(4, metadata.size()); + Assert.assertEquals("v1", metadata.get("k1")); + Assert.assertEquals("v22", metadata.get("k2")); + + Map transitiveMetadata = metadataManager.getConfigTransitiveMetadata(); + Assert.assertEquals(1, transitiveMetadata.size()); + Assert.assertEquals("v1", transitiveMetadata.get("k1")); + + Assert.assertEquals("zone1", metadataManager.getZone()); + Assert.assertEquals("region1", metadataManager.getRegion()); + + Map locationInfo = metadataManager.getLocationMetadata(); + Assert.assertEquals("zone1", locationInfo.get("zone")); + Assert.assertEquals("region1", locationInfo.get("region")); + } + + @Test + public void testCustomSPIMetadata() { + Map content = new HashMap<>(); + content.put("k1", "v1"); + content.put("k2", "v2"); + + when(metadataLocalProperties.getContent()).thenReturn(content); + when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1")); + + StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, + new MockedMetadataProvider()); + + Map metadata = metadataManager.getAllCustomMetadata(); + Assert.assertEquals(3, metadata.size()); + Assert.assertEquals("v1", metadata.get("k1")); + Assert.assertEquals("v22", metadata.get("k2")); + Assert.assertEquals("v33", metadata.get("k3")); + + Map transitiveMetadata = metadataManager.getCustomSPITransitiveMetadata(); + Assert.assertEquals(1, transitiveMetadata.size()); + Assert.assertEquals("v22", metadata.get("k2")); + + Assert.assertEquals("zone2", metadataManager.getZone()); + Assert.assertEquals("region1", metadataManager.getRegion()); + + Map locationInfo = metadataManager.getLocationMetadata(); + Assert.assertEquals("zone2", locationInfo.get("zone")); + Assert.assertEquals("region1", locationInfo.get("region")); + } + + @Test + public void testMergedMetadata() { + Map content = new HashMap<>(); + content.put("k1", "v1"); + content.put("k2", "v2"); + content.put("zone", "zone1"); + content.put("region", "region1"); + content.put("campus", "campus1"); + + when(metadataLocalProperties.getContent()).thenReturn(content); + when(metadataLocalProperties.getTransitive()).thenReturn(Collections.singletonList("k1")); + + StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, + new MockedMetadataProvider()); + + Map metadata = metadataManager.getMergedStaticMetadata(); + Assert.assertEquals(6, metadata.size()); + Assert.assertEquals("v1", metadata.get("k1")); + Assert.assertEquals("v22", metadata.get("k2")); + Assert.assertEquals("v33", metadata.get("k3")); + Assert.assertEquals("zone2", metadata.get("zone")); + Assert.assertEquals("region1", metadata.get("region")); + Assert.assertEquals("campus1", metadata.get("campus")); + + Map transitiveMetadata = metadataManager.getMergedStaticTransitiveMetadata(); + Assert.assertEquals(2, transitiveMetadata.size()); + Assert.assertEquals("v1", metadata.get("k1")); + Assert.assertEquals("v22", metadata.get("k2")); + + Assert.assertEquals("zone2", metadataManager.getZone()); + Assert.assertEquals("region1", metadataManager.getRegion()); + + Assert.assertTrue(CollectionUtils.isEmpty(metadataManager.getAllEnvMetadata())); + Assert.assertTrue(CollectionUtils.isEmpty(metadataManager.getEnvTransitiveMetadata())); + + Map locationInfo = metadataManager.getLocationMetadata(); + Assert.assertEquals("zone2", locationInfo.get("zone")); + Assert.assertEquals("region1", locationInfo.get("region")); + Assert.assertEquals("campus1", locationInfo.get("campus")); + + } + + static class MockedMetadataProvider implements InstanceMetadataProvider { + + @Override + public Map getMetadata() { + Map metadata = new HashMap<>(); + metadata.put("k1", "v1"); + metadata.put("k2", "v22"); + metadata.put("k3", "v33"); + return metadata; + } + + @Override + public Set getTransitiveMetadataKeys() { + Set transitiveKeys = new HashSet<>(); + transitiveKeys.add("k2"); + return transitiveKeys; + } + + @Override + public String getRegion() { + return "region1"; + } + + @Override + public String getZone() { + return "zone2"; + } + + @Override + public String getCampus() { + return null; + } + } + +} diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/java/com/tencent/cloud/polaris/discovery/service/callee/CustomMetadata.java b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/java/com/tencent/cloud/polaris/discovery/service/callee/CustomMetadata.java index efe3f8167..9504e9758 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/java/com/tencent/cloud/polaris/discovery/service/callee/CustomMetadata.java +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/java/com/tencent/cloud/polaris/discovery/service/callee/CustomMetadata.java @@ -21,7 +21,7 @@ package com.tencent.cloud.polaris.discovery.service.callee; import java.util.HashMap; import java.util.Map; -import com.tencent.cloud.polaris.context.spi.InstanceMetadataProvider; +import com.tencent.cloud.common.spi.InstanceMetadataProvider; import org.springframework.stereotype.Component; @@ -39,18 +39,8 @@ public class CustomMetadata implements InstanceMetadataProvider { return metadata; } - @Override - public String getRegion() { - return "shanghai"; - } - @Override public String getZone() { - return null; - } - - @Override - public String getCampus() { - return null; + return "shanghai-zone-1"; } } diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml index 5461bcc10..75b4e33d9 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml @@ -11,6 +11,10 @@ spring: discovery: enabled: true register: true + tencent: + metadata: + content: + region: shanghai # consul: # port: 8500 # host: 127.0.0.1 diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml index e9ccc1f29..2d015e894 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml @@ -5,6 +5,10 @@ spring: application: name: DiscoveryCallerService cloud: + tencent: + metadata: + content: + region: shanghai polaris: address: grpc://183.47.111.80:8091 namespace: default diff --git a/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/CustomRouterLabelResolver.java b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/CustomRouterLabelResolver.java index bd289abb9..815b2baff 100644 --- a/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/CustomRouterLabelResolver.java +++ b/spring-cloud-tencent-examples/polaris-router-example/router-caller-service/src/main/java/com/tencent/cloud/polaris/router/example/CustomRouterLabelResolver.java @@ -58,6 +58,7 @@ public class CustomRouterLabelResolver implements RouterLabelResolver { return labels; } + @Override public int getOrder() { return 0; diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java index 565849774..b57cbfe91 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java @@ -22,6 +22,7 @@ import java.util.List; import com.tencent.cloud.common.constant.ContextConstant; import com.tencent.cloud.common.util.AddressUtils; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.polaris.factory.config.ConfigurationImpl; import org.apache.commons.lang.StringUtils; 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 new file mode 100644 index 000000000..014ca5ba2 --- /dev/null +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PostInitPolarisSDKContext.java @@ -0,0 +1,50 @@ +/* + * 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.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 org.apache.commons.lang.StringUtils; + +/** + * After all configurations are loaded, post-initialize SDKContext. + *@author lepdou 2022-06-28 + */ +public class PostInitPolarisSDKContext { + + public PostInitPolarisSDKContext(SDKContext sdkContext, StaticMetadataManager staticMetadataManager) { + // set instance's location info + String region = staticMetadataManager.getRegion(); + String zone = staticMetadataManager.getZone(); + String campus = staticMetadataManager.getCampus(); + + ValueContext valueContext = sdkContext.getValueContext(); + if (StringUtils.isNotBlank(region)) { + valueContext.setValue(LocationLevel.region.name(), region); + } + if (StringUtils.isNotBlank(zone)) { + valueContext.setValue(LocationLevel.zone.name(), zone); + } + if (StringUtils.isNotBlank(campus)) { + valueContext.setValue(LocationLevel.campus.name(), campus); + } + } +} diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextAutoConfiguration.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java similarity index 58% rename from spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextAutoConfiguration.java rename to spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java index 5ead163dd..eb6238253 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextAutoConfiguration.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java @@ -16,17 +16,14 @@ * */ -package com.tencent.cloud.polaris.context; +package com.tencent.cloud.polaris.context.config; -import com.tencent.cloud.common.metadata.StaticMetadataManager; -import com.tencent.cloud.common.metadata.config.MetadataAutoConfiguration; +import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; +import com.tencent.cloud.polaris.context.ModifyAddress; +import com.tencent.cloud.polaris.context.ServiceRuleManager; import com.tencent.polaris.api.exception.PolarisException; -import com.tencent.polaris.api.plugin.common.ValueContext; -import com.tencent.polaris.api.plugin.route.LocationLevel; import com.tencent.polaris.client.api.SDKContext; -import org.apache.commons.lang.StringUtils; -import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; @@ -38,32 +35,13 @@ import org.springframework.context.annotation.Bean; */ @ConditionalOnPolarisEnabled @EnableConfigurationProperties({PolarisContextProperties.class}) -@ImportAutoConfiguration(MetadataAutoConfiguration.class) public class PolarisContextAutoConfiguration { @Bean(name = "polarisContext", initMethod = "init", destroyMethod = "destroy") @ConditionalOnMissingBean - public SDKContext polarisContext(PolarisContextProperties properties, StaticMetadataManager staticMetadataManager) + public SDKContext polarisContext(PolarisContextProperties properties) throws PolarisException { - SDKContext sdkContext = SDKContext.initContextByConfig(properties.configuration()); - - // init current instance location info from environment - ValueContext valueContext = sdkContext.getValueContext(); - String region = staticMetadataManager.getRegion(); - String zone = staticMetadataManager.getZone(); - String campus = staticMetadataManager.getCampus(); - - if (StringUtils.isNotBlank(region)) { - valueContext.setValue(LocationLevel.region.name(), region); - } - if (StringUtils.isNotBlank(zone)) { - valueContext.setValue(LocationLevel.zone.name(), zone); - } - if (StringUtils.isNotBlank(campus)) { - valueContext.setValue(LocationLevel.campus.name(), campus); - } - - return sdkContext; + return SDKContext.initContextByConfig(properties.configuration()); } @Bean @@ -73,6 +51,7 @@ public class PolarisContextAutoConfiguration { } @Bean + @ConditionalOnMissingBean public ServiceRuleManager serviceRuleManager(SDKContext sdkContext) { return new ServiceRuleManager(sdkContext); } diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextBootstrapAutoConfiguration.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextBootstrapAutoConfiguration.java similarity index 95% rename from spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextBootstrapAutoConfiguration.java rename to spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextBootstrapAutoConfiguration.java index c5b4efbd9..75d11f8b5 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextBootstrapAutoConfiguration.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextBootstrapAutoConfiguration.java @@ -16,7 +16,7 @@ * */ -package com.tencent.cloud.polaris.context; +package com.tencent.cloud.polaris.context.config; import com.tencent.polaris.client.api.SDKContext; diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextPostConfiguration.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextPostConfiguration.java new file mode 100644 index 000000000..f55397462 --- /dev/null +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextPostConfiguration.java @@ -0,0 +1,39 @@ +/* + * 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.context.config; + +import com.tencent.cloud.common.metadata.StaticMetadataManager; +import com.tencent.cloud.polaris.context.PostInitPolarisSDKContext; +import com.tencent.polaris.client.api.SDKContext; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Post-initialization operations after the application initialization phase is completed. + *@author lepdou 2022-06-28 + */ +@Configuration +public class PolarisContextPostConfiguration { + + @Bean + public PostInitPolarisSDKContext postInitPolarisSDKContext(SDKContext sdkContext, StaticMetadataManager staticMetadataManager) { + return new PostInitPolarisSDKContext(sdkContext, staticMetadataManager); + } +} diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java similarity index 96% rename from spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java rename to spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java index 3df1731b5..87a076e21 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java @@ -16,13 +16,14 @@ * */ -package com.tencent.cloud.polaris.context; +package com.tencent.cloud.polaris.context.config; import java.util.Collection; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; +import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.polaris.api.config.ConfigProvider; import com.tencent.polaris.api.config.Configuration; import com.tencent.polaris.factory.ConfigAPIFactory; diff --git a/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 52c48aa8b..7bb1d6596 100644 --- a/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -2,8 +2,8 @@ "groups": [ { "name": "spring.cloud.polaris", - "type": "com.tencent.cloud.polaris.context.PolarisContextProperties", - "sourceType": "com.tencent.cloud.polaris.context.PolarisContextProperties" + "type": "com.tencent.cloud.polaris.context.config.PolarisContextProperties", + "sourceType": "com.tencent.cloud.polaris.context.config.PolarisContextProperties" } ], "properties": [ @@ -11,28 +11,28 @@ "name": "spring.cloud.polaris.address", "type": "java.lang.String", "description": "polaris server address list that can be separated by \",\"", - "sourceType": "com.tencent.cloud.polaris.context.PolarisContextProperties" + "sourceType": "com.tencent.cloud.polaris.context.config.PolarisContextProperties" }, { "name": "spring.cloud.polaris.namespace", "type": "java.lang.String", "description": "polaris namespace", "default": "default", - "sourceType": "com.tencent.cloud.polaris.context.PolarisContextProperties" + "sourceType": "com.tencent.cloud.polaris.context.config.PolarisContextProperties" }, { "name": "spring.cloud.polaris.service", "type": "java.lang.String", "description": "polaris service name", "default": "${spring.application.name}", - "sourceType": "com.tencent.cloud.polaris.context.PolarisContextProperties" + "sourceType": "com.tencent.cloud.polaris.context.config.PolarisContextProperties" }, { "name": "spring.cloud.polaris.enabled", "type": "java.lang.Boolean", "description": "polaris enabled", "default": "true", - "sourceType": "com.tencent.cloud.polaris.context.PolarisContextProperties" + "sourceType": "com.tencent.cloud.polaris.context.config.PolarisContextProperties" }, { "name": "spring.cloud.polaris.local-ip-address", diff --git a/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring.factories b/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring.factories index b53316b9a..723fc1d78 100644 --- a/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/spring.factories @@ -1,4 +1,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration + com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration,\ + com.tencent.cloud.polaris.context.config.PolarisContextPostConfiguration org.springframework.cloud.bootstrap.BootstrapConfiguration=\ - com.tencent.cloud.polaris.context.PolarisContextBootstrapAutoConfiguration + com.tencent.cloud.polaris.context.config.PolarisContextBootstrapAutoConfiguration diff --git a/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextAutoConfigurationTest.java b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextAutoConfigurationTest.java index 0f8ca746d..2a926cc12 100644 --- a/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextAutoConfigurationTest.java +++ b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextAutoConfigurationTest.java @@ -17,6 +17,7 @@ package com.tencent.cloud.polaris.context; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.polaris.client.api.SDKContext; import org.junit.Assert; import org.junit.Test; diff --git a/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextGetHostTest.java b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextGetHostTest.java index 3069c5aa9..9985113df 100644 --- a/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextGetHostTest.java +++ b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/PolarisContextGetHostTest.java @@ -17,6 +17,8 @@ package com.tencent.cloud.polaris.context; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.polaris.client.api.SDKContext; import org.junit.Assert; import org.junit.Test; diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfigurationTest.java b/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfigurationTest.java index b0e9d6a30..5ef38dff3 100644 --- a/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfigurationTest.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfigurationTest.java @@ -17,7 +17,7 @@ package com.tencent.cloud.polaris.loadbalancer.config; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.polaris.router.api.core.RouterAPI; import org.junit.Test; diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRouterAutoConfigurationTest.java b/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRouterAutoConfigurationTest.java index 2593095a8..f296346e3 100644 --- a/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRouterAutoConfigurationTest.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRouterAutoConfigurationTest.java @@ -17,7 +17,7 @@ package com.tencent.cloud.polaris.loadbalancer.config; -import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; +import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; import com.tencent.cloud.polaris.loadbalancer.PolarisServiceInstanceListSupplier; import com.tencent.polaris.router.api.core.RouterAPI; import org.junit.Test;