Java 分布式锁服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1059580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:56:15  来源:igfitidea点击:

Distributed Lock Service

javatimeoutlockingdistributed-lock

提问by ripper234

Which distributed lock service would you use?

您会使用哪种分布式锁服务?

Requirements are:

要求是:

  1. A mutual exclusion (lock) that can be seen from different processes/machines
  2. lock...release semantics
  3. Automatic lock release after a certain timeout - if lock holder dies, it will automatically be freed after X seconds
  4. Java implementation
  5. Nice to have: .Net implementation
  6. If it's free: Deadlock detection / mitigation
  7. Easy deployment, see note below.
  1. 一个可以从不同进程/机器看到的互斥(锁)
  2. 锁定...释放语义
  3. 一定超时后自动释放锁 - 如果锁持有者死亡,它将在 X 秒后自动释放
  4. Java实现
  5. 很高兴拥有:.Net 实现
  6. 如果它是免费的:死锁检测/缓解
  7. 易于部署,请参阅下面的注释。

I'm not interested in answers like "it can be done over a database", or "it can be done over JavaSpaces" - I know. I'm interested in a ready, out-of-the-box, proven implementation.

我对“它可以通过数据库完成”或“它可以通过 JavaSpaces 完成”之类的答案不感兴趣——我知道。我对现成的、开箱即用的、经过验证的实施感兴趣。

采纳答案by Robert Munteanu

Teracotta, including the Open Source edition, has distributed locking semantics by using either synchronizedor the java.util.concurrent.ReentrantReadWriteLock- the latter apparently fitting your requirements.

Teracotta,包括开源版本,通过使用synchronized或分发锁定语义java.util.concurrent.ReentrantReadWriteLock- 后者显然符合您的要求。



Update

更新

Since the question now added the requirement of 'mixing' with GigaSpaces, I'm going to say don'tmix them. It's just going to add more complexity to your technological stack, and the effort of:

由于现在的问题增加了与 GigaSpaces“混合”的要求,我要说不要混合它们。它只会增加您的技术堆栈的复杂性,以及:

  • integrating, in terms of both code and infrastructure;
  • managing synchronisation between them;
  • learning/tuning/debugging Teracotta.
  • 在代码和基础设施方面进行集成;
  • 管理它们之间的同步;
  • 学习/调整/调试 Teracotta。

will be better spent creating or implementing a locking solution based on GigaSpaces.

将更好地用于创建或实施基于 GigaSpaces 的锁定解决方案。

回答by Gareth Davis

A newer kid on the block is hazelcast. I've been playing with it and it is amazingly simple to use and configure.

这个街区的一个新孩子是榛子。我一直在玩它,它的使用和配置非常简单。

As far as I can see there shouldn't be any conflict between Gigaspaces and hazelcast as hazelcast doesn't have any dependencies i.e. no jgroups.jar etc

据我所知,Gigaspaces 和 hazelcast 之间不应该有任何冲突,因为 hazelcast 没有任何依赖关系,即没有 jgroups.jar 等

Hazelcast:

黑兹卡斯特

  1. A mutual exclusion (lock), yep implementation of java.util.concurrency.locks.Lock
  2. Automatic lock release after a certain timeout, yep all locks are released if a member leaves the cluster
  3. Java implementation, yep
  4. Nice to have: .Net implementation, nope is a pure java solution, might be possible to port to j#
  5. If it's free: Deadlock detection / mitigation, nope no effort is made my Hazelcast to handle this
  6. Easy deployment, it's a single jar with a single config file, deployed as part of your application, no additional processes are required
  1. 一个互斥(锁),是的实现 java.util.concurrency.locks.Lock
  2. 一定超时后自动释放锁,是的,如果成员离开集群,所有锁都会被释放
  3. Java 实现,是的
  4. 很高兴拥有:.Net 实现,不是纯 Java 解决方案,可能可以移植到 j#
  5. 如果它是免费的:死锁检测/缓解,不用我的 Hazelcast 来处理这个问题
  6. 易于部署,它是一个带有单个配置文件的 jar,作为应用程序的一部分部署,不需要额外的过程

回答by Gareth Davis

Check out Apache's Zookeeper(A Hadoop sub-project) - it offers distributed synchronization. The documentation isn't great, but what there is makes it look an interesting product - checkout the recipes for ideas on how to use Zookeeper.

查看 Apache 的Zookeeper(一个 Hadoop 子项目)——它提供分布式同步。文档不是很好,但是它看起来是一个有趣的产品 - 查看关于如何使用 Zookeeper 的想法的食谱。

It is lower-level than you'd probably want and it does require additional deployment as it recommends dedicated servers.

它的级别低于您可能想要的级别,并且确实需要额外的部署,因为它建议使用专用服务器。

You can model different locking strategies and it does offer a solution for a lock holder dying (ephemeral nodes).

您可以对不同的锁定策略进行建模,它确实为锁持有者死亡(临时节点)提供了解决方案。

回答by Nikita Koksharov

I recommend to use Redissonit's a Redisbased on In-Memory Data Grid. It implements familiar Java data structures including distributed java.util.Lockand java.util.concurrent.ReentrantReadWriteLockobjects. Including ability to setup leaseTime. Lockusage example:

我建议使用Redisson,它是基于内存数据网格的Redis。它实现了熟悉的 Java 数据结构,包括分布式java.util.Lockjava.util.concurrent.ReentrantReadWriteLock对象。包括设置租用时间的能力。Lock用法示例:

Redisson redisson = Redisson.create(config);

Lock lock = redisson.getLock("anyLock");
try {
   // unlock automatically after 10 seconds of hold
   lock.lock(10, TimeUnit.SECONDS);

} finally {
   lock.unlock();
}

...

redisson.shutdown();

Supports cloud vendors like Azure and AWS.

支持 Azure 和 AWS 等云供应商。

回答by cpurdy

Oracle Coherence, which is very stable and mature, includes mutual exclusion support:

Oracle Coherence 非常稳定和成熟,包括互斥支持:

  cache.lock(key, -1);
    try {
      // .. add your critical code here
    } finally {
      cache.unlock(key);
    }

Locks survive server failures, rolling re-starts, etc.

锁在服务器故障、滚动重启等情况下仍然存在。

For the sake of full disclosure, I work at Oracle. The opinions and views expressed in this post are my own, and do not necessarily reflect the opinions or views of my employer.

为了充分披露,我在 Oracle 工作。这篇文章中表达的观点和观点是我自己的,并不一定反映我雇主的观点或观点。

回答by frail

ZooKeeperbecame a de facto standard in distributed locking with the help of Apache Curatorframework. Check out the locks in recipesfor more information.

ZooKeeperApache Curator框架的帮助下成为分布式锁定的事实标准。查看食谱中的锁以获取更多信息。