zoukankan      html  css  js  c++  java
  • Java动态解析域名

    Java动态解析域名

    Java提供InetAddress类,可以对域名-IP进行正向、逆向解析。

    InetAddress解析的时候一般是调用系统自带的DNS程序。

      linux 默认的DNS方式是读取/etc/resolv.conf进行DNS解析。

      mac 默认的方式是向网关请求获取DNS服务器,然后直接请求DNS服务器进行解析,没有读取/etc/resolv.conf。

    我的业务是根据不同的DNS分别解析域名,因此需要动态的设置DNS。

    JNDI DNS服务提供者设置官方文档

    JNDI DNS service provider settings

    These properties may not be supported in future releases.

      sun.net.spi.nameservice.provider.<n>=<default|dns,sun|...>
    Specifies the name service provider that you can use. By default, Java will use the system configured name lookup mechanism, such as file, nis, etc. You can specify your own by setting this option. <n> takes the value of a positive number, it indicates the precedence order with a small number takes higher precendence over a bigger number. Aside from the default provider, the JDK includes a DNS provider named "dns,sun".

    Prior to JDK 7, the first provider that was successfully loaded was used. In JDK 7, providers are chained, which means that if a lookup on a provider fails, the next provider in the list is consulted to resolve the name.


      sun.net.spi.nameservice.nameservers=<server1_ipaddr,server2_ipaddr ...>
    You can specify a comma separated list of IP addresses that point to the DNS servers you want to use. If the sun.net.spi.nameservice.nameservers property is not defined, then the provider will use any name servers already configured in the platform DNS configuration

    sun.net.spi.nameservice.provider.<n>=<default|dns,sun|...> 用于设置域名服务提供者
    = default的时候调用系统自带的DNS
    = dns,sun的时候,会调用sun.net.spi.nameservice.nameservers=<server1_ipaddr,server2_ipaddr ...>指定的DNS来解析
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.util.concurrent.ConcurrentLinkedQueue;
    
    import org.apache.log4j.Logger;
    
    public class ResolveDomain {
        
        private Logger log = Logger.getLogger(ResolveDomain.class);
        
        public static void main(String[] args) throws UnknownHostException {  
            ResolveDomain rd = new ResolveDomain();
            rd.resolveDomain("www.baidu.com", "114.114.114.114", new ConcurrentLinkedQueue<String>());
        }  

       public void resolveDomain(String domain, String DNS, ConcurrentLinkedQueue<String> queue){ System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun"); System.setProperty("sun.net.spi.nameservice.nameservers", DNS); InetAddress[] addresses; try { addresses = InetAddress.getAllByName(domain); //IP or domain for (int i = 0; i < addresses.length; i++) { String ip = addresses[i].getHostAddress(); log.info(DNS + " " + domain + " " + ip); queue.add(DNS + " " + domain + " " + ip); } } catch (UnknownHostException e) { e.printStackTrace(); } } }

    ps:对于有些域名,例如www.baidu.com,在不同地区拥有不同的IP;因此使用不同的DNS服务器进行解析,得到的IP一般也不一样。


    参考博客: http://blog.chinaunix.net/uid-192452-id-3981087.html

          http://www.jianshu.com/p/f10808ae4b60

          https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html

          https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html

          https://www.cnblogs.com/549294286/p/5307316.html

          http://blog.csdn.net/mofenglian/article/details/74344631

  • 相关阅读:
    PHP $_POST 变量
    PHP $_GET 变量
    PHP 完整表单实例
    PHP 表单
    PHP 表单
    PHP 表单验证
    00_前情回顾
    18_今日回顾
    VMware 12PRO安装Mac OS X 10.10.5
    05_传智播客iOS视频教程_第一个OC程序
  • 原文地址:https://www.cnblogs.com/vincent-vg/p/7908063.html
Copyright © 2011-2022 走看看