# nest-consul-service

> A Nest framework (node.js) module for registering and getting consul service easily.

Latest version **3.0.3** (published 2019-03-05) · MIT license · 0 weekly downloads

## Install

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

## 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.0.3 |
| Published | 2019-03-05 |
| First published | 2018-05-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 107.1 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Miaowing |
| Maintainers | zfeng |

## Links

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

## Dependencies (7)

- [consul](https://npm.io/package/consul.md) ^0.34.0
- [yamljs](https://npm.io/package/yamljs.md) ^0.3.0
- [nest-boot](https://npm.io/package/nest-boot.md) ^1.1.0
- [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
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^5.3.7

## Recent versions

- 3.0.3 (latest) — 2019-03-05
- 3.0.2 — 2019-02-28
- 3.0.1 — 2019-02-15
- 3.0.0 — 2019-02-14
- 2.9.0 — 2019-02-14
- 2.8.1 — 2019-02-11
- 2.8.0 — 2019-01-11
- 2.7.2 — 2019-01-11
- 2.7.1 — 2019-01-11
- 2.7.0 — 2019-01-11
- 2.6.1 — 2019-01-11
- 2.6.0 — 2018-12-06
- 2.5.8 — 2018-12-06
- 2.5.7 — 2018-11-27
- 2.5.6 — 2018-11-26
- … 24 more at https://npm.io/package/nest-consul-service/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-wu-zhu-ce-yu-fa-xian)

This is a [Nest](https://github.com/nestjs/nest) module provide service registration and service discovery.

## Installation

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

## Quick Start

#### Import Module

```typescript
import { Module } from '@nestjs/common';
import { ConsulModule } from 'nest-consul';
import { ConsulServiceModule } from 'nest-consul-service';

@Module({
  imports: [
      ConsulModule.register({
          host: '127.0.0.1',
          port: 8500
      }),
      ConsulServiceModule.register({
        serviceId: 'node1',
            serviceName: 'user-service',
            port: 3001,
            consul: {
                discovery_host: 'localhost',
                health_check: {
                    timeout: '1s',
                    interval: '10s',
                    route: '/health',
                },
                max_retry: 5,
                retry_interval: 3000,
            }
      }),
  ],
})
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 { ConsulServiceModule } from 'nest-consul-service';
import { BootModule } from 'nest-boot';
import { NEST_BOOT } from 'nest-common';

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

#### Nest-boot config file

```yaml
web: 
  serviceId: node1
  serviceName: user-service
  port: 3001
consul:
  host: localhost
  port: 8500
  discovery_host: localhost
  health_check:
    timeout: 1s
    interval: 10s
    route: /health
  # when register / deregister the service to consul fail, it will retry five times.
  max_retry: 5
  retry_interval: 5000
```

#### Usage

```typescript
import { Component } from '@nestjs/common';
import { InjectConsulService, ConsulService } from 'nest-consul-service';

@Component()
export class TestService {
  constructor(@InjectConsulService() private readonly service: ConsulService) {}

  getServices() {
      const services = this.service.getServices('user-service', {passing: true});
      this.service.onUpdate('user-service', services => {
          console.log(services);
      });
      console.log(services);
  }
}
```

## API

### class ConsulServiceModule

#### static register\(options: RegisterOptions\): DynamicModule

Import nest consul service module.

| field | type | description |
| :--- | :--- | :--- |
| options.dependencies | string[] | if you are using nest-boot module, please set [NEST_BOOT] |
| options.serviceId | string | the service id |
| options.serviceName | string | the service name |
| options.port | number | the service port |
| options.consul.discovery_host | string | the discovery ip |
| options.consul.health\_check.timeout | number | the health check timeout, default 1s |
| options.consul.health\_check.interval | number | the health check interval，default 10s |
| options.consul.health\_check.deregistercriticalserviceafter | string | timeout after which to automatically deregister service if check remains in critical state | 
| options.consul.health\_check.protocol | string | https or http, default is http. | 
| options.consul.health\_check.route | string | the health check url, default is /health. | 
| options.consul.max\_retry | number | the max retry count when register service fail |
| options.consul.retry\_interval | number | the retry interval when register service fail |

### class ConsulService

#### getServices\(serviceName: string, options?: object\): Server[]

Get available services.

#### getAllServices()

Get all services

#### onServiceChange(service: string, callback: (servers: Server[]) => void): void

watch service change

#### onServiceListChange(callback: (newServices: string[]) => void): void

watch service list change

## Stay in touch

- Author - [Miaowing](https://github.com/miaowing)

## License

  Nest is [MIT licensed](LICENSE).

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