来自Java的未知主机异常,但主机使用Ping/nslookup/curl解决

0aydgbwb  于 2022-11-08  发布在  Java
关注(0)|答案(2)|浏览(237)

We are getting the following exception when a Java EE server tries to resolve a host name from one of the services. We were able to resolve that host name on the box manually with ping/nslookup command. We were also able to fire that external service using curl with same parameters what server is trying to use. So it seems like this issue happens only when we try to make a request to a external host from a Java/Java EE application.
To resolve this issue, we manually added an entry in /etc/hosts file and it solved the problem but we want a permanent solution so we don't have to keep changing the IP. It was working before without adding that entry but all of a sudden it stopped working.
Detail of our environment:
OS: Cent OS 6.3
Java : JDK 1.6.0_24
Java EE Server : JBoss AS 7.1.1
Http Client library : httpclient-4.0.jar
Thanks in advance for the help!!
Error while checking salesforce license|: java.net.UnknownHostException: na15.salesforce.com at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:175) [rt.jar:1.6.0_24] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384) [rt.jar:1.6.0_24] at java.net.Socket.connect(Socket.java:546) [rt.jar:1.6.0_24] at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:584) [jsse.jar:1.6.0_24] at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:333) [httpclient-4.0.jar:4.0] at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:123) [httpclient-4.0.jar:4.0] at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:147) [httpclient-4.0.jar:4.0] at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:101) [httpclient-4.0.jar:4.0] at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:381) [httpclient-4.0.jar:4.0] at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641) [httpclient-4.0.jar:4.0] at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576) [httpclient-4.0.jar:4.0]

nlejzf6q

nlejzf6q1#

我发现当你的网络禁用IPv6时,同样的症状也会发生。你可以通过运行一个简单的Java程序来看到这一点:

import java.net.InetAddress;
public class NameTest
{
  public static void main(String[] args) throws Exception
  {
    System.out.println(InetAddress.getByName("google.com"));
  }
}

如果IPv6是一个问题,则使用java -cp . NameTest运行程序将失败,并在堆栈跟踪中显示类似“at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)”的行(请注意类名中的Inet6),而使用java -cp . -Djava.net.preferIPv4Stack=true NameTest运行程序将成功(这来自the above answer Java网络属性参考)。
为了克服这一点,我遵循了this question的建议。
在操作系统范围内禁用IPv6可以很好地工作,因为系统没有IPv6。在某些Linux变体上,可以通过首先将此行添加到文件/etc/sysctl.conf的末尾来禁用IPv6

net.ipv6.conf.all.disable_ipv6 = 1

然后运行sudo sysctl -p以立即禁用IPv6。

bwitn5fc

bwitn5fc2#

这很可能是网络管理、变更/配置管理或一般系统管理问题,而不是编程问题。
它以前在没有添加那个条目的情况下工作,但突然停止了工作。
一种方法是尝试找出是什么改变导致它停止工作。这不是那种会自发发生的事情。有人或某物改变了一些软件或配置,使之发生。从它停止(或你注意到)的时间开始,并通过你的更改日志返回。
这听起来像是Java DNS解析过程的问题。解析器要么没有尝试与正常的DNS服务器通信,要么尝试了但失败了。
我们不能为您调试它,但我找到了这些资源,它们有助于解释在Java中进行名称查找时应该发生什么,并提供了一些关于故障排除的想法。
资源:

相关问题