阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Java内部DNS查询实现和参数设置

165次阅读
没有评论

共计 4495 个字符,预计需要花费 12 分钟才能阅读完成。

一、Java 内部 DNS 查询

Java 使用域名查询时,用的自己内部的域名实现机制,最后都是交给 InetAddress 去做 DNS 解析。

源码分析参考:http://www.linuxidc.com/Linux/2016-07/133180.htm

// 域名查询 
String dottedQuadIpAddress = InetAddress.getByName("blog.arganzheng.me" ).getHostAddress();

//IP 对应域名 
InetAddress[] addresses = InetAddress.getAllByName("8.8.8.8"); // ip or DNS name
for (int i = 0; i < addresses.length; i++) {String hostname = addresses[i].getHostName();
    System.out.println(hostname);
} 

二、JNDI DNS 服务提供者设置 (JNDI DNS service provider settings)

http://docs.Oracle.com/javase/7/docs/technotes/guides/net/properties.html

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.

重点是这个参数,区分 jdk 版本,jdk7 之前,只有第一个设置的 provier 生效;jdk7 及其之后,provider 链都生效,从第一个开始,指导解析成功。

Java 有两个实现:

Default:相当于设置 System.setProperty(“sun.net.spi.nameservice.provider.1”, “default”); 具体解析过程是系统调用,依赖系统的 DNS 解析方式。

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

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

<dns,sun>:System.setProperty(“sun.net.spi.nameservice.provider.1”, “dns,sun”); 读取 /etc/resolv.conf 进行 DNS 解析。


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.domain=<domainname>This property specifies the default DNS domain name, for instance, eng.example.com. If the sun.net.spi.nameservice.domain property is not defined then the provider will use any domain or domain search list configured in the platform DNS configuration.

 

使用 dnsjava 的 provider:

1. 工程添加 dnsjava 包。

2. 设置 provider:System.setProperty(“sun.net.spi.nameservice.provider.1″,”dns,dnsjava”);

dnsjava 的 provider 功能强大:

There's no standard way to determine what the local nameserver or DNS search
path is at runtime from within the JVM.  dnsjava attempts several methods
until one succeeds.

 - The properties 'dns.server' and 'dns.search' (comma delimited lists) are
   checked.  The servers can either be IP addresses or hostnames (which are
   resolved using Java's built in DNS support).
 - The sun.net.dns.ResolverConfiguration class is queried.
 - On Unix, /etc/resolv.conf is parsed.
 - On Windows, ipconfig/winipcfg is called and its output parsed.  This may
   fail for non-English versions on Windows.
 - As a last resort, "localhost" is used as the nameserver, and the search
   path is empty.

参考:

http://www.xbill.org/dnsjava/dnsjava-current/README

http://stackoverflow.com/questions/5668058/how-to-change-the-java-dns-service-provider

三、JVM DNS 缓存

如果启动了 security manager,则永久缓存,但一般情况下大家是不会去启动 security manager 的。

可以再程序里面设置不缓存,或者在启动参数里面设置

java.security.Security.setProperty("networkaddress.cache.ttl" , "0")

如果没有启动 security manager,则要区分 JDK 版本:

1.5 及其一下,java 对 DNS 解析 IP 进行缓存,默认缓存超时时间为 -1(在重启 JVM 前永久缓存)

1.6 及其以上,缓存时间根据 ttl。

参考:

http://docs.oracle.com/javase/1.5.0/docs/guide/net/properties.html

http://docs.oracle.com/javase/7/docs/technotes/guides/net/properties.html

设置 ttl:在命令启动 JVM 的时候设置参数 ”-Dnetworkaddress.cache.ttl=60 -Dsun.net.inetaddr.ttl=60″

四、Linux 服务器是否会对 dns 缓存,ttl 是否有用?

linux 本身是没有 dns 缓存的, 想使用 dns 缓存的话需要自己安装一个服务程序 NSCD.

$ ps aux | grep nscd 可以查看

相关问题:http://www.linuxidc.com/Linux/2016-07/133181.htm

五、nginx 自己实现了 dns resolver,并且会对 dns 缓存

六、ping 未知域名的全过程 (依赖于操作系统)

主机 A,B(可不再同一网段),主机 B 有域名假设为 www.baidu.com
首先:1. 本地主机 A 在命令行下执行 ”ipconfig/flushdns” 命令来清空本地 DNS 高速缓存;
      2. 本地主机 A 在命令行下执行 ”arp -d” 命令来清空 arp 缓存

然后,主机 A 执行 ping www.baidu.com(即主机 B 的域名)

在此过程中都发生了那些报文交互?

思路:

1. 要执行 ping 命令主机 A 必须将域名转化为 IP 地址,故而一定会有 DNS 解析过程;
      2. 在 DNS 解析之前,主机 A 一定要知道自己默认网关的 MAC 地址,这就要涉及到 ARP 解析的问题;
      3.ping 命令本身是 ICMP 回显请求,故而肯定要有 ICMP 协议的回显请求交互。

以下是全过程:

(此处可参照“跨网段的 ping 过程”来看,此处假设 DNS 服务器和主机 A 不在同一网段,若二者在同一网段那么我想只需进行简单 arp 就可得到 DNS 服务器的 mac 不需经过网关)
1. 主机 A 发送 ARP 请求报文目的 mac 为 FFFFFF-FFFFFF,目的 IP 为网关的 IP,要求获得网关的 MAC 地址;
2. 路由器(主机 A 的默认网关)发送目的 mac 为 A 的 mac,目的 IP 为 A 的 IP 的 ARP 回答报文,以告知 A 网关的 mac 地址;
3.A 获得网关的 mac 地址后,就向网关发送一个 DNS 查询报文,其目的 mac 地址为网关的 mac 地址,目的 IP 为 DNS 服务器的 IP 地址;
4. 网关收到 DNS 查询报文后,拆包检查发现是 DNS 查询于是将相应(查询)信息封装,向 DNS 服务器发送该报文,其目的 IP 地址为 DNS 服务器的 IP,目的 mac 为下一跳的 mac,解析域名 IP 地址此时就交给了 DNS 服务器;
5. 经过 DNS 解析,主机 A 知道了所要 ping 的域名的 ip 地址;
6. 剩下的 ping 过程就和 ping 一个特定的 ip 地址相同了,首先判断 ping 命令的目的 B 的 IP 地址是否和 A 在同一网段,若在同一网段则相当于同网段内 ping,若不在同一网段,就是不同网段的 ping 只不过此时主机不需要再解析网关的 mac 地址了。

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-07/133182.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计4495字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中