Dirk Hillbrecht
Dirk Hillbrecht

Reputation: 703

Java 11/17 HttpClient - How to try several IPs behind one hostname?

How can I configure Java's own "new" (Java 9/11/17) HttpClient to try more than the first found IP adress behind a hostname? There are (at least) two scenarios:

  1. A server with IPv4 and IPv6 support:
> host localhost
localhost has address 127.0.0.1
localhost has IPv6 address ::1
  1. One "logical" hostname where the service is delivered by multiple redundant servers:
> host frontend.example.com
frontend.example.com has address 245.3.4.5
frontend.example.com has address 246.4.5.6
frontend.example.com has IPv6 address 4f00:5678::19
frontend.example.com has IPv6 address 4f00:7890::20

My observation is that HttpClient only tries the first delivered address and fails if the service is not reachable under that address. Is there a way to tell it to try all assigned IP adresses - InetAddress.getAllByName() deliveres them - until a HTTP server can be found at one of them and fails only with "no connection" if none of them succeeds.

In the first case, this is relevant if a certain service only binds to either the IPv4 or IPv6 address. In the second case, one of the servers or services could be down for maintenance or due to technical problems and the other one serves all incoming requests. So, in my optinion this should be a fairly common requirement for a HTTP client implementation. Still, I have not understood how to archieve this behaviour.

Upvotes: 5

Views: 591

Answers (0)

Related Questions