Can I have multiple configuration files in DropWizard?

喜你入骨 提交于 2019-12-03 08:10:59

问题


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?


回答1:


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);
    }
}



回答2:


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.




回答3:


Firstly, you will write another yml file path in a .yml.

sample.yml

configPath: /another.yml

another.yml

greet: Hello!

and you will be solved by simply using the SnakeYaml.

public void run(SampleConfiguration configuration, Environment environment) {
    Yaml yaml = new Yaml();
    InputStream in = getClass().getResourceAsStream(configuration.getConfigPath());
    AnotherConfig anotherConfig = yaml.loadAs(in, AnotherConfig.class);
    String str = anotherConfig.getGreet(); // Hello!
...
}

For sensitive information, I think it is good to use the environment variable.

For example, use dropwizard-environment-config
https://github.com/tkrille/dropwizard-environment-config



来源:https://stackoverflow.com/questions/28727224/can-i-have-multiple-configuration-files-in-dropwizard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!