Skip to content

Instantly share code, notes, and snippets.

@demisgomes
Last active November 6, 2019 17:14
Show Gist options
  • Save demisgomes/77a9ea99a7b4444efddbb7b5f1819c27 to your computer and use it in GitHub Desktop.
Save demisgomes/77a9ea99a7b4444efddbb7b5f1819c27 to your computer and use it in GitHub Desktop.
Example with Javalin + Consul using consul-client library
var consulValue = "default"
fun main() {
setupConsul()
val app = Javalin.create().start(7000)
app.get("/") { ctx -> ctx.result("Hello World $consulValue") }
}
fun setupConsul(){
val client = Consul.builder().withHostAndPort(HostAndPort.fromParts("localhost", 8500)).build()
val kvClient = client.keyValueClient()
val cache = KVCache.newCache(kvClient, "config/application/",60)
cache.addListener { newValues ->
val newValue = newValues.values.stream()
.filter { value -> value.key == "config/application/key" }
.findAny()
newValue.ifPresent { value ->
val decodedValue = newValue.get().valueAsString
decodedValue.ifPresent { v -> println(String.format("Value is: %s", v)) }
consulValue = decodedValue.get()
}
}
cache.start()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment