我們常說的鎖是單進程多線程鎖,在多線程併發編程中,用於線程之間的數據同步,保護共用資源的訪問。而分散式鎖,指在分散式環境下,保護跨進程、跨主機、跨網路的共用資源,實現互斥訪問,保證一致性。 架構圖: 分散式鎖獲取思路a、在獲取分散式鎖的時候在locker節點下創建臨時順序節點,釋放鎖的時候刪除該臨時 ...
我們常說的鎖是單進程多線程鎖,在多線程併發編程中,用於線程之間的數據同步,保護共用資源的訪問。而分散式鎖,指在分散式環境下,保護跨進程、跨主機、跨網路的共用資源,實現互斥訪問,保證一致性。
架構圖:
分散式鎖獲取思路
a、在獲取分散式鎖的時候在locker節點下創建臨時順序節點,釋放鎖的時候刪除該臨時節點。
b、客戶端調用createNode方法在locker下創建臨時順序節點,然後調用getChildren(“locker”)來獲取locker下麵的所有子節點,註意此時不用設置任何Watcher。
c、客戶端獲取到所有的子節點path之後,如果發現自己創建的子節點序號最小,那麼就認為該客戶端獲取到了鎖。
d、如果發現自己創建的節點並非locker所有子節點中最小的,說明自己還沒有獲取到鎖,此時客戶端需要找到比自己小的那個節點,然後對其調用exist()方法,同時對其註冊事件監聽器。
e、之後,讓這個被關註的節點刪除,則客戶端的Watcher會收到相應通知,此時再次判斷自己創建的節點是否是locker子節點中序號最小的,如果是則獲取到了鎖,如果不是則重覆以上步驟繼續獲取到比自己小的一個節點並註冊監聽。
實現代碼:
import org.I0Itec.zkclient.IZkDataListener; import org.I0Itec.zkclient.ZkClient; import org.I0Itec.zkclient.exception.ZkNoNodeException; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; public class BaseDistributedLock { private final ZkClientExt client; private final String path; private final String basePath; private final String lockName; private static final Integer MAX_RETRY_COUNT = 10; public BaseDistributedLock(ZkClientExt client, String path, String lockName){ this.client = client; this.basePath = path; this.path = path.concat("/").concat(lockName); this.lockName = lockName; } // 刪除成功獲取鎖之後所創建的那個順序節點 private void deleteOurPath(String ourPath) throws Exception{ client.delete(ourPath); } // 創建臨時順序節點 private String createLockNode(ZkClient client, String path) throws Exception{ return client.createEphemeralSequential(path, null); } // 等待比自己次小的順序節點的刪除 private boolean waitToLock(long startMillis, Long millisToWait, String ourPath) throws Exception{ boolean haveTheLock = false; boolean doDelete = false; try { while ( !haveTheLock ) { // 獲取/locker下的經過排序的子節點列表 List<String> children = getSortedChildren(); // 獲取剛纔自己創建的那個順序節點名 String sequenceNodeName = ourPath.substring(basePath.length()+1); // 判斷自己排第幾個 int ourIndex = children.indexOf(sequenceNodeName); if (ourIndex < 0){ // 網路抖動,獲取到的子節點列表裡可能已經沒有自己了 throw new ZkNoNodeException("節點沒有找到: " + sequenceNodeName); } // 如果是第一個,代表自己已經獲得了鎖 boolean isGetTheLock = ourIndex == 0; // 如果自己沒有獲得鎖,則要watch比我們次小的那個節點 String pathToWatch = isGetTheLock ? null : children.get(ourIndex - 1); if ( isGetTheLock ){ haveTheLock = true; } else { // 訂閱比自己次小順序節點的刪除事件 String previousSequencePath = basePath .concat( "/" ) .concat( pathToWatch ); final CountDownLatch latch = new CountDownLatch(1); final IZkDataListener previousListener = new IZkDataListener() { public void handleDataDeleted(String dataPath) throws Exception { latch.countDown(); // 刪除後結束latch上的await } public void handleDataChange(String dataPath, Object data) throws Exception { // ignore } }; try { //訂閱次小順序節點的刪除事件,如果節點不存在會出現異常 client.subscribeDataChanges(previousSequencePath, previousListener); if ( millisToWait != null ) { millisToWait -= (System.currentTimeMillis() - startMillis); startMillis = System.currentTimeMillis(); if ( millisToWait <= 0 ) { doDelete = true; // timed out - delete our node break; } latch.await(millisToWait, TimeUnit.MICROSECONDS); // 在latch上await } else { latch.await(); // 在latch上await } // 結束latch上的等待後,繼續while重新來過判斷自己是否第一個順序節點 } catch ( ZkNoNodeException e ) { //ignore } finally { client.unsubscribeDataChanges(previousSequencePath, previousListener); } } } } catch ( Exception e ) { //發生異常需要刪除節點 doDelete = true; throw e; } finally { //如果需要刪除節點 if ( doDelete ) { deleteOurPath(ourPath); } } return haveTheLock; } private String getLockNodeNumber(String str, String lockName) { int index = str.lastIndexOf(lockName); if ( index >= 0 ) { index += lockName.length(); return index <= str.length() ? str.substring(index) : ""; } return str; } // 獲取/locker下的經過排序的子節點列表 List<String> getSortedChildren() throws Exception { try{ List<String> children = client.getChildren(basePath); Collections.sort( children, new Comparator<String>() { public int compare(String lhs, String rhs) { return getLockNodeNumber(lhs, lockName).compareTo(getLockNodeNumber(rhs, lockName)); } } ); return children; } catch (ZkNoNodeException e){ client.createPersistent(basePath, true); return getSortedChildren(); } } protected void releaseLock(String lockPath) throws Exception{ deleteOurPath(lockPath); } protected String attemptLock(long time, TimeUnit unit) throws Exception { final long startMillis = System.currentTimeMillis(); final Long millisToWait = (unit != null) ? unit.toMillis(time) : null; String ourPath = null; boolean hasTheLock = false; boolean isDone = false; int retryCount = 0; //網路閃斷需要重試一試 while ( !isDone ) { isDone = true; try { // 在/locker下創建臨時的順序節點 ourPath = createLockNode(client, path); // 判斷自己是否獲得了鎖,如果沒有獲得那麼等待直到獲得鎖或者超時 hasTheLock = waitToLock(startMillis, millisToWait, ourPath); } catch ( ZkNoNodeException e ) { // 捕獲這個異常 if ( retryCount++ < MAX_RETRY_COUNT ) { // 重試指定次數 isDone = false; } else { throw e; } } } if ( hasTheLock ) { return ourPath; } return null; } }
import java.util.concurrent.TimeUnit; public interface DistributedLock { /* * 獲取鎖,如果沒有得到就等待 */ public void acquire() throws Exception; /* * 獲取鎖,直到超時 */ public boolean acquire(long time, TimeUnit unit) throws Exception; /* * 釋放鎖 */ public void release() throws Exception; }
import java.io.IOException; import java.util.concurrent.TimeUnit; public class SimpleDistributedLockMutex extends BaseDistributedLock implements DistributedLock { //鎖名稱首碼,成功創建的順序節點如lock-0000000000,lock-0000000001,... private static final String LOCK_NAME = "lock-"; // zookeeper中locker節點的路徑 private final String basePath; // 獲取鎖以後自己創建的那個順序節點的路徑 private String ourLockPath; private boolean internalLock(long time, TimeUnit unit) throws Exception { ourLockPath = attemptLock(time, unit); return ourLockPath != null; } public SimpleDistributedLockMutex(ZkClientExt client, String basePath){ super(client,basePath,LOCK_NAME); this.basePath = basePath; } // 獲取鎖 public void acquire() throws Exception { if ( !internalLock(-1, null) ) { throw new IOException("連接丟失!在路徑:'"+basePath+"'下不能獲取鎖!"); } } // 獲取鎖,可以超時 public boolean acquire(long time, TimeUnit unit) throws Exception { return internalLock(time, unit); } // 釋放鎖 public void release() throws Exception { releaseLock(ourLockPath); } }
import org.I0Itec.zkclient.serialize.BytesPushThroughSerializer; public class TestDistributedLock { public static void main(String[] args) { final ZkClientExt zkClientExt1 = new ZkClientExt("192.168.1.105:2181", 5000, 5000, new BytesPushThroughSerializer()); final SimpleDistributedLockMutex mutex1 = new SimpleDistributedLockMutex(zkClientExt1, "/Mutex"); final ZkClientExt zkClientExt2 = new ZkClientExt("192.168.1.105:2181", 5000, 5000, new BytesPushThroughSerializer()); final SimpleDistributedLockMutex mutex2 = new SimpleDistributedLockMutex(zkClientExt2, "/Mutex"); try { mutex1.acquire(); System.out.println("Client1 locked"); Thread client2Thd = new Thread(new Runnable() { public void run() { try { mutex2.acquire(); System.out.println("Client2 locked"); mutex2.release(); System.out.println("Client2 released lock"); } catch (Exception e) { e.printStackTrace(); } } }); client2Thd.start(); Thread.sleep(5000); mutex1.release(); System.out.println("Client1 released lock"); client2Thd.join(); } catch (Exception e) { e.printStackTrace(); } } }
import org.I0Itec.zkclient.ZkClient; import org.I0Itec.zkclient.serialize.ZkSerializer; import org.apache.zookeeper.data.Stat; import java.util.concurrent.Callable; public class ZkClientExt extends ZkClient { public ZkClientExt(String zkServers, int sessionTimeout, int connectionTimeout, ZkSerializer zkSerializer) { super(zkServers, sessionTimeout, connectionTimeout, zkSerializer); } @Override public void watchForData(final String path) { retryUntilConnected(new Callable<Object>() { public Object call() throws Exception { Stat stat = new Stat(); _connection.readData(path, stat, true); return null; } }); } }