/** * This method will be used by Configuration to get valid value at runtime. * The group is expected to be 'app level', which can be fetched from the 'config.appnamespace' in url if necessary. * But I think Apollo's inheritance feature of namespace can solve the problem . */ @Override public String getInternalProperty(String key) { return dubboConfig.getProperty(key, null); }
/** * Get Application's config instance. * * @return config instance */ public static Config getAppConfig() { return getConfig(ConfigConsts.NAMESPACE_APPLICATION); }
public void addChangeListener(ConfigChangeListener listener) { this.source.addChangeListener(listener); } }
public ApolloConfigDemo() { ConfigChangeListener changeListener = new ConfigChangeListener() { @Override public void onChange(ConfigChangeEvent changeEvent) { logger.info("Changes for namespace {}", changeEvent.getNamespace()); for (String key : changeEvent.changedKeys()) { ConfigChange change = changeEvent.getChange(key); logger.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()); } } }; config = ConfigService.getAppConfig(); config.addChangeListener(changeListener); publicConfig = ConfigService.getConfig("TEST1.apollo"); publicConfig.addChangeListener(changeListener); applicationConfigFile = ConfigService.getConfigFile("application", ConfigFileFormat.Properties); xmlConfigFile = ConfigService.getConfigFile("datasources", ConfigFileFormat.XML); xmlConfigFile.addChangeListener(new ConfigFileChangeListener() { @Override public void onChange(ConfigFileChangeEvent changeEvent) { logger.info(changeEvent.toString()); } }); }
/** * This method will be used to: * 1. get configuration file at startup phase * 2. get all kinds of Dubbo rules */ @Override public String getConfig(String key, String group, long timeout) throws IllegalStateException { if (StringUtils.isNotEmpty(group) && !url.getParameter(Constants.CONFIG_GROUP_KEY, DEFAULT_GROUP).equals(group)) { Config config = ConfigService.getAppConfig(); return config.getProperty(key, null); } return dubboConfig.getProperty(key, null); }
public SimpleApolloConfigDemo() { ConfigChangeListener changeListener = new ConfigChangeListener() { @Override public void onChange(ConfigChangeEvent changeEvent) { logger.info("Changes for namespace {}", changeEvent.getNamespace()); for (String key : changeEvent.changedKeys()) { ConfigChange change = changeEvent.getChange(key); logger.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()); } } }; config = ConfigService.getAppConfig(); config.addChangeListener(changeListener); }
@Override public String[] getPropertyNames() { Set<String> propertyNames = this.source.getPropertyNames(); if (propertyNames.isEmpty()) { return EMPTY_ARRAY; } return propertyNames.toArray(new String[propertyNames.size()]); }
@Override public void removeListener(String key, String group, ConfigurationListener listener) { ApolloListener apolloListener = listeners.get(group + key); if (apolloListener != null) { apolloListener.removeListener(listener); if (!apolloListener.hasInternalListener()) { dubboConfig.removeChangeListener(apolloListener); } } }
private void print(ConfigFile configFile) { if (!configFile.hasContent()) { System.out.println("No config file content found for " + configFile.getNamespace()); return; } System.out.println("=== Config File Content for " + configFile.getNamespace() + " is as follows: "); System.out.println(configFile.getContent()); }
/** * Get the config instance for the namespace. * * @param namespace the namespace of the config * @return config instance */ public static Config getConfig(String namespace) { return s_instance.getManager().getConfig(namespace); }
/** * Manually set the config factory for the namespace specified, use with caution. * * @param namespace the namespace * @param factory the factory instance */ static void setConfigFactory(String namespace, ConfigFactory factory) { s_instance.getRegistry().register(namespace, factory); }
public static ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat) { return s_instance.getManager().getConfigFile(namespace, configFileFormat); }
static void setConfigFactory(ConfigFactory factory) { setConfigFactory(ConfigConsts.NAMESPACE_APPLICATION, factory); }
static void setConfig(Config config) { setConfig(ConfigConsts.NAMESPACE_APPLICATION, config); }
/** * This method will be used to: * 1. get configuration file at startup phase * 2. get all kinds of Dubbo rules */ @Override public String getConfig(String key, String group, long timeout) throws IllegalStateException { if (StringUtils.isNotEmpty(group) && !url.getParameter(Constants.CONFIG_GROUP_KEY, DEFAULT_GROUP).equals(group)) { Config config = ConfigService.getAppConfig(); return config.getProperty(key, null); } return dubboConfig.getProperty(key, null); }
/** * This method will be used by Configuration to get valid value at runtime. * The group is expected to be 'app level', which can be fetched from the 'config.appnamespace' in url if necessary. * But I think Apollo's inheritance feature of namespace can solve the problem . */ @Override public String getInternalProperty(String key) { return dubboConfig.getProperty(key, null); }
@Override public void removeListener(String key, String group, ConfigurationListener listener) { ApolloListener apolloListener = listeners.get(group + key); if (apolloListener != null) { apolloListener.removeListener(listener); if (!apolloListener.hasInternalListener()) { dubboConfig.removeChangeListener(apolloListener); } } }
@Override public Object getProperty(String name) { return this.source.getProperty(name, null); }
@Override public String readSource() throws Exception { return config.getProperty(flowRulesKey, defaultFlowRuleValue); }
private String getConfig(String key) { String result = config.getProperty(key, DEFAULT_VALUE); logger.info(String.format("Loading key : %s with value: %s", key, result)); return result; }