今天在构建一个基于Gradle的Dubbo应用程序的骨架的时候,碰到了一个奇怪的问题,具体错误如下:

2021-01-20 20:57:13  ClientCnxn - Session 0x0 for server 127.0.0.1/<unresolved>:2181, unexpected error, closing socket connection and attempting reconnect
java.nio.channels.UnresolvedAddressException
	at java.base/sun.nio.ch.Net.checkAddress(Net.java:149)
	at java.base/sun.nio.ch.Net.checkAddress(Net.java:157)
	at java.base/sun.nio.ch.SocketChannelImpl.checkRemote(SocketChannelImpl.java:753)
	at java.base/sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:774)
	at org.apache.zookeeper.ClientCnxnSocketNIO.registerAndConnect(ClientCnxnSocketNIO.java:277)
	at org.apache.zookeeper.ClientCnxnSocketNIO.connect(ClientCnxnSocketNIO.java:287)
	at org.apache.zookeeper.ClientCnxn$SendThread.startConnect(ClientCnxn.java:1021)
	at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1064)

 

网上找了一圈都没找到是什么问题,于是我就自己研究了一下,发现了问题出在了哪里,这里记录一下顺便分享给大家。

我的Zookeeper使用的是3.6.2版本,目前的最新版。而Dubbo使用的也是最新的2.7.8,用到了如下的Dubbo组件,gradle配置中去除了无关的配置,防止其他配置干扰大家:

 version.gradle配置

ext {

    def version = [
            dubbo: "2.7.8"
    ]

    dependency = [
            dubbo                       : "org.apache.dubbo:dubbo:${version.dubbo}",
            dubbo_registry_zk           : "org.apache.dubbo:dubbo-registry-zookeeper:${version.dubbo}",
            dubbo_rpc_dubbo             : "org.apache.dubbo:dubbo-rpc-dubbo:${version.dubbo}",
            dubbo_remoting_netty4       : "org.apache.dubbo:dubbo-remoting-netty4:${version.dubbo}",
            dubbo_serialization_hessian2: "org.apache.dubbo:dubbo-serialization-hessian2:${version.dubbo}",
    ]
}

build.gradle配置

apply from: 'version.gradle'


subprojects {
    apply plugin: 'java'
    sourceCompatibility = '15'
    targetCompatibility = '15'

    dependencies {
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
        implementation this.rootProject.dependency.dubbo
        implementation this.rootProject.dependency.dubbo_rpc_dubbo
        implementation this.rootProject.dependency.dubbo_registry_zk
        implementation this.rootProject.dependency.dubbo_remoting_netty4
        implementation this.rootProject.dependency.dubbo_serialization_hessian2
    }
}

至此,运行的时候就会报上面的错误。我就纳闷了,都是最新版,应该没问题的,之前也用过,唯一不同的就是Zookeeper版本变了,难道是Zookeeper的问题?于是我查看了一下Maven中央仓库和我项目当中dubbo-registry-zookeeper插件里面的zookeeper驱动的版本,果然是出在版本上。dubbo-registry-zookeeper插件默认的zookeeper驱动版本是:3.4.13。而到中央仓库中查询了一下该版本的发行时间,分别如下:

dubbo-registry-zookeeper 默认版本
Maven中央仓库

好吧!我的错,以为用了最新的dubbo版本,就一定会有新版本的zookeeper驱动,结果翻车了。既然找到问题的症结所在那就好解决了,直接排除掉dubbo的zookeeper组件里面的版本,自己引入和zookeeper注册中心版本对应的新版驱动。具体配置如下:

version.gradle

ext {

    def version = [
            dubbo: "2.7.8"
    ]

    dependency = [
            dubbo                       : "org.apache.dubbo:dubbo:${version.dubbo}",
            dubbo_registry_zk           : "org.apache.dubbo:dubbo-registry-zookeeper:${version.dubbo}",
            dubbo_rpc_dubbo             : "org.apache.dubbo:dubbo-rpc-dubbo:${version.dubbo}",
            dubbo_remoting_netty4       : "org.apache.dubbo:dubbo-remoting-netty4:${version.dubbo}",
            dubbo_serialization_hessian2: "org.apache.dubbo:dubbo-serialization-hessian2:${version.dubbo}",
            zookeeper                   : "org.apache.zookeeper:zookeeper:3.6.2",
    ]
}

build.gradle

apply from: 'version.gradle'

subprojects {
    apply plugin: 'java'
    sourceCompatibility = '15'
    targetCompatibility = '15'

    dependencies {
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
        implementation this.rootProject.dependency.dubbo
        implementation (this.rootProject.dependency.dubbo_registry_zk) {
            exclude group: 'org.apache.zookeeper', module: 'zookeeper'
        }
        implementation this.rootProject.dependency.dubbo_rpc_dubbo
        implementation this.rootProject.dependency.dubbo_remoting_netty4
        implementation this.rootProject.dependency.dubbo_serialization_hessian2
        implementation this.rootProject.dependency.zookeeper
    }
}

至此,该问题也算解决了。如果你使用的是maven,请按照maven的方式来排除依赖,并导入自己的zookeeper驱动版本,配置都差不多,我就不贴出来了。

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