Search in sources :

Example 6 with MicroserviceConfigLoader

use of org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader in project incubator-servicecomb-java-chassis by apache.

the class TestConfigurationSpringInitializer method testAll.

@Test
public void testAll() {
    ConfigurationSpringInitializer configurationSpringInitializer = new ConfigurationSpringInitializer();
    Assert.assertEquals(Ordered.LOWEST_PRECEDENCE / 2, configurationSpringInitializer.getOrder());
    Assert.assertEquals(true, Deencapsulation.getField(configurationSpringInitializer, "ignoreUnresolvablePlaceholders"));
    Object o = ConfigUtil.getProperty("zq");
    @SuppressWarnings("unchecked") List<Map<String, Object>> listO = (List<Map<String, Object>>) o;
    Assert.assertEquals(3, listO.size());
    Assert.assertEquals(null, ConfigUtil.getProperty("notExist"));
    MicroserviceConfigLoader loader = ConfigUtil.getMicroserviceConfigLoader();
    Assert.assertNotNull(loader);
    Configuration instance = ConfigurationManager.getConfigInstance();
    ConfigUtil.installDynamicConfig();
    // must not reinstall
    Assert.assertEquals(instance, ConfigurationManager.getConfigInstance());
}
Also used : Configuration(org.apache.commons.configuration.Configuration) MicroserviceConfigLoader(org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Example 7 with MicroserviceConfigLoader

use of org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader in project incubator-servicecomb-java-chassis by apache.

the class ConfigUtil method createLocalConfig.

public static ConcurrentCompositeConfiguration createLocalConfig() {
    MicroserviceConfigLoader loader = new MicroserviceConfigLoader();
    loader.loadAndSort();
    LOGGER.info("create local config:");
    for (ConfigModel configModel : loader.getConfigModels()) {
        LOGGER.info(" {}.", configModel.getUrl());
    }
    ConcurrentCompositeConfiguration config = ConfigUtil.createLocalConfig(loader.getConfigModels());
    ConfigUtil.setMicroserviceConfigLoader(config, loader);
    return config;
}
Also used : ConfigModel(org.apache.servicecomb.config.archaius.sources.ConfigModel) MicroserviceConfigLoader(org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader) ConcurrentCompositeConfiguration(com.netflix.config.ConcurrentCompositeConfiguration)

Example 8 with MicroserviceConfigLoader

use of org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader in project java-chassis by ServiceComb.

the class ConfigurationSpringInitializer method addMicroserviceYAMLToSpring.

/**
 * make springboot have a change to add microservice.yaml source earlier<br>
 * to affect {@link Conditional}
 * @param environment environment
 */
private static void addMicroserviceYAMLToSpring(Environment environment) {
    if (!(environment instanceof ConfigurableEnvironment)) {
        return;
    }
    MutablePropertySources propertySources = ((ConfigurableEnvironment) environment).getPropertySources();
    if (propertySources.contains(MICROSERVICE_PROPERTY_SOURCE_NAME)) {
        return;
    }
    propertySources.addLast(new EnumerablePropertySource<MicroserviceConfigLoader>(MICROSERVICE_PROPERTY_SOURCE_NAME) {

        private final Map<String, Object> values = new HashMap<>();

        private final String[] propertyNames;

        {
            MicroserviceConfigLoader loader = new MicroserviceConfigLoader();
            loader.loadAndSort();
            loader.getConfigModels().forEach(configModel -> values.putAll(YAMLUtil.retrieveItems("", configModel.getConfig())));
            propertyNames = values.keySet().toArray(new String[values.size()]);
        }

        @Override
        public String[] getPropertyNames() {
            return propertyNames;
        }

        @SuppressWarnings("unchecked")
        @Override
        public Object getProperty(String name) {
            Object value = this.values.get(name);
            // spring will not resolve nested placeholder of list, so try to fix the problem
            if (value instanceof List) {
                value = ((List<Object>) value).stream().filter(item -> item instanceof String).map(item -> environment.resolvePlaceholders((String) item)).collect(Collectors.toList());
            }
            return value;
        }
    });
}
Also used : Ordered(org.springframework.core.Ordered) DynamicConfigurationChangedEvent(org.apache.servicecomb.config.event.DynamicConfigurationChangedEvent) PropertySource(org.springframework.core.env.PropertySource) RefreshGovernanceConfigurationEvent(org.apache.servicecomb.config.event.RefreshGovernanceConfigurationEvent) BootStrapService(org.apache.servicecomb.foundation.bootstrap.BootStrapService) LoggerFactory(org.slf4j.LoggerFactory) SPIServiceUtils(org.apache.servicecomb.foundation.common.utils.SPIServiceUtils) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) ConfigCenterConfigurationSource(org.apache.servicecomb.config.spi.ConfigCenterConfigurationSource) YAMLUtil(org.apache.servicecomb.config.YAMLUtil) EventManager(org.apache.servicecomb.foundation.common.event.EventManager) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) Map(java.util.Map) PropertySourcesPlaceholderConfigurer(org.springframework.context.support.PropertySourcesPlaceholderConfigurer) Subscribe(com.google.common.eventbus.Subscribe) Properties(java.util.Properties) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConfigurationManager(com.netflix.config.ConfigurationManager) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) WatchedUpdateResult(com.netflix.config.WatchedUpdateResult) DynamicPropertyFactory(com.netflix.config.DynamicPropertyFactory) CompositePropertySource(org.springframework.core.env.CompositePropertySource) List(java.util.List) EnvironmentAware(org.springframework.context.EnvironmentAware) Environment(org.springframework.core.env.Environment) MapPropertySource(org.springframework.core.env.MapPropertySource) MicroserviceConfigLoader(org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Conditional(org.springframework.context.annotation.Conditional) ConfigMapping(org.apache.servicecomb.config.ConfigMapping) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MicroserviceConfigLoader(org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader) List(java.util.List) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Aggregations

MicroserviceConfigLoader (org.apache.servicecomb.config.archaius.sources.MicroserviceConfigLoader)8 ConcurrentCompositeConfiguration (com.netflix.config.ConcurrentCompositeConfiguration)3 ConfigModel (org.apache.servicecomb.config.archaius.sources.ConfigModel)3 MicroserviceDefinition (org.apache.servicecomb.serviceregistry.definition.MicroserviceDefinition)3 Test (org.junit.Test)3 List (java.util.List)2 Map (java.util.Map)2 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)2 EventBus (com.google.common.eventbus.EventBus)1 Subscribe (com.google.common.eventbus.Subscribe)1 ConfigurationManager (com.netflix.config.ConfigurationManager)1 DynamicPropertyFactory (com.netflix.config.DynamicPropertyFactory)1 WatchedUpdateResult (com.netflix.config.WatchedUpdateResult)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Properties (java.util.Properties)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Collectors (java.util.stream.Collectors)1 Expectations (mockit.Expectations)1