集群配置
大佬
命令
查看
bin.zkCli.sh
help
ls /
ls2 /
stat /threeKim
创建节点
create /threeKim "han"
create /threeKim/shuguo "zgl"
get /threeeKim
====================================
create -e /threeKim/wuguo "zhouyu"
===================================
create -s /threeKim/weiguo "simayi"
修改节点
set /ThreeKim/weiguo "cc"
变化监听
get /threeKim/weiguo watch
ls /threeKim watch
删除节点
delete /threeKim/weiguo
rmr /threeKim
Stat结构体
API
- pom.xml
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
</dependency>
- log4j.properties
log4j.rootLogger=INFO,stdout
log4j.logger.mapper=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
- 代码部分
private String connectString = "192.168.199.132:2181,192.168.199.133:2181,192.168.199.134:2181";
private int sessionTimeout = 200000;
private ZooKeeper zkClient;
@Before
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
List<String> children;
try {
children= zkClient.getChildren("/", true);
System.out.println("==============");
for (String child : children) {
System.out.println(child);
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
@Test
public void createNode() throws KeeperException, InterruptedException {
String path = "/IDEA/son";
byte[] bytes = "cjA".getBytes();
String result = zkClient.create(path, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(result);
}
@Test
public void getDataAndWatch() throws KeeperException, InterruptedException {
List<String> children = zkClient.getChildren("/", true);
for (String child : children) {
System.out.println(child);
}
TimeUnit.SECONDS.sleep(2000000);
}
@Test
public void exist() throws KeeperException, InterruptedException {
Stat exists = zkClient.exists("/IDE2A", false);
System.out.println(exists != null);
}
监听服务器动态上下线
- 案例展示
AA