日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java动态解析域名

發(fā)布時間:2024/8/26 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java动态解析域名 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Java動態(tài)解析域名

Java提供InetAddress類,可以對域名-IP進行正向、逆向解析。

InetAddress解析的時候一般是調(diào)用系統(tǒng)自帶的DNS程序。

  linux 默認(rèn)的DNS方式是讀取/etc/resolv.conf進行DNS解析。

  mac 默認(rèn)的方式是向網(wǎng)關(guān)請求獲取DNS服務(wù)器,然后直接請求DNS服務(wù)器進行解析,沒有讀取/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|...>
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|...> 用于設(shè)置域名服務(wù)提供者
= default的時候調(diào)用系統(tǒng)自帶的DNS
= dns,sun的時候,會調(diào)用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 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ù)器進行解析,得到的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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。