简体   繁体   中英

merging multiple levels of configuration using typesafe/akka config

I'm looking at: https://github.com/typesafehub/config

Let's say I want to have a default configuration, eg reference.conf, and then I want to have dev/prod overrides (two different application.conf's), and then I also wanted to have host-specific overrides that inherited from both the application.conf and ultimately the default reference.conf. How would I do this?

eg, I'm imagining a directory structure something like:

resources/reference.conf
resources/prod/application.conf
resources/prod/master.conf
resources/prod/slave.conf
resources/dev/application.conf
resources/dev/master.conf
resources/dev/slave.conf

Or maybe it would be resources/dev/master/application.conf?

Somewhere I would specify an environment, ie maybe extracted from the hostname the application was started on.

If the application was master.dev.example.com, I'm expecting I should be able to do something like:

getConfigurations("dev/master.conf").withDefaultsFrom(
    getConfigurations("dev/application.conf").withDefaultsFrom(
        getConfigurations("resource.conf"))

But I'm having a hard time understanding what exactly that would look like using the given library.

I see I could set a config.resource system property, but it looks like that would only allow for one level of overrides, dev-application.conf -> resources.conf, not something like master-node.conf -> dev-application.conf -> resources.conf.

I see a .withFallback method, but that seems to be if I wanted to mix two kinds of configuration in a single file, not to chain resources/files together.

Use multiple withFallback with the configs that have the highest priority first. For example:

Config finalConfig = 
  ConfigFactory.systemProperties().
    withFallback(masterConfig).
    withFallback(applicationConfig).
    withFallback(referenceConfig)

Each of the configs like masterConfig would have been loaded with Config.parseFile . You can also use ConfigFactor.load as a convenience, but the parseXXX methods give you more control over your hierarchy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM