抱歉,您的瀏覽器無法訪問本站
本頁面需要瀏覽器支持(啟用)JavaScript
了解詳情 >

环境

环境 版本
SpringBoot 2.1.4
Zookeeper 3.6.2

原生Java Api

依赖

首先导入依赖

1
2
3
4
5
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.2</version>
</dependency>

代码

配置一下Zookeeper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Configuration
public class ZookeeperConfig {

private static final Logger log = LoggerFactory.getLogger(ZookeeperConfig.class);

private String address = "127.0.0.1:2181";

private int timeout = 4000;

@Bean(name = "zkClient")
public ZooKeeper zooKeeper() throws IOException {
ZooKeeper zooKeeper = null;
zooKeeper = new ZooKeeper(address,timeout,(event)->{
if (event.getState()== Watcher.Event.KeeperState.SyncConnected){
log.info("连接成功!");
}
});
return zooKeeper;
}
}

然后手写波工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
@Component
public class ZookeeperUtils {

private static final Logger log = LoggerFactory.getLogger(ZookeeperUtils.class);

private static final Stat DEFAULT_STAT = new Stat();

private static final int DEFAULT_VERSION = -1;

@Autowired
private ZooKeeper zooKeeper;


/**
* 判断某个目录是否存在,flag是否使用zookeeper默认watcher
* @author czj
* @date 2020/9/21 20:59
* @param path 路径
* @param flag 是否使用默认watcher
* @return org.apache.zookeeper.data.Stat
*/
public Stat exists(String path,boolean flag){
try {
return zooKeeper.exists(path,flag);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}

/**
* @author czj
* 创建持久层节点,根据order值选择是否有序
* @date 2020/9/21 21:14
* @param [path, data, isOrder]
* @return org.apache.zookeeper.data.Stat
*/
public Stat createPersistentNode(String path,String data,boolean isOrder){
return createNode(path,data,isOrder?CreateMode.PERSISTENT_SEQUENTIAL:CreateMode.PERSISTENT);
}


/**
* @author czj
* 创建临时层节点,根据order值选择是否有序
* @date 2020/9/21 21:15
* @param [path, data, isOrder]
* @return org.apache.zookeeper.data.Stat
*/
public Stat createEphemeralNode(String path,String data,boolean isOrder){
return createNode(path,data,isOrder?CreateMode.EPHEMERAL_SEQUENTIAL:CreateMode.EPHEMERAL);

}


private Stat createNode(String path,String data,CreateMode createMode){
try {
zooKeeper.create(path,data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,createMode);
} catch (KeeperException e) {
if (e instanceof KeeperException.NodeExistsException){
log.error("{}节点已存在,请勿重复创建!",path);
return null;
}
log.error("创建节点出现错误:{}",e.getMessage());
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}


/**
* 删除节点,成功返回true,否则返回false
* @author czj
* @date 2020/9/22 19:20
* @param [path]
* @return boolean
*/
public boolean deleteNode(String path){
try {
zooKeeper.delete(path,DEFAULT_VERSION);
return true;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (KeeperException e) {
e.printStackTrace();
}
return false;
}


/**
* 返回目录下的子目录
* @author czj
* @date 2020/9/22 19:25
* @param [path]
* @return java.util.List<java.lang.String>
*/
public List<String> findChildren(String path){
try {
return zooKeeper.getChildren(path,false);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return Collections.EMPTY_LIST;
}

/**
* @author czj
* 获得目录下的数据
* @date 2020/9/22 19:29
* @param [path]
* @return java.lang.String
*/
public String getData(String path){
try {
return new String(zooKeeper.getData(path,false,DEFAULT_STAT));
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}

}