简体   繁体   English

使用typesafe / akka config合并多个配置级别

[英]merging multiple levels of configuration using typesafe/akka config

I'm looking at: https://github.com/typesafehub/config 我在看: 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. 假设我想要一个默认配置,例如reference.conf,然后我想要dev / prod覆盖(两个不同的application.conf),然后我还想要从这两个应用程序继承的特定于主机的覆盖.conf并最终默认为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? 或者它可能是资源/ 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: 如果应用程序是master.dev.example.com,我希望我能够做类似的事情:

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. 我看到我可以设置一个config.resource系统属性,但看起来它只允许一级覆盖,dev-application.conf - > resources.conf,而不是像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. 我看到一个.withFallback方法,但似乎我想在一个文件中混合两种配置,而不是将资源/文件链接在一起。

Use multiple withFallback with the configs that have the highest priority first. 将多个withFallback与首先具有最高优先级的配置一起使用。 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 . 像CONFIGS的每个masterConfig将已装有Config.parseFile You can also use ConfigFactor.load as a convenience, but the parseXXX methods give you more control over your hierarchy. 您也可以使用ConfigFactor.load作为方便,但parseXXX方法可以让您更好地控制层次结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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