feature:support spring cloud configData

pull/451/head
wulingxiao 3 years ago
parent 60bb8a7c2c
commit d2717186cb

@ -47,12 +47,15 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
private final PolarisConfigFilePuller polarisConfigFilePuller; private final PolarisConfigFilePuller polarisConfigFilePuller;
private final Environment environment;
public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties, public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties,
PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, PolarisContextProperties polarisContextProperties, ConfigFileService configFileService,
PolarisPropertySourceManager polarisPropertySourceManager, Environment environment) { PolarisPropertySourceManager polarisPropertySourceManager, Environment environment) {
this.polarisConfigProperties = polarisConfigProperties; this.polarisConfigProperties = polarisConfigProperties;
this.environment = environment;
this.polarisConfigFilePuller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, this.polarisConfigFilePuller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService,
polarisPropertySourceManager, environment); polarisPropertySourceManager);
} }
@Override @Override
@ -61,7 +64,9 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
POLARIS_CONFIG_PROPERTY_SOURCE_NAME); POLARIS_CONFIG_PROPERTY_SOURCE_NAME);
// load spring boot default config files // load spring boot default config files
polarisConfigFilePuller.initInternalConfigFiles(compositePropertySource); String[] activeProfiles = environment.getActiveProfiles();
String serviceName = environment.getProperty("spring.application.name");
polarisConfigFilePuller.initInternalConfigFiles(compositePropertySource,activeProfiles,serviceName);
// load custom config files // load custom config files
List<ConfigFileGroup> configFileGroups = polarisConfigProperties.getGroups(); List<ConfigFileGroup> configFileGroups = polarisConfigProperties.getGroups();

