/** * 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()); } }); }
@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); }
/** * 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)); }
Config config = ConfigService.getConfig(namespace);
/** * 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); }
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); } }
ApolloDynamicConfiguration(URL url) { this.url = url; // Instead of using Dubbo's configuration, I would suggest use the original configuration method Apollo provides. String configEnv = url.getParameter(APOLLO_ENV_KEY); String configAddr = getAddressWithProtocolPrefix(url); String configCluster = url.getParameter(Constants.CONFIG_CLUSTER_KEY); if (configEnv != null) { System.setProperty(APOLLO_ENV_KEY, configEnv); } if (StringUtils.isEmpty(System.getProperty(APOLLO_ENV_KEY)) && !Constants.ANYHOST_VALUE.equals(configAddr)) { System.setProperty(APOLLO_ADDR_KEY, configAddr); } if (configCluster != null) { System.setProperty(APOLLO_CLUSTER_KEY, configCluster); } dubboConfig = ConfigService.getConfig(url.getParameter(Constants.CONFIG_NAMESPACE_KEY, DEFAULT_GROUP)); // Decide to fail or to continue when failed to connect to remote server. boolean check = url.getParameter(Constants.CONFIG_CHECK_KEY, true); if (dubboConfig.getSourceType() != ConfigSourceType.REMOTE) { if (check) { throw new IllegalStateException("Failed to connect to config center, the config center is Apollo, " + "the address is: " + (StringUtils.isNotEmpty(configAddr) ? configAddr : configEnv)); } else { logger.warn("Failed to connect to config center, the config center is Apollo, " + "the address is: " + (StringUtils.isNotEmpty(configAddr) ? configAddr : configEnv) + ", will use the local cache value instead before eventually the connection is established."); } } }
ApolloDynamicConfiguration(URL url) { this.url = url; // Instead of using Dubbo's configuration, I would suggest use the original configuration method Apollo provides. String configEnv = url.getParameter(APOLLO_ENV_KEY); String configAddr = getAddressWithProtocolPrefix(url); String configCluster = url.getParameter(Constants.CONFIG_CLUSTER_KEY); if (configEnv != null) { System.setProperty(APOLLO_ENV_KEY, configEnv); } if (StringUtils.isEmpty(System.getProperty(APOLLO_ENV_KEY)) && !Constants.ANYHOST_VALUE.equals(configAddr)) { System.setProperty(APOLLO_ADDR_KEY, configAddr); } if (configCluster != null) { System.setProperty(APOLLO_CLUSTER_KEY, configCluster); } dubboConfig = ConfigService.getConfig(url.getParameter(Constants.CONFIG_NAMESPACE_KEY, DEFAULT_GROUP)); // Decide to fail or to continue when failed to connect to remote server. boolean check = url.getParameter(Constants.CONFIG_CHECK_KEY, true); if (dubboConfig.getSourceType() != ConfigSourceType.REMOTE) { if (check) { throw new IllegalStateException("Failed to connect to config center, the config center is Apollo, " + "the address is: " + (StringUtils.isNotEmpty(configAddr) ? configAddr : configEnv)); } else { logger.warn("Failed to connect to config center, the config center is Apollo, " + "the address is: " + (StringUtils.isNotEmpty(configAddr) ? configAddr : configEnv) + ", will use the local cache value instead before eventually the connection is established."); } } }
@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(); } }
@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(); } }
/** * 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)); }
ApolloDynamicConfiguration(URL url) { this.url = url; // Instead of using Dubbo's configuration, I would suggest use the original configuration method Apollo provides. String configEnv = url.getParameter(APOLLO_ENV_KEY); String configAddr = getAddressWithProtocolPrefix(url); String configCluster = url.getParameter(Constants.CONFIG_CLUSTER_KEY); if (configEnv != null) { System.setProperty(APOLLO_ENV_KEY, configEnv); } if (StringUtils.isEmpty(System.getProperty(APOLLO_ENV_KEY)) && !Constants.ANYHOST_VALUE.equals(configAddr)) { System.setProperty(APOLLO_ADDR_KEY, configAddr); } if (configCluster != null) { System.setProperty(APOLLO_CLUSTER_KEY, configCluster); } dubboConfig = ConfigService.getConfig(url.getParameter(Constants.CONFIG_NAMESPACE_KEY, DEFAULT_GROUP)); // Decide to fail or to continue when failed to connect to remote server. boolean check = url.getParameter(Constants.CONFIG_CHECK_KEY, true); if (dubboConfig.getSourceType() != ConfigSourceType.REMOTE) { if (check) { throw new IllegalStateException("Failed to connect to config center, the config center is Apollo, " + "the address is: " + (StringUtils.isNotEmpty(configAddr) ? configAddr : configEnv)); } else { logger.warn("Failed to connect to config center, the config center is Apollo, " + "the address is: " + (StringUtils.isNotEmpty(configAddr) ? configAddr : configEnv) + ", will use the local cache value instead before eventually the connection is established."); } } }
ApolloDynamicConfiguration(URL url) { this.url = url; // Instead of using Dubbo's configuration, I would suggest use the original configuration method Apollo provides. String configEnv = url.getParameter(APOLLO_ENV_KEY); String configAddr = getAddressWithProtocolPrefix(url); String configCluster = url.getParameter(Constants.CONFIG_CLUSTER_KEY); if (configEnv != null) { System.setProperty(APOLLO_ENV_KEY, configEnv); } if (StringUtils.isEmpty(System.getProperty(APOLLO_ENV_KEY)) && !Constants.ANYHOST_VALUE.equals(configAddr)) { System.setProperty(APOLLO_ADDR_KEY, configAddr); } if (configCluster != null) { System.setProperty(APOLLO_CLUSTER_KEY, configCluster); } dubboConfig = ConfigService.getConfig(url.getParameter(Constants.CONFIG_NAMESPACE_KEY, DEFAULT_GROUP)); // Decide to fail or to continue when failed to connect to remote server. boolean check = url.getParameter(Constants.CONFIG_CHECK_KEY, true); if (dubboConfig.getSourceType() != ConfigSourceType.REMOTE) { if (check) { throw new IllegalStateException("Failed to connect to config center, the config center is Apollo, " + "the address is: " + (StringUtils.isNotEmpty(configAddr) ? configAddr : configEnv)); } else { logger.warn("Failed to connect to config center, the config center is Apollo, " + "the address is: " + (StringUtils.isNotEmpty(configAddr) ? configAddr : configEnv) + ", will use the local cache value instead before eventually the connection is established."); } } }