Apache curator Getting Started combat

Source: Internet
Author: User
Tags sleep zookeeper zookeeper client
Apache curator Getting Started combat

Curator is a zookeeper client for Netflix's open source, and curator has a higher level of abstraction compared to the native client provided by zookeeper, simplifying the development of zookeeper clients. 1.Zookeeper Installation Deployment

Zookeeper deployment is simple, if you already have a Java runtime environment, download Tarball decompression can be run.

[ROOT@VM temp]$ wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
[ ROOT@VM temp]$ tar zxvf zookeeper-3.4.6.tar.gz
[root@vm temp]$ cd zookeeper-3.4.6

[ROOT@VM zookeeper-3.4.6]$ CP Conf/zoo_sample.cfg conf/zoo.cfg
[root@vm zookeeper-3.4.6]$ export zookeeper_home=/usr/local/src/ zookeeper-3.4.5
[root@vm zookeeper-3.4.6]$ export path= $ZOOKEEPER _home/bin: $PATH

[ROOT@VM zookeeper-3.4.6] $ bin/zkserver.sh Start
[root@vm zookeeper-3.4.6]$ bin/zkcli.sh-server 127.0.0.1:2181
2. Common Client Operations

After connecting the Zookeeper service with zkcli.sh, use Help to list all the commands:

[root@bc-vm-edce4ac67d304079868c0bb265337bd4 zookeeper-3.4.6]# bin/zkCli.sh- 127.0.0.1:2181 connecting to localhost:2181 2015-06-11 10:55:14,387 [myID:]-INFO [main:environment@100]-Client Enviro

nment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT ... [Zk:localhost:2181 (CONNECTED) 5] help zookeeper-server host:port cmd args connect host:port get path [wa 
        TCH] LS path [watch] set path data [version] RMR path Delquota [-n|-b] path quit Printwatches On|off Create [-S] [-e] Path data ACL stat path [watch] Close LS2 PA th [watch] history Listquota path setacl path ACL getacl path sync path r Edo Cmdno addauth Scheme auth Delete path [version] setquota-n|-b val path 

Try the usual commands below: Create : Create a path node. ls : View all the nodes under the path. Get : Gets the value on the node. Set : Modifies the value on the node. Delete : Deletes the node.

[Zk:localhost:2181 (CONNECTED) 6] create/zktest mydata
created/zktest
[zk:localhost:2181 (CONNECTED)] LS/< C2/>[zktest, zookeeper]
[zk:localhost:2181 (CONNECTED) 7] ls/zktest
[]
[zk:localhost:2181 (CONNECTED) 13 ] Get/zktest
mydata
czxid = 0x1c
CTime = Thu June one 10:58:06 CST
mzxid = 0x1c
mtime = Thu June 1 1 10:58:06 CST
pzxid = 0x1c
cversion = 0
dataversion = 0
aclversion = 0
Ephemeralowner = 0x0
datalength = 6
numchildren = 0
[zk:localhost:2181 (CONNECTED)] set/zktest junk
CZXID = 0x1c
  
   ctime = Thu June 10:58:06 CST
mzxid = 0x1f
mtime = Thu June one 10:59:08 cst
pzxid = 0x1c
C Version = 0
dataversion = 1
aclversion = 0
ephemeralowner = 0x0
datalength = 4
Numchildren = 0
   [zk:localhost:2181 (CONNECTED) delete/zktest
[zk:localhost:2181 (CONNECTED)] LS/
[zookeeper]
  
3. Manage zookeeper with curator

Curator Maven relies on the following, generally direct use of curator-recipes, if you need to encapsulate some of the underlying functions, such as increased connection management retry mechanism, etc., you can introduce the Curator-framework package.

    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactid>curator-recipes </artifactId>
        <version>2.7.0</version>
    </dependency>
3.1 Client Operation

With the client API provided by curator, the functionality of the native Client can be fully implemented. It's worth noting that curator uses streaming style APIs.

Package com.cdai.codebase.bigdata.hadoop.zookeeper.curator;
Import Org.apache.curator.framework.CuratorFramework;
Import Org.apache.curator.framework.CuratorFrameworkFactory;

Import Org.apache.curator.retry.RetryNTimes;
 /** * Curator Framework ' s client test. * Output: * $ create/zktest Hello * $ ls/* [zktest, Zookeeper] * $ get/zktest * Hello * $ set/zktest W

    Orld * $ get/zktest * World * $ delete/zktest * $ ls/* [Zookeeper] * * public class Curatorclienttest {
    /** Zookeeper Info */private static final String zk_address = "192.168.1.100:2181";

    private static final String Zk_path = "/zktest"; public static void Main (string[] args) throws Exception {//1.Connect to ZK curatorframework client = Cur
        Atorframeworkfactory.newclient (zk_address, New Retryntimes (10, 5000));
        Client.start ();

        SYSTEM.OUT.PRINTLN ("ZK client start successfully!"); 2.ClientAPI Test//2.1 Create node String data1 = "Hello";
        Print ("Create", Zk_path, data1);
                Client.create ().
                Creatingparentsifneeded ().

        Forpath (Zk_path, Data1.getbytes ());
        2.2 Get node and data print ("LS", "/");
        Print (Client.getchildren (). Forpath ("/"));
        Print ("Get", Zk_path);

        Print (Client.getdata (). Forpath (Zk_path));
        2.3 Modify Data String data2 = "World";
        Print ("Set", Zk_path, data2);
        Client.setdata (). Forpath (Zk_path, Data2.getbytes ());
        Print ("Get", Zk_path);

        Print (Client.getdata (). Forpath (Zk_path));
        2.4 Remove node print ("delete", Zk_path);
        Client.delete (). Forpath (Zk_path);
        Print ("LS", "/");
    Print (Client.getchildren (). Forpath ("/"));
        } private static void print (String ... cmds) {StringBuilder text = new StringBuilder ("$"); for (String cmd:cmds) {text.Append (cmd). Append ("");
    } System.out.println (Text.tostring ());
                    } private static void print (Object result) {System.out.println (result instanceof byte[] ?
    New String ((byte[]) result): result); }

}
3.2 Listeners

