MyWebConfiguration.java Haben Sie den folgenden Code als Drop-Assistent beschriebenAusgabe in übergeordnete Konfiguration mit env Variablen in dropwizard
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 Datei
tokenSecret: ${TOKEN_SECRET}
aber wenn ich die Anwendung ausführen und Debug, um zu überprüfen, welchen Wert ich für tokenSecret bekomme, zeigt es mir tokenSecret = "$ {TOKEN_SECRET}" in der Debug-Konsole.
versuchte ich MyWebConfiguration.java wie folgt zu ändern: -
bootstrap.setConfigurationSourceProvider(
new SubstitutingSourceProvider(
bootstrap.getConfigurationSourceProvider(),
new EnvironmentVariableSubstitutor(true)// changed false to true
)
);
Aber jetzt, wenn ich versuche, das Programm auszuführen, es zeigt mir die Fehler folgende
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)
mir jemand sagen kann, wo ich könnte schief gegangen sein?
Wo legen Sie die tatsächliche env-Variable fest. Schreiben Sie in Ihrer Klasse ein main und tun Sie: System.out.println (System.getenv ("TOKEN_SECRET")); und sehen, ob die Ausgabe nicht null ist. Laufen Sie in Finsternis? – pandaadb
ja, ich betreibe es in Eclipse. Wenn ich es durch Terminal laufen lasse, funktioniert es. –
Ihre Eclipse-Umgebung kennt Ihre Umgebungsvariablen nicht. Sie müssen in Ihre Laufkonfiguration gehen und die Variablen dort setzen – pandaadb