Distributed lock implementation of. NET Distribution architecture

Source: Internet
Author: User

Distributed locks

Often used to resolve business consistency and coordinate distributed environments in distributed environments.

The actual business scenario, for example, to solve the concurrency of a single moment to repeat the order, repeat confirmation of receipt, re-discovery of gold coupons.

Scenarios that use distributed locks are generally not too many.

Open Source Address: Http://git.oschina.net/chejiangyi/XXF.BaseService.DistributedLock

Open source related groups:. NET open source basic services 238543768

This c#.net the implementation of Redis distributed locks and zookeeper distributed locks for research only. (There may be bugs)

Using Servicestack.redis to implement Redis distributed locks


1

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "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=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

Java implementation of Redis distributed locks from the network (C # Edition)

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "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 =&nBSP; ( redisclient = v =  (v !=                                 (Current < bitconverter.toint64 (v, . Format (

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

Servicestack.redis Internal Implementation version (older)

1

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "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=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

Zookeeper Release for distributed locks

650) this.width= 650, "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

       root = ;            lockName;           waitNode;            myZnode;           timespan sessiontimeout = timespan.frommilliseconds ( IList<Exception > exception =  List<Exception>                                                                        zookeeprdistributedlockfromjava ( config, .lockName =      &Nbsp;      =  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 (&NBSP;STAT&NBSP;=&NBSP;ZK. Exists (root +  + lower,                (stat !=  + system.threading.thread.currentthread.name +   + root + &nbSp;+=  autoresetevent ( r ==                      +-=

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

The above code is only for reference, not pressure test.

Code paste some issues, please download Open source package run research.


Distributed lock implementation of. NET Distribution architecture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.