The Zookeeper distributed a (Zookeeper distributed lock and cluster)

  When it comes to distributed development, I have to say is a zookeeper; zookeeper's official website when it comes to Apache ZooKeeper dedicated to the development and maintenance can achieve a highly reliable distributed coordination of open source server . So exist zk as a coordinator, it is part of a distributed than essential. Ado, directly on the dry

  Zookeeper ( https://zookeeper.apache.org/ ) installation package can be acquired directly on the official website, https://zookeeper.apache.org/doc/current/zookeeperStarted.html here are some of the commonly used simple command Zookeeper, we can try to try it.

  Here's distributed lock, use it in the scene; for example: We often say that the shock group effect, Zookeeper cluster first to read caching. It was mentioned here may be implemented using a distributed lock redis, in fact, the official website redis contrast and narrative Zookeeper, we can clearly find: Zookeeper do more than Redis for Distributed Lock. Zookeeper guarantee the first order and is its consistency, followed by its atomic. You can go to find out details. After further distinguish these, we can try to think next Zookeeper distributed lock how it is going to achieve? Depending on what to achieve?

  You can think of how to get to each node and child node Zookeeper change, Watch mechanism Zookeeper sufficient to achieve this. It can clearly monitor changes to each node through Zookeeper Watch mechanism. Naturally also used here Zookeeper's third-party clients. Zookeeper third-party clients there are two; one is zkclient, it is a curator. In the curator of its own distributed lock has been achieved, interested can go and see it to realize source.

  After a third-party client to connect to Zookeeper, you can begin to implement a distributed lock. Exclusive lock, clogging, reentrancy. The default on the realization of exclusive zk, zk nodes must be unique. Zookeeper distributed lock using a temporary order of nodes to achieve, first obtain a lock, create a temporary order of the nodes of a Zookeeper; and the need for a fence (CountDownLatch), to ensure that everyone got their number, that is, on the same starting line. And then began to grab the lock, to a starting gun (CountDown). Then grab the lock and went to perform their business, watch again monitor data changes node, then the request thread to be blocked waiting for, and then delete the next node, the lock is released.

 

 1 public boolean tryLock() {
 2         
 3         if(currentPath.get() == null || !client.exists(currentPath.get())) {
 4             String node = client.createEphemeralSequential(LockPath+"/", "locked");
 5             currentPath.set(node);
 6         }
 7         
 8         
 9         List<String> children =client.getChildren(LockPath);
10         // 排序list
11         Collections.sort(children);
12         
13         // 判断当前节点是否是最小的
14         if(currentPath.get().equals(LockPath+"/"+children.get(0))) {
15             return true;
16         }else {
17             
18             int curIndex = children.indexOf(currentPath.get().substring(LockPath.length() + 1));
19             String bnode = LockPath+"/"+children.get(curIndex -1);
20             beforePath.set(bnode);
21         }
22         return false;
23     }
24 
25 
26     public void lock() {
27         if(!tryLock ()) {
 28              // blocked waiting for the release of the lock 
29              waitForLock ();
 30              // re-grab the lock 
31 is              Lock ();
 32          }
 33 is      }
 34 is      Private  void waitForLock () {
 35          
36          a CountDownLatch CDL = new new a CountDownLatch (. 1 ) ;
 37          // with zkwatcher event notification 
38 is              IZkDataListener listener = new new IZkDataListener () {
 39  
40              public  void handleDataDeleted (String dataPath) throws Exception {
41 is                  System.out.println ( "node is deleted ZK ================ ================" );
 42 is                  CDL. the countDown ();
 43 is              }
 44 is  
45              public  void handleDataChange (dataPath String, Object data) throws Exception {
 46 is              }
 47          };
 48          // Watcher / data changes ZK 
49          client.subscribeDataChanges (beforePath.get (), listener);
 50          
51 is          // the request thread to wait for blocking 
52 is          IF (client.exists (beforePath.get ())) {
 53 is              the try {
 54 is                 cdl.await ();
 55              } the catch (InterruptedException E) {
 56 is                  e.printStackTrace ();
 57 is              }
 58          }
 59          // unregister event 
60          client.unsubscribeDataChanges (beforePath.get (), listener);
 61 is      } 

far a distribution type lock is achieved.

  Zookeeper cluster of pseudo went on to say. One set up when the need to pay attention:
1, DataDir position.
2, Zookeeper port numbers
3,


4, myid file, myid DataDir file is created in the directory, myid document is to give a number of id, id number to be consistent with the number server.1 ... behind.
Then three servers are started, note the log message server output. Not surprisingly, then Zookeeper cluster can build a success!

 

 

 

Guess you like

Origin www.cnblogs.com/zhanvo/p/11589235.html