dubbo-samples-spring-boot 启动失败,报错:
2022-09-18 22:20:19.982 WARN 6344 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn : Session 0x0 for server 127.0.0.1/<unresolved>:2181, unexpected error, closing socket connection and attempting reconnect
java.lang.IllegalArgumentException: Unable to canonicalize address 127.0.0.1/<unresolved>:2181 because it's not resolvable
at org.apache.zookeeper.SaslServerPrincipal.getServerPrincipal(SaslServerPrincipal.java:65) ~[zookeeper-3.4.14.jar:3.4.14-4c25d480e66aadd371de8bd2fd8da255ac140bcf]
at org.apache.zookeeper.SaslServerPrincipal.getServerPrincipal(SaslServerPrincipal.java:41) ~[zookeeper-3.4.14.jar:3.4.14-4c25d480e66aadd371de8bd2fd8da255ac140bcf]
at org.apache.zookeeper.ClientCnxn$SendThread.startConnect(ClientCnxn.java:1001) ~[zookeeper-3.4.14.jar:3.4.14-4c25d480e66aadd371de8bd2fd8da255ac140bcf]
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1060) ~[zookeeper-3.4.14.jar:3.4.14-4c25d480e66aadd371de8bd2fd8da255ac140bcf]
解决问题:jdk版本问题,换成jdk8就行了。
问题原因分析
看Zookeeper类的构造函数,这里用的是 ConnectStringParser 类来解析地址
ConnectStringParser里面又是用的又是 InetSocketAddress.createUnresolved(host, port)这个方法
InetSocketAddress.createUnresolved(host, port)
这里没什么问题,问题在于InetSocketAddress的toString方法,这个方法,在jdk8和jdk17有差异,在jdk17中,如果传入的addr为空就会拼一个/<unresolved>上去。。。。
// jdk8
private boolean isUnresolved() {
return addr == null;
}
@Override
public String toString() {
if (isUnresolved()) {
return hostname + ":" + port;
} else {
return addr.toString() + ":" + port;
}
}
// jdk17
private boolean isUnresolved() {
return addr == null;
}
@Override
public String toString() {
String formatted;
if (isUnresolved()) {
formatted = hostname + "/<unresolved>";
} else {
formatted = addr.toString();
if (addr instanceof Inet6Address) {
int i = formatted.lastIndexOf("/");
formatted = formatted.substring(0, i + 1)
+ "[" + formatted.substring(i + 1) + "]";
}
}
return formatted + ":" + port;
}