Java apachecurator分布式锁

Java apachecurator分布式锁,java,apache-zookeeper,apache-curator,Java,Apache Zookeeper,Apache Curator,我知道ApacheCurator可以在zookeeper的基础上实现分布式锁功能。根据Apache策展人官方网站上发布的文档,它看起来非常易于使用。例如: RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient("host:ip",retryPolicy); client.start(); InterPr

我知道ApacheCurator可以在zookeeper的基础上实现分布式锁功能。根据Apache策展人官方网站上发布的文档,它看起来非常易于使用。例如:

RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient("host:ip",retryPolicy);
client.start();

InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(client, path);
if(lock.acquire(10, TimeUnit.SECONDS))
{
     try { /*do something*/ }
     finally { lock.release(); }
}
但是“InterProcessSemaphoreMutex”的第二个参数“path”是什么意思?它的意思是基于API的“锁的路径”,但它到底是什么?谁能给我举个例子吗


如果我有数百万个锁,我需要创建数百万个“锁的路径”吗?zookeeper集群的最大锁数(znode)有没有限制?或者,当进程释放锁时,我们可以移除它吗?

ZooKeeper显示了一个分布式文件系统。对于任何ZooKeeper操作、配方等,您将“znodes”写入特定路径并观察更改。参见此处:(关于znodes)

对于策展人配方,它需要知道您要用于执行配方的基本路径。对于进程间SemaphoreMutex,路径是每个参与者都应该使用的。i、 e.进程1和进程2都希望争夺锁。因此,它们都使用相同的路径(比如“/my/lock”)分配进程间SemaphoreUnitex实例。将路径视为锁标识符。在同一ZooKeeper集群中,您可以通过使用不同的路径拥有多个锁


希望这有帮助(免责声明:我是《策展人》的主要作者)。

一些关于收割者的例子

@Test
public void     testSomeNodes() throws Exception
{

  Timing                  timing = new Timing();
  ChildReaper             reaper = null;
  CuratorFramework        client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
  try
  {
    client.start();

    Random              r = new Random();
    int                 nonEmptyNodes = 0;
    for ( int i = 0; i < 10; ++i )
    {
      client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i));
      if ( r.nextBoolean() )
      {
        client.create().forPath("/test/" + Integer.toString(i) + "/foo");
        ++nonEmptyNodes;
      }
    }

    reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1);
    reaper.start();

    timing.forWaiting().sleepABit();

    Stat    stat = client.checkExists().forPath("/test");
    Assert.assertEquals(stat.getNumChildren(), nonEmptyNodes);
  }
  finally
  {
    CloseableUtils.closeQuietly(reaper);
    CloseableUtils.closeQuietly(client);
  }
}
@测试
public void testSomeNodes()引发异常
{
正时=新正时();
儿童收割机收割机=空;
CuratorFramework client=CuratorFrameworkFactory.newClient(server.getConnectString(),timing.session(),timing.connection(),new RetryOneTime(1));
尝试
{
client.start();
随机r=新随机();
int非空节点=0;
对于(int i=0;i<10;++i)
{
client.create().creatingParentsIfNeeded().forPath(“/test/”+Integer.toString(i));
if(r.nextBoolean())
{
client.create().forPath(“/test/”+Integer.toString(i)+“/foo”);
++非空节点;
}
}
收割机=新的儿童收割机(客户端,“/test”,收割机.Mode.reau,直到删除,1);
reaper.start();
timeing.forWaiting().sleepABit();
Stat=client.checkExists().forPath(“/test”);
Assert.assertEquals(stat.getNumChildren(),非空节点);
}
最后
{
安静地关闭(收割者);
CloseableUtils.closequilly(客户端);
}
}

好的,那么对于路径“/my/lock”,当调用release()方法时,这个节点是否被删除?不,它没有被删除。然而,馆长有一个单独的食谱(收割者),可以用来清理像这样的老路。@Randgalt关于收割者,你能给我举个例子吗?添加的路径是锁本身还是父路径?当我使用锁定路径时,似乎什么都没有发生……不,目前没有示例。路径应该是父路径。