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?
Because you can't be a web application without HTTP, Dropwizard uses the Jetty HTTP library to embed an incredibly tuned HTTP server directly into your project.
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.
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.
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));
}
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With