consul

consul

Consul client module.

Exports a Consul client.

Also register the application as a service and setup a health check.

dependency

<dependency>
  <groupId>org.jooby</groupId>
  <artifactId>jooby-consul</artifactId>
  <version>1.6.6</version>
</dependency>

usage

{
  use(new Consulby());
  
  get("/myservice/health", req -> {
    Consul consul = require(Consul.class);
    List<ServiceHealth> serviceHealths = consul.healthClient()
      .getHealthyServiceInstances("myservice")
      .getResponse();
    return serviceHealths;
  });
}

configuration

Configuration is done via .conf.

For example, one can change the consul endpoint url, change the advertised service host, and disable registration health check:

consul.default.url = "http://consul.internal.domain.com:8500"
consul.default.register.host = 10.0.0.2
consul.default.register.check = null

or, disable the automatic registration feature completely:

consul.default.register = null

Also, Consul and Registration objects can be configured programmatically:

{
  use(new Consulby()
      .withConsulBuilder(consulBuilder -> {
        consulBuilder.withPing(false);
        consulBuilder.withBasicAuth("admin", "changeme");
      })
      .withRegistrationBuilder(registrationBuilder -> {
        registrationBuilder.enableTagOverride(true);
        registrationBuilder.id("custom-service-id");
      }));
}

multiple consul

The module can be instantiated more than one time to allow connecting to many Consul installations:

{
  use(new Consulby("consul1"));
  use(new Consulby("consul2"));
}

Since the module will fallback on the consul.default config prefix, it is possible to only override the desired properties in the .conf, for example, here, disabling health check only for consul2:

consul.consul1.url = "http://consul1.internal.domain.com:8500"

consul.consul2.url = "http://consul2.internal.domain.com:8500"
consul.consul2.register.check = null

consul.conf

consul {
  default {
    url = "http://localhost:8500"
    register {
      name = ${application.name}
      host = ${application.host}
      port = ${application.port}
      tags = []
      check {
        path = /health
        response = ${application.name}-${application.version}
        interval = 15s
        timeout = 5s
      }
    }
  }
}