1. Basic use
Org.apache.zookeeper.Zookeeper is the client Portal main class , which is responsible for establishing a session with the server. It provides the main methods shown in table 1:
function |
|
create |
Create in local directory tree One node |
delete |
Delete a node |
exists |
|
get/set data |
read/write from the target node |
get/set ACL |
get/Set Target node access control list information |
get children |
retrieving a list on a child node |
sync |
wait for data to be transmitted |
Table 1:zookeeper API Description
2.pom.xml Configuring a dependency package
Share a pom.xml in this article
<dependencies> <dependency> <groupId>junit</groupId> <artifactid>junit</arti factid> <version>4.10</version> </dependency> <dependency> <groupid>org.a Pache.zookeeper</groupid> <artifactId>zookeeper</artifactId> <version>3.4.6</version& Gt </dependency> <dependency> <groupId>jline</groupId> <artifactid>jline</artif actid> <version>0.9.94</version> </dependency> <dependency> <groupid>log4 j</groupid> <artifactId>log4j</artifactId> <version>1.2.17</version> </depen dency> <dependency> <groupId>io.netty</groupId> <ARTIFACTID>NETTY</ARTIFACTID&G T <version>3.9.4.Final</version> </dependency> <dependency> <groupid>org.slf4j</ Groupid&gT <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <DEP endency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> < ;version>1.6.1</version> </dependency> </dependencies>
3. Delete and change to check the demo
Zookeeper on the Znode node additions and deletions change to check the demo
Package Cn.itcast.zk;import Org.apache.zookeeper.*;import Org.apache.zookeeper.data.stat;import Org.junit.Before; Import Org.junit.test;import java.util.list;/** * @author y15079 * @create 2018-03-16 16:53 * @desc simple additions and deletions to check the demo **/public Class Simplezkclient {private static final String connectstring = "mimi1:2181,mimi2:2181,mini3:2181";p rivate static FINA l int sessiontimeout = 2000; ZooKeeper zkclient = null, @Beforepublic void init () throws Exception {zkclient = new ZooKeeper (connectstring, Sessiontimeo UT, new Watcher () {public void process (Watchedevent watchedevent) {//Receive event notification after the callback function (should be our own event handling logic) System.out.println ( Watchedevent.gettype () + "---" + watchedevent.getpath ()), try {zkclient.getchildren ("/", true);} catch (Exception e) {}}} );} /** * Data additions and deletions *///create a data node into ZK @testpublic void Testcreate (string[] args) throws exception{//parameter 1, the path of the node to be created parameter 2: node data parameter 3: node Permission parameter 4: The type of the node, string node = Zkclient.create ("/idea", "Hellozk". GetBytes (), ZooDefs.Ids.OPEN_ACL_UNSAFE, createmode.persistent);//uploaded data can beIs any type, but all must be turned into byte[]}//to determine whether Znode exists public void testexist () throws exception{//node meta-data Stat stat = zkclient.exists ("/idea", FALSE); SYSTEM.OUT.PRINTLN (stat = = null?) "Not exist": "exist");} Get child node @testpublic void GetChildren () throws exception{list<string> children = Zkclient.getchildren ("/", true); for (String Child:children) {System.out.println (child);} Thread.Sleep (Long.max_value);//Real runtime can annotate}//get Znode data @testpublic void GetData () throws exception{byte[] data = Zkclient.getdata ("/idea", false, NULL); System.out.println (new String (data));} Delete znode data @testpublic void Deleteznode () throws exception{//Parameter 2: Specify the version to be deleted, 1 means delete all versions Zkclient.delete ("/idea",-1);} Set znode@testpublic void SetData () throws Exception{zkclient.setdata ("/app1", "Hello". GetBytes (),-1); byte[] data = Zkclient.getdata ("/app1", false, NULL); System.out.println (new String (data));}}
3. Zookeeper application case (distributed shared lock | | Distributed Application ha) 3.1 Simple implementation of distributed shared lock
Package Cn.itcast.zkclock;import Org.apache.zookeeper.*;import Java.util.collections;import java.util.List;import java.util.random;/** * @author y15079 * @create 2018-03-17 18:40 * @desc distributed shared lock single thread **/public class Distributedclientlock {//Session timeout private static final int session_timeout = 5000;//zookeeper cluster address private String hosts = "hadoop1:2181,hadoop2:2181, hadoop3:2181 ";p rivate string groupnode =" Locks ";p rivate String subnode =" Sub ";p rivate boolean havelock = False;private Z Ookeeper zk;//Record Client-created child node path private volatile String thispath;/** * Connection ZK * @throws Exception */public void Connectzookeep ER () throws Exception{zk = new ZooKeeper (hosts, Session_timeout, New Watcher () {public void process (Watchedevent Watchedev ENT) {try {//To determine event type, where only child node change events are processed if (watchedevent.gettype () = = Event.EventType.NodeChildrenChanged && Watchedevent.getpath (). Equals ("/" + Groupnode)) {///Get child nodes, listen to the parent node, start listening, listen for lock changes list<string> Childrennodes = Zk.getchildren ("/" + Groupnode, True); String Thisnode = Thispath.substring (("/" + Groupnode + "/"). Length ()); Gets the child node path under the parent directory, without the parent directory//To compare whether it is the minimum ID, the minimum contract ID is the first to get the lock Collections.sort (childrennodes); if (Childrennodes.indexof ( Thisnode) = = 0) {//indexof is the meaning of the index, to see if the Sub-node path index in all child nodes is 0, 0 proves that the child node ID is the smallest, first acquires lock//access to the shared resource processing business, and deletes the lock dosomething () after processing is complete; Delete the lock, listen to, temporarily do not call Watcher inside the process method//re-register a new lock, do not understand why register a new lock Thispath = Zk.create ("/" + Groupnode + "/" + subnode, NULL, ZooDefs.Ids.OPEN_ACL_UNSAFE, createmode.ephemeral_sequential);//The Process method}} in Watcher is called after the lock is created above ( Exception e) {}});//The program will first register a lock to ZK, that is, to create a child node, has not started listening, ZK watcher inside the process method will not execute Thispath = zk.create ("/" + Groupnode + " /"+ subnode, NULL, ZooDefs.Ids.OPEN_ACL_UNSAFE, createmode.ephemeral_sequential);//wait a small meeting, easy to observe thread.sleep (new Random (). Nextint (1000));//from the ZK lock directory, get all the child nodes, and listen for changes in the child nodes, start listening, listen to the lock change list<string> childrennodes = Zk.getchildren ("/" + Groupnode, true);//The list has only one child node, which is definitely thispath, indicating that the client obtains the lock if (childrennodes.size () = = 1) { DoSomething (); Delete the lock, listen to, temporarily do not call Watcher inside the process method//Why also create a lock? Prepare for the next client acquisition lock Thispath = Zk.create ("/" + Groupnode + "/" + subnode, NULL, ZooDefs.Ids.OPEN_ACL_UNSAFE, Createmode.eph emeral_sequential);//The Process method inside the watcher is called when the lock is created above}}/** * The access logic of the shared resource is written in this method * processing business logic and releasing * @throws Exception */private void DoSomething () throws Exception{try {System.out.println ("Gain Lock:" + Thispath); Thread.Sleep (2000);} catch (Interruptedexception e) {e.printstacktrace ();} finally {System.out.println ("finished:" + Thispath),//Will thispath deleted, listener Thispath's client will be notified//equivalent to release lock Zk.delete ( This.thispath,-1);}} public static void Main (string[] args) throws Exception{distributedclientlock DCL = new Distributedclientlock (); Dcl.connectzookeeper (); Thread.Sleep (Long.max_value);}}
3.2 Simple implementation of distributed HA applications
Client implementations:
Package Cn.itcast.zkdist;import Org.apache.zookeeper.watchedevent;import Org.apache.zookeeper.watcher;import Org.apache.zookeeper.zookeeper;import java.util.arraylist;import java.util.list;/** * @author y15079 * @create 2018-03-17 15:20 * @desc distributed HA application client **/public class Distributedclient {private static final String connectstring = "had oop1:2181,hadoop2:2181,hadoop3:2181 ";p rivate static final int sessiontimeout = 2000;private static final String ParentNode = "/servers";//Note: What is the point of adding volatile? Maintain consistency private volatile list<string> serverlist;private ZooKeeper ZK = null;/** * Create client connection to ZK * @throws Exception */pu Blic void GetConnect () throws Exception{zk = new ZooKeeper (connectstring, Sessiontimeout, New Watcher () {public void Proce SS (Watchedevent watchedevent) {//Receive the event notification after the callback function (should be our own event handling logic) try {//re-update the server list, and register the Listener getserverlist ();} catch ( Exception e) {}}}); /** * Get Server information list * @throws Exception */public void Getserverlist () throws exception{//Get Server child node information and listen to the parent node list<string > ChiLdren = Zk.getchildren (ParentNode, true);//Create a local list to store server information list<string> servers = new arraylist<string > (); for (String Child:children) {//child is only the node name of the child node byte[] data = zk.getdata (parentnode + "/" + children, false, NULL); server S.add (new String (data));} Assign the servers to the member variable serverlist, which has been provided to each business thread using ServerList = servers;//print server list System.out.println (serverlist);} /** * Business functions * @throws Exception */public void Handlebusiness () throws Exception{system.out.println ("client start working". ...."); Thread.Sleep (Long.max_value);} public static void Main (string[] args) throws Exception {//Get ZK connection Distributedclient client = new Distributedclient (); Clien T.getconnect ();//Gets the child node information of servers (and listens), obtains the Server information list client.getserverlist ();//Start Business function client.handlebusiness ();}}
Server-side implementation:
Package Cn.itcast.zkdist;import org.apache.zookeeper.*;/** * @author y15079 * @create 2018-03-17 1:12 * @desc distributed HA Application Server **/public class Distributedserver {private static final String connectstring = "hadoop1:2181,hadoop2:2181,hadoop3:2181" ;p rivate static final int sessiontimeout = 2000;private static final String parentnode = "/servers";p rivate ZooKeeper ZK = null;/** * Create a client connection to ZK * @throws Exception */public void GetConnect () throws Exception{zk = new ZooKeeper (connectstring, Sessiontimeout, New Watcher () {public void process (Watchedevent event) {// The callback function (which should be our own event handling logic) after receiving the event notification System.out.println (Event.gettype () + "---" + event.getpath ()); try {Zk.getchildren ("/", true);} catch (Exception e) {}}}); /** * Register server information with the ZK cluster * @param hostname * @throws Exception */public void RegisterServer (String hostname) throws Exception{st Ring Create = zk.create (parentnode+ "Server", Hostname.getbytes (), ZooDefs.Ids.OPEN_ACL_UNSAFE, Createmode.ephemeral_ sequential); SYSTEM.OUT.PRINTLN (hostname + "is online... "+ create);} /** * Business functions * @throws Exception */public void handlebusiness (String hostname) throws EXCEPTION{SYSTEM.OUT.PRINTLN (hostname + "start working ..."); Thread.Sleep (Long.max_value);} public static void Main (string[] args) throws Exception {//Get ZK connection Distributedserver server = new Distributedserver (); serve R.getconnect ();//Use ZK connection to register server information Server.registerserver (args[0]);//Start Business function server.handlebusiness (Args[0]);}}
GitHub Address:
Https://github.com/qiushangwenyue/Zookeeper-Java-API-Demo
Java-side API application for zookeeper