出现异常

来源:6-11 acl - 默认匿名权限

慕粉1124262302

2018-10-01

public class ZKConnectSessionWatcher implements Watcher {

final static Logger log = LoggerFactory.getLogger(ZKConnectSessionWatcher.class);

public static final String zkServerPath = "192.168.0.130:2181";
public static final Integer timeout = 5000;

public static void main(String[] args) throws Exception {
	
	ZooKeeper zk = new ZooKeeper(zkServerPath, timeout, new ZKConnectSessionWatcher());
	
	long sessionId = zk.getSessionId();
	byte[] sessionPassword = zk.getSessionPasswd();
	log.warn("客户端开始连接zookeeper服务器...");
	log.warn("连接状态:{}", zk.getState());
	Thread.sleep(1000);
	log.warn("连接状态:{}", zk.getState());
    String result = zk.create("/test", "456".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
	Thread.sleep(6000);

    System.out.println("睡六秒后的状态:"+zk.getState());
	// 开始会话重连
	log.warn("开始会话重连...");
	ZooKeeper zkSession = new ZooKeeper(zkServerPath, 
										timeout, 
										new ZKConnectSessionWatcher(), 
										sessionId, 
										sessionPassword);
	log.warn("重新连接状态zkSession:{}", zkSession.getState());
    Thread.sleep(1000);
	log.warn("重新连接状态zkSession:{}", zkSession.getState());
}

@Override
public void process(WatchedEvent event) {
	log.warn("接受到watch通知:{}", event);
}

}

2018-10-01 18:50:33,874 [main] [com.imooc.zk.demo.ZKConnectSessionWatcher.main(ZKConnectSessionWatcher.java:25)] - [WARN] 客户端开始连接zookeeper服务器…
2018-10-01 18:50:34,287 [main] [com.imooc.zk.demo.ZKConnectSessionWatcher.main(ZKConnectSessionWatcher.java:26)] - [WARN] 连接状态:CONNECTING
2018-10-01 18:50:34,901 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:47)] - [WARN] 接受到watch通知:WatchedEvent state:SyncConnected type:None path:null
2018-10-01 18:50:36,925 [main] [com.imooc.zk.demo.ZKConnectSessionWatcher.main(ZKConnectSessionWatcher.java:28)] - [WARN] 连接状态:CONNECTED
2018-10-01 18:50:38,949 [main-SendThread(192.168.0.130:2181)] [org.apache.zookeeper.ClientCnxnKaTeX parse error: Expected 'EOF', got '接' at position 280: …:47)] - [WARN] 接̲受到watch通知:Watch…SendThread.run(ClientCnxn.java:1111)] - [WARN] Client session timed out, have not heard from server in 10648ms for sessionid 0x1662cf8c0890049
Disconnected from the target VM, address: ‘127.0.0.1:52018’, transport: 'socket’
Exception in thread “main” org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /test
at org.apache.zookeeper.KeeperException.create(KeeperException.java:102)
at org.apache.zookeeper.KeeperException.create(KeeperException.java:54)
at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:786)
at com.imooc.zk.demo.ZKConnectSessionWatcher.main(ZKConnectSessionWatcher.java:29)

执行到String result = zk.create("/test", “456”.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); 这一行就跑异常出来了 ,请老师帮忙看一下~

写回答

1回答

风间影月

2018-10-01

失去链接啊,获取不了数据

0
4
风间影月
你的超时时间比心跳时间短,所以才会这样,这个视频里有说过哈
2018-10-01
共4条回复

ZK分布式专题与Dubbo微服务入门,成长与加薪必备

进阶中高级工程师必备技能,大数据与微服务最常用的中间件

1859 学习 · 321 问题

查看课程