Search in sources :

Example 56 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class ApolloAnnotationProcessor method processMethod.

@Override
protected void processMethod(final Object bean, String beanName, final Method method) {
    ApolloConfigChangeListener annotation = AnnotationUtils.findAnnotation(method, ApolloConfigChangeListener.class);
    if (annotation == null) {
        return;
    }
    Class<?>[] parameterTypes = method.getParameterTypes();
    Preconditions.checkArgument(parameterTypes.length == 1, "Invalid number of parameters: %s for method: %s, should be 1", parameterTypes.length, method);
    Preconditions.checkArgument(ConfigChangeEvent.class.isAssignableFrom(parameterTypes[0]), "Invalid parameter type: %s for method: %s, should be ConfigChangeEvent", parameterTypes[0], method);
    ReflectionUtils.makeAccessible(method);
    String[] namespaces = annotation.value();
    ConfigChangeListener configChangeListener = new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            ReflectionUtils.invokeMethod(method, bean, changeEvent);
        }
    };
    for (String namespace : namespaces) {
        Config config = ConfigService.getConfig(namespace);
        config.addChangeListener(configChangeListener);
    }
}
Also used : ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) Config(com.ctrip.framework.apollo.Config) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent)

Example 57 with Config

use of com.ctrip.framework.apollo.Config in project duangframework by tcrct.

the class SimpleApolloConfig method getConfig.

/**
 * 根据关键字取配置信息值
 * 先取私有空间的,再取公有或关联的,取公有或关联里第一个匹配的信息返回并退出
 * @param key   关键字
 * @return
 */
public String getConfig(String key) {
    String result = appConfig.getProperty(key, DEFAULT_VALUE);
    if (DEFAULT_VALUE.equals(result)) {
        for (String nameSpace : configMaps.keySet()) {
            Config config = configMaps.get(nameSpace);
            if (null != config) {
                result = config.getProperty(key, DEFAULT_VALUE);
                if (!DEFAULT_VALUE.equals(result)) {
                    break;
                }
            }
        }
    }
    logger.warn(String.format("Loading key : %s with value: %s", key, result));
    return DEFAULT_VALUE.equals(result) ? "" : result;
}
Also used : Config(com.ctrip.framework.apollo.Config)

Example 58 with Config

use of com.ctrip.framework.apollo.Config in project nutzboot by nutzam.

the class ApolloConfigureLoader method init.

public void init() throws Exception {
    Config config = ConfigService.getAppConfig();
    conf = new PropertiesProxy();
    config.getPropertyNames().forEach((key) -> conf.put(key, config.getProperty(key, null)));
}
Also used : PropertiesProxy(org.nutz.ioc.impl.PropertiesProxy) Config(com.ctrip.framework.apollo.Config)

Example 59 with Config

use of com.ctrip.framework.apollo.Config in project SpringCloudDemo by RickJou.

the class ApolloJavaClientDemo method privateConfig.

public static void privateConfig() {
    // 私有配置
    String privateNamespace = "application";
    Config privateConfig = ConfigService.getConfig(privateNamespace);
    String someKey = "private-key1";
    String someDefaultValue = "defaultValue";
    String value = privateConfig.getProperty(someKey, someDefaultValue);
    log.info("私有配置" + someKey + ":" + value);
    // 私有配置基础了公有配置,所以可以获取共有配置
    String somePublicNamespace = "org1.public-namespace";
    // config instance is singleton for each namespace and is never null
    Config config = ConfigService.getConfig(somePublicNamespace);
    someKey = "common.setting.value";
    someDefaultValue = "someDefaultValueForTheKey";
    value = config.getProperty(someKey, someDefaultValue);
    log.info("公共配置" + someKey + ":" + value);
}
Also used : Config(com.ctrip.framework.apollo.Config)

Example 60 with Config

use of com.ctrip.framework.apollo.Config in project apollo by ctripcorp.

the class DefaultConfigManager method getConfig.

@Override
public Config getConfig(String namespace) {
    Config config = m_configs.get(namespace);
    if (config == null) {
        synchronized (this) {
            config = m_configs.get(namespace);
            if (config == null) {
                ConfigFactory factory = m_factoryManager.getFactory(namespace);
                config = factory.create(namespace);
                m_configs.put(namespace, config);
            }
        }
    }
    return config;
}
Also used : Config(com.ctrip.framework.apollo.Config) ConfigFactory(com.ctrip.framework.apollo.spi.ConfigFactory)

Aggregations

Config (com.ctrip.framework.apollo.Config)94 Test (org.junit.Test)79 EnableApolloConfig (com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig)39 Matchers.anyString (org.mockito.Matchers.anyString)31 ApolloConfig (com.ctrip.framework.apollo.spring.annotation.ApolloConfig)26 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)22 SimpleConfig (com.ctrip.framework.apollo.internals.SimpleConfig)20 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)16 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)13 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)13 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)13 ApolloConfigChangeListener (com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener)10 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)10 Properties (java.util.Properties)6 ConfigFactory (com.ctrip.framework.apollo.spi.ConfigFactory)5 PureApolloConfigFactory (com.ctrip.framework.apollo.config.data.internals.PureApolloConfigFactory)4 DefaultConfigFactory (com.ctrip.framework.apollo.spi.DefaultConfigFactory)4 OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)4 Set (java.util.Set)4 ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)3