Java代码如下:
public class ZookeeperTest {
String connectString = “192.168.90.5:2181,192.168.90.5:2182,192.168.90.5:2183”;
int sessionTimeout = 10000;
ZooKeeper zooClient;
@Before
public void init() throws IOException {
zooClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
}
});
}
@Test
public void createNode() throws KeeperException, InterruptedException {
String path = zooClient.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("path:"+path);
}
}
在这里我们也许会报一个错误;如下:
这里的解决方法:在你的代码中加入:
Thread t = Thread.currentThread();
t.sleep(20000);
原因:
在使用zkClient调用方法之前,先用t.sleep(20000)停留几秒就行了。它去世要去获取连接需要一点时间才能获得.
版权声明:本文为qq_37605747原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。