Zookeeper curator frame simple to use

Source: Internet
Author: User
Tags sleep zookeeper

purpose of the Github:https://github.com/zhaikaishun/zookeeper_tutorial curator framework

Homepage Introduction is guava are to Java what curator are to Xookeeper, in order to better implement Java operation Zookeeper Server, later appeared curator framework, very powerful, is currently the top-level project of Apache, It provides more rich operations, such as session timeout, master-slave election, distributed calculators, distributed locks, and more, for the API encapsulation of a variety of complex zookeeper scenarios.
Maven Dependency
Jar Package Download
All go to the official website to download, http://curator.apache.org/ Curatot frame Use (a)

The Curatir framework uses a chained programming style that is more readable and uses engineering methods to create connection objects.
1 using the Curatorframeworkfactory two static factory methods (different parameters) to achieve: parameter 1:connectstring, connection string parameter 2:retypolicy, retry the connection policy. There are four implementations, respectively:
Exponentialbackoffretry, Retrytimes, Retryonetimes, retryuntilelapsed (the meaning of the specific parameters will be explained later, you can also go online to view) parameters 3:sessiontimeoutms Session time-out defaults to 60000ms parameter 4:connectiontimeoutms connection time-out, default to 15000ms
Note: For the Retrypolicy policy, a user-defined implementation is made through an interface.
Code in package bjsxt.curator.base;

code Example
the previous Settings

    /** Zookeeper Address */
    static final String connect_addr = "192.168.1.31:2181,192.168.1.32:2181,192.168.1.33:2181";
    /** SESSION Timeout time *
    /static final int session_outtime = 5000;//ms public 

    static void Main (string[] args) throws Exce ption {

        //1 retry policy: initial time is 1s retry 10 times
        retrypolicy retrypolicy = new Exponentialbackoffretry (+);
        2 Create a connection through the factory
        curatorframework CF = Curatorframeworkfactory.builder ()
                    . ConnectString (CONNECT_ADDR)
                    . Sessiontimeoutms (Session_outtime)
                    . Retrypolicy (Retrypolicy)
//                  . Namespace ("Super")
                    . Build ( );
        3 Open Connection
        cf.start ();

        System.out.println (states.connected);
        System.out.println (Cf.getstate ());
basic methods of curator 1. Create a connection 2.Curator Creating Nodes

Create method, optional chain items: creatingparentslfneeded, Withmode, Forpath, WITHACL, etc.
For example

4 Build node specifies the node type (no Withmode defaults to persistent type node), path, data content
cf.create (). creatingparentsifneeded (). Withmode ( createmode.persistent). Forpath ("/super/c1", "C1 content". GetBytes ());

or 

//      cf.create (). creatingparentsifneeded (). Withmode (createmode.persistent). Forpath ("/super/c1", "C1 content" . GetBytes ());      cf.create (). creatingparentsifneeded (). Withmode (createmode.persistent). Forpath ("/super/c2", "C2 content". GetBytes ());
3. Delete a node

Delete method, optional chained items: deletingchildrenifneeded, Guranteed, Withversion, Forpath, and so on.
For example

Cf.delete (). Guaranteed (). deletingchildrenifneeded (). Forpath ("/super");
4. Reading and modifying Data

GetData, SetData method

Read node
string ret1 = new String (Cf.getdata (). Forpath ("/SUPER/C2"));
System.out.println (RET1);
Modify the Node
cf.setdata (). Forpath ("/super/c2", "Modify C2 content". GetBytes ());
String Ret2 = new String (Cf.getdata (). Forpath ("/SUPER/C2"));
System.out.println (Ret2);   
5. Asynchronous callback method.

For example, when creating a node, a callback function is bound to output the server's status code and the server event type. You can also add a thread pool for tuning operations.

Executorservice pool = Executors.newcachedthreadpool ();
Cf.create (). creatingparentsifneeded (). Withmode (Createmode.persistent)
. Inbackground (New Backgroundcallback ( {
    @Override public
    void Processresult (curatorframework CF, curatorevent CE) throws Exception {
        System.out.println ("code:" + Ce.getresultcode ());
        System.out.println ("type:" + ce.gettype ());
        SYSTEM.OUT.PRINTLN ("Thread is:" + thread.currentthread (). GetName ());
    }
}, Pool)
. Forpath ("/super/c3", " C3 content ". GetBytes ());
Thread.Sleep (Integer.max_value);
6. Read child node Methods

GetChildren

list<string> list = Cf.getchildren (). Forpath ("/super");
for (String p:list) {
    System.out.println (p);
}
7. Determine if a child node exists

Checkexists method

Stat stat = cf.checkexists (). Forpath ("/super/c3");
SYSTEM.OUT.PRINTLN (STAT);
The role of the thread pool that speaks of the asynchronous callback above

For example, a single operation to create 500 nodes, it is impossible to use 500 threads at a time to handle. So here is a line pool for control Curatorwatcher

The principle of using the cached method of judging, do not need to re-register ... Most of the place, it is estimated that the problem of downtime subscription can be thought of. Specific principles, suggest a deeper understanding, feel very bad. 1. Method 1

Note the last parameter, whether this is compression or not, notice that pattern when the Cache.star is post_initallzed_event
Directly on the code to see can

public class CuratorWatcher1 {/** zookeeper address */static final String connect_addr = "192.168.1.31:2181,192.168.1
    .32:2181,192.168.1.33:2181 "; /** SESSION Timeout time */static final int session_outtime = 5000;//ms public static void Main (string[] args) throws Ex
        ception {//1 retry policy: initial time is 1s retry 10 times retrypolicy retrypolicy = new Exponentialbackoffretry (1000, 10); 2 Create a connection through the factory curatorframework CF = Curatorframeworkfactory.builder (). ConnectString (connect_
                    ADDR). SESSIONTIMEOUTMS (Session_outtime). Retrypolicy (Retrypolicy)

        . build ();

        3 Establishing the connection Cf.start ();
        4 Create a cache buffer final Nodecache cache = new Nodecache (cf, "/super", false);
        Cache.start (TRUE); Cache.getlistenable (). AddListener (New Nodecachelistener () {/** * <B> method Name: </b>nodecha nged<br> * <B> Description:</b> TouchThe event is the creation of the node and the update node, which does not trigger the operation when the node is deleted.
            <BR> * @see org.apache.curator.framework.recipes.cache.nodecachelistener#nodechanged () */  @Override public void NodeChanged () throws Exception {System.out.println ("path:" +
                Cache.getcurrentdata (). GetPath ());
                SYSTEM.OUT.PRINTLN ("Data is:" + New String (Cache.getcurrentdata (). GetData ()));
                System.out.println ("Status:" + Cache.getcurrentdata (). Getstat ());
            System.out.println ("---------------------------------------");

        }
        });
        Thread.Sleep (1000);

        Cf.create (). Forpath ("/super", "123". GetBytes ());
        Thread.Sleep (1000);

        Cf.setdata (). Forpath ("/super", "456". GetBytes ());
        Thread.Sleep (1000);

        Cf.delete (). Forpath ("/super");

    Thread.Sleep (Integer.max_value); }
}

