简体   繁体   English

Hazelcast的分布式锁定无法正常工作

[英]Distributed lock with Hazelcast not working

I am trying to test my distributed lock implementation but I still haven't found a way to make it work. 我试图测试我的分布式锁实现,但我还没有找到一种方法使其工作。 I deployed a REST service with two simple methods, like this: 我用两个简单的方法部署了一个REST服务,如下所示:

@GET
@Path("/lock")
@Produces("text/*")
public String lock() throws InterruptedException {
    Lock lock = distributedService.getDistributedLock("test");
    boolean result = lock.tryLock(5, TimeUnit.SECONDS);
    return result ? "locked" : "timeout";
}

@GET
@Path("/unlock")
@Produces("text/*")
public String unlock() {
    Lock lock = distributedService.getDistributedLock("test");
    lock.unlock();
    return "unlocked";
}

The distributedService object implements the getDistributedLock() method: distributedService对象实现getDistributedLock()方法:

@Override
public Lock getDistributedLock(String lockName) {
    return Hazelcast.getDefaultInstance().getLock(lockName);
}

In the hazelcast.xml file, I enabled TCP-IP connection and disabled everything else: 在hazelcast.xml文件中,我启用了TCP-IP连接并禁用了其他所有内容:

<network>
<port auto-increment="true">5701</port>
<join>
  <multicast enabled="false" />
  <tcp-ip enabled="true">
    <interface>192.168.0.01</interface>
    <interface>192.168.0.02</interface>
  </tcp-ip>
</join>
<interfaces enabled="false" />
<symmetric-encryption enabled="false" />
<asymmetric-encryption enabled="false" />

I deployed the application in the two machines, with IP adresses corresponding to the .xml file (192.168.0.01 and 192.168.0.02) and when I call the service from the browser It works for the first time (it locks and returns "locked") and everytime I call the unlock() method it returns correctly (I get the string "unlocked") but after the first time, everytime that I call the lock() method I get a timeout. 我在两台机器上部署了应用程序,IP地址对应于.xml文件(192.168.0.01和192.168.0.02),当我从浏览器调用服务时它第一次工作(它锁定并返回“锁定” )每次我调用unlock()方法它都会正确返回(我得到字符串“unlocked”)但是在第一次之后,每当我调用lock()方法时,我都会得到一个超时。 It doesn't look like the unlock() method is unlocking it. 它看起来不像unlock()方法解锁它。

Can someone point me the correct way to use Distributed Lock with hazelcast? 有人能指出我使用分布式锁定与淡褐色的正确方法吗?

Only the thread that locked can unlock it. 只有锁定的线程才能解锁它。 You are saying that you have implemented REST for lock and unlock. 您说您已实施REST以进行锁定和解锁。 And I guess the thread that locks and unlocks are different. 我想锁定和解锁的线程是不同的。 That's why it is not working. 这就是它无法正常工作的原因。 Try to print the Thread name and see yourself. 尝试打印线程名称并查看自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM