Last active
November 6, 2019 17:14
-
-
Save demisgomes/77a9ea99a7b4444efddbb7b5f1819c27 to your computer and use it in GitHub Desktop.
Example with Javalin + Consul using consul-client library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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