【发布时间】:2016-02-21 04:08:13
【问题描述】:
我已经为 DNS 解析构建了一个小帮助类:
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;
}
}
这个类被不同的线程使用。但是,这个类产生的结果在两个线程调用之间略有不同。
例如,在我得到这些结果的同一天:
<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.
为什么我得到一个带有 time.microsoft.akadns.net. 的最终规范名称?
有时第二次调用可以获得多个这样的规范名称:
qq.com.edgesuite.net.
a1574.b.akamai.net.
为什么两次调用之间的结果如此不同?为什么每次调用都没有一个 CNAME?
【问题讨论】:
-
尝试使用其他 DNS 服务器添加,看看是否有问题,您可以添加 Google 的公共 DNS 服务器,添加:
env.put(Context.PROVIDER_URL, "dns://8.8.8.8");请记住,任何非本地 DNS 服务器可能会更慢。