A simple example of ZooKeeper (3.4.5) Open-source client Curator: zookeepercurator
1. Create a session 1. Create a session
Package com. huey. dream. demo; import org. apache. curator. framework. curatorFramework; import org. apache. curator. framework. curatorFrameworkFactory; import org. apache. curator. retry. exponentialBackoffRetry;/*** use Curator to create a session * @ author huey * @ version 1.0 * @ created 2015-3-1 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. newClient ("192.168.1.109: 2181", // server list 5000, // Session Timeout, in milliseconds 3000, // connection creation timeout, in milliseconds new ExponentialBackoffRetry (1000, 3) // retry policy); client. start (); client. close ();}}
2. Create a session using a chained API
Package com. huey. dream. demo; import org. apache. curator. framework. curatorFramework; import org. apache. curator. framework. curatorFrameworkFactory; import org. apache. curator. retry. exponentialBackoffRetry; /*** use a chained API to create a session * @ author huey * @ version 1.0 * @ created 2015-3-1 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. builder (). connectString ("192.168.1.109: 2181 "). sessionTimeoutMs (5000 ). connectionTimeoutMs (3000 ). retryPolicy (new ExponentialBackoffRetry (1000, 3 )). build (); client. start (); client. close ();}}
Ii. Create a node
Package com. huey. dream. demo; import org. apache. curator. framework. curatorFramework; import org. apache. curator. framework. curatorFrameworkFactory; import org. apache. curator. retry. exponentialBackoffRetry; import org. apache. zookeeper. createMode;/*** use Curator to create a node * @ author huey * @ version 1.0 * @ created 2015-3-1 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. builder (). connectString ("192.168.1.109: 2181 "). sessionTimeoutMs (5000 ). connectionTimeoutMs (3000 ). retryPolicy (new ExponentialBackoffRetry (1000, 3 )). build (); client. start (); client. create (). creatingParentsIfNeeded (). withMode (CreateMode. PERSISTENT ). forPath ("/zk-huey/cnode", "hello ". getBytes (); client. close ();}}
3. delete a node
Package com. huey. dream. demo; import org. apache. curator. framework. curatorFramework; import org. apache. curator. framework. curatorFrameworkFactory; import org. apache. curator. retry. exponentialBackoffRetry; import org. apache. zookeeper. createMode;/*** use Curator to delete a node * @ author huey * @ version 1.0 * @ created 2015-3-1 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. builder (). connectString ("192.168.1.109: 2181 "). sessionTimeoutMs (5000 ). connectionTimeoutMs (3000 ). retryPolicy (new ExponentialBackoffRetry (1000, 3 )). build (); client. start (); client. create (). creatingParentsIfNeeded (). withMode (CreateMode. PERSISTENT ). forPath ("/zk-huey/cnode", "hello ". getBytes (); client. delete (). guaranteed (). deletingChildrenIfNeeded (). withVersion (-1 ). forPath ("/zk-huey"); client. close ();}}
4. Read node data
Package com. huey. dream. demo; import org. apache. curator. framework. curatorFramework; import org. apache. curator. framework. curatorFrameworkFactory; import org. apache. curator. retry. exponentialBackoffRetry; import org. apache. zookeeper. createMode; import org. apache. zookeeper. data. stat;/*** use Curator to read node data * @ author huey * @ version 1.0 * @ created 2015-3-1 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. builder (). connectString ("192.168.1.109: 2181 "). sessionTimeoutMs (5000 ). connectionTimeoutMs (3000 ). retryPolicy (new ExponentialBackoffRetry (1000, 3 )). build (); client. start (); client. create (). creatingParentsIfNeeded (). withMode (CreateMode. PERSISTENT ). forPath ("/zk-huey/cnode", "hello ". getBytes (); Stat stat = new Stat (); byte [] nodeData = client. getData (). storingStatIn (stat ). forPath ("/zk-huey/cnode"); System. out. println ("NodeData:" + new String (nodeData); System. out. println ("Stat:" + stat); client. close ();}}
5. Update node data
Package com. huey. dream. demo; import org. apache. curator. framework. curatorFramework; import org. apache. curator. framework. curatorFrameworkFactory; import org. apache. curator. retry. exponentialBackoffRetry; import org. apache. zookeeper. createMode; import org. apache. zookeeper. data. stat;/*** use Curator to update node data * @ author huey * @ version 1.0 * @ created 2015-3-1 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. builder (). connectString ("192.168.1.109: 2181 "). sessionTimeoutMs (5000 ). connectionTimeoutMs (3000 ). retryPolicy (new ExponentialBackoffRetry (1000, 3 )). build (); client. start (); client. create (). creatingParentsIfNeeded (). withMode (CreateMode. PERSISTENT ). forPath ("/zk-huey/cnode", "hello ". getBytes (); client. setData (). withVersion (-1 ). forPath ("/zk-huey/cnode", "world ". getBytes (); client. close ();}}
6. Obtain the subnode list
Package com. huey. dream. demo; import java. util. list; import org. apache. curator. framework. curatorFramework; import org. apache. curator. framework. curatorFrameworkFactory; import org. apache. curator. retry. exponentialBackoffRetry; import org. apache. zookeeper. createMode;/*** use Curator * @ author huey * @ version 1.0 * @ created 2015-3-1 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. builder (). connectString ("192.168.1.109: 2181 "). sessionTimeoutMs (5000 ). connectionTimeoutMs (3000 ). retryPolicy (new ExponentialBackoffRetry (1000, 3 )). build (); client. start (); client. create (). creatingParentsIfNeeded (). withMode (CreateMode. PERSISTENT ). forPath ("/zk-huey/cnode", "hello ". getBytes (); List <String> children = client. getChildren (). forPath ("/zk-huey"); System. out. println ("Children:" + children); client. close ();}}