Skip to content
This repository has been archived by the owner. It is now read-only.
[Migrated to nestcloud repository]A Nest framework (node.js) module for getting service from consul and sending http request by loadbalance.
TypeScript JavaScript
Branch: master
Clone or download
Latest commit 03e98b4 Mar 19, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib upate nest version Mar 19, 2019
.eslintrc.js initial commit May 15, 2018
.gitignore initial commit May 15, 2018
.npmignore initial commit May 15, 2018
.prettierrc initial commit May 15, 2018
README.md migrate to nestcloud npm org Mar 5, 2019
index.d.ts initial commit May 15, 2018
index.js
index.ts.js initial commit May 15, 2018
package.json update optional deps Mar 19, 2019
tsconfig.json migrate to nestcloud npm org Mar 5, 2019
tslint.json initial commit May 15, 2018
yarn.lock update optional deps Mar 19, 2019

README.md

Nest Logo

Description

A component of nestcloud. NestCloud is a nest framework micro-service solution.

中文文档

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';

@Module({
  imports: [
      ConsulModule.register({
        host: '127.0.0.1',
        port: 8500
      }),
      LoadbalanceModule.register({
        rules: [
            {service: 'test-service', ruleCls: 'RandomRule'},
            {service: 'user-service', ruleCls: '../rules/CustomRule'}
        ]
      })
  ],
})
export class ApplicationModule {}

If you use @nestcloud/boot 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]})
  ],
})
export class ApplicationModule {}
Nest-boot config file
consul:
  host: localhost
  port: 8500
loadbalance:
  rules:
    - {service: 'test-service', ruleCls: 'RandomRule'}
    - {service: 'user-service', 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('user-service');
  }
}

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.

field type description
options.dependencies string[] if you are using @nestcloud/boot module, please set [NEST_BOOT]
options.ruleCls string | class lb rule,support:RandomRule, RoundRobinRule, WeightedResponseTimeRule or custom lb rule, use relative path
options.rules RuleOption one service use one rule, eg:[{service: '', ruleCls: ''}]

class Loadbalance

choose(service: string): Server

Choose a node that running the specific service.

field type description
service string the service name

chooseLoadbalancer(service: string): Loadbalancer

Get loadbalancer.

field type description
service string the service name

Stay in touch

License

NestCloud is MIT licensed.

You can’t perform that action at this time.