- ZooKeeper主要服务于分布式系统,可以用ZooKeeper来做:统一配置管理、统一命名服务、分布式锁、集群管理。
- 使用分布式系统就无法避免对节点管理的问题(需要实时感知节点的状态、对节点进行统一管理等等),而由于这些问题处理起来可能相对麻烦和提高了系统的复杂性,ZooKeeper作为一个能够通用解决这些问题的中间件就应运而生了。
一.引入相关依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
</dependency>
二.编写Test类
public class ZKClient {
//连接的zk的地址及端口号配置
private String connectString = "127.0.0.1:2181,127.0.0.1:2181,127.0.0.1:2181";
//会话超时时间单位 毫秒
private int sessionTimeout = 2000;
private ZooKeeper zkClient=null;
@Before
public void initZK() throws Exception {
//1.创建zk客户端
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
//监听发生后触发的事件
System.out.println(event.getType()+"------"+event.getPath());
Stat stat = null;
try {
stat = zkClient.exists("/atguigu", true);
System.out.println(stat==null?"不存在":"存在");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//2.创建子节点
@Test
public void createNode() throws Exception {
// 参数1:创建的路径,参数2:创建节点存储的数据,参数3:创建节点后节点具有的权限,参数4:节点类型
String create = zkClient.create("/atguigu", "ss.avi".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(create);
}
//3.获取节点下子节点
@Test
public void getChild() throws KeeperException, InterruptedException {
List<String> children = zkClient.getChildren("/", false);
System.out.println("---------------获取节点下子节点------------------");
for (String child : children) {
System.out.println(child);
}
}
//4.判断某一节点是否存在
@Test
public void isExist() throws Exception{
Stat stat = zkClient.exists("/atguigu", true);
System.out.println(stat==null?"不存在":"存在");
Thread.sleep(Long.MAX_VALUE);
}
三.项目实战
/**
* 刷新zk上的job信息
*/
private void refreshJobToZk(Integer id) {
JobInfoEntity jobInfoEntity = jobInfoDubboService.findById(id);
if (jobInfoEntity != null) {
String jobPath = jobBasePath + "/" + jobInfoEntity.getId();
String jobJson = JSONObject.toJSONString(jobInfoEntity);
try {
boolean isExists = curatorFrameworkManager.checkExists(jobPath);
if (isExists) {//已经存在则更新
curatorFrameworkManager.setData(jobPath, jobJson);
} else {//添加到zk上
curatorFrameworkManager.createNode(jobPath, jobJson);
}
} catch (Exception e) {
logger.error("create or update zk node [{}] error:{}", jobInfoEntity, e);
}
}
}
@Value("${zookeeper.job-base-path:/job}")
private String jobBasePath;
application.properties 中配置
zookeeper.job-base-path=${zookeeper.job-base-path}
test-filter.properties 测试环境配置
zookeeper.job-base-path=/job
private CuratorFramework client; //阿帕奇工具
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper, hbase]
[zk: localhost:2181(CONNECTED) 18] ls /hbase
[replication, meta-region-server, rs, splitWAL, backup-masters, table-lock, flush-table-proc, region-in-transition, online-snapshot, master, running, recovering-regions, draining, namespace, hbaseid, table]
[zk: localhost:2181(CONNECTED) 19] get /hbase/meta-region-server
�regionserver:16020�Q0K$w�zPBUF
node1�}й�˓0
cZxid = 0x108d
ctime = Mon Jun 06 22:45:08 CST 2022
mZxid = 0x108d
mtime = Mon Jun 06 22:45:08 CST 2022
pZxid = 0x108d
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 58
numChildren = 0
zk 所在的服务器 一定要开网络监控,zk会产生大量的网络io