Twitter Finagle客户端:如何进行外部REST API调用?

问题描述:

我试图让外部(以欺骗服务器)REST得到我的欺骗代码的要求,对URI是:http://service.site-dev.com/subservices/listTwitter Finagle客户端:如何进行外部REST API调用?

我使用在例子中,客户端代码:https://twitter.github.io/scala_school/finagle.html#client

我(Scala编写的)代码如下所示,但它只是挂起即使我设置了超时限制:

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) 
} 

我怀疑我的主机参数是错误的?但是我想在这里放置什么?这是对外部REST服务的调用?

+0

您应该使用Future.Await,以便您的主线程不会死亡或完成。 – Falmarri 2014-10-31 22:09:14

的字符串参数hosts不是URI,而是应该有形式"host:port"(或"host1:port1,host2:port"一组主机),所以改变该行.hosts("service.site-dev.com:80")应该解决这个问题。

我有点惊讶,你没有看到NumberFormatException-你正在使用Finagle的版本?

你可能要检查这个 https://github.com/opower/finagle-resteasy

这是一个简易的基于欺骗,替代RestEasy的的客户端执行。