zookeeper自身的ZooKeeper构建一个连接,然后往zookeeper节点写入数据,但是代码写法略显复杂。现在使用github上的一个开源项目ZkClient 

https://github.com/sgroschupf/zkclient

 

可以大大简化往zookeeper分布式集群中的节点写入数据的复杂度。

首先需要在pom.xml添加引用:

    <dependencies>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.5.5</version>
        </dependency>

        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.12.1</version>
        </dependency>
    </dependencies>

 

Java代码程序:

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.*;

import java.util.List;

public class Main {
    public static void main(String[] args) {
        //初始化log4j,zookeeper否则报错。
        org.apache.log4j.BasicConfigurator.configure();

        try {
            Main m = new Main();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Main() throws Exception {
        String ip = "localhost";
        String addrs = ip + ":2181," + ip + ":2182," + ip + ":2183";

        //连接zookeeper服务器。
        //addrs是一批地址,如果其中某一个服务器挂掉,其他仍可用。
        ZkClient zkClient = new ZkClient(addrs);
        //zkClient.connect(300 * 1000, new MyWatcher());

        System.out.println("连接建立");

        String node = "/zhang_phil_node_2";

        //数据变化监听。
        zkClient.subscribeDataChanges(node, new IZkDataListener() {
            @Override
            public void handleDataChange(String dataPath, Object data) throws Exception {
                System.out.println("handleDataChange:" + dataPath);
                System.out.println("handleDataChange:" + data.toString());
            }

            @Override
            public void handleDataDeleted(String dataPath) throws Exception {

            }
        });

        zkClient.subscribeChildChanges(node, new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                System.out.println("handleChildChange:" + parentPath);
                for (String c : currentChilds) {
                    System.out.println("handleChildChange:" + c);
                }
            }
        });

        if (!zkClient.exists(node)) {
            zkClient.create(node, "hello,world! 2", CreateMode.PERSISTENT);
        }

        String s = zkClient.readData(node);
        System.out.println(s);
    }

    
    /*
    private class MyWatcher implements Watcher {
        @Override
        public void process(WatchedEvent event) {
            if (event.getState() == Event.KeeperState.SyncConnected) {
                System.out.println("状态:" + Event.KeeperState.SyncConnected);
            }
        }
    }
     */
}

 

程序运行后,用zookeeper提供的命令行进入节点查看数据情况:

表明写入成功。

乱码是序列号导致。用ZkClient读取出来的数据仍然是:

hello,world! 2

 

Logo

更多推荐