问题
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