tomcat 9 webapp的inetaddress.getbyname不工作(java.net.unknownhostexception)

gz5pxeao  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(426)

我在tomcat9上部署了一个.war webapp(以前在tomcat6上运行良好)。
在我的.war webapp中,我有一个对inetaddress.getbyname的调用,这导致了一个未知的hostexception。但是,我知道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上的连接器缺少enablelookups=“true”属性,但添加它没有任何效果:

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

有趣的是,如果我将有问题的主机添加到/etc/hosts,查找就会成功——但是我想弄清楚发生了什么,并允许我的webapp实际使用dns。
我在centos 7上运行openjdk-1.8。
编辑:我应该补充一点,tcpdump显示,当tomcat webapp失败时,端口53上没有流量-看起来它甚至没有尝试使用dns。

ux6nzvsh

ux6nzvsh1#

很自然,我在发布问题后就发现了问题:
我的tomcat用户没有读取/etc/resolv.conf的适当权限

sudo chmod a+r /etc/resolv.conf

修好了。。。

相关问题