I want to have several yaml files for DropWizard. One of them contain sensitive info and one non sensitive.
Can you point me to any docs or example how to have multiple configurations in DropWizard?
Creating A Configuration Class Each Dropwizard application has its own subclass of the Configuration class which specifies environment-specific parameters. These parameters are specified in a YAML configuration file which is deserialized to an instance of your application's configuration class and validated.
Dropwizard uses Logback for its logging backend. It provides an slf4j implementation, and even routes all java. util. logging , Log4j, and Apache Commons Logging usage through Logback.
A Dropwizard Bundle is a reusable group of functionality (sometimes provided by the Dropwizard project itself), used to define blocks of an application's behavior.
Dropwizard stores configurations in YAML files. You will need to have the file configuration. yml in your application root folder. This file will then be deserialized to an instance of your application's configuration class and validated.
ConfigurationSourceProvider
is your answer.
bootstrap.setConfigurationSourceProvider(new MyMultipleConfigurationSourceProvider());
The following is how dropwizard does it by default. You can easily change it to your own liking.
public class FileConfigurationSourceProvider implements ConfigurationSourceProvider {
@Override
public InputStream open(String path) throws IOException {
final File file = new File(path);
if (!file.exists()) {
throw new FileNotFoundException("File " + file + " not found");
}
return new FileInputStream(file);
}
}
Ideally you should be configuring your app by putting your sensitive information or configurable data inside Environment Variables, rather than managing multiple files. See twelve factor rule on config: http://12factor.net/config
To enable this approach in Dropwizard, you can either override your config with Environment Variables at run time using the -Ddw
flag:
java -Ddw.http.port=$PORT -jar yourapp.jar server yourconfig.yml
or you can use this handy add on: https://github.com/tkrille/dropwizard-template-config to put Environment Variable placeholders inside your config:
server:
type: simple
connector:
type: http
# replacing environment variables
port: ${env.PORT}
Both of the above solutions are compatible with Heroku and Docker containers, where the Environment Variable are only available when you run the app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With