1.0.2 • Published 4 years ago
@anpingli/distributed v1.0.2
Henson Library for Distributed Service
Table of Contents
Install
$ npm install @anpingli/distributed
Design and Usage
Service Discovery
import {discovery} from '@anpingli/distributed';
Task Coordinator
import {TaskCoordinator} from '@anpingli/distributed';
Consul KV API
import {kv} from '@anpingli/distributed';
Master Election API
Option: MasterElectionOption
interval: number
- Election interval, in seconds
Example:
import {MasterElection} from '@anpingli/distributed';
// @param service: string - unique identity of the service
// @param option: { interval: number }
let test = new MasterElection('datacleanup', { interval: 10 });
test.start();
// check if it is the master
if (test.isMaster()) {
//do something
}
// stop when quit
test.stop();
Get bucket's ip
import {ServiceInfo, getServiceInfo} from '@anpingli/distributed';
let serviceInfo: ServiceInfo = await getServiceInfo('servicename', 1);
Get nodes(running task coordinator for specified service) that owns at least 1 bucket
method:
getBucketOwners(servicename:string):ServiceInfo[]
It return array of ServiceInfo which have structure like this:
ServiceInfo { Tags: string[]; Address: string; Port: number; }
import {getBucketOwners} from '@anpingli/distributed';
const nodes = await getBucketOwners('connectivitymanager');
if(serviceInfos.length > 0){
const ip = nodes[0].Address;
const port = nodes[0].Port;
...
}