# nest-consul-loadbalance

> A Nest framework (node.js) module for getting service from consul and sending http request by loadbalance.

Latest version **3.5.1** (published 2019-02-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install nest-consul-loadbalance
pnpm add nest-consul-loadbalance
yarn add nest-consul-loadbalance
bun add nest-consul-loadbalance
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.5.1 |
| Published | 2019-02-28 |
| First published | 2018-05-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 90.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Miaowing |
| Maintainers | zfeng |

## Links

- npm: https://www.npmjs.com/package/nest-consul-loadbalance
- Repository: https://github.com/nest-cloud/nest-consul-loadbalance
- Homepage: https://github.com/nest-cloud/nest-consul-loadbalance#readme
- Issues: https://github.com/nest-cloud/nest-consul-loadbalance/issues
- npm.io page: https://npm.io/package/nest-consul-loadbalance

## Dependencies (8)

- [yamljs](https://npm.io/package/yamljs.md) ^0.3.0
- [nest-boot](https://npm.io/package/nest-boot.md) ^1.1.0
- [random-js](https://npm.io/package/random-js.md) ^1.0.8
- [blueimp-md5](https://npm.io/package/blueimp-md5.md) ^2.10.0
- [nest-common](https://npm.io/package/nest-common.md) 0.0.1
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.14.108
- [nest-consul-config](https://npm.io/package/nest-consul-config.md) ^1.3.0
- [nest-consul-service](https://npm.io/package/nest-consul-service.md) ^2.8.0

## Recent versions

- 3.5.1 (latest) — 2019-02-28
- 3.5.0 — 2019-02-14
- 3.4.0 — 2019-01-11
- 3.3.2 — 2018-12-10
- 3.3.1 — 2018-12-05
- 3.3.0 — 2018-12-03
- 3.2.1 — 2018-11-01
- 3.2.0 — 2018-09-26
- 3.1.0 — 2018-09-26
- 3.0.0 — 2018-09-10
- 3.0.0-alpha3 — 2018-09-10
- 3.0.0-alpha2 — 2018-09-10
- 3.0.0-alpha1 — 2018-09-10
- 1.1.0 — 2018-07-11
- 1.0.2 — 2018-07-11
- … 5 more at https://npm.io/package/nest-consul-loadbalance/versions

## README

<p align="center">
  <a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo_text.svg" width="320" alt="Nest Logo" /></a>
</p>

## Description

A component of [nestcloud](http://github.com/nest-cloud/nestcloud). NestCloud is a nest framework micro-service solution.
  
[中文文档](https://nestcloud.org/solutions/fu-zai-jun-heng)

This is a software load balancers primary for rest calls.

## Installation

```bash
$ npm i --save nest-consul consul nest-consul-loadbalance
```

## Quick Start

#### Import Module

```typescript
import { Module } from '@nestjs/common';
import { ConsulModule } from 'nest-consul';
import { LoadbalanceModule } from 'nest-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 [nest-boot](https://github.com/miaowing/nest-boot) module.

```typescript
import { Module } from '@nestjs/common';
import { ConsulModule } from 'nest-consul';
import { LoadbalanceModule } from 'nest-consul-loadbalance';
import { BootModule } from 'nest-boot';
import { NEST_BOOT } from 'nest-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

```yaml
consul:
  host: localhost
  port: 8500
loadbalance:
  rules:
    - {service: 'test-service', ruleCls: 'RandomRule'}
    - {service: 'user-service', ruleCls: '../rules/CustomRule'}
```

#### Usage

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

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

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

### Custom Loadbalance Rule

```typescript
import { Rule, Loadbalancer } from 'nest-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 nest-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

- Author - [NestCloud](https://github.com/nest-cloud)

## License

  NestCloud is [MIT licensed](LICENSE).

---
_Source: https://npm.io/package/nest-consul-loadbalance · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
