標籤:原始碼 .net public
分布式鎖
經常用於在解決分布式環境下的業務一致性和協調分布式環境。
實際業務情境中,比如說解決並發一瞬間的重複下單,重複確認收貨,重複發現金券等。
使用分布式鎖的情境一般不能太多。
開源地址:http://git.oschina.net/chejiangyi/XXF.BaseService.DistributedLock
開源相關群: .net 開源基礎服務 238543768
這裡整理了C#.net關於redis分布式鎖和zookeeper分布式鎖的實現,僅用於研究。(可能有bug)
採用ServiceStack.Redis實現Redis分布式鎖
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
RedisDistributedLock( redisserver, LockResult TryGetDistributedLock(TimeSpan? getlockTimeOut, TimeSpan? (lockresult == DistributedLockException(= ._lock = =.Format(= (._lock != (_client != .Format(
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
來自網路的java實現Redis分布式鎖(C#版)
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
RedisDistributedLockFromJava( redisserver, LockResult TryGetDistributedLock(TimeSpan? getlockTimeOut, TimeSpan? (lockresult == DistributedLockException( taskexpiredMilliseconds = (taskrunTimeOut != ? ()taskrunTimeOut.Value.TotalMilliseconds : ( getlockexpiredMilliseconds = (getlockTimeOut != ? ()getlockTimeOut.Value.TotalMilliseconds : hassleepMilliseconds = ( ( redisclient = value = CurrentUnixTimeMillis() + taskexpiredMilliseconds + acquired = redisclient.Add<>(@lock, value, TimeSpan.FromMilliseconds(taskexpiredMilliseconds + (acquired == = oldValueBytes = (oldValueBytes != && BitConverter.ToInt64(oldValueBytes, ) < getValueBytes = o1 = redisclient.ExpireEntryIn(@lock, TimeSpan.FromMilliseconds(taskexpiredMilliseconds + DistributedLockConfig.TaskLockDelayCleepUpTime));= = = (lockresult == (hassleepMilliseconds >==+=.Format(= ()(System.DateTime.UtcNow - System.DateTime(, , , , , (lockresult == LockResult.Success || lockresult == current = ( redisclient = v = (v != (current < BitConverter.ToInt64(v, .Format(
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
ServiceStack.Redis內部實現版本(較舊)
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
RedisDistributedLockFromServiceStack( redisserver, LockResult TryGetDistributedLock(TimeSpan? getlockTimeOut, TimeSpan? (lockresult == DistributedLockException( ( redisClient ==> lockString = (expireTime.ToUnixTimeMs() + = lockExpireString = redisClient.Get<> (!.TryParse(lockExpireString, trans.QueueCommand(r => (t == ==.Format(= ( redisClient =.Format(
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
Zookeeper 版本實現分布式鎖
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
root = ; lockName; waitNode; myZnode; TimeSpan sessionTimeout = TimeSpan.FromMilliseconds( IList<Exception> exception = List<Exception> ZooKeeprDistributedLockFromJava( config, .lockName = = ZooKeeper(config, sessionTimeout, stat = zk.Exists(root, (stat == zk.Create(root, [ (.autoevent != splitStr = myZnode = zk.Create(root + + lockName + splitStr, [+ IList<> subNodes = zk.GetChildren(root, IList<> lockObjNodes = List<> ( node =+ + lockObjNodes[ (myZnode.Equals(root + + lockObjNodes[ subMyZnode = myZnode.Substring(myZnode.LastIndexOf(, StringComparison.Ordinal) + = lockObjNodes[Array.BinarySearch(alockObjNodes, subMyZnode) - ( waitForLock( stat = zk.Exists(root + + lower, (stat != + System.Threading.Thread.CurrentThread.Name + + root + += AutoResetEvent( r == +-=
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="margin:0px;padding:0px;border:none;" />
以上代碼僅做參考,未壓測。
代碼粘貼有些問題,詳細請下載開源包運行研究。
.net 分布式架構之分布式鎖實現