死鎖場景
redis分佈式鎖都不可避免會遇到死鎖問題,我們先來討論一下,什麼場景會發生死鎖問題。
因爲redis鎖對應的key還在,如果不將redis鎖對應的key刪除,下次獲取鎖的時候還是會失敗。
redis integration原理
有童鞋會說,那還不簡單,重啓的時候直接將redis鎖對應的key刪除不就可以了嗎?當然不行,再講這個問題之前,我們先來簡單的看一下redis integratio鎖的實現原理。
首先integration在獲取鎖的時候會檢驗本地是否持有該鎖
@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
long now = System.currentTimeMillis();
// 檢查本地是否持有鎖
if (!this.localLock.tryLock(time, unit)) {
return false;
}
try {
long expire = now + TimeUnit.MILLISECONDS.convert(time, unit);
boolean acquired;
// 獲取鎖
while (!(acquired = obtainLock()) && System.currentTimeMillis() < expire) { //NOSONAR
Thread.sleep(100); //NOSONAR
}
if (!acquire