A native Scala client for interacting with Consul built on top of sttp-client.
Currently consul4s
supports these endpoints:
- agent
- catalog
- coordinate
- event
- health
- kv
- prepared queries (only existing query execution)
- session
- status
- transaction
Consul4s uses multi-project structure and contains of the following modules_:
consul4s-core
- core classes/functionsconsul4s-circe
- circe integrationconsul4s-json4s
- json4s integrationconsul4s-spray-json
- spray-json integration
consul4s is published for Scala 2.13 and 2.12 to Maven Central, so just add the following to your build.sbt:
libraryDependencies ++= Seq(
"com.nryanov.consul4s" %% "consul4s-core" % "[version]",
// And one of the following:
"com.nryanov.consul4s" %% "consul4s-circe" % "[version]",
"com.nryanov.consul4s" %% "consul4s-json4s" % "[version]",
"com.nryanov.consul4s" %% "consul4s-spray-json" % "[version]",
)
Also you can add any sttp-client backend you want.
Without any additional dependencies you can use the default HttpURLConnectionBackend
as backend for requests. But sttp also supports other backend implementations:
STTP: Backend implementations include ones based on akka-http, async-http-client, http4s, OkHttp, and HTTP clients which ship with Java. They integrate with Akka, Monix, fs2, cats-effect, scalaz and ZIO.
import consul4s.v1._
import consul4s.circe._
import sttp.client.HttpURLConnectionBackend
object Main {
def main(args: Array[String]): Unit = {
val backend = HttpURLConnectionBackend()
val client = ConsulClient(backend) // will use default host and port: http://localhost:8500
for {
datacenters <- client.getDatacenters().body
} yield datacenters
}
}