Curator provides three types of watcher (cache) to listen for changes to the node: path Cache: Monitoring a path under 1) child node creation, 2) deletion, 3), and update of the node data. The resulting event is passed to the registered Pathchildrencachelistener. Node Cache: Monitors the creation, update, and deletion of a node and caches the data for the node locally. Tree Cache: Path cache and Node cache "fit" to monitor creation, update, delete events under the path, and cache all child node data under the path.

Let's test the simplest path watcher:

Package com.cdai.codebase.bigdata.hadoop.zookeeper.curator;
Import Org.apache.curator.framework.CuratorFramework;
Import Org.apache.curator.framework.CuratorFrameworkFactory;
Import Org.apache.curator.framework.recipes.cache.ChildData;
Import Org.apache.curator.framework.recipes.cache.PathChildrenCache;
Import Org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode;

Import Org.apache.curator.retry.RetryNTimes;
 /** * Curator Framework watch test. */public class Curatorwatchertest {/** Zookeeper info */private static final String zk_address = "192.168.1.10
    0:2,181 ";

    private static final String Zk_path = "/zktest"; public static void Main (string[] args) throws Exception {//1.Connect to ZK curatorframework client = Cur
        Atorframeworkfactory.newclient (zk_address, New Retryntimes (10, 5000));
        Client.start ();

        SYSTEM.OUT.PRINTLN ("ZK client start successfully!"); 2.Register Watcher Pathchildrencache watcher = new Pathchildrencache (client, Zk_path,
        True/if cache data);
            Watcher.getlistenable (). AddListener ((client1, event), {childdata data = Event.getdata ();
            if (data = = null) {System.out.println ("No data in event[" + Event + "]");
                        } else {System.out.println ("Receive event:" + "type=[" + event.gettype () + "]" + ", path=[" + data.getpath () + "]" + ", data=[" + New String (Data.getdata (
            ) + "]" + ", stat=[" + data.getstat () + "]");
        }
        });
        Watcher.start (Startmode.build_initial_cache);

        System.out.println ("Register ZK watcher successfully!");
    Thread.Sleep (Integer.max_value); }

}

The following is the output of the Java program when operating in zkcli.sh:

JAVA:ZK Client Start successfully!
Java:register ZK Watcher successfully!

ZKCLI: [zk:localhost:2181 (CONNECTED)] Create/zktest/hello mydata
java:receive event:type=[child_added], path= [/zktest/hello], Data=[mydata], stat=[121,121,1434001221097,1434001221097,0,0,0,0,6,0,121]

zkCli: [ZK: localhost:2181 (CONNECTED) Set/zktest/hello otherdata
java:receive event:type=[child_updated], path=[/zktest /hello], Data=[otherdata], stat=[121,122,1434001221097,1434001228467,1,0,0,0,9,0,121]

zkCli: [zk:localhost : 2181 (CONNECTED) Delete/zktest/hello
java:receive event:type=[child_removed], Path=[/zktest/hello], data=[ Otherdata], stat=[121,122,1434001221097,1434001228467,1,0,0,0,9,0,121]
4.Curator "Recipes"

Since the MAVEN package is called curator-recipes, it means that curator has its own "recipe": lock : Includes shared locks, shared reentrant locks, read and write locks, and so on. election : Leader election algorithm. Barrier: A "fence" that blocks distributed computing until a condition is met can be seen as a distributed implementation of Barrier in the JDK concurrent package. Cache : Three types of caches and listening mechanisms mentioned previously. Persistent node : A node that is still present in the zookeeper after the connection or session terminates. queues : Distributed queues, distributed priority queues, and so on. 4.1 Distributed Locks

When distributed programming, such as the most vulnerable situation is the application on-line multi-machine deployment, so when multiple applications access to a resource at the same time, there is a mechanism to coordinate them. For example, an app is now rebuild cache content, temporarily locking a zone for temporary access, or, for example, a scheduler that only wants one task to be executed by one app at a time, and so on.

The following program starts two threads T1 and T2 to compete for locks, and the thread that gets the lock takes 5 seconds. Running several times can be observed, sometimes T1 first get the lock and T2 wait, and sometimes vice versa. Curator will use the node of the lock path we provide as a global lock, the data for this node is similar to this format: [_c_64e0811f-9475-44ca-aa36-c1db65ae5350-lock-0000000005], This string is generated each time a lock is acquired, and the data is emptied when the lock is released.

Package com.cdai.codebase.bigdata.hadoop.zookeeper.curator;
Import Org.apache.curator.framework.CuratorFramework;
Import Org.apache.curator.framework.CuratorFrameworkFactory;
Import Org.apache.curator.framework.recipes.locks.InterProcessMutex;

Import Org.apache.curator.retry.RetryNTimes;

Import Java.util.concurrent.TimeUnit;
 /** * Curator Framework ' s distributed lock test. */public class Curatordistrlocktest {/** Zookeeper info */private static final String zk_address = "192.168.1.
    100:2,181 ";

    private static final String Zk_lock_path = "/zktest"; public static void Main (string[] args) throws Interruptedexception {//1.Connect to ZK curatorframework C Lient = Curatorframeworkfactory.newclient (zk_address, New Retryntimes (10, 5000))
        ;
        Client.start ();

        SYSTEM.OUT.PRINTLN ("ZK client start successfully!");
        thread T1 = new Thread ((), {dowithlock (client); }, "T1");
        Thread t2 = new Thread ((), {dowithlock (client);

        }, "T2");
        T1.start ();
    T2.start (); } private static void Dowithlock (Curatorframework client) {Interprocessmutex lock = new Interprocessmutex (cl
        Ient, Zk_lock_path); try {if (Lock.acquire (timeunit.seconds)) {System.out.println (thread.currentthread
                (). GetName () + "hold Lock");
                Thread.Sleep (5000L);
            System.out.println (Thread.CurrentThread (). GetName () + "release lock");
        }} catch (Exception e) {e.printstacktrace ();
            } finally {try {lock.release ();
            } catch (Exception e) {e.printstacktrace (); }
        }

    }

}
4.2 leader Elections

When one of the services in the cluster is down, we may want to select a new master from the slave node, which requires a set of leader election methods that can be automatically coordinated in a distributed environment. The curator provides a Leaderselector listener for leader election functions. At the same time, only one listener will enter the Takeleadership () method, indicating that it is the current leader. Note: when listener exits from takeleadership () it indicates that it has abandoned "leader identity", when curator uses zookeeper to select a new listener from the remaining leader. Autorequeue () method to give up the listener of leadership to regain leadership, if not set the abandoned listener will not become leader.

Package com.cdai.codebase.bigdata.hadoop.zookeeper.curator;
Import Org.apache.curator.framework.CuratorFramework;
Import Org.apache.curator.framework.CuratorFrameworkFactory;
Import Org.apache.curator.framework.recipes.leader.LeaderSelector;
Import Org.apache.curator.framework.recipes.leader.LeaderSelectorListener;
Import Org.apache.curator.framework.state.ConnectionState;
Import Org.apache.curator.retry.RetryNTimes;

Import Org.apache.curator.utils.EnsurePath;
 /** * Curator Framework ' s leader election test.
 * Output: * LeaderSelector-2 take leadership!
 * LeaderSelector-2 Relinquish leadership!
 * LeaderSelector-1 Take leadership!
 * LeaderSelector-1 Relinquish leadership!
 * LeaderSelector-0 Take leadership! 
 * LeaderSelector-0 Relinquish leadership! *. * * * public class Curatorleadertest {/** Zookeeper info */private static final String zk_address = "1
    92.168.1.100:2181 ";

    private static final String Zk_path = "/zktest"; public static void Main (STring[] args) throws interruptedexception {Leaderselectorlistener listener = new Leaderselectorlistener () { @Override public void Takeleadership (Curatorframework client) throws Exception {System

                . Out.println (Thread.CurrentThread (). GetName () + "Take leadership!");
                Takeleadership () method should only return if leadership is being relinquished.

                Thread.Sleep (5000L);
            System.out.println (Thread.CurrentThread (). GetName () + "relinquish leadership!");
            } @Override public void statechanged (Curatorframework client, ConnectionState state) {

        }
        };
        New Thread ((), {registerlistener (listener);

        }). Start ();
        New Thread ((), {registerlistener (listener);

        }). Start ();
        New Thread ((), {registerlistener (listener);

        }). Start (); Thread.Sleep (InteGer.
    Max_value); } private static void Registerlistener (Leaderselectorlistener listener) {//1.Connect to ZK Curatorf 
        Ramework client = curatorframeworkfactory.newclient (zk_address, New Retryntimes (10, 5000)
        );

        Client.start ();
        2.Ensure path try {new Ensurepath (Zk_path). ensure (Client.getzookeeperclient ());
        } catch (Exception e) {e.printstacktrace ();
        }//3.Register listener Leaderselector selector = new Leaderselector (client, Zk_path, listener);
        Selector.autorequeue ();
    Selector.start (); }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.