DeploymentContext and Cascade Property Not working out of the box
I am trying to use Archaius out of the box( without programming any Configuration myself) . How can I use Deployment Context and cascaded property files. There are two ways to supply property files to Archaius - One make it default property file which limits to only 1 configuration file(config.properties) other is additional URLs. I tried using additional URL's and supplied property files. Looks like default configuration is not respecting the "archaius.deployment.environment" system parameter.
I know default implementation use URLConfigurationSource.java when you load additional URL'S but it is not cascading the property files.
I tried calling "ConfigurationManager.loadCascadedPropertiesFromResources(configName)" method which actually did the cascading but when i change any value in source property file it does not pick up the change(polling is not working).
Is there any easy way to do following things - a) load more than one property file b) cascade property according to the environment while making sure any update to the source property file will sync the in memory version. c) it could be a stretch but support @next attribute ..
If it requires me creating my own configuration source, can you give any references about the approach ??
@abhimaloo In your case, those multiple property files contain the same properties or some files provide override properties over other files? Either way, I believe you can provide multiple cascaded configurations through ConfigurationManager.loadCascadedPropertiesFromResources and/or ConfigurationManager.loadAppOverrideProperties.
As you already pointed out, as of current implementation, polled sources don't support cascading so cascaded resources won't change after load.
If you want to combine cascading with polling, you can look at comments above and I think that provides some leads into a possible solution. I'll look around more. I'll update the thread if I can find more.
@howardyuan I'm having the same troubles here (trying this out of the box and cascading not working).
I've defined a config.properties file as follow
key1 = val1
key2 =
key3 = val3a
and a config-dev.properties file as follow
key2 = val2
key3 = val3b
I've even tried adding to config.properties
@next=config-${@environment}.properties
It doesn't pick up my specific environment properties.
DynamicPropertyFactory.getInstance().getStringProperty("key2", "nothingwasfound").get(); // #=> (empty string)
DynamicPropertyFactory.getInstance().getStringProperty("key3", "nothingwasfound").get(); // #=> val3a
Note: i've of course set -Darchaius.deployment.environment=dev and it is picked up as ConfigurationManager.getDeploymentContext().getDeploymentEnvironment() == "dev"
I don't really understand your answer here and how i can achieve this simple task. Any pointers ?
FYI, i'm not interested in the polling of those cascaded files as they serve as a base and are usually compiled into the jar (as is the config.properties file). However, i will need the polling on the additionalUrls. But if i read you right, that should be working.
If those config files are compiled into the jar, you don't have to worry about polling. Do you mind posting your config.properties and config-dev.properties so I can play with them?
Also, if you look at our unit test, you maybe able to figure out yourself. The test is CascadedPropertiesWithDeploymentContextTest.
I think I know what what the problem is. By default, Archaius is using the polling for the default config.properties instead of ConfigurationManager.loadCascadedPropertiesFromResources. Polling won't do cascading. It won't even look at @next inside config.properties.
Let me go through the Wiki document and see if it indeed is misleading and needs an update. In the meantime, please use ConfigurationManager.loadCascadedPropertiesFromResources to load the config files explicitly. That should take care of your problem.
I added a note in Dynamic Context section of Users Guide. Let me know if it needs further clarification.
Yes, the first thing i tried was to use ConfigurationManager.loadCascadedPropertiesFromResources
Here's what i did:
static {
try {
ConfigurationManager.loadCascadedPropertiesFromResources("config");
} catch (IOException e) {
throw new RuntimeException(e);
}
logger.error(ConfigurationManager.getDeploymentContext().getDeploymentEnvironment());
logger.error(ConfigurationManager.getLoadedPropertiesURLs().toString());
logger.error(DynamicPropertyFactory.getInstance().getStringProperty("key1", "(nothing was found)").get());
logger.error(DynamicPropertyFactory.getInstance().getStringProperty("key2", "(nothing was found)").get());
logger.error(DynamicPropertyFactory.getInstance().getStringProperty("key3", "(nothing was found)").get());
}
And here's the logs output:
2014-07-11 08:56:44 INFO URLConfigurationSource:125 - URLs to be used as dynamic configuration source: [file:/data/home/me/my-project/module1/target/classes/config.properties]
2014-07-11 08:56:44 INFO ConfigurationUtils:180 - Loaded properties file file:/data/home/me/my-project/module1/target/classes/config.properties
2014-07-11 08:56:44 INFO ConfigurationUtils:180 - Loaded properties file file:/data/home/me/my-project/module1/target/classes/config-dev.properties
2014-07-11 08:56:44 INFO ConfigurationUtils:162 - Loaded properties file file:/data/home/me/my-project/module1/target/classes/config-user.properties
2014-07-11 08:56:44 ERROR Config:23 - dev
2014-07-11 08:56:44 ERROR Config:24 - [file:/data/home/me/my-project/module1/target/classes/config.properties, file:/data/home/me/my-project/module1/target/classes/config-dev.properties, file:/data/home/me/my-project/module1/target/classes/config-user.properties]
2014-07-11 08:56:44 INFO DynamicPropertyFactory:281 - DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@f57e182
2014-07-11 08:56:44 ERROR Config:25 -
2014-07-11 08:56:44 ERROR Config:26 - value2
2014-07-11 08:56:44 ERROR Config:27 -
And finally here are the config files:
config.properties
key1 =
key2 = value2
key3 =
config-dev.properties
key1 = value1
key3 =
@next=config-user.properties
config-user.properties
key3 = value3
Should i be using ConfigurationManager.getConfigInstance().getProperty instead of DynamicPropertyFactory.getInstance().getStringProperty ? Does that mean i get no polling at all ? What if i add additionnalUrls ?
Ok i've just tested something. Instead of using config*.properties files i named my files "test" i.e.:
test.properties
test-dev.properties
test-user.properties
And i used ConfigurationManager.loadCascadedPropertiesFromResources("test"); to load them, exactly as shown above.
And guess what ? it works. Values are properly loaded and fetched by DynamicPropertyFactory as well as ConfigurationManager. Is there any restrictions with that config file ? Or maybe does it think it's already loaded (using the wrong method) so it won't reload it when i do ConfigurationManager.loadCascadedPropertiesFromResources("config"); ?
Thanks for your feedback. I'm gona test the additionalUrls a bit see how it works.
I tried having my test*.properties files alongside the config*.properties files (i had just renamed the files previously) and it doesn't work anymore. It seems that if a config.properties file is found ConfigurationManager.loadCascadedPropertiesFromResources("test"); is ignored even though the output says the test*.properties files were loaded.
Could this be related to #94 ?
So i removed the config*.properties files to have cascading working and tried adding a 4th configuration file using
-Darchaius.configurationSource.additionalUrls=file:///data/home/me/my-project/module1/target/classes/test-add.properties
Edit: The additionalUrl is initially loaded and polling seems to work fine.
To summarize this, if one wants this to work out of the box, he needs to use
ConfigurationManager.loadCascadedPropertiesFromResources("[config_name]");
But be careful, [config_name] cannot be "config" and there must not be any config.properties file in the project's classpath.
@howardyuan I guess you should adapt the changes you made to the wiki.
@Crystark Thanks for letting us know your findings. I haven't got a chance to verify my thoughts, but here's what I think. By default, Archaius treats config.properties as a dynamic source. That means the same property (key1, key2, key3) in the config.properties will take the precedence over the test.properties you loaded through loadCascadedPropertiesFromResources call. Those are static configurations which are lower in hierarchy. So rule of thumb is that if you define the same property in multiple places, be aware of the hierarchy and precedence rule. Let me know if you think there's a better way to document this and make it clearer to users.