Skip to content

Latest commit

 

History

History
81 lines (74 loc) · 2.5 KB

README.md

File metadata and controls

81 lines (74 loc) · 2.5 KB

Consul extension for Ktor

Version Java CI with Gradle 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>exktor</id>
        <url>https://maven.pkg.github.com/paslavsky/exktor</url>
    </repository>
</repositories>

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

Gradle

repositories {
    maven {
        url = uri("https://maven.pkg.github.com/paslavsky/exktor")
    }
}

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)
}