.net 分布式架構之分布式鎖實現

來源:互聯網
上載者:User

標籤:原始碼   .net   public   

分布式鎖

經常用於在解決分布式環境下的業務一致性和協調分布式環境。

實際業務情境中,比如說解決並發一瞬間的重複下單,重複確認收貨,重複發現金券等。

使用分布式鎖的情境一般不能太多。

 

開源地址:http://git.oschina.net/chejiangyi/XXF.BaseService.DistributedLock

開源相關群: .net 開源基礎服務 238543768

這裡整理了C#.net關於redis分布式鎖和zookeeper分布式鎖的實現,僅用於研究。(可能有bug)

採用ServiceStack.Redis實現Redis分布式鎖


1

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內部實現版本(較舊)

1

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 分布式架構之分布式鎖實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.