Java Shell
Clone or download
Latest commit 4ca2fc2 Jul 14, 2018

README.md

Dropwizard Consul Bundle

Build Status Maven Central GitHub license Become a Patron

A bundle for using Consul in Dropwizard applications. Features:

  • Integrated client-side load balancer based on Ribbon
  • Dropwizard health check that monitors reachablility of Consul
  • The Dropwizard service is registered as a Consul service with a Consul-side health check querying the Dropwizard health check
  • Ability to resolve configuration properties from Consul's KV store
  • Admin task to toggle Consul's maintenance mode

Dependency Info

<dependency>
    <groupId>com.smoketurner.dropwizard</groupId>
    <artifactId>consul-core</artifactId>
    <version>1.3.5-1</version>
</dependency>
<dependency>
    <groupId>com.smoketurner.dropwizard</groupId>
    <artifactId>consul-ribbon</artifactId>
    <version>1.3.5-1</version>
</dependency>

Usage

Add a ConsulBundle to your Application class.

@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
    // ...
    bootstrap.addBundle(new ConsulBundle<MyConfiguration>(getName()) {
        @Override
        public ConsulFactory getConsulFactory(MyConfiguration configuration) {
            return configuration.getConsulFactory();
        }
    });
}

The bundle also includes a ConsulSubsitutor to retrieve configuration values from the Consul KV store. You can define settings in your YAML configuration file:

template: ${helloworld/template:-Hello, %s!}
defaultName: ${helloworld/defaultName:-Stranger}

The setting with the path helloworld/template will be looked up in the KV store and will be replaced in the configuration file when the application is started. You can specify a default value after the :-. This currently does not support dynamically updating values in a running Dropwizard application.

Configuration

For configuring the Consul connection, there is a ConsulFactory:

consul:
  # Optional properties
  # endpoint for consul (defaults to localhost:8500)
  endpoint: localhost:8500
  # service port
  servicePort: 8080
  # check interval frequency
  checkInterval: 1 second

Example Application

This bundle includes a modified version of the HelloWorldApplication from Dropwizard's Getting Started documentation.

You can execute this application by first starting Consul on your local machine then running:

mvn clean package
java -jar consul-example/target/consul-example-1.3.5-2-SNAPSHOT.jar server consul-example/hello-world.yml

This will start the application on port 8080 (admin port 8180). This application demonstrations the following Consul integration points:

  • The application is registered as a service with Consul (with the service port set to the applicationConnectors port in the configuration file.
  • The application will lookup any variables in the configuration file from Consul upon startup (it defaults to connecting to a Consul agent running on localhost:8500 for this functionality)
  • The application exposes an additional HTTP endpoint for querying Consul for available healthy services:
curl -X GET localhost:8080/consul/helloworld -i
HTTP/1.1 200 OK
Date: Mon, 25 Jan 2016 03:42:10 GMT
Content-Type: application/json
Vary: Accept-Encoding
Content-Length: 591

[{
    "Node": {
        "Node": "mac",
        "Address": "192.168.1.100"
    },
    "Service": {
        "ID": "e16fc421-f430-4361-9bee-8a700a61c06f",
        "Service": "helloworld",
        "Tags": [],
        "Address": "",
        "Port": 8080
    },
    "Checks": [{
        "Node": "mac",
        "CheckID": "serfHealth",
        "Name": "Serf Health Status",
        "Status": "passing",
        "Notes": "",
        "Output": "Agent alive and reachable",
        "ServiceID": "",
        "ServiceName": ""
    }, {
        "Node": "mac",
        "CheckID": "service:e16fc421-f430-4361-9bee-8a700a61c06f",
        "Name": "Service 'helloworld' check",
        "Status": "passing",
        "Notes": "",
        "Output": "",
        "ServiceID": "e16fc421-f430-4361-9bee-8a700a61c06f",
        "ServiceName": "helloworld"
    }]
}]
  • The application will periodically checkin with Consul every second to notify the service check that it is still alive
  • Upon shutdown, the application will deregister itself from Consul

Credits

This bundle was inspired by an older bundle (Dropwizard 0.6.2) that Chris Gray created at https://github.com/chrisgray/dropwizard-consul. I also incorporated the configuration provider changes from https://github.com/remmelt/dropwizard-consul-config-provider

Support

Please file bug reports and feature requests in GitHub issues.

License

Copyright (c) 2018 Smoke Turner, LLC

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.