[英]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:
这是工作流程:
setCachePolicyInJVM(0)
in my Java code. setCachePolicyInJVM(0)
。 mytest.com
to 192.168.1. mytest.com
设置为192.168.1。 188 in BIND9, restart. InetAddress.getByName("mytest.com").getHostAddress();
-> 192.168.1. mytest.com
-> 192.168.1. mytest.com
- > 192.168.1。 160 in BIND9, restart. InetAddress.getByName("mytest.com").getHostAddress();
-> 192.168.1. InetAddress.getByName("mytest.com").getHostAddress();
-> 192.168.1. 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缓存 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.