0.3.17 • Published 5 years ago

@nestcloud/consul-loadbalance v0.3.17

Weekly downloads
26
License
MIT
Repository
github
Last release
5 years ago

NestCloud - ConsulLoadbalance

Description

This is a software load balancers primary for rest calls.

中文文档

Installation

$ npm i --save @nestcloud/consul consul @nestcloud/consul-loadbalance

Quick Start

Import Module

import { Module } from '@nestjs/common';
import { ConsulModule } from '@nestcloud/consul';
import { LoadbalanceModule } from '@nestcloud/consul-loadbalance';
import { BootModule } from '@nestcloud/boot';
import { NEST_BOOT } from '@nestcloud/common';

@Module({
  imports: [
      ConsulModule.register({dependencies: [NEST_BOOT]}),
      BootModule.register(__dirname, 'bootstrap.yml'),
      LoadbalanceModule.register({dependencies: [NEST_BOOT], customRulePath: __dirname})
  ],
})
export class ApplicationModule {}

Configurations

consul:
  host: localhost
  port: 8500
loadbalance:
  ruleCls: RandomRule
  rules:
    - {service: 'your-service-name', ruleCls: 'RoundRobinRule'}
    - {service: 'your-service-name', ruleCls: 'rules/CustomRule'}

Usage

import { Component } from '@nestjs/common';
import { InjectLoadbalancee, Loadbalance } from '@nestcloud/consul-loadbalance';

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

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

Use Choose decorator

import { Injectable } from '@nestjs/common';
import { IServer } from '@nestcloud/common';
import { Choose } from '@nestcloud/consul-loadbalance';

@Injectable()
export class TestService {
  @Choose('your-service-name')
  private readonly yourServiceServer: Iserver;
}

Custom Loadbalance Rule

import { Rule, Loadbalancer } from '@nestcloud/consul-loadbalance';

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

    choose() {
        const servers = this.loadbalancer.servers;
        return servers[0];
    }
}

API

class LoadbalanceModule

static register(options): DynamicModule

Import nest consul loadbalance module.

fieldtypedescription
options.dependenciesstring[]if you are using @nestcloud/boot module, please set NEST_BOOT
options.ruleClsstring | classlb rule,support:RandomRule, RoundRobinRule, WeightedResponseTimeRule or custom lb rule, use relative path
options.rulesRuleOptionone service use one rule, eg:[{service: '', ruleCls: ''}]

class Loadbalance

choose(service: string): Server

Choose a node that running the specific service.

fieldtypedescription
servicestringthe service name

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

List all servers info for all services.

chooseLoadbalancer(service: string): Loadbalancer

Get loadbalancer.

fieldtypedescription
servicestringthe service name

Stay in touch

License

NestCloud is MIT licensed.

0.3.17

5 years ago

0.3.16

5 years ago

0.3.16-0

5 years ago

0.3.15

5 years ago

0.3.14

5 years ago

0.3.14-2

5 years ago

0.3.14-1

5 years ago

0.3.14-0

5 years ago

0.3.13

5 years ago

0.3.12

5 years ago

0.3.10

5 years ago

0.3.9

5 years ago

0.3.9-0

5 years ago

0.3.8

5 years ago

0.3.8-5

5 years ago

0.3.8-4

5 years ago

0.3.8-3

5 years ago

0.3.8-2

5 years ago

0.3.8-1

5 years ago

0.3.8-0

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.5-3

5 years ago

0.2.5-2

5 years ago

0.2.5-1

5 years ago

0.2.5-0

5 years ago

0.2.4

5 years ago

0.2.4-3

5 years ago

0.2.4-2

5 years ago

0.2.4-1

5 years ago

0.2.4-0

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.1-5

5 years ago

0.2.1-4

5 years ago

0.2.1-3

5 years ago

0.2.1-2

5 years ago

0.2.1-1

5 years ago

0.2.1-0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago