Skip to content

ntent/scala-consul

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

scala-consul

Codacy Badge Circle CI Maven Central

An asynchronous Scala (http://scala-lang.org/) client for Consul (https://consul.io/)

on an sbt project add this line to your build.sbt:

libraryDependencies += "com.codacy" %% "scala-consul" % "1.1.0"

import Consul

import consul.Consul

instanciate a consul supplying an ip and a port indicating a working consul agent:

val myConsul = new consul.Consul(CONSUL_IP, CONSUL_PORT)
import myConsul.v1._

now you can use the consul API as indicated in the official documentation (http://www.consul.io/docs/agent/http.html)

Example - add a tuple to the Key/Value store:

kv.put("myKey","myValue")

Example - query the registered nodes:

catalog.nodes().map{ case nodes => 
    //do something with my nodes
}

Example - register a service with an http-check on the local node:

val myServicePort = 5000
val myServiceCheck = agent.service.httpCheck(s"http://localhost:$myServicePort/health","15s")
val myService = agent.service.LocalService(ServiceId("myServiceId"),ServiceType("myTypeOfService"),Set(ServiceTag("MyTag")),Some(myServicePort),Some(myServiceCheck))
agent.service.register(myService)

the check ID of the registered service-check is available via:

val myCheckId = myService.checkId

the other 2 types of checks are created with:

agent.service.ttlCheck

and

agent.service.scriptCheck

Error Handling:

All api methods return Futures that can fail. To parse Consul responses Play's Json library is used. In the unlikely case that the client cannot parse the response the Future will fail and you might want to access the JsError parsing resulted in. You can do so by recovering the Future:

import consul.v1.common.Types.ConsulResponseParseException
catalog.nodes().recover{ 
  case ConsulResponseParseException(jsError) =>  //do something with the JsError
  case NonFatal(otherException) => //something else
}

Licence

This software is licensed under the Apache 2 license: http://www.apache.org/licenses/LICENSE-2.0

Copyright (C) 2014 Codacy (https://www.codacy.com)

About

Consul API for scala

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Scala 100.0%