Zookeeper the actual combat configuration service

Source: Internet
Author: User
Zookeeper The actual combat configuration service

The recent collation of information found two years ago I wrote some examples of zookeeper, today sorted out, put here, perhaps later. Server is used to start a zookeeper service.

Package my.zookeeperstudy.config;

Import org.apache.zookeeper.server.ServerCnxnFactory;
Import Org.apache.zookeeper.server.ZooKeeperServer;

Import Java.io.File;
Import java.net.InetSocketAddress;

public class Server {public

    static void Main (string[] args) throws Exception {
        int ticktime =;
        int maxclientcnxns =;
        File dir = new file (System.getproperty ("Java.io.tmpdir"), "Zookeeper"). Getabsolutefile ();
        Zookeeperserver zkserver = new Zookeeperserver (dir, dir, ticktime);
        Servercnxnfactory standaloneserverfactory = servercnxnfactory.createfactory (new inetsocketaddress (2181), MAXCLIENTCNXNS);
        Standaloneserverfactory.startup (Zkserver);
    }

Config configuration class, which contains the configuration items and methods to be manipulated, and so on.
Package my.zookeeperstudy.config;

Import org.apache.zookeeper.*;
Import java.io.IOException;
Import java.util.Collections;

Import java.util.List;

    public class Config implements watcher {private Zookeeper ZK;

    Private String BasePath = "/config"; Public Config () {} public void Connect (String hosts) throws IOException, Keeperexception, interruptedexception
        {This.zk = new Zookeeper (hosts, 1000, this); if (Zk.exists (basepath, this) = null) {zk.create (basepath, NULL, ZooDefs.Ids.OPEN_ACL_UNSAFE, Createmode.per
        Sistent); } public void set (string key, String value) throws Interruptedexception, Keeperexception {string path
        = BasePath + "/" + key; if (zk.exists (path, this) = = null) {zk.create (path, value.getbytes (), ZooDefs.Ids.OPEN_ACL_UNSAFE, Createmode .
        Persistent);
        else {zk.setdata (path, value.getbytes (),-1); } public string get (string key) thRows Interruptedexception, keeperexception {String path = BasePath + "/" + key;
            if (zk.exists (path, this)!= null) {byte[] data = zk.getdata (path, this, null);
        return new String (data);
    return null; public void Printconfig () throws Interruptedexception, keeperexception {System.out.println ("-----Begin-
        ---");
        list<string> children = Zk.getchildren (BasePath, false);
        Collections.sort (children);
        for (String Child:children) {System.out.println [child + ': ' + Get (child)];
    } System.out.println ("-----End-----"); public void Clear () throws Keeperexception, interruptedexception {if (Zk.exists (BasePath, false)!= null)
            {list<string> children = Zk.getchildren (BasePath, false);
                for (String Child:children) {zk.delete (BasePath + "/" + Child,-1); System.out.println ("Deleted"+ BasePath +"/"+ child";
        } zk.delete (BasePath,-1);
        } @Override public void process (Watchedevent event) {System.out.println ("event:" + event);
                if (event.gettype () = = Event.EventType.NodeDataChanged | |
            Event.gettype () = = Event.EventType.NodeChildrenChanged) {try {printconfig ();
            catch (Interruptedexception e) {e.printstacktrace ();
            catch (Keeperexception e) {e.printstacktrace ();
 }
        }
    }
}
Clienta and CLIENTB-two clients, analog configuration modification and synchronization

Clienta.java

Package my.zookeeperstudy.config;

public class Clienta {public
    static void Main (string[] args) throws Exception {
        config config = new Config (); 
  
   config.connect ("localhost:2181");

        while (true) {
            thread.sleep (1000);
            Config.get ("MyKey");}}


  

Clientb.java

Package my.zookeeperstudy.config;

public class ClientB {public
    static void Main (string[] args) throws Exception {
        config config = new Config (); 
  config.connect ("localhost:2181");

        for (int i = 0; I < 10000 i++) {
            config.set ("MyKey", "myvalue_" + i);
            Thread.Sleep (5 * 1000);
        }

        Config.clear ();
    }
Test
Start the server class first, then start Clienta and CLIENTB, and then observe the output of Clienta and CLIENTB.

Reprint please link the form to indicate this article link
This article link: http://blog.csdn.net/kongxx/article/details/51457636

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.