【问题标题】:Twitter Finagle client: How to make external REST api call?Twitter Finagle 客户端:如何进行外部 REST api 调用?
【发布时间】:2014-10-31 03:47:48
【问题描述】:

我正在尝试在我的 finagle 代码中发出一个外部(到 finagle 服务器)REST GET 请求,其 URI 是:http://service.site-dev.com/subservices/list

我正在使用示例中的客户端代码: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)
}

我怀疑我的 hosts 参数有误?但是我想在其中添加什么,这是对外部 REST 服务的调用?

【问题讨论】:

  • 你应该使用 Future.Await 这样你的主线程就不会死掉或完成。

标签: scala rest finagle twitter-finagle


【解决方案1】:

hosts 的字符串参数不是 URI,而是应该具有 "host:port" 的形式(或 "host1:port1,host2:port" 用于一组主机),因此将该行更改为 .hosts("service.site-dev.com:80") 应该可以解决问题。

我有点惊讶你没有看到NumberFormatException——你使用的是什么版本的 Finagle?

【讨论】:

    【解决方案2】:

    您可能想检查一下 https://github.com/opower/finagle-resteasy

    它是基于 Finagle 的替代 Resteasy 客户端执行器的插件。

    【讨论】:

      猜你喜欢
      • 2022-12-18
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 2013-12-15
      • 2016-04-19
      相关资源
      最近更新 更多