This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
45 lines (40 loc) · 1.44 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
const zookeeper = require('zookeeper-cluster-client');
const path = '/test/default/a.b.c.service/c/1.1.1.1';
const client = zookeeper.createClient('10.218.140.224:2181');
client.on('connected', function () {
console.log('pid: %s connected, leader: %s', process.pid, client.isClusterClientLeader);
});
client.on('disconnected', function () {
console.log('pid: %s disconnected, leader: %s', process.pid, client.isClusterClientLeader);
});
client.on('expired', function () {
console.log('pid: %s expired, leader: %s', process.pid, client.isClusterClientLeader);
});
client.once('connected', function () {
client.watch(path, function (err, value) {
if (err) {
if (err.name === 'NO_NODE') {
client.mkdirp(path, function(err, meta) {
if (err) {
return console.error('create %s error: %s', path, err);
}
console.log('create %s successfully, meta: %s', path, meta);
});
}
return console.log('watch %s error: %s', path, err)
}
console.log(process.pid, path, 'changed', value ? value.toString() : 'no value');
});
});
client.connect();
let count = 1;
setInterval(function () {
const data = 'data' + ++count + ', pid:' + process.pid;
client.setData(path, new Buffer(data), function (err) {
if (err) {
return console.log('%s setData %s error: %s', process.pid, path, err);
}
console.log('setData: %s', data);
});
}, 5000);