【问题标题】:Tomcat 9 webapp's InetAddress.getByName not working (java.net.UnknownHostException)Tomcat 9 webapp 的 InetAddress.getByName 不工作 (java.net.UnknownHostException)
【发布时间】:2023-09-20 21:46:01
【问题描述】:

我在 tomcat9 上部署了一个 .war webapp(之前在 tomcat6 上运行良好)。

在我的 .war webapp 中,我调用了 InetAddress.getByName,这会导致 UnknownHostException。但是,我知道 dns 正在系统中工作(ping、dig 和 nslookup 都可以正常工作)

java.net.UnknownHostException: google.com: Name or service not known
    at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_242]
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:929) ~[na:1.8.0_242]
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324) ~[na:1.8.0_242]
    at java.net.InetAddress.getAllByName0(InetAddress.java:1277) ~[na:1.8.0_242]
    at java.net.InetAddress.getAllByName(InetAddress.java:1193) ~[na:1.8.0_242]
    at java.net.InetAddress.getAllByName(InetAddress.java:1127) ~[na:1.8.0_242]
    at java.net.InetAddress.getByName(InetAddress.java:1077) ~[na:1.8.0_242]

最初我以为是 server.xml 上的 Connector 中缺少 enableLookups="true" 属性,但添加它没有效果:

    <Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           enableLookups="true" />

有趣的是,如果我将相关主机添加到 /etc/hosts,则查找成功 - 但是我想弄清楚发生了什么并允许我的 webapp 实际使用 DNS。

我在 CentOS 7 上,运行 openjdk-1.8。

编辑:我应该补充一点,当 tomcat webapp 失败时,tcpdump 显示端口 53 上没有流量 - 它似乎甚至没有尝试使用 DNS。

【问题讨论】:

    标签: java tomcat centos7 tomcat9


    【解决方案1】:

    我发布问题后自然就发现了问题:

    我的 tomcat 用户没有正确的权限来读取 /etc/resolv.conf

    sudo chmod a+r /etc/resolv.conf
    

    修复它...

    【讨论】:

      最近更新 更多