Java – override server connector configuration with env variable with dropwizard

I have posted this question on the DW mailing list, but I have no answer

I can assume that the following YML format is no longer applicable to DW 0.7 0? (insert env VaR with @ char)

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

Error:

So I decided to use this format:

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

And try to override it with the JVM option:

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 variable:

OPENSHIFT_DIY_IP=localhost
OPENSHIFT_DIY_PORT=8080

The error I got from this setting:

What on earth did I do wrong?

Solution

From dropwizard version 0.8 Starting with 0, you can access environment variables from a configuration YML file It also supports setting default values in case environment variables are unavailable

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: in order for it to work, you need to set up a substitutingsourceprovider using the environmentvariablesubstitutor

// Enable variable substitution with environment variables
bootstrap.setConfigurationSourceProvider(
    new SubstitutingSourceProvider(
        bootstrap.getConfigurationSourceProvider(),new EnvironmentVariableSubstitutor())
);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>