nest-consul-service
TypeScript icon, indicating that this package has built-in type declarations

3.0.3 • Public • Published

Nest Logo

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.

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

License

Nest is MIT licensed.

Readme

Keywords

none

Package Sidebar

Install

npm i nest-consul-service

Weekly Downloads

3

Version

3.0.3

License

MIT

Unpacked Size

110 kB

Total Files

43

Last publish

Collaborators

  • zfeng