Java动态解析域名

Java动态解析域名html

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

InetAddress解析的时候通常是调用系统自带的DNS程序。linux

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

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

 

个人业务是根据不一样的DNS分别解析域名,所以须要动态的设置DNS。oracle

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

JNDI DNS service provider settings

These properties may not be supported in future releases.dom

  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.ide


  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 + "\t" + domain + "\t" + ip); queue.add(DNS + "\t" + domain + "\t" + ip); } } catch (UnknownHostException e) { e.printStackTrace(); } } }

 

ps:对于有些域名,例如www.baidu.com,在不一样地区拥有不一样的IP;所以使用不一样的DNS服务器进行解析,获得的IP通常也不同。 ui


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

      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

相关文章
相关标签/搜索