Use Consul's KV store for Dropwizard settings

I wanted to see how hard it would be to get Dropwizard config from Consul's key value store. Turns out it is very easy to do!

Make sure you're using Dropwizard >= 0.8.0, this has the SubstitutingSourceProvider that we're going to use.

Create a default Dropwizard project with the following configuration line in its config.yml:

someSetting: ${settings/someSetting}  

Don't forget to add it to the xxxConfiguration:

    private String someSetting;

Now in the xxxApplication class, register a new ConsulKVSubstitutor in the initialize method:

    public void initialize(Bootstrap<ConsulConfigExampleConfiguration> bootstrap) {
                new SubstitutingSourceProvider(
                        new ConsulKVSubstitutor(false)

The setting with path settings/someSetting while be looked up in the KV store and will be replaced in the config.yml, after which the app will resume startup.

Lookup is done using Ecwid's Consul API Java bindings.

Note that this does not register changes made to the settings in the KV store. This could be added by using a watch but it doesn't look like that is currently supported in this Java lib.

Github repo with example project.

comments powered by Disqus