Java 使用 dropwizard 使用环境变量覆盖服务器连接器配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23464451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:09:42  来源:igfitidea点击:

Overriding server connector config with env variables with dropwizard

javadropwizard

提问by StephenNYC

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

我已经在 dw 邮件列表上发布了这个问题,但没有得到答案。

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

我可以假设下面的 YML 格式不再适用于 DW 0.7.0 吗?(使用@char 插入env var)

server:
  applicationConnectors:
    - type: http
      bindHost: @OPENSHIFT_DIY_IP@
      port: @OPENSHIFT_DIY_PORT@

Error:

错误:

Malformed YAML at line: 28, column: 17; while scanning for the next token; found character @ '@' that cannot start any token. (Do not use @ for indentation); in 'reader', line 28, column 17: bindHost: @OPENSHIFT_DIY_IP@

格式错误的 YAML 第 28 行,第 17 列;在扫描下一个令牌时;发现无法开始任何标记的字符@'@'。(不要使用@ 缩进);在“读者”中,第 28 行,第 17 列:bindHost:@OPENSHIFT_DIY_IP@

So I decided to use this format:

所以我决定使用这种格式:

server:
  type: simple
  applicationContextPath: /
  adminContextPath: /admin
  connector:
      type: http
      bindHost: localhost
      port: 8080

And tried to override it via jvm options:

并尝试通过 jvm 选项覆盖它:

java -Ddw.server.connector.bindHost=$OPENSHIFT_DIY_IP -Ddw.server.connector.port=$OPENSHIFT_DIY_PORT -jar target/myapp.jar server myapp.yml

My local env variables:

我的本地环境变量:

OPENSHIFT_DIY_IP=localhost
OPENSHIFT_DIY_PORT=8080

The error I got from this setup:

我从这个设置中得到的错误:

Exception in thread "main" java.lang.RuntimeException: java.net.SocketException: Unresolved address at org.eclipse.jetty.setuid.SetUIDListener.lifeCycleStarting(SetUIDListener.java:213) ... Caused by: java.net.SocketException: Unresolved address at sun.nio.ch.Net.translateToSocketException(Net.java:157) ... WARN [2014-05-03 20:11:19,412] org.eclipse.jetty.util.component.AbstractLifeCycle: FAILED org.eclipse.jetty.server.Server@91b85: java.lang.RuntimeException: java.net.SocketException: Unresolved address

线程“main”中的异常 java.lang.RuntimeException: java.net.SocketException: Unresolved address at org.eclipse.jetty.setuid.SetUIDListener.lifeCycleStarting(SetUIDListener.java:213) ... 引起:java.net.SocketException :sun.nio.ch.Net.translateToSocketException(Net.java:157) 处的未解析地址...警告 [2014-05-03 20:11:19,412] org.eclipse.jetty.util.component.AbstractLifeCycle: FAILED org .eclipse.jetty.server.Server@91b85: java.lang.RuntimeException: java.net.SocketException: 未解析地址

What am I doing wrong?

我究竟做错了什么?

采纳答案by StephenNYC

someone created a bundlefor DW to be able to embed env vars

有人为 DW创建了一个,以便能够嵌入 env vars

回答by WarFox

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.

从 Dropwizard 版本0.8.0 开始,您可以从配置 yml 文件访问环境变量。它还支持在环境变量不可用的情况下设置默认值。 请参阅此处的文档。

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 SubstitutingSourceProviderwith an EnvironmentVariableSubstitutor.

重要说明:要使其正常工作,您需要设置一个SubstitutingSourceProvider带有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 UndefinedEnvironmentVariableExceptionif the environment variable is not defined, unless you set strictmode to falseby using new EnvironmentVariableSubstitutor(false)https://github.com/dropwizard/dropwizard/blob/master/dropwizard-configuration/src/main/java/io/dropwizard/configuration/EnvironmentVariableSubstitutor.java

更新:15/Nov/2017 正如@EFreak 在评论部分所提到的,如果未定义环境变量,new EnvironmentVariableSubstitutor()则会抛出异常UndefinedEnvironmentVariableException,除非您使用https://github.com/dropwizard/dropwizard/blob/master/strict模式设置为dropwizard-configuration/src/main/java/io/dropwizard/configuration/EnvironmentVariableSubstitutor.javafalsenew EnvironmentVariableSubstitutor(false)