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); }
/** * Get Application's config instance. * * @return config instance */ public static Config getAppConfig() { return getConfig(ConfigConsts.NAMESPACE_APPLICATION); }
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()); } }); }
@Bean @ConditionalOnMissingBean public Config apolloConfig() { String namespace = environment.getProperty(ApolloConstant.APOLLO_PLUGIN_NAMESPACE); if (StringUtils.isNotEmpty(namespace)) { return ConfigService.getConfig(namespace); } else { return ConfigService.getAppConfig(); } }
/** * 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); }
static void setConfig(Config config) { setConfig(ConfigConsts.NAMESPACE_APPLICATION, config); }
@Bean @ConditionalOnMissingBean public Config apolloConfig() { String namespace = environment.getProperty(ApolloConstant.APOLLO_PLUGIN_NAMESPACE); if (StringUtils.isNotEmpty(namespace)) { return ConfigService.getConfig(namespace); } else { return ConfigService.getAppConfig(); } }
public static ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat) { return s_instance.getManager().getConfigFile(namespace, configFileFormat); }
/** * Manually set the config for the namespace specified, use with caution. * * @param namespace the namespace * @param config the config instance */ static void setConfig(String namespace, final Config config) { s_instance.getRegistry().register(namespace, new ConfigFactory() { @Override public Config create(String namespace) { return config; } @Override public ConfigFile createConfigFile(String namespace, ConfigFileFormat configFileFormat) { return null; } }); }
@Override protected void processField(Object bean, String beanName, Field field) { ApolloConfig annotation = AnnotationUtils.getAnnotation(field, ApolloConfig.class); if (annotation == null) { return; } Preconditions.checkArgument(Config.class.isAssignableFrom(field.getType()), "Invalid type: %s for field: %s, should be Config", field.getType(), field); String namespace = annotation.value(); Config config = ConfigService.getConfig(namespace); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, bean, 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); }
/** * Constructs the Apollo data source * * @param namespaceName the namespace name in Apollo, should not be null or empty * @param flowRulesKey the flow rules key in the namespace, should not be null or empty * @param defaultFlowRuleValue the default flow rules value when the flow rules key is not found or any error * occurred * @param parser the parser to transform string configuration to actual flow rules */ public ApolloDataSource(String namespaceName, String flowRulesKey, String defaultFlowRuleValue, Converter<String, T> parser) { super(parser); Preconditions.checkArgument(!Strings.isNullOrEmpty(namespaceName), "Namespace name could not be null or empty"); Preconditions.checkArgument(!Strings.isNullOrEmpty(flowRulesKey), "FlowRuleKey could not be null or empty!"); this.flowRulesKey = flowRulesKey; this.defaultFlowRuleValue = defaultFlowRuleValue; this.config = ConfigService.getConfig(namespaceName); initialize(); RecordLog.info(String.format("Initialized rule for namespace: %s, flow rules key: %s", namespaceName, flowRulesKey)); }
/** * 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); }
/** * Initialize Apollo Configurations Just after environment is ready. * * @param environment */ protected void initialize(ConfigurableEnvironment environment) { if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) { //already initialized return; } String namespaces = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, ConfigConsts.NAMESPACE_APPLICATION); logger.debug("Apollo bootstrap namespaces: {}", namespaces); List<String> namespaceList = NAMESPACE_SPLITTER.splitToList(namespaces); CompositePropertySource composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME); for (String namespace : namespaceList) { Config config = ConfigService.getConfig(namespace); composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config)); } environment.getPropertySources().addFirst(composite); }
public ApolloConfigLoader(String appId, String metaDomain) { this.config = ConfigService.getAppConfig(); }
Config config = ConfigService.getConfig(namespace);
public void start() throws Exception { Config config = ConfigService.getAppConfig(); appContext.getBeans(ConfigChangeListener.class).forEach((listener) -> config.addChangeListener(listener)); }
private void initializePropertySources() { if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME)) { //already initialized return; } CompositePropertySource composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME); //sort by order asc ImmutableSortedSet<Integer> orders = ImmutableSortedSet.copyOf(NAMESPACE_NAMES.keySet()); Iterator<Integer> iterator = orders.iterator(); while (iterator.hasNext()) { int order = iterator.next(); for (String namespace : NAMESPACE_NAMES.get(order)) { Config config = ConfigService.getConfig(namespace); composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config)); } } // clean up NAMESPACE_NAMES.clear(); // add after the bootstrap property source or to the first if (environment.getPropertySources() .contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) { // ensure ApolloBootstrapPropertySources is still the first ensureBootstrapPropertyPrecedence(environment); environment.getPropertySources() .addAfter(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME, composite); } else { environment.getPropertySources().addFirst(composite); } }
public void init() throws Exception { Config config = ConfigService.getAppConfig(); conf = new PropertiesProxy(); config.getPropertyNames().forEach((key) -> conf.put(key, config.getProperty(key, null))); }