命令行操作
配置环境变量:vi /etc/profile
export ZOOKEEPER_HOME=/root/hd/zookeeper-3.4.10
export PATH=
$ZOOKEEPER_HOME/bin:
$PATH
声明环境变量:source /etc/profile
发送到其他机器
scp /etc/profile hsiehchou122:/etc/
scp /etc/profile hsiehchou123:/etc/
scp /etc/profile hsiehchou124:/etc/
启动zookeeper
zkServer.sh start
查看zookeeper状态
zkServer.sh status
1)启动客户端
bin/zkCli.sh
2)连接其它机器客户端操作
没有太大必要,每台机器内容都一样
connect hsiehchou122:2181
connect hsiehchou123:2181
connect hsiehchou124:2181
3)查看历史操作记录
history
4)查看当前节点的内容
ls /
5)存储:创建节点
create /hsiehchou 10(存储的数据)
6)查看节点的值
get /hsiehchou
10cZxid = 0x400000004ctime = Sat Feb 23 20:05:58 PST 2019mZxid = 0x400000004mtime = Sat Feb 23 20:05:58 PST 2019pZxid = 0x400000004cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 2numChildren = 0
7)创建节点的可选项
create [-s] [-e]
path
data acl
[-p]永久节点–默认
[-e] 短暂节点
[-s] 带序号
create -e /re hm
注意:此时-e创建的是临时的短暂节点,退出客户端后消失。退出客户端:quit
create -s /re hm
注意:此时-s创建是带序号的节点,可以创建节点名相同的,序号依次累加
[
zk: localhost:2181(
CONNECTED)
1] create -s /mm hm
Created /mm0000000002
[
zk: localhost:2181(
CONNECTED)
2] create -s /mm hm
Created /mm0000000003
[
zk: localhost:2181(
CONNECTED)
3] create -s /mm hm
Created /mm0000000004
[
zk: localhost:2181(
CONNECTED)
4] create /re hm
Created /re
[
zk: localhost:2181(
CONNECTED)
5] create /re hm
Node already exists: /re
创建短暂带序号节点
create -e -s /tt bt
8)修改节点值
set path data [version]
例如:set /re hm2 1
[version] 版本
注意:设置版本号 必须从0开始
9)删除节点
delete path
[zk: localhost:2181(CONNECTED) 12] ls /
[mm0000000004, re, zookeeper, mm0000000002, mm0000000003, hsiehchou]
[zk: localhost:2181(CONNECTED) 13] delete /mm0000000002
[zk: localhost:2181(CONNECTED) 14] ls /
[mm0000000004, re, zookeeper, mm0000000003, hsiehchou]
10)创建子节点
create /re/pa qi
11)递归删除
rmr /re
12)监听
获得监听(文件):get path watch
获得当前节点下增减变化(文件夹):ls path watch
13)查看当前节点的状态
stat /hsiehchou
节点状态信息
czxid:zookeeper事务id
ctime:节点创建时间
mZxid:最后更新的czxid
mtime:最后修改的时间*
pZxid:最后更新子节点的czxid
cversion:子节点的变化号、子节点修改次数
dataVersion:数据变化号
aclVersion:访问控制列表的变化号
ephemeralOwner:临时节点判断
dataLength:节点数据长度
numChildren:子节点个数
JAVA-API 练习
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.hsiehchou
</groupId>
<artifactId>ZKTest
</artifactId>
<version>1.0-SNAPSHOT
</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper
</groupId>
<artifactId>zookeeper
</artifactId>
<version>3.4.10
</version>
</dependency>
<dependency>
<groupId>junit
</groupId>
<artifactId>junit
</artifactId>
<version>RELEASE
</version>
<scope>compile
</scope>
</dependency>
</dependencies>
</project>
练习1
ZkClient类
package com.hsiehchou.zk;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
public
class ZkClient {
private String conected =
"hsiehchou121:2181,hsiehchou122:2181,hsiehchou123:2181,hsiehchou124:2181";
//毫秒
private
int timeout =
2000;
ZooKeeper zkCli =
null;
//连接zookeeper集群
@Before
public void init() throws IOException {
//String:连接集群的IP端口号,Int:超时设置,Watcher:监听
zkCli =
new ZooKeeper(conected, timeout,
new Watcher() {
//回调方法,显示/节点
public void process(WatchedEvent watchedEvent) {
List<String> children;
//获得节点信息 get
try {
children = zkCli.getChildren(
"/",
true);
}
catch (KeeperException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
//测试 是否连通集群 创建节点
@Test
public void createNode() throws KeeperException, InterruptedException {
String p = zkCli.create(
"/bq",
"sk".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(p);
}
//查看子节点
@Test
public void getChild() throws KeeperException, InterruptedException {
List<String> children = zkCli.getChildren(
"/",
true);
for(String c:children){
System.out.println(c);
}
}
//删除子节点数据:delete path
@Test
public void deleteData() throws KeeperException, InterruptedException {
zkCli.delete(
"/da", -
1);
}
//修改数据:set path data
@Test
public void setData() throws KeeperException, InterruptedException {
zkCli.setData(
"/hsiehchou",
"nihao".getBytes(),-
1);
//查看/hsiehchou
byte[] data = zkCli.getData(
"/hsiehchou",
false,
new Stat());
System.out.println(
new String(data));
}
//指定节点是否存在
@Test
public void testExist() throws KeeperException, InterruptedException {
Stat exists = zkCli.exists(
"/hsiehchou",
false);
System.out.println(exists ==
null ?
"no have":
"have");
}
}
练习2
WatchDemo类
package com.hsiehchou.watch;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
public
class WatchDemo {
public
static
void main(
String[] args) throws IOException, KeeperException, InterruptedException {
String
connected =
"hsiehchou121:2181,hsiehchou122:2181,hsiehchou123:2181,hsiehchou124:2181,";
//毫秒
int timeout =
2000;
//1.连接zookeeper集群
ZooKeeper zkCli =
new ZooKeeper(
connected, timeout,
new Watcher() {
//监听回调
public
void
process(WatchedEvent watchedEvent) {
System.out.
println(
"正在监听中.........");
}
});
//2.监听: ls / watch get / watch
zkCli.getChildren(
"/",
new Watcher() {
public
void
process(WatchedEvent watchedEvent) {
System.out.
println(
"此时监听的路径是:"+watchedEvent.getPath());
System.out.
println(
"此时监听的类型为:"+watchedEvent.getType());
System.out.
println(
"有人正在修改数据!!!");
}
},null);
Thread.sleep(Long.MAX_VALUE);
}
}
WatchDemo1类
package com.hsiehchou.watch;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
public
class WatchDemo1 {
public
static
void main(
String[] args) throws IOException, KeeperException, InterruptedException {
ZooKeeper zkCli =
new ZooKeeper(
"hsiehchou121:2181,hsiehchou122:2181,hsiehchou123:2181,hsiehchou124:2181",
2000,
new Watcher() {
public
void
process(WatchedEvent watchedEvent) {
}
});
byte[] data = zkCli.getData(
"/re",
new Watcher() {
//具体监听的内容
public
void
process(WatchedEvent watchedEvent) {
System.out.
println(
"此时监听的路径是:" + watchedEvent.getPath());
System.out.
println(
"此时监听的类型为:" + watchedEvent.getType());
System.out.
println(
"有人正在修改数据!!!");
}
}, null);
System.out.
println(
new
String(data));
Thread.sleep(Long.MAX_VALUE);
}
}
练习3
ZkClient类
package com.hsiehchou.qq;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 实现对zookeeper / 的监听
*/
public class ZkClient {
public
static
void main(
String[] args)
throws IOException, KeeperException, InterruptedException {
//1.获取zookeeper的连接
ZkClient zkCli =
new ZkClient();
zkCli.getConnect();
//2.指定监听的节点路径
zkCli.getServers();
//3.写业务逻辑,一直监听
zkCli.getWatch();
}
//1.获得zookeeper连接
private
String connected =
"hsiehchou121:2181,hsiehchou122:2181,hsiehchou123:2181,hsiehchou124:2181";
//毫秒
private
int timeout =
2000;
ZooKeeper zkCli;
public
void getConnect()
throws IOException {
zkCli =
new ZooKeeper(connected, timeout,
new Watcher() {
public
void process(WatchedEvent watchedEvent) {
List<
String> children;
try {
children = zkCli.getChildren(
"/",
true);
//服务器列表
ArrayList<
String> serverList =
new ArrayList<
String>();
//获取每个节点的数据
for (
String c:children){
byte[] data = zkCli.getData(
"/" + c,
true,
null);
serverList.
add(
new
String(data));
}
//查看服务器列表
System.out.
println(serverList);
}
catch (KeeperException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
//2.指定监听节点路径
public
void getServers()
throws KeeperException, InterruptedException {
List<
String> children = zkCli.getChildren(
"/",
true);
//存储服务器列表
ArrayList<
String> serverList =
new ArrayList<
String>();
for (
String c:children){
byte[] data = zkCli.getData(
"/" + c,
true,
null);
//添加集合中
serverList.
add(
new
String(data));
}
//打印服务器列表
System.out.
println(serverList);
}
//3.一直监听
public
void getWatch()
throws InterruptedException {
//循环监听
Thread.sleep(Long.MAX_VALUE);
}
}