Java中运行ZooKeeper(客户端)

234 阅读2分钟

一、manven项目里pom环境

    <dependencies>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
   <build>
           <plugins>
               <plugin>
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <version>3.1</version>
                   <configuration>
                       <source>1.8</source>
                       <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>

</project>

二、编码实现:

public class ZookeeperClientDemo {
	ZooKeeper zk = null;
	@Before
	public void init()  throws Exception{
		// 构造一个连接zookeeper的客户端对象
		//创建一个与服务器的连接 需要(服务端的 ip+端口号)(session过期时间)(Watcher监听注册)
		zk = new ZooKeeper("hdp-01:2181,hdp-02:2181,hdp-03:2181", 2000, null);
	}
	
	
	@Test
	public void testCreate() throws Exception{

		// 参数1:要创建的节点路径  参数2:数据  参数3:访问权限  参数4:节点类型
		String create = zk.create("/fengze/wangcc", "hello eclipse".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		
		System.out.println(create);
		
		zk.close();
		
	}
	
	
	@Test
	public void testUpdate() throws Exception {
		
		// 参数1:节点路径   参数2:数据    参数3:所要修改的版本,-1代表任何版本
		zk.setData("/fengze", "我爱你".getBytes("UTF-8"), -1);
		
		zk.close();
		
	}
	
	
	@Test	
	public void testGet() throws Exception {
		// 参数1:节点路径    参数2:是否要监听    参数3:所要获取的数据的版本,null表示最新版本
		byte[] data = zk.getData("/fengze", false, null);
		System.out.println(new String(data,"UTF-8"));
		
		zk.close();
	}
	
	
	
	@Test	
	public void testListChildren() throws Exception {
		// 参数1:节点路径    参数2:是否要监听   
		// 注意:返回的结果中只有子节点名字,不带全路径
		List<String> children = zk.getChildren("/fengze", false);
		
		for (String child : children) {
			System.out.println(child);
		}
		
		zk.close();
	}
	
	
	@Test
	public void testRm() throws InterruptedException, KeeperException{
		
		zk.delete("/fengze", -1);
		
		zk.close();
	}

三、监听器,触发,匿名内部类

重写process方法,触发器

    @Before
    public void init() throws IOException {
        zk = new ZooKeeper("192.168.88.201:2181,192.168.88.202:2181,192.168.88.201:2181", 2000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                event.getState();
                if (event.getState() == Event.KeeperState.SyncConnected && event.getType() == Event.EventType.NodeDataChanged) {
                    System.out.println(event.getPath()); // 收到的事件所发生的节点路径
                    System.out.println(event.getType()); // 收到的事件的类型
                    System.out.println("赶紧换照片,换浴室里面的洗浴套装....."); // 收到事件后,我们的处理逻辑

                    try {
                        zk.getData("/fengze", true, null);
                    } catch (KeeperException | InterruptedException e) {
                        e.printStackTrace();
                    }
                }else if(event.getState() == Event.KeeperState.SyncConnected && event.getType() == Event.EventType.NodeChildrenChanged){
                    System.out.println(event.getPath());
                    System.out.println("子节点变化了......");
                }
            }
        });
    }

监听器的方法

@Test
    public void testGetWatch() throws Exception {

        byte[] data1 = zk.getData("/fengze", true, null); // 监听节点数据变化

        List<String> children = zk.getChildren("/fengze", true,null); //监听节点的子节点变化事件

        System.out.println(new String(data1, "UTF-8"));
        System.out.println(children);

        Thread.sleep(Long.MAX_VALUE); // 线程永久睡眠

    }

在Linux系统中打开zkClie.sh交互界面:

输入cd /    =>   ls /  =>   get /fengze  =>  set /fengze "123" => IDEA界面通知机制触发