Easy Tutorial
❮ Android Tutorial Xfermode Porterduff2 Python Understanding Dict Copy Shallow Or Deep ❯

4.0 Setting Up the Zookeeper Java Client

Category Zookeeper Tutorial

This tutorial uses IntelliJ IDEA as the IDE. A Maven project named zookeeper-demo is created and includes the following dependencies. You can select the appropriate version from the Maven Central Repository. The tutorial covers both the native API and Curator.

Related Introduction to IntelliJ IDEA:

Maven project directory structure:

I. The Client's Zookeeper Native API

Using the Zookeeper native API, connect to the cluster composed of three services set up in the previous tutorial. Since the connection takes time, use a CountDownLatch to block and wait for the connection to succeed, and output the connection status on the console!

Example

...public static void main(String[] args) {
    try {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        ZooKeeper zooKeeper =
            new ZooKeeper("192.168.3.33:2181," +
                    "192.168.3.35:2181,192.168.3.37:2181",
                    4000, new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    if (Event.KeeperState.SyncConnected == event.getState()) {
                        //If a response event is received from the server, the connection is successful
                        countDownLatch.countDown();
                    }
                }
            });
        countDownLatch.await();
        //CONNECTED
        System.out.println(zooKeeper.getState());

    }
}
...

The console output "connected" indicates a successful connection!

A simple example of adding a node API:

zooKeeper.create("/tutorialpro", "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

Note: For more command functions, please refer to the later sections of this tutorial.

At the same time, execute the command in the server terminal to display that the setting is successful.

II. Curator Connection on the Client

Curator is an open-source Zookeeper client framework developed by Netflix, which solves many low-level details of Zookeeper client development, including connection reconnection, repeated registration of Watcher, and NodeExistsException exceptions, etc.

Curator includes several packages:

Simple usage example:

Example

public class CuratorDemo {

    public static void main(String[] args) throws Exception {
        CuratorFramework curatorFramework = CuratorFrameworkFactory.
                builder().connectString("192.168.3.33:2181," +
                "192.168.3.35:2181,192.168.3.37:2181").
                sessionTimeoutMs(4000).retryPolicy(new
                ExponentialBackoffRetry(1000, 3)).
                namespace("").build();
        curatorFramework.start();
        Stat stat = new Stat();
        //Query node data
        byte[] bytes = curatorFramework.getData().storingStatIn(stat).forPath("/tutorialpro");
        System.out.println(new String(bytes));
        curatorFramework.close();
    }
}

The previous step set the value of the /tutorialpro node, so the console output.

Related reference links for Curator: http://curator.apache.org/.

Demo source code package:

Download

❮ Android Tutorial Xfermode Porterduff2 Python Understanding Dict Copy Shallow Or Deep ❯