Skip to content

dropwizard/dropwizard-discovery

 
 

Repository files navigation

Dropwizard Discovery

Build Status Coverage Status Maven Central GitHub license

dropwizard-discovery is a Dropwizard bundle that can be used to register a Dropwizard service into Zookeeper upon startup. Connectivity to Zookeeper is provided by Netflix's Curator library and its built in Service Discovery framework. This code was originally open sourced by General Electric as snowizard-discovery under a BSD license.

Usage

In your Dropwizard Configuration file, add a property to represent the discovery configuration for your service:

# Discovery-related settings.
discovery:
    serviceName: hello-world

And have your configuration class expose the DiscoveryFactory:

public class HelloWorldConfiguration extends Configuration {

    @Valid
    @NotNull
    private DiscoveryFactory discovery = new DiscoveryFactory();

    @JsonProperty("discovery")
    public DiscoveryFactory getDiscoveryFactory() {
        return discovery;
    }

    @JsonProperty("discovery")
    public void setDiscoveryFactory(DiscoveryFactory discoveryFactory) {
        this.discovery = discoveryFactory;
    }
}

If you only wish to have your service register itself with Zookeeper and you don't intend on consuming any other services, you just need to add the following into the Application#initialize method:

public class HelloWorldApplication extends Application<HelloWorldConfiguration> {

    private final DiscoveryBundle<HelloWorldConfiguration> discoveryBundle = new DiscoveryBundle<HelloWorldConfiguration>() {
        @Override
        public DiscoveryFactory getDiscoveryFactory(HelloWorldConfiguration configuration) {
            return configuration.getDiscoveryFactory();
        }

    };

    @Override
    public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
        bootstrap.addBundle(discoveryBundle);
    }
}

where HelloWorldConfiguration is your configuration class name.

If you want to also consume other services, you can store an instance of the DiscoveryBundle so that you can retrieve a new DiscoveryClient to access additional services.

public class HelloWorldApplication extends Application<HelloWorldConfiguration> {

    private final DiscoveryBundle<HelloWorldConfiguration> discoveryBundle = new DiscoveryBundle<HelloWorldConfiguration>() {
        @Override
        public DiscoveryFactory getDiscoveryFactory(HelloWorldConfiguration configuration) {
            return configuration.getDiscoveryFactory();
        }
    };

    @Override
    public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
        bootstrap.addBundle(discoveryBundle);
    }

    @Override
    public void run(HelloWorldConfiguration configuration, Environment environment) throws Exception {
        final DiscoveryClient client = discoveryBundle.newDiscoveryClient("other-service");
        environment.lifecycle().manage(new DiscoveryClientManager(client));
    }
}

Be sure to register the DiscoveryClient using the DiscoveryClientManager as a Managed Object so that it is properly started and shutdown when your service is stopped and started.

Maven Artifacts

This project is available on Maven Central. To add it to your project simply add the following dependencies to your pom.xml:

<dependency>
  <groupId>io.dropwizard.modules</groupId>
  <artifactId>dropwizard-discovery</artifactId>
  <version>2.0.0</version>
</dependency>

Enhancements

  1. Support an "Advertise Locally, Lookup Globally" model that Camille Fournier outlined on her blog by supporting separate Zookeeper connections, one that connects locally and one that connects to a global instance.

Support

Please file bug reports and feature requests in GitHub issues.

License

Copyright (c) 2018 Justin Plock

This library is licensed under the Apache License, Version 2.0.

See http://www.apache.org/licenses/LICENSE-2.0.html or the LICENSE file in this repository for the full license text.