etcd4s is scala etcd client library. It support etcd-v3 API with
akka-streams. In this post I’m gonna build etcd client application with using
cast-effects IO monad stack. All the source codes which related to this post available in gitlab. Please clone the repo and continue the post.
First I need to run etcd key value pair storage. I have run etcd with
docker. Following is way to run etcd.
I have used
IntelliJ Idea as my IDE to work with Scala applications. I need to create
sbt project and add the
build.sbt dependency file with
cats-effects and other dependencies. Following is the
build.sbt dependency file.
I have used
cats-effects IO monads to build this application. To load etcd the configurations with IO monads I have used
circe-config library. Following is the way to parse configurations into
I have used etcd to store microservice health statuses. etcd keys which corresponds to services starting with
services/ prefix and follow
services/<service-name> format. Assume I need to create a service named
octopus with health status
running. When creating this service, it will add
services/octopus: running key/value pair in etcd storage.
I have created
createService add a new service status in etcd.
updateService updates the existing service status in etcd.
getService returns the services status in etcd.
getServices returns all service statuses in etcd.
deleteService delete service status in etcd.
EtcdRepoImpl is the
cats-effects based implementation of
etcd4s library provides
Future based API to deal with etcd storage. All the functions in
EtcdRepoImpl wraps with
IO.fromFuture to handle the
Future API calls.
Finally I have run the functions on
etcdRepo, which wraps with
IO monad. To unwrap and run the side-effecting code in the
IO monad I need to invoke
unsafeRunSync(). Following are the different functions that I have run and their outputs.
We can view the values stored in etcd by using
etcdctl command line client. In this example I have used etcd-v3 API. Following is the way to interact with etcd-v3 API by using