Giter Site home page Giter Site logo

kstafford3 / dropwizard-discovery Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dropwizard/dropwizard-discovery

0.0 2.0 0.0 1.54 MB

Service discovery for Dropwizard using Curator

Home Page: https://dropwizard.github.io/dropwizard-discovery/1.3.0-1/

License: Apache License 2.0

Shell 0.93% Java 99.07%

dropwizard-discovery's Introduction

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>1.3.10</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.

dropwizard-discovery's People

Contributors

jplock avatar kstafford3 avatar dependabot[bot] avatar theo avatar joschi avatar dependabot-support avatar mchiodo avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.