scalarestfinagletwitter-finagle

Twitter Finagle client: How to make external REST api call?


I'm trying to make an external (to finagle server) REST GET request in my finagle code, the URI for is: http://service.site-dev.com/subservices/list

I'm using the client code found in the example at : https://twitter.github.io/scala_school/finagle.html#client

My code (written in Scala) looks like the following, but the it just hangs even though I set a timeout limit:

val client: Service[HttpRequest, HttpResponse] = ClientBuilder()
  .codec(Http())
  .hosts("http://service.site-dev.com") // If >1 host, client does simple load-balancing
  .hostConnectionLimit(1)
  .tcpConnectTimeout(1.second)
  .requestTimeout(20.seconds)
  .retries(2)
  .build()

val req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://service.site-dev.com/subservices/list")

val f = client(req) // Client, send the request

// Handle the response:
f onSuccess { res =>
  println("got response", res)
} onFailure { exc =>
  println("failed :-(", exc)
}

I suspect my hosts param is wrong? But what am I suppose to put in there is this is an call to an external REST service?


Solution

  • The string argument to hosts isn't a URI, but instead should have the form "host:port" (or "host1:port1,host2:port" for a set of hosts), so changing that line to .hosts("service.site-dev.com:80") should fix the issue.

    I'm a little surprised that you're not seeing a NumberFormatException—what version of Finagle are you using?