@nestcfork/loadbalance
TypeScript icon, indicating that this package has built-in type declarations

0.7.5 • Public • Published

NestCloud - Loadbalance

NPM Version Package License NPM Downloads Travis Linux Coverage

Description

This is a software load balancers primary for rest calls.

Installation

$ npm i --save @nestcfork/loadbalance

Quick Start

Import Module

Before you import the lb module, you need import @nestcfork/service module at first.

import { Module } from '@nestjs/common';
import { LoadbalanceModule } from '@nestcfork/loadbalance';

@Module({
  imports: [
      LoadbalanceModule.forRoot({
        rule: 'RandomRule',
        services: [{
          name: 'test-service',
          rule: 'RoundRobinRule',
        }],
      })
  ],
})
export class AppModule {}

Import With Boot Module

Except @nestcfork/boot module you can also use @nestcfork/config module too.

app.module.ts:

import { Module } from '@nestjs/common';
import { BOOT } from "@nestcfork/common";
import { BootModule } from "@nestcfork/boot";
import { LoadbalanceModule } from '@nestcfork/loadbalance';
import { resolve } from 'path';

@Module({
  imports: [
    BootModule.forRoot({
      filePath: resolve(__dirname, 'config.yaml'),
    }),
    LoadbalanceModule.forRootAsync({ inject: [BOOT] }),
  ]
})
export class AppModule {
}

config.yaml:

loadbalance:
  rule: RandomRule
  services:
    - name: test-service
      rule: RoundRobinRule

Usage

import { Injectalbe } from '@nestjs/common';
import { InjectLoadbalancee, Loadbalance } from '@nestcfork/loadbalance';

@Injectalbe()
export class TestService {
  constructor(
    @InjectLoadbalancee() private readonly lb: Loadbalance
  ) {
  }

  chooseOneNode() {
      const node = this.lb.choose('your-service-name');
  }
}

Use Choose decorator

It will call lb.choose every you use this.server.

import { Injectable } from '@nestjs/common';
import { Choose, Server } from '@nestcfork/loadbalance';

@Injectable()
export class TestService {
  @Choose('test-service')
  private readonly server: Server;
}

Custom Loadbalance Rule

import { Rule, Loadbalancer } from '@nestcfork/loadbalance';

export class CustomLoadbalanceRule implements Rule {
    private loadbalancer: Loadbalancer;
    
    init(loadbalancer: Loadbalancer) {
        this.loadbalancer = loadbalancer;
    }

    choose() {
        const servers = this.loadbalancer.servers;
        return servers[0];
    }
}
import { Injectable } from '@nestjs/common';
import { UseRules } from '@nestcfork/loadbalance';
import { CustomLoadbalanceRule } from './CustomLoadbalanceRule';

@Injectable()
@UseRules(CustomLoadbalanceRule)
export class LoadbalanceRuleRegister {
}

API

class LoadbalanceModule

static forRoot(options): DynamicModule

Import loadbalance module.

field type description
options.rule string global lb rule name
options.services ServiceOptions set service rule

static forRootAsync(options): DynamicModule

Import loadbalance module.

field type description
options.inject string[] BOOT CONFIG SERVICE

class Loadbalance

choose(service: string): Server

Choose a node that running the specific service.

field type description
service string the service name

state(): {[service: string]: IServer[]}

List all servers info for all services.

chooseLoadbalancer(service: string): Loadbalancer

Get loadbalancer.

field type description
service string the service name

Stay in touch

License

NestCloud is MIT licensed.

Readme

Keywords

none

Package Sidebar

Install

npm i @nestcfork/loadbalance

Weekly Downloads

1

Version

0.7.5

License

MIT

Unpacked Size

59.3 kB

Total Files

65

Last publish

Collaborators

  • kurbar