Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have multiple configuration files in DropWizard?

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?

like image 887
Victor Ronin Avatar asked Feb 25 '15 18:02

Victor Ronin


People also ask

What is configuration class 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.

Does Dropwizard use Log4j?

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.

What is a Dropwizard bundle?

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.

What language does the Dropwizard framework use to create the configuration file?

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.


2 Answers

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);
    }
}
like image 80
Natan Avatar answered Oct 21 '22 23:10

Natan


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.

like image 32
yunspace Avatar answered Oct 21 '22 23:10

yunspace