zookeeper异常ConnectionLossException的解决办法
今天在用centerOS装zookeeper,然后本地搭建dubbo项目,注册到zookeeper上时出现了这个错误Exception in thread "main" org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /dubbo
今天在用centerOS装zookeeper,然后本地搭建dubbo项目,注册到zookeeper上时出现了这个错误
Exception in thread "main" org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /dubbo
搞了快一天的时间,人都快要崩溃了
上网一查,全是说什么到Zookeeper服务的连接未完成,说是加下面这段代码就好了:
CountDownLatch connectedLatch = new CountDownLatch(1); if (States.CONNECTING == zk.getState()) { try { connectedLatch.await(); } catch (InterruptedException e) { throw new IllegalStateException(e); } }
最后研究了下,有两方面的原因:
1、linux上的防火墙没关
CentOS关闭防火墙命令:
CentOS Linux开启和关闭防火墙命令有两种,一种是临时的,重启即复原;另外一种是永久性的,重启不会复原。
1) 临时生效,重启后复原
开启: service iptables start
关闭: service iptables stop
2) 永久性生效,重启后不会复原
开启: chkconfig iptables on
关闭: chkconfig iptables off
2、也是最主要的原因,确实是zookeeper的链接注册过程没完成然后就去获取zk客户端的链接状态了,只需将注册zookeeper的超时时间加大就好了。
把这两个的超时时间加大一点就好了。
<dubbo:registry protocol="zookeeper" address="192.168.88.113:2181" timeout="30000"/>
<dubbo:service interface="com.cn.ankang.api.XpopEcterInterface" ref="xpopEnterInterface" timeout="30000"/>
可以通过客户端是测试链接zk,看是否通畅
客户端链接栗子:
package org.gome.ankang;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
public class ZookeeperTest {
public static void main(String[] args) throws Exception {
ZooKeeper zk = new ZooKeeper("192.168.88.113:2181", 1000, null);
System.out.println(zk.getClass());
if(zk.exists("/test", false) == null)
{
zk.create("/test", "znode1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
System.out.println(new String(zk.getData("/test", false, null)));
}
}
更多推荐
所有评论(0)