dropwizard-consul

Dropwizard Consul Bundle

License

License

Categories

Categories

DropWizard Container Microservices
GroupId

GroupId

com.smoketurner.dropwizard
ArtifactId

ArtifactId

dropwizard-consul
Last Version

Last Version

2.0.12-1
Release Date

Release Date

Type

Type

pom
Description

Description

dropwizard-consul
Dropwizard Consul Bundle
Project URL

Project URL

https://github.com/smoketurner/dropwizard-consul
Source Code Management

Source Code Management

https://github.com/smoketurner/dropwizard-consul

Download dropwizard-consul

How to add to project

<!-- https://jarcasting.com/artifacts/com.smoketurner.dropwizard/dropwizard-consul/ -->
<dependency>
    <groupId>com.smoketurner.dropwizard</groupId>
    <artifactId>dropwizard-consul</artifactId>
    <version>2.0.12-1</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/com.smoketurner.dropwizard/dropwizard-consul/
implementation 'com.smoketurner.dropwizard:dropwizard-consul:2.0.12-1'
// https://jarcasting.com/artifacts/com.smoketurner.dropwizard/dropwizard-consul/
implementation ("com.smoketurner.dropwizard:dropwizard-consul:2.0.12-1")
'com.smoketurner.dropwizard:dropwizard-consul:pom:2.0.12-1'
<dependency org="com.smoketurner.dropwizard" name="dropwizard-consul" rev="2.0.12-1">
  <artifact name="dropwizard-consul" type="pom" />
</dependency>
@Grapes(
@Grab(group='com.smoketurner.dropwizard', module='dropwizard-consul', version='2.0.12-1')
)
libraryDependencies += "com.smoketurner.dropwizard" % "dropwizard-consul" % "2.0.12-1"
[com.smoketurner.dropwizard/dropwizard-consul "2.0.12-1"]

Dependencies

test (6)

Group / Artifact Type Version
io.dropwizard : dropwizard-testing jar
org.junit.jupiter : junit-jupiter jar 5.6.2
org.assertj : assertj-core jar 3.16.1
org.mockito : mockito-core jar 3.5.2
org.openjdk.jmh : jmh-core jar 1.25
org.openjdk.jmh : jmh-generator-annprocess jar 1.25

Project Modules

  • consul-core
  • consul-ribbon
  • consul-example

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>2.0.7-1</version>
</dependency>
<dependency>
    <groupId>com.smoketurner.dropwizard</groupId>
    <artifactId>consul-ribbon</artifactId>
    <version>2.0.7-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-2.0.7-4-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/hello-world -i
HTTP/1.1 200 OK
Date: Mon, 25 Jan 2016 03:42:10 GMT
Content-Type: application/json
Vary: Accept-Encoding
Content-Length: 870

[
    {
        "Node": {
            "Node": "mac",
            "Address": "192.168.1.100",
            "Datacenter": "dc1",
            "TaggedAddresses": {
                "wan": "192.168.1.100",
                "lan": "192.168.1.100"
            },
            "Meta": {
                "consul-network-segment": ""
            }
        },
        "Service": {
            "ID": "test123",
            "Service": "hello-world",
            "EnableTagOverride": false,
            "Tags": [],
            "Address": "",
            "Meta": {
                "scheme": "http"
            },
            "Port": 8080,
            "Weights": {
                "Passing": 1,
                "Warning": 1
            }
        },
        "Checks": [
            {
                "Node": "mac",
                "CheckID": "serfHealth",
                "Name": "Serf Health Status",
                "Status": "passing",
                "Notes": "",
                "Output": "Agent alive and reachable",
                "ServiceID": "",
                "ServiceName": "",
                "ServiceTags": []
            },
            {
                "Node": "mac",
                "CheckID": "service:test123",
                "Name": "Service 'hello-world' check",
                "Status": "passing",
                "Notes": "",
                "Output": "HTTP GET http:\/\/127.0.0.1:8180\/healthcheck: 200 OK Output: {\"consul\":{\"healthy\":true},\"deadlocks\":{\"healthy\":true}}",
                "ServiceID": "test123",
                "ServiceName": "hello-world",
                "ServiceTags": []
            }
        ]
    }
]
  • 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) 2020 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.

com.smoketurner.dropwizard

Smoke Turner, LLC

Versions

Version
2.0.12-1
2.0.7-1
2.0.0-1
1.3.15-1
1.3.14-1
1.3.12-3
1.3.12-1
1.3.10-1
1.3.8-1
1.3.7-2
1.3.7-1
1.3.5-3
1.3.5-2
1.3.5-1
1.3.4-1
1.3.0-1
1.2.4-2
1.2.4-1
1.2.3-2
1.2.3-1
1.2.2-1
1.2.0-1
1.1.3-1
1.1.1-1
1.1.0-3
1.1.0-2
1.1.0-1
1.0.6-1
1.0.5-3
1.0.5-2
1.0.5-1
1.0.3-3
1.0.3-2
1.0.3-1
1.0.2-2
1.0.2-1
1.0.1-1
1.0.0-4
1.0.0-3
1.0.0-2
1.0.0-1
0.9.3-2
0.9.3-1
0.9.2-8
0.9.2-7
0.9.2-6
0.9.2-5
0.9.2-4
0.9.2-3
0.9.2-2
0.9.2-1