使用nodejs的Hazelcast分布式锁

Distributed lock of Hazelcast using nodejs

我有 Hazelcast 集群服务器 运行 在 172.30.56.60, 61, 62

()

 [ Member {
    address: Address { host: '172.30.56.60', port: 5701, type: 4 },
    uuid: 'bd6428f0-e888-453f-872f-6fe8296d751d',
    isLiteMember: false,
    attributes: {} },
  Member {
    address: Address { host: '172.30.56.61', port: 5701, type: 4 },
    uuid: 'e0cd795a-0ca5-41ab-907a-492b61690a18',
    isLiteMember: false,
    attributes: {} },
  Member {
    address: Address { host: '172.30.56.62', port: 5701, type: 4 },
    uuid: '0a834ae8-e707-4b5b-945b-362bfea08cf5',
    isLiteMember: false,
    attributes: {} } ]

我尝试使用 nodejs 使用以下代码实现 Hazelcast 分布式锁定,

    // Initialize the hazelcast client instance.
var HazelcastClient = require('hazelcast-client').Client;
var Config = require('hazelcast-client').Config; 
var config = new Config.ClientConfig();
config.networkConfig.addresses = [{host: '172.30.56.60', port: '5701'},{host: '172.30.56.61', port: '5701'}, {host: '172.30.56.62', port: '5701'}]; 
var lock = {};
var sleep = require('sleep');
HazelcastClient
    .newHazelcastClient(config)       
    .then(function (hazelcastClient) {  
        lock = hazelcastClient.getLock("lock1");
         // do stuff with lock
    lock.lock();
    console.log('Am locked in node with lock1...will be locked for 20 seconds');
    sleep.sleep(20);
    console.log('Unlocked now...');
    lock.unlock();
    process.exit();
    });

我一个节点一个节点地启动脚本,我希望一个节点一个节点地建立锁,但它却同时锁定了所有节点。所以它不能作为分布式锁工作,所以所有脚本同时开始和结束(注意:为了测试我提供了 20 秒的睡眠)

请告诉我,如何在 Hazelcast 中使用节点 js 建立分布式锁。

我自己找到了答案,我没有意识到return的承诺,我的错误(nodejs新手)

  // Initialize the hazelcast client instance.
var HazelcastClient = require('hazelcast-client').Client;
var Config = require('hazelcast-client').Config; 
var config = new Config.ClientConfig();
config.networkConfig.addresses = [{host: '172.30.56.60', port: '5701'},{host: '172.30.56.61', port: '5701'}, {host: '172.30.56.62', port: '5701'}]; 



var sleep = require('sleep');
// Test process
HazelcastClient
    .newHazelcastClient(config)       
    .then(function (hazelcastClient) {  
        var lock = hazelcastClient.getLock('rowId_tablename');
         // lock the code here
    lock.lock().then(function() { 
        console.log('Am locked in node with lock3...will be locked for 20 seconds');
        sleep.sleep(20);
        // unlock after process
        return lock.unlock();
    }).then(function() { 
        console.log('unlocked now');
    });
});