背景:apache dubbo爆出高危漏洞,需要统一升级 为最新 2.7.7
1、使用dubbo所需包,netty、dubbo、zookeeper、zk节点操作客户端
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.7</version>
<exclusions>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- netty -https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.50.Final</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
2、dubbo 升级为 2.7.7 启动抛出错误。。。。导致 spring初始化中断 。。。。。emmmm
Caused by: java.lang.IllegalStateException: zookeeper not connected
at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient.<init>(CuratorZookeeperClient.java:83)
at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter.createZookeeperClient(CuratorZookeeperTransporter.java:26)
at org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperTransporter.connect(AbstractZookeeperTransporter.java:70)
at org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter$Adaptive.connect(ZookeeperTransporter$Adaptive.java)
at org.apache.dubbo.configcenter.support.zookeeper.ZookeeperDynamicConfiguration.<init>(ZookeeperDynamicConfiguration.java:70)
at org.apache.dubbo.configcenter.support.zookeeper.ZookeeperDynamicConfigurationFactory.createDynamicConfiguration(ZookeeperDynamicConfigurationFactory.java:37)
at org.apache.dubbo.common.config.configcenter.AbstractDynamicConfigurationFactory.lambda$getDynamicConfiguration$0(AbstractDynamicConfigurationFactory.java:39)
at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
at org.apache.dubbo.common.config.configcenter.AbstractDynamicConfigurationFactory.getDynamicConfiguration(AbstractDynamicConfigurationFactory.java:39)
at org.apache.dubbo.common.config.configcenter.DynamicConfiguration.getDynamicConfiguration(DynamicConfiguration.java:224)
at org.apache.dubbo.config.bootstrap.DubboBootstrap.prepareEnvironment(DubboBootstrap.java:882)
at org.apache.dubbo.config.bootstrap.DubboBootstrap.startConfigCenter(DubboBootstrap.java:605)
at org.apache.dubbo.config.bootstrap.DubboBootstrap.useRegistryAsConfigCenterIfNecessary(DubboBootstrap.java:674)
at org.apache.dubbo.config.bootstrap.DubboBootstrap.initialize(DubboBootstrap.java:511)
at org.apache.dubbo.config.bootstrap.DubboBootstrap.init(DubboBootstrap.java:496)
at org.apache.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:190)
at org.apache.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:158)
at org.apache.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:68)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:178)
... 38 common frames omitted
Caused by: java.lang.IllegalStateException: zookeeper not connected
at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient.<init>(CuratorZookeeperClient.java:80)
... 56 common frames omitted
细看,发现了 zookeeper.curator.CuratorZookeeperClient.(CuratorZookeeperClient.java:83)
了解下,Curator是Netflix公司开源的一套zookeeper客户端框架,解决了很多Zookeeper客户端非常底层的细节开发工作,包括连接重连、反复注册Watcher和NodeExistsException异常等等。Patrixck Hunt(Zookeeper)以一句“Guava is to Java that Curator to Zookeeper”给Curator予高度评价
猜测这块进行socket连接时出现了问题,追踪代码,,,
package org.apache.dubbo.remoting.zookeeper.curator;
public class CuratorZookeeperClient extends AbstractZookeeperClient<CuratorZookeeperClient.CuratorWatcherImpl, CuratorZookeeperClient.CuratorWatcherImpl> {
protected static final Logger logger = LoggerFactory.getLogger(CuratorZookeeperClient.class);
private static final String ZK_SESSION_EXPIRE_KEY = "zk.session.expire";
static final Charset CHARSET = Charset.forName("UTF-8");
private final CuratorFramework client;
private Map<String, TreeCache> treeCacheMap = new ConcurrentHashMap();
public CuratorZookeeperClient(URL url) {
super(url);
try {
int timeout = url.getParameter("timeout", this.DEFAULT_CONNECTION_TIMEOUT_MS);
int sessionExpireMs = url.getParameter("zk.session.expire", this.DEFAULT_SESSION_TIMEOUT_MS);
Builder builder = CuratorFrameworkFactory.builder().connectString(url.getBackupAddress()).retryPolicy(new RetryNTimes(1, 1000)).connectionTimeoutMs(timeout).sessionTimeoutMs(sessionExpireMs);
String authority = url.getAuthority();
if (authority != null && authority.length() > 0) {
builder = builder.authorization("digest", authority.getBytes());
}
this.client = builder.build();
this.client.getConnectionStateListenable().addListener(new CuratorZookeeperClient.CuratorConnectionStateListener(url));
this.client.start();
boolean connected = this.client.blockUntilConnected(timeout, TimeUnit.MILLISECONDS);
if (!connected) {
throw new IllegalStateException("zookeeper not connected");
}
} catch (Exception var7) {
throw new IllegalStateException(var7.getMessage(), var7);
}
}
public synchronized boolean blockUntilConnected(int maxWaitTime, TimeUnit units) throws InterruptedException {
long startTime = System.currentTimeMillis();
boolean hasMaxWait = units != null;
long maxWaitTimeMs = hasMaxWait ? TimeUnit.MILLISECONDS.convert((long)maxWaitTime, units) : 0L;
while(!this.isConnected()) {
if (hasMaxWait) {
long waitTime = maxWaitTimeMs - (System.currentTimeMillis() - startTime);
if (waitTime <= 0L) {
return this.isConnected();
}
this.wait(waitTime);
} else {
this.wait();
}
}
return this.isConnected();
}
protected int DEFAULT_CONNECTION_TIMEOUT_MS = 5000;
发现大概意思就是超过timeout时间,没有获取连接就 直接返回。。。抛出 zookeeper not connected、而默认只有5秒。
问题已经定位,接着对症下药。
3、针对xml配置,增加timeout项 即可
<!-- 注册中心的地址 -->
<dubbo:registry id="dubbo1" protocol="zookeeper" address="${dubbo.registry.address}" timeout="60000"/>