Zookeeper使用

1、创建服务端ZookeeperServer

/**
 * 监听服务器动态上下线案例:服务端
 */
public class ZookeeperServer {
    private String connectString="192.168.106.131:2181,192.168.106.132:2181,192.168.106.133:2181";
    private int sessionTimeout=2000;
    private ZooKeeper zkClient;

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        BasicConfigurator.configure();
        ZookeeperServer server = new ZookeeperServer();
        //1、连接Zookeeper集群
        server.getConnect();
        //2、注册节点
        server.regist(args[0]);
        //3、业务逻辑
        server.business();
    }

    private void getConnect() throws IOException {
        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            public void process(WatchedEvent watchedEvent) {

            }
        });
    }

    private void regist(String hostname) throws KeeperException, InterruptedException {
        zkClient.create("/servers/server", hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(hostname+" is online");
    }

    private void business() throws InterruptedException {
        Thread.sleep(Long.MAX_VALUE);
    }

}

2、创建客户端ZookeeperClient

/**
 * 监听服务器动态上下线案例:客户端
 */
public class ZookeeperClient {
    private String connectString="192.168.106.131:2181,192.168.106.132:2181,192.168.106.133:2181";
    private int sessionTimeout=2000;
    private ZooKeeper zkClient;

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        BasicConfigurator.configure();
        ZookeeperClient client = new ZookeeperClient();
        //1、获取Zookeeper连接
        client.getConnect();
        //2、注册监听
        client.getChildren();
        //3、业务逻辑
        client.business();
    }

    private void getConnect() throws IOException {
        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                try {
                    getChildren();
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void getChildren() throws KeeperException, InterruptedException {
        List<String> children = zkClient.getChildren("/servers", true);

        //存储服务器节点主机名称集合
        ArrayList<String> hosts = new ArrayList<String>();
        for(String child : children){
            byte[] data = zkClient.getData("/servers/" + child, false, null);
            hosts.add(new String(data));
        }

        //打印所有在线主机名称
        System.out.println(hosts);
    }

    private void business() throws InterruptedException {
        Thread.sleep(Long.MAX_VALUE);
    }

}

3、测试Zookeeper

public class TestZookeeper {

    private String connectString="192.168.106.131:2181,192.168.106.132:2181,192.168.106.133:2181";
    private int sessionTimeout=2000;
    private ZooKeeper zkClient;

    /**获取Zookeeper客户端对象*/
    @Before
    public void init() throws IOException {
        BasicConfigurator.configure();
        zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                List<String> children;
                try {
                    children = zkClient.getChildren("/", true);
                    for(String child : children){
                        System.out.println(child);
                    }
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**创建Zookeeper节点*/
    @Test
    public void createNode() throws KeeperException, InterruptedException {
        String path = zkClient.create("/shanxi", "guxiang".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println(path);
    }

    /**获取子节点并监控节点的变化*/
    @Test
    public void getDataAndWatch() throws KeeperException, InterruptedException {
        List<String> children = zkClient.getChildren("/", true);
        for(String child : children){
            System.out.println(child);
        }
        Thread.sleep(Long.MAX_VALUE);
    }

    /**判断Zookeeper节点是否存在*/
    @Test
    public void exist() throws KeeperException, InterruptedException {
        Stat exists = zkClient.exists("/shanxi", false);
        System.out.println(exists==null?"节点不存在":"节点存在");
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值