问题
I have build a small helper class for DNS resolving:
public class DNSService {
private static Properties env;
private static final String CNAME_ATTRIB = "CNAME";
private static String[] CNAME_ATTRIBS = { CNAME_ATTRIB };
static {
env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
}
public static String getCNAME(String host) throws NamingException {
return getCNAME(new InitialDirContext(env), host);
}
private static String getCNAME(InitialDirContext idc, String host) throws NamingException {
String cname = host;
Attributes attrs = idc.getAttributes(host, CNAME_ATTRIBS);
Attribute attr = attrs.get(CNAME_ATTRIB);
if (attr != null) {
int count = attr.size();
if (count == 1) {
cname = getCNAME(idc, (String) attr.get(0));
} else {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
sb.append("-> " + attr.get(i) + "\n");
}
throw new NamingException("Unexpected count while looking for CNAME of " + host + ". Expected 1. Got " + count + ".\n"
+ sb.toString());
}
}
return cname;
}
}
This class is used by different threads. However, the results produced by this class are slightly different between two thread calls.
For instance, the same day I get those results:
<Date> <Hour> <Thread Name> <Host> <Canonical Name>
02/12/2012 09:51 thread-1 www.site.com www.site.com
02/12/2012 12:06 thread-2 www.site.com time.microsoft.akadns.net.
Why do I get a final canonical name with time.microsoft.akadns.net.
?
Sometimes the second call can get multiple canonical names like this:
qq.com.edgesuite.net.
a1574.b.akamai.net.
Why do I have so different results between two calls? Why there is not ONE single CNAME at each call ?
回答1:
Maybe the hosts are using DNS resolving as a kind of load balance? For example, look at this official documentation from amazon:
http://aws.amazon.com/en/route53/faqs/
Where it talks about WRR and Elastic Load Balance. Maybe your data get mingled with some load balancing that resolve your requests against different backends depending on load considerations, and that is why you get different answers
来源:https://stackoverflow.com/questions/20394696/how-to-get-the-canonical-name-of-a-hostname-in-java