@ -90,9 +90,7 @@ public class PolarisConfigDataLoader implements ConfigDataLoader<PolarisConfigDa
public ConfigData load(ConfigurableBootstrapContext bootstrapContext, PolarisConfigDataResource resource) { public ConfigData load(ConfigurableBootstrapContext bootstrapContext, PolarisConfigDataResource resource) {
CompositePropertySource compositePropertySource = locate(bootstrapContext, resource); CompositePropertySource compositePropertySource = locate(bootstrapContext, resource);
List<PropertySource<?>> propertySources = new ArrayList<>(); return new ConfigData(compositePropertySource.getPropertySources(), getOptions(resource));
propertySources.add(compositePropertySource);
return new ConfigData(propertySources, getOptions(resource));
} }
private CompositePropertySource locate(ConfigurableBootstrapContext bootstrapContext, private CompositePropertySource locate(ConfigurableBootstrapContext bootstrapContext,
@ -110,7 +108,9 @@ public class PolarisConfigDataLoader implements ConfigDataLoader<PolarisConfigDa
Profiles profiles = resource.getProfiles(); Profiles profiles = resource.getProfiles();
if (INTERNAL_CONFIG_FILES_LOADED.compareAndSet(false, true)) { if (INTERNAL_CONFIG_FILES_LOADED.compareAndSet(false, true)) {
log.info("loading internal config files"); log.info("loading internal config files");
this.puller.initInternalConfigFiles(compositePropertySource, profiles, resource.getServiceName()); List<String> profilesActive = profiles.getActive();
String[] activeProfiles = profilesActive.toArray(new String[]{});
this.puller.initInternalConfigFiles(compositePropertySource, activeProfiles, resource.getServiceName());
} }
PolarisConfigProperties polarisConfigProperties = resource.getPolarisConfigProperties(); PolarisConfigProperties polarisConfigProperties = resource.getPolarisConfigProperties();

@ -252,30 +252,10 @@ public class PolarisConfigDataLocationResolver implements
private SDKContext sdkContext(ConfigDataLocationResolverContext resolverContext, private SDKContext sdkContext(ConfigDataLocationResolverContext resolverContext,
PolarisConfigProperties polarisConfigProperties, PolarisConfigProperties polarisConfigProperties,
PolarisContextProperties polarisContextProperties) { PolarisContextProperties polarisContextProperties) {
List<PolarisConfigModifier> modifierList = modifierList(polarisConfigProperties, polarisContextProperties);
// 1. Read user-defined polaris.yml configuration return SDKContext.initContextByConfig(polarisContextProperties.configuration(modifierList, () -> {
ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory return loadPolarisConfigProperties(resolverContext, String.class, "spring.cloud.client.ip-address");
.defaultConfig(ConfigProvider.DEFAULT_CONFIG); }));
// 2. Override user-defined polaris.yml configuration with SCT configuration
String defaultHost = polarisContextProperties.getLocalIpAddress();
if (StringUtils.isBlank(defaultHost)) {
defaultHost = loadPolarisConfigProperties(resolverContext, String.class, "spring.cloud.client.ip-address");
}
configuration.getGlobal().getAPI().setBindIP(defaultHost);
Collection<PolarisConfigModifier> modifiers = modifierList(polarisConfigProperties, polarisContextProperties);
modifiers = modifiers.stream()
.sorted(Comparator.comparingInt(PolarisConfigModifier::getOrder))
.collect(Collectors.toList());
if (!CollectionUtils.isEmpty(modifiers)) {
for (PolarisConfigModifier modifier : modifiers) {
modifier.modify(configuration);
}
}
return SDKContext.initContextByConfig(configuration);
} }
private List<PolarisConfigModifier> modifierList(PolarisConfigProperties polarisConfigProperties, private List<PolarisConfigModifier> modifierList(PolarisConfigProperties polarisConfigProperties,

@ -20,7 +20,6 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.context.config.Profiles; import org.springframework.boot.context.config.Profiles;
import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.Environment;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -32,153 +31,19 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class PolarisConfigFilePullerTest { public class PolarisConfigFilePullerTest {
@Mock
private PolarisConfigProperties polarisConfigProperties;
@Mock @Mock
private PolarisContextProperties polarisContextProperties; private PolarisContextProperties polarisContextProperties;
@Mock @Mock
private ConfigFileService configFileService; private ConfigFileService configFileService;
@Mock @Mock
private PolarisPropertySourceManager polarisPropertySourceManager; private PolarisPropertySourceManager polarisPropertySourceManager;
@Mock
private Environment environment;
@Mock
private Profiles profiles;
private final String testNamespace = "testNamespace"; private final String testNamespace = "testNamespace";
private final String testServiceName = "testServiceName"; private final String testServiceName = "testServiceName";
private final String polarisConfigPropertySourceName = "polaris-config"; private final String polarisConfigPropertySourceName = "polaris-config";
@Test @Test
public void testBootstrapPullInternalConfigFiles() { public void testPullInternalConfigFiles() {
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService,
polarisPropertySourceManager, environment);
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName);
// application.properties
Map<String, Object> applicationProperties = new HashMap<>();
applicationProperties.put("k1", "v1");
applicationProperties.put("k2", "v2");
applicationProperties.put("k3", "v3");
ConfigKVFile propertiesFile = new MockedConfigKVFile(applicationProperties);
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties"))
.thenReturn(propertiesFile);
Map<String, Object> emptyMap = new HashMap<>();
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
when(polarisConfigProperties.getGroups()).thenReturn(null);
when(environment.getActiveProfiles()).thenReturn(new String[]{});
CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName);
puller.initInternalConfigFiles(compositePropertySource);
Assert.assertEquals("v1", compositePropertySource.getProperty("k1"));
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
}
@Test
public void testBootstrapPullInternalConfigFilesWithProfile() {
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService,
polarisPropertySourceManager, environment);
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName);
// application.properties
Map<String, Object> applicationProperties = new HashMap<>();
applicationProperties.put("k1", "v1");
applicationProperties.put("k2", "v2");
applicationProperties.put("k3", "v3");
ConfigKVFile propertiesFile = new MockedConfigKVFile(applicationProperties);
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties"))
.thenReturn(propertiesFile);
// application-dev.properties
Map<String, Object> devProperties = new HashMap<>();
devProperties.put("k1", "v11");
ConfigKVFile devFile = new MockedConfigKVFile(devProperties);
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application-dev.properties"))
.thenReturn(devFile);
Map<String, Object> emptyMap = new HashMap<>();
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application-dev.yml")).thenReturn(emptyConfigFile);
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap-dev.properties")).thenReturn(emptyConfigFile);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yml")).thenReturn(emptyConfigFile);
when(polarisConfigProperties.getGroups()).thenReturn(null);
when(environment.getActiveProfiles()).thenReturn(new String[]{"dev"});
CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName);
puller.initInternalConfigFiles(compositePropertySource);
Assert.assertEquals("v11", compositePropertySource.getProperty("k1"));
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
}
@Test
public void testBootstrapPullCustomConfigFilesWithProfile() {
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService,
polarisPropertySourceManager, environment);
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName);
Map<String, Object> emptyMap = new HashMap<>();
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties")).thenReturn(emptyConfigFile);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
List<ConfigFileGroup> customFiles = new LinkedList<>();
ConfigFileGroup configFileGroup = new ConfigFileGroup();
String customGroup = "group1";
configFileGroup.setName(customGroup);
String customFile1 = "file1.properties";
String customFile2 = "file2.properties";
configFileGroup.setFiles(Lists.newArrayList(customFile1, customFile2));
customFiles.add(configFileGroup);
when(polarisConfigProperties.getGroups()).thenReturn(customFiles);
when(environment.getActiveProfiles()).thenReturn(new String[]{});
// file1.properties
Map<String, Object> file1Map = new HashMap<>();
file1Map.put("k1", "v1");
file1Map.put("k2", "v2");
ConfigKVFile file1 = new MockedConfigKVFile(file1Map);
when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile1)).thenReturn(file1);
// file2.properties
Map<String, Object> file2Map = new HashMap<>();
file2Map.put("k1", "v11");
file2Map.put("k3", "v3");
ConfigKVFile file2 = new MockedConfigKVFile(file2Map);
when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile2)).thenReturn(file2);
CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName);
puller.initCustomPolarisConfigFiles(compositePropertySource, customFiles);
Assert.assertEquals("v1", compositePropertySource.getProperty("k1"));
Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
Assert.assertEquals("v3", compositePropertySource.getProperty("k3"));
}
@Test
public void testConfigDataPullInternalConfigFiles() {
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService,
polarisPropertySourceManager); polarisPropertySourceManager);
@ -199,13 +64,9 @@ public class PolarisConfigFilePullerTest {
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile); when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile); when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile); when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
when(polarisConfigProperties.getGroups()).thenReturn(null);
when(profiles.getActive()).thenReturn(Lists.newArrayList());
CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName); CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName);
puller.initInternalConfigFiles(compositePropertySource, profiles, testServiceName);
puller.initInternalConfigFiles(compositePropertySource, new String[]{}, testServiceName);
Assert.assertEquals("v1", compositePropertySource.getProperty("k1")); Assert.assertEquals("v1", compositePropertySource.getProperty("k1"));
Assert.assertEquals("v2", compositePropertySource.getProperty("k2")); Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
@ -213,7 +74,7 @@ public class PolarisConfigFilePullerTest {
} }
@Test @Test
public void testConfigDataPullInternalConfigFilesWithProfile() { public void testPullInternalConfigFilesWithProfile() {
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService,
polarisPropertySourceManager); polarisPropertySourceManager);
@ -244,14 +105,11 @@ public class PolarisConfigFilePullerTest {
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap-dev.properties")).thenReturn(emptyConfigFile); when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap-dev.properties")).thenReturn(emptyConfigFile);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile); when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yml")).thenReturn(emptyConfigFile); when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yml")).thenReturn(emptyConfigFile);
when(polarisConfigProperties.getGroups()).thenReturn(null);
List<String> active = new ArrayList<>(); List<String> active = new ArrayList<>();
active.add("dev"); active.add("dev");
when(profiles.getActive()).thenReturn(active); String[] activeProfiles = active.toArray(new String[]{});
CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName); CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName);
puller.initInternalConfigFiles(compositePropertySource, profiles, testServiceName); puller.initInternalConfigFiles(compositePropertySource, activeProfiles, testServiceName);
Assert.assertEquals("v11", compositePropertySource.getProperty("k1")); Assert.assertEquals("v11", compositePropertySource.getProperty("k1"));
Assert.assertEquals("v2", compositePropertySource.getProperty("k2")); Assert.assertEquals("v2", compositePropertySource.getProperty("k2"));
@ -259,20 +117,11 @@ public class PolarisConfigFilePullerTest {
} }
@Test @Test
public void testConfigDataPullCustomConfigFilesWithProfile() { public void testPullCustomConfigFilesWithProfile() {
PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService,
polarisPropertySourceManager); polarisPropertySourceManager);
when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace);
when(polarisContextProperties.getService()).thenReturn(testServiceName);
Map<String, Object> emptyMap = new HashMap<>();
ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap);
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties")).thenReturn(emptyConfigFile);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile);
when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile);
when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile);
List<ConfigFileGroup> customFiles = new LinkedList<>(); List<ConfigFileGroup> customFiles = new LinkedList<>();
ConfigFileGroup configFileGroup = new ConfigFileGroup(); ConfigFileGroup configFileGroup = new ConfigFileGroup();
@ -283,9 +132,6 @@ public class PolarisConfigFilePullerTest {
configFileGroup.setFiles(Lists.newArrayList(customFile1, customFile2)); configFileGroup.setFiles(Lists.newArrayList(customFile1, customFile2));
customFiles.add(configFileGroup); customFiles.add(configFileGroup);
when(polarisConfigProperties.getGroups()).thenReturn(customFiles);
when(profiles.getActive()).thenReturn(Lists.newArrayList());
// file1.properties // file1.properties
Map<String, Object> file1Map = new HashMap<>(); Map<String, Object> file1Map = new HashMap<>();
file1Map.put("k1", "v1"); file1Map.put("k1", "v1");

@ -46,7 +46,9 @@ public class PolarisContextAutoConfiguration {
public SDKContext polarisContext(PolarisContextProperties properties, Environment environment, public SDKContext polarisContext(PolarisContextProperties properties, Environment environment,
List<PolarisConfigModifier> modifierList) List<PolarisConfigModifier> modifierList)
throws PolarisException { throws PolarisException {
return SDKContext.initContextByConfig(properties.configuration(environment, modifierList)); return SDKContext.initContextByConfig(properties.configuration(modifierList,() -> {
return environment.getProperty("spring.cloud.client.ip-address");
}));
} }
@Bean @Bean

@ -21,6 +21,7 @@ package com.tencent.cloud.polaris.context.config;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.cloud.polaris.context.PolarisConfigModifier;
@ -31,7 +32,6 @@ import com.tencent.polaris.factory.config.ConfigurationImpl;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
/** /**
@ -67,7 +67,7 @@ public class PolarisContextProperties {
*/ */
private String service; private String service;
public Configuration configuration(Environment environment, List<PolarisConfigModifier> modifierList) { public Configuration configuration(List<PolarisConfigModifier> modifierList, Supplier<String> ipAddressSupplier) {
// 1. Read user-defined polaris.yml configuration // 1. Read user-defined polaris.yml configuration
ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory
.defaultConfig(ConfigProvider.DEFAULT_CONFIG); .defaultConfig(ConfigProvider.DEFAULT_CONFIG);
@ -75,7 +75,7 @@ public class PolarisContextProperties {
// 2. Override user-defined polaris.yml configuration with SCT configuration // 2. Override user-defined polaris.yml configuration with SCT configuration
String defaultHost = this.localIpAddress; String defaultHost = this.localIpAddress;
if (StringUtils.isBlank(localIpAddress)) { if (StringUtils.isBlank(localIpAddress)) {
defaultHost = environment.getProperty("spring.cloud.client.ip-address"); defaultHost = ipAddressSupplier.get();
} }
configuration.getGlobal().getAPI().setBindIP(defaultHost); configuration.getGlobal().getAPI().setBindIP(defaultHost);

Loading…
Cancel
Save