2010-07-29 6 views
1

여러 컴퓨터에서 작동하는 잠금 메커니즘에 대한 권장 사항을 찾고 있습니다. 제 경우에는 기본적으로 2 대의 기계에서 서비스를 시작하고 서비스 기계가 고장 나더라도 이중화를 보장하는 간단한 방법으로 다른 블록이 완료 될 때까지 한 블록을 갖기를 원합니다..NET의 분산 잠금

Distributed Lock Service과 같은 줄을 정렬하지만 사람들이 .NET과 성공적으로 통합 된 프로젝트를 찾고 있습니다.

+0

누구도 이들 중 아직 하나를 찾았습니까? 나는 또한 하나를 찾고있다. Paxos와 아마도 Bully Algoros와 같은 algos가 작동 할 것 같습니까? 이와 같은 .NET 구현을보고 싶습니다. –

+0

http://blog.kristandyson.com/2011/01/distributed-lock.html을보십시오 – krisdyson

답변

1

Windows 서버용 AppFabric을 사용하는 경우 this DataCache extension을 사용할 수 있습니다. ServiceStack's redis client과 함께 redis 잠금을 사용할 수도 있습니다.

둘 다 .NET 구현이지만 서버 구성 요소가 필요합니다. 필자는 피어 투 피어 (Peer to Peer) 통신을 사용하고 서버 인프라를 필요로하지 않는 분산 락 (distributed lock)의 PeerChannel 구현을 시도해 왔습니다. 이것이 당신이 관심을 가질만한 것이면 알려주십시오.

1

우리는 분산 잠금을 위해 SqlServer의 application lock 기능을 사용합니다. 이것은 SqlServer가 이미 스택의 일부인 경우 특히 편리합니다.

.NET에서이 작업을보다 쉽게 ​​수행 할 수 있도록이 기능을 쉽게 사용할 수 있도록 NuGet package을 만들었습니다. 기본 SQLSERVER 기능은 매우 유연하기 때문에,도있다

var @lock = new SqlDistributedLock("my_lock_name", connectionString); 
using (@lock.Acquire()) 
{ 
    // critical region 
} 

지지 된 tryAcquire 의미를 과부하 비동기 잠금 타임 아웃이 라이브러리

처럼 코드 보인다.

+0

로드 균형 조정 된 SQL Server와 함께 작동합니까? –

+0

@PeterKnaggs 저는 그 설정에 익숙하지 않습니다. 서버가 정말로 분리되어 있고 무작위로 서버를 치면 작동하지 않습니다. 이것이 서버 간 잠금 유지와 같은 작업을 동기화 할 수있는 내장 SQLServer 기능이라면 작동 할 수 있습니까? 분산 잠금 공급자 역할은 많은 리소스를 사용하지 않으므로 잠금 데이터베이스로 작동하도록로드 균형 조정되지 않은 별도의 데이터베이스를 항상 만들 수 있습니다. – ChaseMedallion

0

NCache를 사용하여이 특정 사용 사례에 대해 비관적 잠금을 사용할 수 있습니다. 낙관적 인 잠금은 읽기 집중적 인 응용 프로그램을 다룰 때 유용합니다.

NCache를 사용하면 쉽게 달성 할 수 있습니다. http://blogs.alachisoft.com/ncache/distributed-locking/

// Instance of the object used to lock and unlock cache items in NCache 
LockHandle lockHandle = new LockHandle(); 

// Specify time span of 10 sec for which the item remains locked 
// NCache will auto release the lock after 10 seconds. 
TimeSpan lockSpan = new TimeSpan(0, 0, 10); 

try 
{ 
    // If item fetch is successful, lockHandle object will be populated 
    // The lockHandle object will be used to unlock the cache item 
    // acquireLock should be true if you want to acquire to the lock. 
    // If item does not exists, account will be null 
    BankAccount account = cache.Get(key, lockSpan, 
    ref lockHandle, acquireLock) as BankAccount; 
    // Lock acquired otherwise it will throw LockingException exception 

    if(account != null &&; account.IsActive) 
    { 
     // Withdraw money or Deposit 
     account.Balance += withdrawAmount; 
     // account.Balance -= depositAmount; 

     // Insert the data in the cache and release the lock simultaneously 
     // LockHandle initially used to lock the item must be provided 
     // releaseLock should be true to release the lock, otherwise false 
     cache.Insert("Key", account, lockHandle, releaseLock); 
    } 
    else 
    { 
     // Either does not exist or unable to cast 
     // Explicitly release the lock in case of errors 
     cache.Unlock("Key", lockHandle); 
    } 
} 
catch(LockingException lockException) 
{ 
    // Lock couldn't be acquired 
    // Wait and try again 
}