optimize static metadata manager (#346)

pull/349/head
lepdou 2 years ago committed by GitHub
parent 72495e7264
commit 7b77cf20b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,3 +17,4 @@
- [Feature: add spring cloud tencent logo](https://github.com/Tencent/spring-cloud-tencent/pull/332)
- [UT: add metadata-transfer unit test](https://github.com/Tencent/spring-cloud-tencent/pull/335)
- [feat:Add GitHub action of codecov.yml and update junit of metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/344)
- [Feature: optimize static metadata manager](https://github.com/Tencent/spring-cloud-tencent/pull/346)

@ -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;

@ -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;

@ -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;

@ -21,7 +21,7 @@ package com.tencent.cloud.polaris.circuitbreaker;
import com.tencent.cloud.polaris.circuitbreaker.config.PolarisRestTemplateAutoConfiguration;
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 org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;

@ -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;

@ -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;

@ -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;

@ -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;

@ -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<String, String> 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;

@ -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

@ -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;

@ -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;

@ -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;

@ -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;

@ -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;

@ -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;

@ -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

@ -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;

@ -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;

@ -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;

@ -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;

@ -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<String, String> resolve(RequestTemplate requestTemplate);
default Map<String, String> 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<String, String> resolve(HttpRequest request, byte[] body);
default Map<String, String> resolve(HttpRequest request, byte[] body) {
return Collections.emptyMap();
}
}

@ -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<String, String> envTransitiveMetadata;
private Map<String, String> configMetadata;
private Map<String, String> configTransitiveMetadata;
private Map<String, String> customSPIMetadata;
private Map<String, String> customSPITransitiveMetadata;
private Map<String, String> mergedStaticMetadata;
private Map<String, String> 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,27 +138,90 @@ 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<String, String> allMetadata = instanceMetadataProvider.getMetadata();
if (allMetadata == null) {
customSPIMetadata = Collections.emptyMap();
}
else {
customSPIMetadata = Collections.unmodifiableMap(allMetadata);
}
// resolve transitive metadata
Set<String> transitiveKeys = instanceMetadataProvider.getTransitiveMetadataKeys();
Map<String, String> 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<String, String> 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<String, String> mergedTransitiveMetadataResult = new HashMap<>();
mergedTransitiveMetadataResult.putAll(configTransitiveMetadata);
mergedTransitiveMetadataResult.putAll(envTransitiveMetadata);
mergedTransitiveMetadataResult.putAll(customSPITransitiveMetadata);
this.mergedStaticTransitiveMetadata = Collections.unmodifiableMap(mergedTransitiveMetadataResult);
}
private void parseLocationMetadata() {
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<String, String> getAllEnvMetadata() {
return envMetadata;
@ -163,6 +239,14 @@ public class StaticMetadataManager {
return configTransitiveMetadata;
}
public Map<String, String> getAllCustomMetadata() {
return customSPIMetadata;
}
public Map<String, String> getCustomSPITransitiveMetadata() {
return customSPITransitiveMetadata;
}
public Map<String, String> 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 + '\'' +

@ -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);
}
/**

@ -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<String, String> getMetadata();
default Map<String, String> getMetadata() {
return Collections.emptyMap();
}
/**
* @return the keys of transitive metadata.
*/
default Set<String> 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 "";
}
}

@ -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<String, String> 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<String, String> metadata = metadataManager.getAllConfigMetadata();
Assert.assertEquals(4, metadata.size());
Assert.assertEquals("v1", metadata.get("k1"));
Assert.assertEquals("v22", metadata.get("k2"));
Map<String, String> 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<String, String> locationInfo = metadataManager.getLocationMetadata();
Assert.assertEquals("zone1", locationInfo.get("zone"));
Assert.assertEquals("region1", locationInfo.get("region"));
}
@Test
public void testCustomSPIMetadata() {
Map<String, String> 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<String, String> 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<String, String> 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<String, String> locationInfo = metadataManager.getLocationMetadata();
Assert.assertEquals("zone2", locationInfo.get("zone"));
Assert.assertEquals("region1", locationInfo.get("region"));
}
@Test
public void testMergedMetadata() {
Map<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> getMetadata() {
Map<String, String> metadata = new HashMap<>();
metadata.put("k1", "v1");
metadata.put("k2", "v22");
metadata.put("k3", "v33");
return metadata;
}
@Override
public Set<String> getTransitiveMetadataKeys() {
Set<String> 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;
}
}
}

@ -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";
}
}

@ -11,6 +11,10 @@ spring:
discovery:
enabled: true
register: true
tencent:
metadata:
content:
region: shanghai
# consul:
# port: 8500
# host: 127.0.0.1

@ -5,6 +5,10 @@ spring:
application:
name: DiscoveryCallerService
cloud:
tencent:
metadata:
content:
region: shanghai
polaris:
address: grpc://183.47.111.80:8091
namespace: default

@ -0,0 +1,35 @@
/*
* 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.example;
import com.tencent.cloud.common.spi.InstanceMetadataProvider;
import org.springframework.stereotype.Component;
/**
*@author lepdou 2022-06-29
*/
@Component
public class CustomMetadataProvider implements InstanceMetadataProvider {
@Override
public String getRegion() {
return "huadong";
}
}

@ -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;

@ -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);
}
}
}

@ -16,19 +16,17 @@
*
*/
package com.tencent.cloud.polaris.context;
package com.tencent.cloud.polaris.context.config;
import java.util.List;
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.PolarisConfigModifier;
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;
@ -41,33 +39,14 @@ import org.springframework.core.env.Environment;
*/
@ConditionalOnPolarisEnabled
@EnableConfigurationProperties({PolarisContextProperties.class})
@ImportAutoConfiguration(MetadataAutoConfiguration.class)
public class PolarisContextAutoConfiguration {
@Bean(name = "polarisContext", initMethod = "init", destroyMethod = "destroy")
@ConditionalOnMissingBean
public SDKContext polarisContext(PolarisContextProperties properties, Environment environment,
List<PolarisConfigModifier> modifierList, StaticMetadataManager staticMetadataManager)
List<PolarisConfigModifier> modifierList)
throws PolarisException {
SDKContext sdkContext = SDKContext.initContextByConfig(properties.configuration(environment, modifierList));
// 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(environment, modifierList));
}
@Bean

@ -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);
}
}

@ -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;
@ -66,7 +67,7 @@ public class PolarisContextProperties {
*/
private String service;
protected Configuration configuration(Environment environment, List<PolarisConfigModifier> modifierList) {
public Configuration configuration(Environment environment, List<PolarisConfigModifier> modifierList) {
// 1. Read user-defined polaris.yml configuration
ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory
.defaultConfig(ConfigProvider.DEFAULT_CONFIG);

@ -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",

@ -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

@ -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;

@ -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;

@ -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;

@ -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;

Loading…
Cancel
Save