1.定義分布式鎖接口
package com.ljq.lock; import java.util.concurrent.TimeUnit; public interface DistributedLock { /** * 獲取鎖,如果沒有得到鎖就一直等待 * * @throws Exception */ public void acquire() throws Exception; /** * 獲取鎖,如果沒有得到鎖就一直等待直到超時 * * @param time 超時時間 * @param unit time參數時間單位 * * @return 是否獲取到鎖 * @throws Exception */ public boolean acquire(long time, TimeUnit unit) throws Exception; /** * 釋放鎖 * * @throws Exception */ public void release() throws Exception; }
2.定義一個簡單的互斥鎖
定義一個互斥鎖類,實現以上定義的鎖接口,同時繼承一個基類BaseDistributedLock,該基類主要用於與Zookeeper交互,包含一個嘗試獲取鎖的方法和一個釋放鎖。
package com.ljq.lock; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.I0Itec.zkclient.ZkClient; public class SimpleDistributedLock extends BaseDistributedLock implements DistributedLock { /* * 用於保存Zookeeper中實現分布式鎖的節點,如名稱為locker:/locker, * 該節點應該是持久節點,在該節點下面創建臨時順序節點來實現分布式鎖 */ private final String basePath; /* * 鎖名稱前綴,locker下創建的順序節點例如都以lock-開頭,這樣便於過濾無關節點 * 這樣創建后的節點類似:lock-00000001,lock-000000002 */ private static final String LOCK_NAME = "lock-"; /* 用於保存某個客戶端在locker下面創建成功的順序節點,用於后續相關操作使用(如判斷) */ private String ourLockPath; /** * 傳入Zookeeper客戶端連接對象,和basePath * * @param client * Zookeeper客戶端連接對象 * @param basePath * basePath是一個持久節點 */ public SimpleDistributedLock(ZkClient client, String basePath) { /* * 調用父類的構造方法在Zookeeper中創建basePath節點,並且為basePath節點子節點設置前綴 * 同時保存basePath的引用給當前類屬性 */ super(client, basePath, LOCK_NAME); this.basePath = basePath; } /** * 用於獲取鎖資源,通過父類的獲取鎖方法來獲取鎖 * * @param time 獲取鎖的超時時間 * @param unit 超時時間單位 * * @return 是否獲取到鎖 * @throws Exception */ private boolean internalLock(long time, TimeUnit unit) throws Exception { // 如果ourLockPath不為空則認為獲取到了鎖,具體實現細節見attemptLock的實現 ourLockPath = attemptLock(time, unit); return ourLockPath != null; } /** * 獲取鎖,如果沒有得到鎖就一直等待 * * @throws Exception */ public void acquire() throws Exception { // -1表示不設置超時時間,超時由Zookeeper決定 if (!internalLock(-1, null)) { throw new IOException("連接丟失!在路徑:'" + basePath + "'下不能獲取鎖!"); } } /** * 獲取鎖,如果沒有得到鎖就一直等待直到超時 * * @param time 超時時間 * @param unit time參數時間單位 * * @return 是否獲取到鎖 * @throws Exception */ public boolean acquire(long time, TimeUnit unit) throws Exception { return internalLock(time, unit); } /** * 釋放鎖 */ public void release() throws Exception { releaseLock(ourLockPath); System.out.println(ourLockPath + "鎖已釋放..."); } }
3. 分布式鎖的實現細節
獲取分布式鎖的重點邏輯在於BaseDistributedLock,實現了基於Zookeeper實現分布式鎖的細節。
package com.ljq.lock; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.I0Itec.zkclient.IZkDataListener; import org.I0Itec.zkclient.ZkClient; import org.I0Itec.zkclient.exception.ZkNoNodeException; public class BaseDistributedLock { private final ZkClient client; //Zookeeper客戶端 private final String basePath; //用於保存Zookeeper中實現分布式鎖的節點,例如/locker節點,該節點是個持久節點,在該節點下面創建臨時順序節點來實現分布式鎖 private final String path; //同basePath變量一樣 private final String lockName; //鎖名稱前綴,/locker下創建的順序節點,例如以lock-開頭,這樣便於過濾無關節點 private static final Integer MAX_RETRY_COUNT = 10; //最大重試次數 public BaseDistributedLock(ZkClient client, String path, String lockName) { this.client = client; this.basePath = path; this.path = path.concat("/").concat(lockName); this.lockName = lockName; } /** * 刪除節點 * * @param path * @throws Exception */ private void deletePath(String path) throws Exception { client.delete(path); } /** * 創建臨時順序節點 * * @param client Zookeeper客戶端 * @param path 節點路徑 * @return * @throws Exception */ private String createEphemeralSequential(ZkClient client, String path) throws Exception { return client.createEphemeralSequential(path, null); } /** * 獲取鎖的核心方法 * * @param startMillis 當前系統時間 * @param millisToWait 超時時間 * @param path * @return * @throws Exception */ private boolean waitToLock(long startMillis, Long millisToWait, String path) throws Exception { boolean haveTheLock = false; //獲取鎖標志 boolean doDelete = false; //刪除鎖標志 try { while (!haveTheLock) { // 獲取/locker節點下的所有順序節點,並且從小到大排序 List<String> children = getSortedChildren(); // 獲取子節點,如:/locker/node_0000000003返回node_0000000003 String sequenceNodeName = path.substring(basePath.length() + 1); // 計算剛才客戶端創建的順序節點在locker的所有子節點中排序位置,如果是排序為0,則表示獲取到了鎖 int ourIndex = children.indexOf(sequenceNodeName); /* * 如果在getSortedChildren中沒有找到之前創建的[臨時]順序節點,這表示可能由於網絡閃斷而導致 * Zookeeper認為連接斷開而刪除了我們創建的節點,此時需要拋出異常,讓上一級去處理 * 上一級的做法是捕獲該異常,並且執行重試指定的次數,見后面的 attemptLock方法 */ if (ourIndex < 0) { throw new ZkNoNodeException("節點沒有找到: " + sequenceNodeName); } // 如果當前客戶端創建的節點在locker子節點列表中位置大於0,表示其它客戶端已經獲取了鎖 // 此時當前客戶端需要等待其它客戶端釋放鎖 boolean isGetTheLock = ourIndex == 0; //是否得到鎖 // 如何判斷其它客戶端是否已經釋放了鎖?從子節點列表中獲取到比自己次小的那個節點,並對其建立監聽 String pathToWatch = isGetTheLock ? null : children.get(ourIndex - 1); //獲取比自己次小的那個節點,如:node_0000000002 if (isGetTheLock) { haveTheLock = true; } else { // 如果次小的節點被刪除了,則表示當前客戶端的節點應該是最小的了,所以使用CountDownLatch來實現等待 String previousSequencePath = basePath.concat("/").concat(pathToWatch); final CountDownLatch latch = new CountDownLatch(1); final IZkDataListener previousListener = new IZkDataListener() { /** * 監聽指定節點刪除時觸發該方法 */ public void handleDataDeleted(String dataPath) throws Exception { // 次小節點刪除事件發生時,讓countDownLatch結束等待 // 此時還需要重新讓程序回到while,重新判斷一次! latch.countDown(); } /** * 監聽指定節點的數據發生變化觸發該方法 * */ public void handleDataChange(String dataPath, Object data) throws Exception { } }; 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); } else { latch.await(); } } catch (ZkNoNodeException e) { // ignore } finally { client.unsubscribeDataChanges(previousSequencePath, previousListener); } } } } catch (Exception e) { // 發生異常需要刪除節點 doDelete = true; throw e; } finally { // 如果需要刪除節點 if (doDelete) { deletePath(path); } } 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; } /** * 獲取parentPath節點下的所有順序節點,並且從小到大排序 * * @return * @throws Exception */ private 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(); } } /** * 釋放鎖 * * @param lockPath * @throws Exception */ protected void releaseLock(String lockPath) throws Exception { deletePath(lockPath); } /** * 嘗試獲取鎖 * * @param time * @param unit * @return * @throws Exception */ 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 { // createLockNode用於在locker(basePath持久節點)下創建客戶端要獲取鎖的[臨時]順序節點 ourPath = createEphemeralSequential(client, path); /** * 該方法用於判斷自己是否獲取到了鎖,即自己創建的順序節點在locker的所有子節點中是否最小 * 如果沒有獲取到鎖,則等待其它客戶端鎖的釋放,並且稍后重試直到獲取到鎖或者超時 */ hasTheLock = waitToLock(startMillis, millisToWait, ourPath); } catch (ZkNoNodeException e) { if (retryCount++ < MAX_RETRY_COUNT) { isDone = false; } else { throw e; } } } System.out.println(ourPath + "鎖獲取" + (hasTheLock ? "成功" : "失敗")); if (hasTheLock) { return ourPath; } return null; } }
4. 獲取鎖調用demo
package com.ljq.lock; import org.I0Itec.zkclient.ZkClient; public class LockTest { public static void main(String[] args) throws Exception { ZkClient zkClient = new ZkClient("192.168.2.249:2181", 3000); SimpleDistributedLock simple = new SimpleDistributedLock(zkClient, "/locker"); for (int i = 0; i < 10; i++) { try { simple.acquire(); System.out.println("正在進行運算操作:" + System.currentTimeMillis()); } catch (Exception e) { e.printStackTrace(); } finally { simple.release(); System.out.println("=================\r\n"); } } } }
5. 獲取鎖控制台信息
/locker/lock-0000000131鎖獲取成功 正在進行運算操作:1479128867323 /locker/lock-0000000131鎖已釋放... ================= /locker/lock-0000000132鎖獲取成功 正在進行運算操作:1479128867424 /locker/lock-0000000132鎖已釋放... ================= /locker/lock-0000000133鎖獲取成功 正在進行運算操作:1479128867503 /locker/lock-0000000133鎖已釋放... ================= /locker/lock-0000000134鎖獲取成功 正在進行運算操作:1479128867577 /locker/lock-0000000134鎖已釋放... ================= /locker/lock-0000000135鎖獲取成功 正在進行運算操作:1479128867670 /locker/lock-0000000135鎖已釋放... ================= /locker/lock-0000000136鎖獲取成功 正在進行運算操作:1479128867744 /locker/lock-0000000136鎖已釋放... ================= /locker/lock-0000000137鎖獲取成功 正在進行運算操作:1479128867885 /locker/lock-0000000137鎖已釋放... ================= /locker/lock-0000000138鎖獲取成功 正在進行運算操作:1479128868108 /locker/lock-0000000138鎖已釋放... ================= /locker/lock-0000000139鎖獲取成功 正在進行運算操作:1479128868192 /locker/lock-0000000139鎖已釋放... ================= /locker/lock-0000000140鎖獲取成功 正在進行運算操作:1479128868286 /locker/lock-0000000140鎖已釋放... =================
