Skip to content

Latest commit

 

History

History
80 lines (73 loc) · 2.4 KB

README.md

File metadata and controls

80 lines (73 loc) · 2.4 KB

Consul extension for Ktor

Download Build Status GitHub License

This module allows to automatically register the application at the Consul and integrate Ktor HTTP clients with Consul to discovery hosts.

Quick start

Maven

<repositories>
    <repository>
      <id>jcenter</id>
      <name>jcenter</name>
      <url>http://jcenter.bintray.com</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>net.paslavsky</groupId>
        <artifactId>ktor-consul</artifactId>
        <version>${exktor.version}</version>
    </dependency>
</dependencies>

Gradle

repositories {
  jcenter()
}

dependencies {
  implementation "net.paslavsky:ktor-consul:$exktorVersion"
}

Registering application

fun Application.module() {
    install(ConsulFeature) {
        serviceName = "MyService" // by default - "application" or ktor.application.id from config file
        host = "my-host.com" // by default - "localhost" or connector.host
        port = 80 // by default - 80 or ktor.deployment.port from config file
        consulUrl = "http://192.168.99.100:8500"
        config { // this: Consul.Builder
            // ...
        }
        registrationConfig { // this: ImmutableRegistration.Builder
            // ...
            // Health check example:
            // check(Registration.RegCheck.http("$host:$port/health", 120))
        }
    }
}

Consul client

val client = HttpClient(Apache) {
    install(ConsulClientFeature) {
        consulUrl = "http://192.168.99.100:8500"
        serviceName = "MyService"
        loadBalancer { // this: List<ServiceHealth>
            // Your implementation:
            // Return one of the ServiceHealth
            // by default:
            getOrNull(0)
        }
        // Also, one more load balancer implementation available:
        loadBalancer(roundRobin())
        config { // this: Consul.Builder
            // ...
        }
    }
    install(JsonFeature)
}