Jdk8 DNS解析

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

InetAddress解析的时候一般是调用系统自带的DNS程序。

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

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

DNSNameService 根据sun.net.spi.nameservice.nameservers指定的name server或/etc/resolv.conf文件中配置的name server进行DNS解析

根据不同的DNS分别解析域名,因此需要动态的设置DNS。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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来解析

参考文章

评论