1、zookeeper的应用:
注册中心、配置集中管理、集群管理、分布式锁和分布式任务、队列的管理。
2、zookeeper的znode类型:
持久节点:永久保存数据。
持久有序节点:永久保存数据,会给节点添加一个有序的序号。
临时节点:当客户端和zookeeper断开连接时,节点自动删除。
临时有序节点:断开连接时自动删除,会给节点添加有序的序号。
3、zookeeper的监听通知机制:
客户端可以监听zookeeper的znode节点,znode改变时会通知客户端。
4、zookeeper的常用命令:
4.1查询
# 查询当前节点下的全部子节点 ls 节点名称 #ls /
# 查询当前节点下的数据 get 节点名称 # get /zookeeper
4.2创建
create [-s] [-e] znode名称 znode数据 #参数-s:加上-s表示有序 #参数-e:加上-e表示临时节点
4.3修改
set znode节点 znode数据
4.4删除
delete znode名称 #没有子节点的znode deleteall znode名称 #删除当前节点和其全部子节点,新版本命令是rmr
5、使用Java访问Zookeeper:
5.1 使用IDEA创建maven工程
5.2 导入依赖,pom文件为:
<?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.yas</groupId> <artifactId>zkDemo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.6.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
5.3 添加配置类:
1 package com.yas; 2 3 import org.apache.curator.RetryPolicy; 4 import org.apache.curator.framework.CuratorFramework; 5 import org.apache.curator.framework.CuratorFrameworkFactory; 6 import org.apache.curator.retry.ExponentialBackoffRetry; 7 8 public class ZkUtil { 9 10 public static CuratorFramework cf(){ 11 RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,2); 12 CuratorFramework cf = CuratorFrameworkFactory.builder() 13 .connectString("127.0.0.1:2181") 14 .retryPolicy(retryPolicy) 15 .build(); 16 cf.start(); 17 return cf; 18 } 19 }
5.4 测试:
1 import com.yas.ZkUtil; 2 import org.apache.curator.framework.CuratorFramework; 3 import org.apache.zookeeper.CreateMode; 4 import org.apache.zookeeper.data.Stat; 5 import org.junit.Test; 6 7 import java.util.List; 8 9 public class Test01 { 10 //连接 11 //@Test 12 // public void connect(){ 13 // CuratorFramework cf = ZkUtil.cf(); 14 // System.out.println(cf); 15 // } 16 17 CuratorFramework cf = ZkUtil.cf(); 18 19 @Test 20 public void selectZnodeh() throws Exception { 21 final List<String> strings = cf.getChildren().forPath("/"); 22 for (String str: strings) { 23 System.out.println(str); 24 } 25 //dubbo 26 //zookeeper 27 } 28 29 @Test 30 public void getData() throws Exception { 31 byte[] bytes = cf.getData().forPath("/dubbo"); 32 System.out.println(new String(bytes,"UTF-8")); 33 //192.168.0.101 34 } 35 36 @Test 37 public void getStat() throws Exception{ 38 final Stat stat = cf.checkExists().forPath("/dubbo"); 39 System.out.println(stat); 40 //212,212,1635987213053,1635987213053,0,2,0,0,13,2,236 41 } 42 43 @Test 44 public void create() throws Exception { 45 cf.create().withMode(CreateMode.PERSISTENT).forPath("/yas","asen".getBytes()); 46 } 47 48 @Test 49 public void update() throws Exception{ 50 cf.setData().forPath("/yas","asenyang".getBytes()); 51 } 52 53 @Test 54 public void delete() throws Exception{ 55 cf.delete().deletingChildrenIfNeeded().forPath("/yas"); 56 } 57 }
关于监听通知机制(watch机制):
1 import com.yas.ZkUtil; 2 import org.apache.curator.framework.CuratorFramework; 3 import org.apache.curator.framework.recipes.cache.NodeCache; 4 import org.apache.curator.framework.recipes.cache.NodeCacheListener; 5 import org.apache.zookeeper.data.Stat; 6 import org.junit.Test; 7 8 public class Test02 { 9 CuratorFramework cf = ZkUtil.cf(); 10 11 @Test 12 public void listen() throws Exception { 13 //1.创建NodeCache对象,指定要监听的znode 14 NodeCache nodeCache = new NodeCache(cf,"/yas"); 15 nodeCache.start(); 16 //2.添加一个监听器 17 nodeCache.getListenable().addListener(new NodeCacheListener() { 18 @Override 19 public void nodeChanged() throws Exception { 20 final byte[] data = nodeCache.getCurrentData().getData(); 21 final Stat stat = nodeCache.getCurrentData().getStat(); 22 final String path = nodeCache.getCurrentData().getPath(); 23 24 System.out.println("监听的节点是:"+path); 25 System.out.println("节点现在的数据是:"+new String(data,"UTF-8")); 26 System.out.println("节点的状态是:"+stat); 27 } 28 }); 29 30 System.out.println("监听已经启动"); 31 //3.System.in.Read(); 32 System.in.read(); 33 } 34 }
当/yas 节点的值被修改后,会自动触发watch机制。
效果如下: