目录
前置准备
在idea上新建一个maven项目
在pom.xml中添加相关依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
</dependencies>
在项目的src/main/resources 目录下,新建一个名为log4j.properties
的文件,在
文件中粘贴
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
在zookeeper项目-》src-》main-》java下创建包名com.heria.zk
创建节点
完整代码
package com.heria.zk;
import org.apache.zookeeper.*;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class zkClient {
private String connectString = "master:2181,slave1:2181,slave2:2181";
private int sessionTimeout = 5000;
private ZooKeeper zkClient;
@Before
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
}
});
}
@Test
public void create() throws InterruptedException, KeeperException {
String nodeCreated = zkClient.create("/bigdata", "heria.s".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
初始化函数
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
}
});
}
新建一个Zookeeper名为zkClient,为全局变量,参数connectString, sessionTimeout, new Watcher()
idea中ctrl+p可以提示参数信息
//connectString:连接的zookeeper的地址,这里的书写要求不能有空格
private String connectString = "master:2181,slave1:2181,slave2:2181";
//sessionTimout:连接超时时间,如果电脑配置不好可以设长点,我这里设为2秒
private int sessionTimeout = 5000;
//Watcher:如果需要监听器,就在里面配相关参数,不需要就设个匿名的
创建子节点
public void create() throws InterruptedException, KeeperException {
String nodeCreated = zkClient.create("/bigdata", "heria.s".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
解析:zkClient.create(String path, byte[]t data,List<ACL> acl,CreateMode createMode)
1)String path:创建子节点的路径
2)byte[]t data:每个节点所给定的内容
3)List acl:权限控制
OPEN_ACL_UNSAFE:完全开放
CREATOR_ALL_ACL:创建该znode的连接拥有所有权限
READ_ACL_UNSAFE:所有的客户端都可读
4)CreateMode createMode:创建节点的类型
PERSISTENT:持久化节点
PERSISTENT_SEQUENTIAL:持久化有序节点
EPHEMERAL:临时节点(连接断开自动删除)
EPHEMERAL_SEQUENTIAL:临时有序节点
运行结果
登录zookeeper客户端,查看能看到新建节点及信息
可能遇到问题1:右键没有package解决方法
右键java,点击mark directory as ,di点击test sources root
可能遇到问题2:java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
下载两个jar包,有些教程说是slf4j-api.jar与slf4j-log4j12-1.7.12.jar,但我引入第二个后就报错了,可以都尝试一下。在这里我下载的时slf4j-api-1.7.12.jar与slf4j-simple-1.7.12.jar。
在idea中ctrl+alt+shift+s打开project structure,添加jar包
获取子节点并监听节点变化
完整代码
package com.heria.zk;
import org.apache.zookeeper.*;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
public class zkClient {
private String connectString = "master:2181,slave1:2181,slave2:2181";
private int sessionTimeout = 5000;
private ZooKeeper zkClient;
@Before
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("-------------------------");
List<String> children = null;
try {
children = zkClient.getChildren("/", true);
for (String child : children) {
System.out.println(child);
}
System.out.println("-------------------------");
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
@Test
public void create() throws InterruptedException, KeeperException {
String nodeCreated = zkClient.create("/bigdata", "heria.s".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
@Test
public void getChildren() throws InterruptedException, KeeperException {
List<String> children = zkClient.getChildren("/", true);
//延时
Thread.sleep(Long.MAX_VALUE);
}
}
添加监听器信息
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("-------------------------");
List<String> children = null;
try {
children = zkClient.getChildren("/", true);
for (String child : children) {
System.out.println(child);
}
System.out.println("-------------------------");
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
获取路径并延时
使得能够长期监听zookeeper节点变化
public void getChildren() throws InterruptedException, KeeperException {
List<String> children = zkClient.getChildren("/", true);
//延时
Thread.sleep(Long.MAX_VALUE);
}
运行结果
当我们创建或者删除子节点后,idea会自动打印修改后的路径
判断节点是否存在
代码如下
@Test
public void exist() throws InterruptedException, KeeperException {
Stat stat = zkClient.exists("/bigdata", false);
System.out.println(stat==null?"not exist":"exist");
}