Java动态解析域名
Java動態(tài)解析域名
Java提供InetAddress類,可以對域名-IP進(jìn)行正向、逆向解析。
InetAddress解析的時候一般是調(diào)用系統(tǒng)自帶的DNS程序。
linux 默認(rèn)的DNS方式是讀取/etc/resolv.conf進(jìn)行DNS解析。
mac 默認(rèn)的方式是向網(wǎng)關(guān)請求獲取DNS服務(wù)器,然后直接請求DNS服務(wù)器進(jìn)行解析,沒有讀取/etc/resolv.conf。
?
我的業(yè)務(wù)是根據(jù)不同的DNS分別解析域名,因此需要動態(tài)的設(shè)置DNS。
JNDI DNS服務(wù)提供者設(shè)置官方文檔
JNDI DNS service provider settings
These properties may not be supported in future releases.
sun.net.spi.nameservice.provider.<n>=<default|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.
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 domainfor (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,在不同地區(qū)擁有不同的IP;因此使用不同的DNS服務(wù)器進(jìn)行解析,得到的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
?
轉(zhuǎn)載于:https://www.cnblogs.com/vincent-vg/p/7908063.html
總結(jié)
以上是生活随笔為你收集整理的Java动态解析域名的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Selenium2之Web自动化编写AP
- 下一篇: 跟风