Finagle Send request to random server within cluster

194 views Asked by At

I am using finagle as rest client. In ClientBuilder I specify a range of hosts, but the request requires setting a url with host. How can I avoid specifying host in the request and let finagle choose one ? Thanks.

    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
1

There are 1 answers

0
Wildfire On BEST ANSWER

It looks like you're using finagle-http module. It's not possible to build request without host in URL using RequestBuilder. Still, you can construct Request manually (or create your own RequestBuilder for further use)

I'd recommend however switching to finagle-httpx module (https://github.com/twitter/finagle/tree/develop/finagle-httpx). It's not compatible with finagle-http, but it has lots of API improvements and the ability to create requests without host in URL in among them, for example:

val client = Httpx.client.withTls("my.api")
  .newService("host1.my.api:443,host2.my.api:443")

val req = Request("/get")

val rep = client(req)