Output

The path is:/super
data: 123
Status: 38654705677,38654705677,1509971265443,1509971265443,0,0,0,0,3,0,38654705677 The

---------------------------------------
path is: The/super data is: The
456
status is: 38654705677, 38654705678,1509971265443,1509971266479,1,0,0,0,3,0,38654705677

---------------------------------------
2. Method 2

Note the third parameter, which indicates whether to accept the node data content, or false to not accept the

public class CuratorWatcher2 {/** zookeeper address */static final String connect_addr = "192.168.1.31:2181,192.168.1
    .32:2,181 "; /** SESSION Timeout time */static final int session_outtime = 10000;//ms public static void Main (string[] args) throws Ex
        ception {//1 retry policy: initial time is 1s retry 10 times retrypolicy retrypolicy = new Exponentialbackoffretry (1000, 10); 2 Create a connection through the factory curatorframework CF = Curatorframeworkfactory.builder (). ConnectString (connect_
                    ADDR). SESSIONTIMEOUTMS (Session_outtime). Retrypolicy (Retrypolicy)

        . build ();

        3 Establishing the connection Cf.start (); 4 Create a Pathchildrencache cache, the third parameter is whether to accept the node data content if False does not accept pathchildrencache cache = new Pathchildrencache (cf, "/su
        Per ", true);
        5 Cache monitoring Cache.start (startmode.post_initialized_event) at initialization time;
     Cache.getlistenable (). AddListener (New Pathchildrencachelistener () {/**        * <B> method name:</b> listener Change <BR> * <B> profile:</b> New, modify, delete <BR> * @see Org.apache.curator.framework.recipes.cache.pathchildrencachelistener#childevent ( Org.apache.curator.framework.CuratorFramework, org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) */@Override Publi c void Childevent (Curatorframework CF, pathchildrencacheevent event) throws Exception {switch (Event.gett Ype ()) {case CHILD_ADDED:System.out.println ("child_added:" + event.getdata (). Getpat
                    h ());
                    You can also get content System.out.println ("child_added content:" + new String (Event.getdata (). GetData (), "Utf-8"));
                Break
                    Case CHILD_UPDATED:System.out.println ("child_updated:" + event.getdata (). GetPath ()); System.out.println ("child_updated content:" + new String (event.getdATA (). GetData (), "Utf-8"));
                Break
                    Case CHILD_REMOVED:System.out.println ("child_removed:" + event.getdata (). GetPath ());
                Break
                Default:break;

        }
            }
        });

        The create itself node does not change Cf.create (). Forpath ("/super", "Init". GetBytes ());
        Add child nodes Thread.Sleep (1000);
        Cf.create (). Forpath ("/super/c1", "C1 content". GetBytes ());
        Thread.Sleep (1000);

        Cf.create (). Forpath ("/super/c2", "C2 content". GetBytes ());
        Modify child nodes Thread.Sleep (1000);

        Cf.setdata (). Forpath ("/super/c1", "C1 update content". GetBytes ());
        Delete child nodes Thread.Sleep (1000);       

        Cf.delete (). Forpath ("/super/c2");
        Delete itself node Thread.Sleep (1000);

        Cf.delete (). deletingchildrenifneeded (). Forpath ("/super");
        SYSTEM.OUT.PRINTLN ("------End------");

    Thread.Sleep (Integer.max_value); }
}

