我可以在我的 java 应用程序上指定 config.yml 的默认值吗?

jpg*_*z18 1 java yaml dropwizard

我目前正在使用 dropwizard 处理我的 java 应用程序。

它工作正常,我运行它并发送完整的 yml 文件或参数。

例如,其中之一是:

reporting:
  enabled: false
Run Code Online (Sandbox Code Playgroud)

所以,我想做的就是将此 yml 文件设置为默认值,并在需要时(用于将来的功能)发送此值var

所以我想做这样的事情

reporting:
  enabled: ${REPORTING_FLAG:false}
Run Code Online (Sandbox Code Playgroud)

然后我可以将 REPORTING_FLAG 作为环境参数(docker)发送,并且应该可以正常工作......

问题是我发现我的应用程序无法识别该模型。

我有什么办法可以做到这一点吗?是否需要考虑额外的配置?我之前用 Spring 应用程序做过,但这似乎有所不同。

现在我收到一个错误,预期的布尔值无效(将整行作为字符串)

有想法吗?

jpg*_*z18 5

因此,在阅读了更多 dropwizard 文档后,我发现了这一点: https: //www.dropwizard.io/0.8.2/docs/manual/core.html

环境变量

dropwizard-configuration 模块还提供了使用 SubstitutingSourceProvider 和 EnvironmentVariableSubstitutor 用环境变量值替换配置设置的功能。

public class MyApplication extends Application<MyConfiguration> {
    // [...]
    @Override
    public void initialize(Bootstrap<MyConfiguration> bootstrap) {
        // Enable variable substitution with environment variables
        bootstrap.setConfigurationSourceProvider(
                new SubstitutingSourceProvider(bootstrap.getConfigurationSourceProvider(),
                                                   new EnvironmentVariableSubstitutor()
                )
        );

    }

    // [...]
}
Run Code Online (Sandbox Code Playgroud)

应替换的配置设置需要显式写入配置文件中,并遵循 Apache Commons Lang 库中的 StrSubstitutor 的替换规则。

mySetting: ${DW_MY_SETTING}
defaultSetting: ${DW_DEFAULT_SETTING:-default value}
Run Code Online (Sandbox Code Playgroud)