Mangat Rai Modi
Mangat Rai Modi

Reputation: 5706

Java: Get Ip Adress of a Server behind DNS Load Balancer

I am connecting to a server from a client using hostname as below:-

    HttpPost post = new HttpPost(serverUrl);
    post.setEntity(new StringEntity(jsonRequestString, ContentType.APPLICATION_JSON));
    HttpResponse response = httpClient.execute(post);
    int ret = response.getStatusLine().getStatusCode();

I am using org.apache.http.* packages. Now the server is behind the DNS load balancer with 8 unique ip addresses bound to the hostname. But I believe that all the requests in a single run of the JVM are going to the same IP address.

  1. Is there a way to print the actual IP returned after DNS resolution?
  2. Does JVM does local DNS caching?

Edit:

Setting networkaddress.cache.ttl and networkaddress.cache.negative.ttl as 0 isn't working. Either I am not setting them correct.

public static void main(String[] args) throws Exception {
    java.security.Security.setProperty("networkaddress.cache.ttl", "0"); // no
    // cache
   java.security.Security.setProperty("networkaddress.cache.negative.ttl", "0"); // no
    while (true) {
        System.out.println(InetAddress.getByName("google.com"));
        Thread.sleep(100);
    }

}

Output:

google.com/216.58.197.78
google.com/216.58.197.78
google.com/216.58.197.78
google.com/216.58.197.78
google.com/216.58.197.78
google.com/216.58.197.78
google.com/216.58.197.78
google.com/216.58.197.78
google.com/216.58.197.78
google.com/216.58.197.78
google.com/216.58.197.78
........
........

Upvotes: 3

Views: 1758

Answers (1)

rkosegi
rkosegi

Reputation: 14678

Is there a way to print the actual IP returned after DNS resolution?

You can try to configure logger and check log messages from "org.apache.http.impl.conn.HttpClientConnectionOperator"

Reference

Does JVM does local DNS caching?

It depends on configuration, but most probably yes, check these settings:

networkaddress.cache.ttl Specified in java.security to indicate the caching policy for successful name lookups from the name service.. The value is specified as integer to indicate the number of seconds to cache the successful lookup.

A value of -1 indicates "cache forever". The default behavior is to cache forever when a security manager is installed, and to cache for an implementation specific period of time, when a security manager is not installed.

networkaddress.cache.negative.ttl (default: 10) Specified in java.security to indicate the caching policy for un-successful name lookups from the name service.. The value is specified as integer to indicate the number of seconds to cache the failure for un-successful lookups.

A value of 0 indicates "never cache". A value of -1 indicates "cache forever".

Reference

UPDATE

Did you try to call API directly to resolve hostname to all IP addresses?

InetAddress.getAllByName(host)

From javadocs:

Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system.

Also interesting article on this topic (not tried by myself)

Upvotes: 2

Related Questions