Environment variables para Dropwizard

Si estas usando dropwizard puedes usar el link oficial http://www.dropwizard.io/0.8.5/docs/manual/core.html, pero si quieres usar variables de entorno en tu config.yml sigue estos pasos

Paso 1.. Cambia los parámetros que quieres que sean variables de entorno por ${variable}
Actual
config.yml
database:
  driverClass: com.microsoft.sqlserver.jdbc.SQLServerDriver
  user: db_admin
  password: db_admin
  url: jdbc:sqlserver://localhost:1433;database=java-client;encrypt=false;trustServerCertificate=false;loginTimeout=30;
  properties: {}
  maxWaitForConnection: 1s
  validationQuery: "/* MyService Health Check */ SELECT 1"

Con variables de entorno
config.yml
database:
  driverClass: com.microsoft.sqlserver.jdbc.SQLServerDriver
  user: ${DB_USER}
  password: ${DB_PASSWORD}
  url: ${DB_URL}
  properties: {}
  maxWaitForConnection: 1s
  validationQuery: "/* MyService Health Check */ SELECT 1"


Paso2. Modifica tu clase
public class TestDropwizardService extends Application<TestDropwizardConfiguration>
{

.......................
@Override
    public void initialize(Bootstrap<TestDropwizardConfiguration> bootstrap)
    {
        bootstrap.setConfigurationSourceProvider(
        new SubstitutingSourceProvider(
           bootstrap.getConfigurationSourceProvider(),
             new EnvironmentVariableSubstitutor()
            )
        );
        ObjectMapper objectMapper = bootstrap.getObjectMapper();
        objectMapper.setSerializationInclusion(Include.NON_NULL);
        objectMapper.disable(SerializationFeature.INDENT_OUTPUT);
    }
..........................
}

Y debes importar
import io.dropwizard.configuration.EnvironmentVariableSubstitutor;
import io.dropwizard.configuration.SubstitutingSourceProvider;


Paso3.  Corre tu app
SET DB_URL=jdbc:sqlserver://localhost:1433;database=java-client;encrypt=false;trustServerCertificate=false;loginTimeout=30;
SET DB_USER=db_admin
SET DB_PASSWORD=db_admin
gradlew run config.yml


Si no especificas una de las variables tendras una Exception al momento de correr

Comentarios