简体   繁体   English

JVM和OS DNS缓存

[英]JVM and OS DNS Caching

I am facing a problem with JVM and DNS. 我正面临着JVM和DNS的问题。

Everything I'm reading (including the docs and this ) says that I can disable JVM DNS caching using networkaddress.cache.ttl , which can be set using java.security.Security.setProperties , but through the standard approach of using system properties. 一切我读(包括该文档 )说,我可以通过禁用JVM DNS缓存networkaddress.cache.ttl ,它可以使用设置java.security.Security.setProperties ,而是通过使用系统属性的标准方法。 I have successfully changed this to 0, so no more caching in my JVM. 我已成功将其更改为0,因此我的JVM中不再有缓存。

But now, on each call of InetAddress.getByName("mytest.com") , it seems that my JVM is using the system DNS cache (in my case Windows 8). 但现在,在每次调用InetAddress.getByName("mytest.com") ,似乎我的JVM正在使用系统 DNS缓存(在我的情况下是Windows 8)。 Indeed, between 2 calls of the method, I have changed the BIND9 properties for "mytest.com", but the IP return is still the same. 实际上,在方法的两次调用之间,我已经更改了“mytest.com”的BIND9属性,但IP返回仍然是相同的。 Here is the workflow: 这是工作流程:

  1. setCachePolicyInJVM(0) in my Java code. 我的Java代码中的setCachePolicyInJVM(0)
  2. set mytest.com to 192.168.1. mytest.com设置为192.168.1。 188 in BIND9, restart. 在BIND9中188 ,重启。
  3. InetAddress.getByName("mytest.com").getHostAddress(); -> 192.168.1. - > 192.168.1。 188 188
  4. set mytest.com -> 192.168.1. 设置mytest.com - > 192.168.1。 160 in BIND9, restart. 在BIND9 160 ,重启。
  5. InetAddress.getByName("mytest.com").getHostAddress(); -> 192.168.1. - > 192.168.1。 188 (should be 160 if there was no caching). 188 (如果没有缓存,则应为160 )。
  6. Flush the Windows DNS 刷新Windows DNS
  7. InetAddress.getByName("mytest.com").getHostAddress(); -> 192.168.1. - > 192.168.1。 160 160

I have read several times that the JVM does not use the system cache, but that is wrong: it clearly does. 我已多次读过JVM不使用系统缓存,但这是错误的:它显然是这样。

How do we force a new DNS resolution on each call, bypassing the OS DNS cache? 我们如何绕过操作系统 DNS缓存强制每次调用新的DNS解析?

I think I've run into this problem, or a very similar one. 我想我遇到过这个问题,或者是一个非常类似的问题。 What I did then was to implement my own DNS provider for the JVM, see how to change the java dns service provider for details. 我当时做的是为JVM实现我自己的DNS提供程序,请参阅如何更改java dns服务提供程序以获取详细信息。 You can use the dnsjava mentioned there or roll your own. 你可以使用那里提到的dnsjava或自己动手。

You can either edit your $JAVA_HOME/jre/lib/security/java.security for Java 6-8 and $JAVA_HOME/conf/security/java.security property file to add the following property . 您可以编辑Java 6-8的$JAVA_HOME/jre/lib/security/java.security$JAVA_HOME/conf/security/java.security属性文件来添加以下属性。

networkaddress.cache.ttl=1

It is not available to set it in command line. 无法在命令行中设置它。

Since these 2 properties are part of the security policy, they are not set by either the -D option or the System.setProperty() API, instead they are set as security properties. 由于这两个属性是安全策略的一部分,因此它们不是由-D选项或System.setProperty()API设置的,而是设置为安全属性。

To set this property inside the code, you can use the following method. 要在代码中设置此属性,可以使用以下方法。

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

Or add the following property in the java command line. 或者在java命令行中添加以下属性。

-Dnetworkaddress.cache.ttl=1

It is also important to note that values are effective only if the corresponding networkaddress.cache.* properties are not set. 同样重要的是要注意,仅当未设置相应的networkaddress.cache.*属性时,值才有效。

See Java 8 Networking Properties , Java 9 Networking Properties and VeriSign DNS Caching in Java Virtual Machines for more details. 有关更多详细信息,请参阅Java 虚拟机中的 Java 8网络属性Java 9网络属性VeriSign DNS缓存

This answer also adds some details. 这个答案也增加了一些细节。

From here it seems you should set sun.net.inetaddr.ttl . 这里看来你应该设置sun.net.inetaddr.ttl This worked for me. 这对我有用。

Example from link: 链接示例:

java -Dsun.net.inetaddr.ttl=1 test
Enter the hostname
rrr
Output isrrr/129.145.146.100
Enter the hostname
rrr
Output isrrr/129.147.146.100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM