在 dropwizard 中使用环境变量覆盖配置的问题
Issue in overriding config with env variables in dropwizard
MyWebConfiguration.java 有如下代码作为下拉向导描述
public void initialize(Bootstrap<MyWebConfiguration> bootstrap) {
LOG.info("Initializing configuration");
// Enable variable substitution with environment variables
bootstrap.setConfigurationSourceProvider(
new SubstitutingSourceProvider(
bootstrap.getConfigurationSourceProvider(),
new EnvironmentVariableSubstitutor(false)
)
);
}
dev-services.yaml 文件有
tokenSecret: ${TOKEN_SECRET}
但是当我 运行 应用程序并调试以检查我为 tokenSecret 获得的值时,它在调试控制台中显示 tokenSecret = "${TOKEN_SECRET}"。
我试着改变 MyWebConfiguration.java 如下:-
bootstrap.setConfigurationSourceProvider(
new SubstitutingSourceProvider(
bootstrap.getConfigurationSourceProvider(),
new EnvironmentVariableSubstitutor(true)// changed false to true
)
);
但现在当我尝试 运行 程序时,它显示以下错误
Exception in thread "main" io.dropwizard.configuration.UndefinedEnvironmentVariableException: The environment variable 'TOKEN_SECRET' is not defined; could not substitute the expression '${TOKEN_SECRET}'.
at io.dropwizard.configuration.EnvironmentVariableLookup.lookup(EnvironmentVariableLookup.java:41)
at org.apache.commons.lang3.text.StrSubstitutor.resolveVariable(StrSubstitutor.java:726)
at org.apache.commons.lang3.text.StrSubstitutor.substitute(StrSubstitutor.java:649)
at org.apache.commons.lang3.text.StrSubstitutor.substitute(StrSubstitutor.java:563)
at org.apache.commons.lang3.text.StrSubstitutor.replace(StrSubstitutor.java:305)
at io.dropwizard.configuration.SubstitutingSourceProvider.open(SubstitutingSourceProvider.java:39)
at io.dropwizard.configuration.YamlConfigurationFactory.build(YamlConfigurationFactory.java:80)
at io.dropwizard.cli.ConfiguredCommand.parseConfiguration(ConfiguredCommand.java:124)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:72)
at io.dropwizard.cli.Cli.run(Cli.java:75)
at io.dropwizard.Application.run(Application.java:79)
谁能告诉我哪里可能出错了?
您的环境变量未传播。没有从您的系统自动传播到您启动 DropWizard 的IDE。
其次,使用new EnvironmentVariableSubstitutor(false)
(非严格)时,需要提供默认值,即使它们应该为空:
tokenSecret: ${TOKEN_SECRET:-}
缺少时,严格会抛出 UndefinedEnvironmentVariableException
,非严格会使用空字符串。
MyWebConfiguration.java 有如下代码作为下拉向导描述
public void initialize(Bootstrap<MyWebConfiguration> bootstrap) {
LOG.info("Initializing configuration");
// Enable variable substitution with environment variables
bootstrap.setConfigurationSourceProvider(
new SubstitutingSourceProvider(
bootstrap.getConfigurationSourceProvider(),
new EnvironmentVariableSubstitutor(false)
)
);
}
dev-services.yaml 文件有
tokenSecret: ${TOKEN_SECRET}
但是当我 运行 应用程序并调试以检查我为 tokenSecret 获得的值时,它在调试控制台中显示 tokenSecret = "${TOKEN_SECRET}"。
我试着改变 MyWebConfiguration.java 如下:-
bootstrap.setConfigurationSourceProvider(
new SubstitutingSourceProvider(
bootstrap.getConfigurationSourceProvider(),
new EnvironmentVariableSubstitutor(true)// changed false to true
)
);
但现在当我尝试 运行 程序时,它显示以下错误
Exception in thread "main" io.dropwizard.configuration.UndefinedEnvironmentVariableException: The environment variable 'TOKEN_SECRET' is not defined; could not substitute the expression '${TOKEN_SECRET}'.
at io.dropwizard.configuration.EnvironmentVariableLookup.lookup(EnvironmentVariableLookup.java:41)
at org.apache.commons.lang3.text.StrSubstitutor.resolveVariable(StrSubstitutor.java:726)
at org.apache.commons.lang3.text.StrSubstitutor.substitute(StrSubstitutor.java:649)
at org.apache.commons.lang3.text.StrSubstitutor.substitute(StrSubstitutor.java:563)
at org.apache.commons.lang3.text.StrSubstitutor.replace(StrSubstitutor.java:305)
at io.dropwizard.configuration.SubstitutingSourceProvider.open(SubstitutingSourceProvider.java:39)
at io.dropwizard.configuration.YamlConfigurationFactory.build(YamlConfigurationFactory.java:80)
at io.dropwizard.cli.ConfiguredCommand.parseConfiguration(ConfiguredCommand.java:124)
at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:72)
at io.dropwizard.cli.Cli.run(Cli.java:75)
at io.dropwizard.Application.run(Application.java:79)
谁能告诉我哪里可能出错了?
您的环境变量未传播。没有从您的系统自动传播到您启动 DropWizard 的IDE。
其次,使用new EnvironmentVariableSubstitutor(false)
(非严格)时,需要提供默认值,即使它们应该为空:
tokenSecret: ${TOKEN_SECRET:-}
缺少时,严格会抛出 UndefinedEnvironmentVariableException
,非严格会使用空字符串。