采纳答案成功!
向帮助你的同学说点啥吧!感谢那些助人为乐的人
我的sessionid也是0,但是我把获取session的代码放到监听里就可以了。好像是异步的。
代码改了
public class ZKConnectSessionWatcher {
final static Logger log = LoggerFactory.getLogger(ZKConnectSessionWatcher.class);
public static final String zkServerPath = "192.168.1.223:2181";
public static final Integer timeout = 5000;
static long sessionId;
static ZooKeeper zk;
static ZooKeeper zkSession;
public static void main(String[] args) throws Exception {
zk = new ZooKeeper(zkServerPath, timeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
log.warn("接受到watch通知:{}", event);
sessionId = zk.getSessionId();
String ssid = "0x" + Long.toHexString(sessionId);
System.out.println(ssid);
byte[] sessionPassword = zk.getSessionPasswd();
log.warn("客户端开始连接zookeeper服务器...");
log.warn("连接状态:{}", zk.getState());
try {
new Thread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.warn("连接状态:{}", zk.getState());
try {
new Thread().sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 开始会话重连
log.warn("开始会话重连...");
try {
zkSession = new ZooKeeper(zkServerPath,
timeout,
new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
log.warn("重新连接状态zkSession:{}", zkSession.getState());
try {
new Thread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.warn("重新连接状态zkSession:{}", zkSession.getState());
}
},
sessionId,
sessionPassword);
} catch (IOException e) {
e.printStackTrace();
}
}
});
Thread.sleep(5000);
}
}
我感觉也是如此,老师的代码中创建完ZooKeeper直接就去获取sessionId了,此时sessionId还没有获取呢,因为它是异步的,连接还没有建立
老师,如下所示:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.imooc</groupId>
<artifactId>imooc-zookeeper-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.11</version>
</dependency>
<!-- 日志处理 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<!--<dependency>-->
<!--<groupId>io.netty</groupId>-->
<!--<artifactId>netty-all</artifactId>-->
<!--<version>4.1.12.Final</version>-->
<!--</dependency>-->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.10.5.Final</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.jline</artifactId>
<version>0.9.94_1</version>
</dependency>
<dependency>
<groupId>org.apache.yetus</groupId>
<artifactId>audience-annotations</artifactId>
<version>0.5.0</version>
</dependency>
</dependencies>
</project>
谢谢老师,这个代码是session重连那一节的代码:
package com.imooc.zk.demo;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @Title: ZKConnectDemo.java
* @Description: zookeeper 恢复之前的会话连接demo演示
*/
public class ZKConnectSessionWatcher implements Watcher {
final static Logger log = LoggerFactory.getLogger(ZKConnectSessionWatcher.class);
public static final String zkServerPath = "eshop-cache01:2181";
public static final Integer timeout = 10000;
public static void main(String[] args) throws Exception {
ZooKeeper zk = new ZooKeeper(zkServerPath, timeout, new ZKConnectSessionWatcher());
long sessionId = zk.getSessionId();
String ssid = "0x" + Long.toHexString(sessionId);
System.out.println(ssid);
byte[] sessionPassword = zk.getSessionPasswd();
//放到session 或者 redis当中
log.warn("客户端开始连接zookeeper服务器...");
log.warn("连接状态:{}", zk.getState());
new Thread().sleep(1000);
log.warn("连接状态:{}", zk.getState());
new Thread().sleep(200);
// 开始会话重连
log.warn("开始会话重连...");
ZooKeeper zkSession = new ZooKeeper(zkServerPath,
timeout,
new ZKConnectSessionWatcher(),
sessionId,
sessionPassword);
log.warn("重新连接状态zkSession:{}", zkSession.getState());
new Thread().sleep(1000);
log.warn("重新连接状态zkSession:{}", zkSession.getState());
}
public void process(WatchedEvent event) {
log.warn("接受到watch通知:{}", event);
}
}