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
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)
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