3.0.3 • Published 5 years ago

nest-consul-service v3.0.3

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

Description

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

中文文档

This is a Nest module provide service registration and service discovery.

Installation

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

Quick Start

Import Module

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 module.

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

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

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.

fieldtypedescription
options.dependenciesstring[]if you are using nest-boot module, please set NEST_BOOT
options.serviceIdstringthe service id
options.serviceNamestringthe service name
options.portnumberthe service port
options.consul.discovery_hoststringthe discovery ip
options.consul.health_check.timeoutnumberthe health check timeout, default 1s
options.consul.health_check.intervalnumberthe health check interval,default 10s
options.consul.health_check.deregistercriticalserviceafterstringtimeout after which to automatically deregister service if check remains in critical state
options.consul.health_check.protocolstringhttps or http, default is http.
options.consul.health_check.routestringthe health check url, default is /health.
options.consul.max_retrynumberthe max retry count when register service fail
options.consul.retry_intervalnumberthe 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

License

Nest is MIT licensed.

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.9.0

5 years ago

2.8.1

5 years ago

2.8.0

5 years ago

2.7.2

5 years ago

2.7.1

5 years ago

2.7.0

5 years ago

2.6.1

5 years ago

2.6.0

5 years ago

2.5.8

5 years ago

2.5.7

5 years ago

2.5.6

5 years ago

2.5.5

5 years ago

2.5.4

5 years ago

2.5.3

5 years ago

2.5.2

5 years ago

2.5.1

5 years ago

2.5.0

5 years ago

2.4.4

5 years ago

2.4.3

5 years ago

2.4.2

5 years ago

2.4.1

5 years ago

2.4.0

6 years ago

2.3.0

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.0.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago