.NET 中的分布式锁定
我正在寻找可跨多台机器工作的锁定机制的建议。就我而言,我基本上只是希望能够在两台机器上启动一项服务,并在另一台机器完成之前阻止一个服务,这是一种简单的方法,可以在服务机器出现故障时确保冗余。
与分布式锁服务有点类似,但专门寻找人们已成功集成的项目。网。
I'm looking for recommendations for a locking mechanism that will work across multiple machines. In my case I basically just want to be able to start a service on 2 machines and have one block until the other finishes as a simple way to insure redundancy in case a service machine goes down.
Sort of along the same lines as Distributed Lock Service but specifically looking for projects that people have successfully integrated with .NET.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们使用 SqlServer 的应用程序锁功能进行分布式锁定。如果 SqlServer 已经是您的堆栈的一部分,这尤其方便。
为了使其更容易在 .NET 中使用,我创建了一个 NuGet 包,这使得使用它变得容易功能。该库还支持其他后端,例如 Postgres、Azure blob 存储和 Redis。
使用这个库,代码如下所示:
因为底层 SqlServer 功能非常灵活,所以还有支持 TryAcquire 语义、超时和异步锁定的重载。
We use SqlServer's application lock functionality for distributed locking. This is especially convenient if SqlServer is already a part of your stack.
To make this easier to work with from .NET, I created a NuGet package which makes it easy to use this functionality. The library also supports other backends such as Postgres, Azure blob storage, and Redis.
With this library, the code looks like:
Because the underlying SqlServer functionality is very flexible, there are also overloads supporting TryAcquire semantics, timeouts, and async locking.
如果您使用的是 Windows Server 版 AppFabric,则可以使用 此 DataCache 扩展。您还可以将 Redis 锁与 ServiceStack 的 redis 客户端结合使用。
两者都是 .NET 实现,但需要服务器组件。我一直在修改分布式锁的 PeerChannel 实现,它使用点对点通信并且不需要任何服务器基础设施。如果您对此感兴趣,请告诉我。
If you are using AppFabric for Windows Server, you can use this DataCache extension. You can also use redis locks with ServiceStack's redis client.
Both are .NET implementations but require a server component. I've been tinkering with a PeerChannel implementation of a distributed lock that uses peer to peer communication and doesn't require any server infrastructure. Let me know if this is something you would be interested in.
您可以使用 NCache 对这个特定用例使用悲观锁定。
当您处理读取密集型应用程序时,乐观锁定非常有用,
NCache 可以帮助您实现这一目标。
http://blogs.alachisoft.com/ncache/distributed-locking/
You could use Pessimistic Locking for this specific use case using NCache.
Optimistic locking is beneficial for scenarios when your dealing with read intensive applications
NCache helps you achieve it.
http://blogs.alachisoft.com/ncache/distributed-locking/