java 我可以在 DropWizard 中拥有多个配置文件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28727224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:04:56  来源:igfitidea点击:

Can I have multiple configuration files in DropWizard?

javadropwizard

提问by Victor Ronin

I want to have several yaml files for DropWizard. One of them contain sensitive info and one non sensitive.

我想为 DropWizard 准备几个 yaml 文件。其中一个包含敏感信息,一个不敏感。

Can you point me to any docs or example how to have multiple configurations in DropWizard?

你能指点我任何文档或示例如何在 DropWizard 中进行多个配置吗?

回答by Natan

ConfigurationSourceProvideris your answer.

ConfigurationSourceProvider是你的答案。

bootstrap.setConfigurationSourceProvider(new MyMultipleConfigurationSourceProvider());

The following is how dropwizard does it by default. You can easily change it to your own liking.

以下是dropwizard 默认情况下的执行方式。您可以根据自己的喜好轻松更改它。

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

回答by yunspace

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

理想情况下,您应该通过将敏感信息或可配置数据放入Environment Variables来配置您的应用程序,而不是管理多个文件。请参阅配置上的十二因素规则:http: //12factor.net/config

To enable this approach in Dropwizard, you can either override your config with Environment Variables at run time using the -Ddwflag:

要在 Dropwizard 中启用此方法,您可以使用以下-Ddw标志在运行时使用环境变量覆盖您的配置:

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-configto put Environment Variable placeholders inside your config:

或者您可以使用这个方便的附加组件:https: //github.com/tkrille/dropwizard-template-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.

上述两种解决方案都兼容 Heroku 和 Docker 容器,其中环境变量仅在您运行应用程序时可用。

回答by ko2ic

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

首先,您将在.yml.

sample.yml

样本.yml

configPath: /another.yml

another.yml

另一个.yml

greet: Hello!

and you will be solved by simply using the SnakeYaml.

只需使用 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

例如,使用 dropwizard-environment-config
https://github.com/tkrille/dropwizard-environment-config