如何在Java中获取主机名的规范名称? [英] How to get the canonical name of a hostname in Java?

查看:151
本文介绍了如何在Java中获取主机名的规范名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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;
属性attrs = idc.getAttributes(host,CNAME_ATTRIBS);
属性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(); (int i = 0; i< count; i ++){
sb.append( - >+ attr.get(i)+\\\
);

}

抛出新的NamingException(找不到+主机+的CNAME时出现意外的计数,预期为1.+ count +.\\\

+ sb.toString());
}
}

return cname;
}
}

这个类被不同的线程使用。但是,这个类生成的结果在两个线程调用之间略有不同。



例如,同一天我得到这些结果:

 < Date> <&小时GT; <线程名称> <主机> <规范名称> 
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 。净。

为什么要使用 time.microsoft.akadns获得最终的规范名称。网络



有时第二次通话可以得到如下这样的多个规范名称:

  qq.com.edgesuite.net。 
a1574.b.akamai.net。

为什么在两个电话之间有如此不同的结果?为什么每个电话没有一个CNAME?

解决方案

也许主机正在使用DNS解析作为一种负载平衡?例如,请查看亚马逊的官方文档:



http://aws.amazon.com/en/route53/faqs/



它涉及到WRR和弹性负载平衡。也许您的数据与一些负载平衡混合,根据负载考虑,根据不同的后端解决您的请求,这就是为什么您得到不同的答案


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 ?

解决方案

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

这篇关于如何在Java中获取主机名的规范名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