npm.io
1.0.2 • Published 6 years ago

@anpingli/distributed

Licence
UNLICENSED
Version
1.0.2
Deps
3
Size
57 kB
Vulns
1
Weekly
0

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;
    ...
}