feature:support spring cloud configData

pull/451/head
wulingxiao 3 years ago
parent d2717186cb
commit 7ad4e8c577

@ -66,7 +66,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
// load spring boot default config files
String[] activeProfiles = environment.getActiveProfiles();
String serviceName = environment.getProperty("spring.application.name");
polarisConfigFilePuller.initInternalConfigFiles(compositePropertySource,activeProfiles,serviceName);
polarisConfigFilePuller.initInternalConfigFiles(compositePropertySource, activeProfiles, serviceName);
// load custom config files
List<ConfigFileGroup> configFileGroups = polarisConfigProperties.getGroups();

@ -38,7 +38,6 @@ import org.springframework.boot.context.config.ConfigDataResourceNotFoundExcepti
import org.springframework.boot.context.config.Profiles;
import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
@ -65,9 +64,9 @@ public class PolarisConfigDataLoader implements ConfigDataLoader<PolarisConfigDa
private PolarisConfigFilePuller puller;
private static final AtomicBoolean INTERNAL_CONFIG_FILES_LOADED = new AtomicBoolean(false);
static final AtomicBoolean INTERNAL_CONFIG_FILES_LOADED = new AtomicBoolean(false);
private static final AtomicBoolean CUSTOM_POLARIS_CONFIG_FILE_LOADED = new AtomicBoolean(false);
static final AtomicBoolean CUSTOM_POLARIS_CONFIG_FILE_LOADED = new AtomicBoolean(false);
public PolarisConfigDataLoader(DeferredLogFactory logFactory) {
this.log = logFactory.getLog(getClass());

@ -18,11 +18,8 @@
package com.tencent.cloud.polaris.config.configdata;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import com.tencent.cloud.polaris.config.ConfigurationModifier;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
@ -30,12 +27,8 @@ import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.context.ModifyAddress;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.polaris.api.config.ConfigProvider;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.factory.ConfigAPIFactory;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import org.apache.commons.logging.Log;
import org.springframework.boot.BootstrapRegistry;
@ -167,7 +160,7 @@ public class PolarisConfigDataLocationResolver implements
BindHandler bindHandler = getBindHandler(context);
T instance;
if (context.getBootstrapContext().isRegistered(typeClass)) {
if (!registerNotNecessary(typeClass) && context.getBootstrapContext().isRegistered(typeClass)) {
instance = context.getBootstrapContext().get(typeClass);
}
else {
@ -271,5 +264,13 @@ public class PolarisConfigDataLocationResolver implements
modifierList.add(configurationModifier);
return modifierList;
}
private boolean registerNotNecessary(Class<?> typeClass) {
return typeClass.isPrimitive() ||
Number.class.isAssignableFrom(typeClass) ||
String.class.isAssignableFrom(typeClass) ||
Character.class.isAssignableFrom(typeClass) ||
Boolean.class.isAssignableFrom(typeClass);
}
}

@ -7,6 +7,9 @@ import org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcess
/**
* Class for most {@code FailureAnalyzer} implementations, to analyze ImportException when
* miss Polaris configData config.
* <p>Refer to the Nacos project implementation
* <code><a href=https://github.com/alibaba/spring-cloud-alibaba/blob/2021.x/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/configdata/NacosConfigDataMissingEnvironmentPostProcessor.java>
* ImportExceptionFailureAnalyzer</a></code>
*
* @author wlx
* @see AbstractFailureAnalyzer

@ -8,7 +8,6 @@ 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.config.PolarisContextProperties;
import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
@ -18,7 +17,6 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.context.config.Profiles;
import org.springframework.core.env.CompositePropertySource;
import static org.mockito.Mockito.when;

@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.google.common.collect.Lists;
import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile;
@ -19,7 +18,6 @@ import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.MockitoJUnitRunner;
@ -31,7 +29,10 @@ import org.springframework.boot.logging.DeferredLogs;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.PropertySource;
import static com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLoader.CUSTOM_POLARIS_CONFIG_FILE_LOADED;
import static com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLoader.INTERNAL_CONFIG_FILES_LOADED;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
@ -44,29 +45,22 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class PolarisConfigDataLoaderTest {
@Mock
private ConfigDataLoaderContext context;
@Mock
private PolarisConfigDataResource polarisConfigDataResource;
@Mock
private ConfigurableBootstrapContext bootstrapContext;
@Mock
private PolarisConfigProperties polarisConfigProperties;
@Mock
private PolarisContextProperties polarisContextProperties;
@Mock
private ConfigFileService configFileService;
@Mock
private Profiles profiles;
private static SDKContext sdkContext;
private static final SDKContext sdkContext = SDKContext.initContext();
private final String testNamespace = "testNamespace";
private final String testServiceName = "testServiceName";
private final String polarisConfigPropertySourceName = "polaris-config";
@Test
public void loadConfigDataInternalConfigFilesTest() {
try (MockedStatic<ConfigFileServiceFactory> mockedStatic = mockStatic(ConfigFileServiceFactory.class)) {
ConfigDataLoaderContext context = mock(ConfigDataLoaderContext.class);
PolarisConfigDataResource polarisConfigDataResource = mock(PolarisConfigDataResource.class);
ConfigurableBootstrapContext bootstrapContext = mock(ConfigurableBootstrapContext.class);
PolarisConfigProperties polarisConfigProperties = mock(PolarisConfigProperties.class);
PolarisContextProperties polarisContextProperties = mock(PolarisContextProperties.class);
ConfigFileService configFileService = mock(ConfigFileService.class);
Profiles profiles = mock(Profiles.class);
Map<String, Object> emptyMap = new HashMap<>();
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
@ -79,13 +73,9 @@ public class PolarisConfigDataLoaderTest {
ConfigKVFile propertiesFile = new MockedConfigKVFile(applicationProperties);
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties"))
.thenReturn(propertiesFile);
mockSDKContext();
when(context.getBootstrapContext()).thenReturn(bootstrapContext);
when(bootstrapContext.isRegistered(eq(SDKContext.class))).thenReturn(false);
when(bootstrapContext.get(eq(SDKContext.class))).thenReturn(sdkContext);
when(bootstrapContext.isRegistered(eq(PolarisPropertySourceManager.class))).thenReturn(false);
when(bootstrapContext.get(eq(PolarisPropertySourceManager.class))).thenReturn(new PolarisPropertySourceManager());
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
@ -95,6 +85,12 @@ public class PolarisConfigDataLoaderTest {
when(profiles.getActive()).thenReturn(Lists.newArrayList());
PolarisConfigDataLoader polarisConfigDataLoader = new PolarisConfigDataLoader(new DeferredLogs());
if (INTERNAL_CONFIG_FILES_LOADED.get()) {
INTERNAL_CONFIG_FILES_LOADED.compareAndSet(true, false);
}
if (CUSTOM_POLARIS_CONFIG_FILE_LOADED.get()) {
CUSTOM_POLARIS_CONFIG_FILE_LOADED.compareAndSet(true, false);
}
when(polarisConfigDataResource.getPolarisConfigProperties()).thenReturn(polarisConfigProperties);
when(polarisConfigDataResource.getPolarisContextProperties()).thenReturn(polarisContextProperties);
when(polarisConfigDataResource.getServiceName()).thenReturn(testServiceName);
@ -102,25 +98,28 @@ public class PolarisConfigDataLoaderTest {
mockedStatic.when(() -> {
ConfigFileServiceFactory.createConfigFileService(sdkContext);
}).thenReturn(this.configFileService);
}).thenReturn(configFileService);
ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource);
List<PropertySource<?>> propertySources = configData.getPropertySources();
Optional<PropertySource<?>> propertySource = propertySources.stream().findFirst();
if (propertySource.isPresent()) {
PropertySource<?> source = propertySource.get();
Assert.assertTrue(source instanceof CompositePropertySource);
CompositePropertySource compositePropertySource = (CompositePropertySource) source;
Assert.assertEquals("v1", compositePropertySource.getProperty("k1"));
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
}
CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName);
propertySources.forEach(compositePropertySource::addPropertySource);
Assert.assertEquals("v1", compositePropertySource.getProperty("k1"));
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
}
}
@Test
public void loadConfigDataInternalConfigFilesTestWithProfile() {
try (MockedStatic<ConfigFileServiceFactory> mockedStatic = mockStatic(ConfigFileServiceFactory.class)) {
ConfigDataLoaderContext context = mock(ConfigDataLoaderContext.class);
PolarisConfigDataResource polarisConfigDataResource = mock(PolarisConfigDataResource.class);
ConfigurableBootstrapContext bootstrapContext = mock(ConfigurableBootstrapContext.class);
PolarisConfigProperties polarisConfigProperties = mock(PolarisConfigProperties.class);
PolarisContextProperties polarisContextProperties = mock(PolarisContextProperties.class);
ConfigFileService configFileService = mock(ConfigFileService.class);
Profiles profiles = mock(Profiles.class);
Map<String, Object> applicationProperties = new HashMap<>();
applicationProperties.put("k1", "v1");
applicationProperties.put("k2", "v2");
@ -151,12 +150,8 @@ public class PolarisConfigDataLoaderTest {
active.add("dev");
when(profiles.getActive()).thenReturn(active);
mockSDKContext();
when(context.getBootstrapContext()).thenReturn(bootstrapContext);
when(bootstrapContext.isRegistered(eq(SDKContext.class))).thenReturn(false);
when(bootstrapContext.get(eq(SDKContext.class))).thenReturn(sdkContext);
when(bootstrapContext.isRegistered(eq(PolarisPropertySourceManager.class))).thenReturn(false);
when(bootstrapContext.get(eq(PolarisPropertySourceManager.class))).thenReturn(new PolarisPropertySourceManager());
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
@ -165,33 +160,44 @@ public class PolarisConfigDataLoaderTest {
when(polarisConfigProperties.getGroups()).thenReturn(null);
PolarisConfigDataLoader polarisConfigDataLoader = new PolarisConfigDataLoader(new DeferredLogs());
if (INTERNAL_CONFIG_FILES_LOADED.get()) {
INTERNAL_CONFIG_FILES_LOADED.compareAndSet(true, false);
}
if (CUSTOM_POLARIS_CONFIG_FILE_LOADED.get()) {
CUSTOM_POLARIS_CONFIG_FILE_LOADED.compareAndSet(true, false);
}
when(polarisConfigDataResource.getPolarisConfigProperties()).thenReturn(polarisConfigProperties);
when(polarisConfigDataResource.getPolarisContextProperties()).thenReturn(polarisContextProperties);
when(polarisConfigDataResource.getServiceName()).thenReturn(testServiceName);
when(polarisConfigDataResource.getProfiles()).thenReturn(profiles);
mockedStatic.when(() -> {
ConfigFileServiceFactory.createConfigFileService(sdkContext);
}).thenReturn(this.configFileService);
}).thenReturn(configFileService);
ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource);
List<PropertySource<?>> propertySources = configData.getPropertySources();
Optional<PropertySource<?>> propertySource = propertySources.stream().findFirst();
if (propertySource.isPresent()) {
PropertySource<?> source = propertySource.get();
Assert.assertTrue(source instanceof CompositePropertySource);
CompositePropertySource compositePropertySource = (CompositePropertySource) source;
Assert.assertEquals("v11", compositePropertySource.getProperty("k1"));
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
}
CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName);
propertySources.forEach(compositePropertySource::addPropertySource);
Assert.assertEquals("v11", compositePropertySource.getProperty("k1"));
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
}
}
@Test
public void loadConfigDataCustomConfigFilesTestWithProfile() {
try (MockedStatic<ConfigFileServiceFactory> mockedStatic = mockStatic(ConfigFileServiceFactory.class)) {
ConfigDataLoaderContext context = mock(ConfigDataLoaderContext.class);
PolarisConfigDataResource polarisConfigDataResource = mock(PolarisConfigDataResource.class);
ConfigurableBootstrapContext bootstrapContext = mock(ConfigurableBootstrapContext.class);
PolarisConfigProperties polarisConfigProperties = mock(PolarisConfigProperties.class);
PolarisContextProperties polarisContextProperties = mock(PolarisContextProperties.class);
ConfigFileService configFileService = mock(ConfigFileService.class);
Profiles profiles = mock(Profiles.class);
Map<String, Object> emptyMap = new HashMap<>();
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
@ -216,13 +222,9 @@ public class PolarisConfigDataLoaderTest {
ConfigKVFile file1 = new MockedConfigKVFile(file1Map);
when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile1)).thenReturn(file1);
mockSDKContext();
when(context.getBootstrapContext()).thenReturn(bootstrapContext);
when(bootstrapContext.isRegistered(eq(SDKContext.class))).thenReturn(false);
when(bootstrapContext.get(eq(SDKContext.class))).thenReturn(sdkContext);
when(bootstrapContext.isRegistered(eq(PolarisPropertySourceManager.class))).thenReturn(false);
when(bootstrapContext.get(eq(PolarisPropertySourceManager.class))).thenReturn(new PolarisPropertySourceManager());
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
@ -232,6 +234,13 @@ public class PolarisConfigDataLoaderTest {
when(profiles.getActive()).thenReturn(Lists.newArrayList());
PolarisConfigDataLoader polarisConfigDataLoader = new PolarisConfigDataLoader(new DeferredLogs());
if (INTERNAL_CONFIG_FILES_LOADED.get()) {
INTERNAL_CONFIG_FILES_LOADED.compareAndSet(true, false);
}
if (CUSTOM_POLARIS_CONFIG_FILE_LOADED.get()) {
CUSTOM_POLARIS_CONFIG_FILE_LOADED.compareAndSet(true, false);
}
when(polarisConfigDataResource.getPolarisConfigProperties()).thenReturn(polarisConfigProperties);
when(polarisConfigDataResource.getPolarisContextProperties()).thenReturn(polarisContextProperties);
when(polarisConfigDataResource.getServiceName()).thenReturn(testServiceName);
@ -239,19 +248,17 @@ public class PolarisConfigDataLoaderTest {
mockedStatic.when(() -> {
ConfigFileServiceFactory.createConfigFileService(sdkContext);
}).thenReturn(this.configFileService);
}).thenReturn(configFileService);
ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource);
List<PropertySource<?>> propertySources = configData.getPropertySources();
Optional<PropertySource<?>> propertySource = propertySources.stream().findFirst();
if (propertySource.isPresent()) {
PropertySource<?> source = propertySource.get();
Assert.assertTrue(source instanceof CompositePropertySource);
CompositePropertySource compositePropertySource = (CompositePropertySource) source;
Assert.assertEquals("v1", compositePropertySource.getProperty("k1"));
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
}
CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName);
propertySources.forEach(compositePropertySource::addPropertySource);
Assert.assertEquals("v1", compositePropertySource.getProperty("k1"));
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
}
}
@ -261,10 +268,4 @@ public class PolarisConfigDataLoaderTest {
sdkContext.destroy();
}
}
private void mockSDKContext() {
if (sdkContext == null) {
sdkContext = SDKContext.initContext();
}
}
}

@ -1,10 +1,63 @@
package com.tencent.cloud.polaris.config.configdata;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataLocationResolverContext;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.logging.DeferredLogs;
import org.springframework.mock.env.MockEnvironment;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
/**
* Test for {@link PolarisConfigDataLocationResolver}.
*
* @author wlx
* @date 2022/7/16 4:10
*/
@RunWith(MockitoJUnitRunner.class)
public class PolarisConfigDataLocationResolverTest {
private final PolarisConfigDataLocationResolver resolver = new PolarisConfigDataLocationResolver(new DeferredLogs());
@Mock
private ConfigDataLocationResolverContext context;
private final MockEnvironment environment = new MockEnvironment();
private final Binder environmentBinder = Binder.get(this.environment);
@Mock
private ConfigurableBootstrapContext bootstrapContext;
@Test
public void testIsResolvable() {
when(context.getBinder()).thenReturn(environmentBinder);
when(context.getBootstrapContext()).thenReturn(bootstrapContext);
assertThat(
this.resolver.isResolvable(this.context, ConfigDataLocation.of("configserver:")))
.isFalse();
assertThat(
this.resolver.isResolvable(this.context, ConfigDataLocation.of("polaris:")))
.isTrue();
assertThat(
this.resolver.isResolvable(this.context, ConfigDataLocation.of("polaris")))
.isTrue();
}
@Test
public void unEnabledPolarisConfigData() {
environment.setProperty("spring.cloud.polaris.config.enabled", "false");
when(context.getBinder()).thenReturn(environmentBinder);
when(context.getBootstrapContext()).thenReturn(bootstrapContext);
assertThat(
this.resolver.isResolvable(this.context, ConfigDataLocation.of("polaris:")))
.isFalse();
}
}

@ -15,6 +15,9 @@ spring:
config:
import:
- optional:polaris
- optional:polaris:test.yml
- optional:polaris:configdataexample:test.yml
- optional:polaris:config/bootstrap.yml
management:
endpoints:
web:

Loading…
Cancel
Save