gpt4 book ai didi

java - 在 dropwizard 中使用环境变量覆盖配置时出现问题

转载 作者:行者123 更新时间:2023-11-30 07:07:15 25 4
gpt4 key购买 nike

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,非严格将使用空字符串。

关于java - 在 dropwizard 中使用环境变量覆盖配置时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39873240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com