Overriding server connector config with env variables with dropwizard

前端 未结 2 942
孤城傲影
孤城傲影 2020-12-15 06:10

I have posted this question on dw mailing list but didnt get an answer.

Can I assume the YML format below doesnt work for DW 0.7.0 anymore? (The use of @ char to ins

2条回答
  •  无人及你
    2020-12-15 06:58

    Starting from Dropwizard version 0.8.0, you can access environment variables from the configuration yml file. It also supports setting a default value in case the environment variable is not available. See the docs here.

    Example

    // put environment variable inside ${}
    // use :- operator to provide default value
    
    dbHost: ${DB_HOST}
    dbPort: ${DB_PORT:-1234}
    // dbPort = 1234, if DB_PORT environment variable has no value
    

    Important Note: For this to work you need to set up a SubstitutingSourceProvider with an EnvironmentVariableSubstitutor.

    // Enable variable substitution with environment variables
    bootstrap.setConfigurationSourceProvider(
        new SubstitutingSourceProvider(
            bootstrap.getConfigurationSourceProvider(),
            new EnvironmentVariableSubstitutor())
    );
    

    Update: 15/Nov/2017 As mentioned by @EFreak in the comments section, new EnvironmentVariableSubstitutor() will throw UndefinedEnvironmentVariableException if the environment variable is not defined, unless you set strict mode to false by using new EnvironmentVariableSubstitutor(false) https://github.com/dropwizard/dropwizard/blob/master/dropwizard-configuration/src/main/java/io/dropwizard/configuration/EnvironmentVariableSubstitutor.java

提交回复
热议问题