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 + "\t" + domain + "\t" + ip); queue.add(DNS + "\t" + domain + "\t" + 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