Zookeeper simply implements monitoring

Start the zookeeper service of all machines first

Client side

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.List;

/*
    客户端:
    1.创建zk对象
    2.获取子节点并监听
 */
public class zkClient {
    
    
    private static ZooKeeper zk = null;

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
    
    
        //1.创建zk对象
        /*
            ZooKeeper(String connectString, int sessionTimeout, Watcher watcher)
            第一个参数: zookeeper服务器的地址
            第二个参数: 超时时间一般为心跳时间的2倍
            第三个参数: 该对象用来处理zookeeper服务器响应的事件
         */
        String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
        int sessionTimeout = 4000;
        zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
    
    
         /*
                回调函数: 当我们需要监听的事件发生以后,那么zookeeper服务器就会调用该方法。
                当前这个构造器中的该对象是用来响应所有的事件。
             */
            public void process(WatchedEvent event) {
    
    

            }
        });
        //2.获取子节点并监听
        registerListener();
//防止线程结束
        Thread.sleep(Long.MAX_VALUE);
    }

    public static void registerListener() throws KeeperException, InterruptedException {
    
    
        //2.获取子节点并监听
        lic void test2() throws KeeperException, InterruptedException {
    
    
        /*
            getChildren(final String path, Watcher watcher)
            第一个参数 :监听(获取)的节点路径
            第二个参数 : 该对象用来处理zookeeper节点发生变化后响应的事件
         */
        List<String> children = zk.getChildren("/parent", new Watcher() {
    
    
            //节点一旦发生变化就会调用该方法
            public void process(WatchedEvent event) {
    
    
                //1.再次注册监听
                try {
    
    
                    //一旦再次调用此方法-该方法作用:①注册监听 ②遍历子节点
                    registerListener();
                } catch (KeeperException e) {
    
    
                    e.printStackTrace();
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }

            }
        });
        //2.实现发生事件后需要做的事情----遍历所有子节点
        for (String node : children) {
    
    
            System.out.println(node);
        }
    }
}

Server side

package com.atguigu.online;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;

/*
    服务器端:
    1.创建客户端对象
    2.创建临时节点
 */
public class zkServer {
    
    
    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
    
    
        //1.创建zk对象
        String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
        int sessionTimeout = 4000;
        ZooKeeper zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
    
    
            public void process(WatchedEvent event) {
    
    

            }
        });

        //2.判断父节点是否存在如果不存在则创建
        Stat exists = zk.exists("/parent", false);
        if (exists == null){
    
    
            //父节点不存在则创建
            /*
            create(final String path, byte data[], List<ACL> acl,
                    CreateMode createMode)
             第一个参数 :节点路径
             第二个参数 :节点上存储的数据
             第三个参数 :访问控制权限
             第四个参数 :节点的类型
         */
            zk.create("/parent", "".getBytes(),
                    ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        //3.创建临时节点
        zk.create("/parent/" + args[0], args[1].getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);

        //4.服务器需要一直在线
        Thread.sleep(Long.MAX_VALUE);
    }
}

Guess you like

Origin blog.csdn.net/qq_38705144/article/details/111604599