Zookeeper ipv6的坑

问题

zookeeper抛这样的错误:

 [main-SendThread(localhost:2000)] ERROR org.apache.zookeeper.ClientCnxn  - Unable to open socket to localhost/0:0:0:0:0:0:0:1:2181
4549 [main-SendThread(localhost:2000)] WARN  org.apache.zookeeper.ClientCnxn  - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.SocketException: Address family not supported by protocol family: connect
    at sun.nio.ch.Net.connect(Native Method)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:532)
    at org.apache.zookeeper.ClientCnxn$SendThread.startConnect(ClientCnxn.java:1071)
    at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1104)

注意到

localhost/0:0:0:0:0:0:0:1

localhost被翻译成了0:0:0:0:0:0:0:1。

打开/etc/hosts,可以看到127.0.0.1和::1都有localhost的配置:
这里写图片描述

显然在打开ipv6的情况下,localhost的值被后者覆盖了,所以zk抛错了。

解决

两个简单的解决方案:
1. 关闭ipv6。
2. 在hosts里,把::1对应的localhost改个别名,比如localhost-ipv6。

全文完 :)

版权声明:本文为weixin_33998125原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_33998125/article/details/90686643