zookeeper简单操作示例

package com.fun.zkdemo;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;

/**
 * @author fun
 * Date: 2017/9/1 下午2:55
 */
public class ZKTest implements Watcher {

    public static final int SESSION_TIMEOUT = 2000;
    ZooKeeper zk;

    public static void main(String[] args) {

        ZKTest zkTest = new ZKTest("127.0.0.1:2181",SESSION_TIMEOUT);
        String testData = zkTest.getData("/test_node");
        System.out.println(testData);
        zkTest.setData("/test_node","hello zookeeper");
    }

    public ZKTest(String connString,int sessionTimeout) { // 创建链接
        try {
            zk = new ZooKeeper(connString, sessionTimeout, this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getData(String path) { // 读取数据
        byte[] data = new byte[0];
        try {
            data = this.zk.getData(path,this,null); // 读取数据并设置watch
            System.out.println("session id=" + zk.getSessionId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String(data);
    }

    public void setData(String path,String data) { // 设置数据
        try {
            Stat stat = this.zk.exists(path,null);
            this.zk.setData(path,data.getBytes(),stat.getVersion());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void process(WatchedEvent event) { // watch事件触发的时候执行
        System.out.println("keeperState=" + event.getState().getIntValue());
        System.out.println("EventType=" + event.getState().getIntValue());
    }
}

示例中,先去读数据,并设置了watch,之后在修改数据,就触发了process方法。会执行process里面的代码

你可能感兴趣的:(zookeeper简单操作示例)