简体   繁体   English

奇怪的未知主机异常

[英]Odd unknown host exception

I have a server that is experiencing some wierd exceptions. 我的服务器遇到一些奇怪的异常。 The server is calling a backend service and during "normal" operation this works without fault, however if that service goes down for a short timespan my server completely dies. 服务器正在调用后端服务,并且在“正常”操作期间可以正常运行,但是,如果该服务在短时间内出现故障,则我的服务器将完全关闭。 After the backend service has restarted I am getting unknown host exceptions for all my http calls. 后端服务重新启动后,我的所有http调用都收到未知的主机异常。 If I restart my server it works again, but it seems odd that I even get the error in the first place. 如果我重新启动服务器,它可以再次运行,但是我什至首先遇到错误似乎很奇怪。

An example of a call I make: 我拨打电话的示例:

        Client client = ClientBuilder.newClient();

        response = client.target(uri)
                .request(MediaType.APPLICATION_JSON_TYPE)
                .header("XApiKey", catalogApiKey)
                .get();

and the stacktrace is rather standard: 并且stacktrace是相当标准的:

java.net.UnknownHostException.
      at java.net.InetAddress.getAllByName0(InetAddress.java:1280)
      at java.net.InetAddress.getAllByName(InetAddress.java:1192)
      at java.net.InetAddress.getAllByName(InetAddress.java:1126)
      at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
      at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:259)
      at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:159)
      at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
      at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
      at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
      at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:882)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
      at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:283)
      at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:436)
      at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.get(ClientInvocationBuilder.java:159)

if it is relevant I am running on a wildfly server. 如果相关,我正在Wildfly服务器上运行。

What could cause this kind of behaviour? 什么会导致这种行为?

If you look at the source of InetAddress (eg http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/net/InetAddress.java ) everywhere that UnknownHostException is instantiated it is instantiated with either a host string or a clearly non-empty string as the constructor parameter. 如果您查看InetAddress的来源(例如, http : UnknownHostException ), UnknownHostException到处都是UnknownHostException实例化时,使用host字符串或明显非空的字符串作为构造函数参数进行实例化。

However, the exception in the stacktrace appears to have an empty message . 但是,stacktrace中的异常似乎有一个空message So I infer that the code is doing a lookup on a hostname that is an empty string. 因此,我推断代码正在对一个空字符串的主机名进行查找。

I would add some code to print the value of uri when this problem occurs. 发生此问题时,我将添加一些代码以打印uri的值。 I suspect that is (sporadically) bogus in some interesting way. 我怀疑这在某种程度上是虚假的。

Java caches DNS entries. Java缓存DNS条目。 The default is "forever" as defined in $JAVA_HOME/jre/lib/security/java.security under the entry networkaddress.cache.ttl . 默认值为“永久”,如$JAVA_HOME/jre/lib/security/java.security中条目networkaddress.cache.ttl下的定义。

I would encourage you to consider reducing the "positive" cache to a small amount, perhaps 10 seconds or so. 我鼓励您考虑将“正数”缓存减少到少量,大概10秒左右。 You can modify the java.security file or have code like: 您可以修改java.security文件或使用类似以下的代码:

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

In your code. 在您的代码中。 There is also a "negative" cache that caches entries that have failed. 还有一个“负”缓存,用于缓存失败的条目。 It may take some experimentation in your environment with the positive and negative caches to find the right setting. 在您的环境中,可能需要对正负缓存进行一些试验才能找到正确的设置。

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

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