【问题标题】:Finagle Send request to random server within clusterFinagle 向集群内的随机服务器发送请求
【发布时间】:2015-11-14 08:37:39
【问题描述】:

我使用finagle 作为rest 客户端。在ClientBuilder 中,我指定了主机范围,但请求需要使用主机设置 url。 如何避免在请求中指定主机并让 finagle 选择一个? 谢谢。

    val client = ClientBuilder().hosts("host1:81,host2:82").codec(Http()).build()

    val request = RequestBuilder()
        // .url("http://host1/get") // dont want to specify host
        // .url("/get") // MalformedURLException: no protocol
        .buildGet()

    var resp = client(request) // sent to host specified by url

【问题讨论】:

    标签: finagle twitter-finagle


    【解决方案1】:

    您似乎正在使用finagle-http 模块。使用 RequestBuilder 在 URL 中构建没有主机的请求是不可能的。不过,您可以手动构造Request(或创建自己的 RequestBuilder 以供进一步使用)

    不过,我建议切换到 finagle-httpx 模块 (https://github.com/twitter/finagle/tree/develop/finagle-httpx)。它与finagle-http 不兼容,但它有很多API 改进,并且能够在其中创建没有URL 中的主机的请求,例如:

    val client = Httpx.client.withTls("my.api")
      .newService("host1.my.api:443,host2.my.api:443")
    
    val req = Request("/get")
    
    val rep = client(req)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 2017-04-21
      相关资源
      最近更新 更多