dropwizard: read configuration from a non-file source

荒凉一梦 提交于 2019-12-23 10:23:31

问题


What's the right way to read configuration in dropwizard from something like a database, or a REST call? I have a use case where I cannot have a yml file with some values, and should retrieve settings/config at startup time from a preconfigured URL with REST calls.

Is it right to just invoke these REST calls in the get methods of the ApplicationConfiguration class?


回答1:


Similar to my answer here, you implement the ConfigurationSourceProvider interface the way you wish to implement and configure your dropwizard application to use it on your Application class by:

@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap){
  bootstrap.setConfigurationSourceProvider(new MyDatabaseConfigurationSourceProvider());
}

By default, the InputStream you return is read as YAML and mapped to the Configuration object. The default implementation

You can override this via

bootstrap.setConfigurationFactoryFactory(new MyDatabaseConfigurationFactoryFactory<>());

Then you have your FactoryFactory :) that returns a Factory which reads the InputStream and returns your Configuration.

public T build(ConfigurationSourceProvider provider, String path {
  Decode.onWhateverFormatYouWish(provider.open(path));
}



回答2:


elaborating a bit further on Nathan's reply, you might want to consider using the UrlConfigurationSourceProvider , which is also provided with dropwizard, and allows to retrieve the configuration from an URL.

Something like:

@Override
public void initialize(Bootstrap<MyRestApplicationConfiguration> bootstrap) {
    bootstrap.setConfigurationSourceProvider(new UrlConfigurationSourceProvider());
}


来源:https://stackoverflow.com/questions/35292840/dropwizard-read-configuration-from-a-non-file-source

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