Run results

child_added:/super/c1
child_added Content: C1 content
child_added:/super/c2
child_added content: C2 content
Child_ UPDATED:/super/c1
child_updated content: C1 update content
child_removed:/super/c2
child_removed:/super/c1
----- -end------
curator Scene Application (i) Distributed lock function

In distributed scenarios, in order to ensure the consistency of the data, we often need to synchronize at one point in the program's operation (Java can provide synchronized or Reentrantlock implementations) For example, let's look at a small example that shows a problem with distributed synchronization:
Because what we said before is high concurrency access to a program, now we are in high concurrency access to multiple server nodes (distributed)
We use the distributed locks provided by curator based on the Zookeeper feature to handle data consistency in distributed scenarios, zookeeper native write distribution is cumbersome, and we strongly recommend the use of curator distributed locks.
Curator mainly uses Interprocessmutex to control distributed locks.

public class Lock2 {/** zookeeper address */static final String connect_addr = "192.168.1.31:2181,192.168.1.32:2181";
    /** SESSION Timeout time */static final int session_outtime = 20000;//ms static int count = 10;
            public static void Genarno () {try {count--;
        System.out.println (count); } finally {}} public static void Main (string[] args) throws Exception {//1 retry policy: initial time is 1s retry 10
        Secondary Retrypolicy retrypolicy = new Exponentialbackoffretry (1000, 10); 2 Create a connection through the factory curatorframework CF = Curatorframeworkfactory.builder (). ConnectString (Connect_ad                  DR). SESSIONTIMEOUTMS (Session_outtime). Retrypolicy (Retrypolicy)//
        . namespace ("Super"). Build ();

        3 Open Connection Cf.start ();

        4 Distributed lock final Countdownlatch countdown = new Countdownlatch (1); for (int i = 0; i <; i++){New Thread (new Runnable () {@Override public void run () {
                    Interprocessmutex lock = new Interprocessmutex (cf, "/super");
                        try {countdown.await ();
                        Locking Lock.acquire ();
                        -------------Business Processing begins genarno (); SimpleDateFormat SDF = new SimpleDateFormat ("hh:mm:ss|

                        SSS ");
                        Thread.Sleep (500);
                        System.out.println (Thread.CurrentThread (). GetName () + "perform this operation");
                    -------------Business Processing End} catch (Exception e) {e.printstacktrace (); } finally {try {//release Lock.rele
                        ASE ();
         } catch (Exception e) {e.printstacktrace ();               }}}, "T" + i). Start ();
        } thread.sleep (100);
    Countdown.countdown (); }
}

Output

9
T9 do this
8 T8 do this
7 T3 perform this action
6 t0 do this 5 T6 do this
4
T7 do this
3
T2 do this 2 T5 perform this action
1
T4 do this
0
t1 do this

We can see that here new 10 threads, but each thread has its own lock, according to the truth, their various parts of the interference, but from the results can be seen, the program is synchronous, but also implemented the principle of the lock. (equivalent to different programs placed on different machines, there are similar effects). Distributed counter function

When it comes to distributed counters, you might think of the classic way of Atomicinteger, if there's no problem with a JVM scenario, but we're in a distributed scenario, We need to use the Distributedatomicinteger of the curator framework.
Code

public class Curatoratomicinteger {/** zookeeper address */static final String connect_addr = "192.168.1.31:2181,192.
    168.1.32:2181 "; /** SESSION Timeout time */static final int session_outtime = 5000;//ms public static void Main (string[] args) throws Ex
        ception {//1 retry policy: initial time is 1s retry 10 times retrypolicy retrypolicy = new Exponentialbackoffretry (1000, 10); 2 Create a connection through the factory curatorframework CF = Curatorframeworkfactory.builder (). ConnectString (connect_
                    ADDR). SESSIONTIMEOUTMS (Session_outtime). Retrypolicy (Retrypolicy)
        . build ();
        3 Open Connection Cf.start ();


        Cf.delete (). Forpath ("/super"); 4 using Distributedatomicinteger Distributedatomicinteger Atomicintger = new Distributedatomicinteg
        ER (cf, "/super", New Retryntimes (3, 1000));  Atomicintger.forceset (0);

        First time you need to have it. atomicvalue<integer> value = ATOMICINTGEr.add (1);
Atomicintger.increment ();
        atomicvalue<integer> value = Atomicintger.get ();
        System.out.println (value.succeeded ());  System.out.println (Value.postvalue ());   Latest Value System.out.println (Value.prevalue ()); Original Value}}

Run for the first time

True
1
0

Second run

True
2
1

Third run

True
3
2

In fact, this also simulates the distributed Count function barrier function

There are scenarios where multiple programs are in different machines, waiting to be ready and running together.
There are two ways, one is to wait for all to run together, one is to have a switch, this switch opens and runs, directly look at the code
Way one: Estimated not used

public class CuratorBarrier1 {/** zookeeper address */static final String connect_addr = "192.168.1.31:2181,192.168.1
    .32:2,181 "; /** SESSION Timeout time */static final int session_outtime = 5000;//ms public static void Main (string[] args) throws Ex
                ception {for (int i = 0; i < 5; i++) {New Thread (new Runnable () {@Override public void Run () {try {retrypolicy retrypolicy = new Exponentia
                        Lbackoffretry (1000, 10);
                                    Curatorframework CF = Curatorframeworkfactory.builder (). ConnectString (CONNECT_ADDR)
                        . Retrypolicy (Retrypolicy). build ();

                        Cf.start ();
                        Distributeddoublebarrier barrier = new Distributeddoublebarrier (cf, "/super", 5); 
   Thread.Sleep (New Random ()). Nextint (3));                     System.out.println (Thread.CurrentThread (). GetName () + "ready");
                        Barrier.enter ();
                        System.out.println ("Start running at the same time ...");
                        Thread.Sleep (New Random ()). Nextint (3));
                        System.out.println (Thread.CurrentThread (). GetName () + "run Complete");
                        Barrier.leave ();
                    SYSTEM.OUT.PRINTLN ("Quit running at the same time ...");
                    } catch (Exception e) {e.printstacktrace ();
        }}}, "T" + i). Start (); }

    }
}

Run results

T4 is ready to T2 already ready T0 is ready to
T1 already ready
T3 is ready to
start running
at the same time ... Start running
at the same time ... T2 run
and start running
... T4 run
and start running
... T0 run
and start running
... T3 Run finished
T1 run
and exit run
... Quit running
at the same time ... Quit running
at the same time ... Quit running
at the same time ... Quit running at the same time ...

Way two: May use more, closest actually some
The code is as follows

public class CuratorBarrier2 {

    /** zookeeper address */
    static final String connect_addr = " 192.168.1.31:2181,192.168.1.32:2181 ";
    /** Session Timeout Period */
    

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.