Zookeeper异常org.apache.zookeeper.KeeperException$ConnectionLossException

在虚拟机上安装了CenOS Linux系统,然后配置好了 zookeeper的集群环境,在本地写了一个Zookeeper测试程序,如下:

package com.xbq.zookeeper;

import java.io.IOException;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

/** 
 * @ClassName: ZookeeperDemo
 * @Description: TODO zookeeper测试
 * @author xbq
 * @version 1.0
 * @date 2017-3-10 下午5:05:16
 */
public class ZookeeperDemo {

    private static final int SESSION_TIMEOUT = 3000;
    
    public static void main(String[] args) {
        try {
            ZooKeeper zooKeeper = new ZooKeeper("192.168.242.128:2183", SESSION_TIMEOUT, new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    System.out.println("触发事件:" + event.getType());
                }
            });
            zooKeeper.create("/node_xbq", "coder".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println(new String(zooKeeper.getData("/node_xbq", true, null)));
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

执行之后,出现如下错误:

org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /node_xbq
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:99)
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
    at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:1412)
    at com.xbq.zookeeper.ZookeeperDemo.main(ZookeeperDemo.java:32)

解决方法:

  关闭Linux的防火墙。 执行 service iptables stop。

  CentOS Linux开启和关闭防火墙命令有两种,一种是临时的,重启即复原;另外一种是永久性的,重启不会复原。  

 1) 临时生效,重启后复原  
     开启: service iptables start  
     关闭: service iptables stop  
2) 永久性生效,重启后不会复原 开启: chkconfig iptables on 关闭: chkconfig iptables off

 

你可能感兴趣的:(Zookeeper异常org.apache.zookeeper.KeeperException$ConnectionLossException)