0.4.4 • Published 5 years ago

@nestcloud/consul-service v0.4.4

Weekly downloads
26
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 @nestcloud/consul-service @nestcloud/consul consul

Quick Start

Import Module

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

@Module({
  imports: [
      ConsulModule.register({
          host: '127.0.0.1',
          port: 8500
      }),
      ConsulServiceModule.register({
           serviceId: 'node1',
           serviceName: 'user-service',
           port: 3001,
           consul: {
               discoveryHost: 'localhost',
               healthCheck: {
                   timeout: '1s',
                   interval: '10s',
                   route: '/health',
               },
               maxRetry: 5,
               retryInterval: 3000,
           }
      }),
  ],
})
export class ApplicationModule {}

If you use @nestcloud/boot module.

import { Module } from '@nestjs/common';
import { ConsulModule } from '@nestcloud/consul';
import { ConsulServiceModule } from '@nestcloud/consul-service';
import { BootModule } from '@nestcloud/boot';
import { NEST_BOOT } from '@nestcloud/common';

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

Simple Boot Config

web: 
  serviceId: node1
  serviceName: user-service
  port: 3001
consul:
  host: localhost
  port: 8500
  discoveryHost: localhost
  healthCheck:
    timeout: 1s
    interval: 10s
    route: /health
  # when register / deregister the service to consul fail, it will retry five times.
  maxRetry: 5
  retryInterval: 5000

Usage

import { Component } from '@nestjs/common';
import { InjectConsulService, ConsulService } from '@nestcloud/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);
  }
}

Checks

Script + Interval

consul:
  healthCheck:
    timeout: 1s
    interval: 10s
    script: /root/script/check.sh

Http + Interval

consul:
  healthCheck:
    timeout: 1s
    interval: 10s
    protocol: http
    route: /health

Tcp + Interval

consul:
  healthCheck:
    timeout: 1s
    interval: 10s
    tcp: localhost:3000

Time To Live

consul:
  healthCheck:
    ttl: 60s

Docker + Interval

consul:
  healthCheck:
    dockerContainerId: 2ddd99fd268c

API

class ConsulServiceModule

static register(options: RegisterOptions): DynamicModule

Import nest consul service module.

fieldtypedescription
options.dependenciesstring[]if you are using @nestcloud/boot module, please set NEST_BOOT
options.serviceIdstringthe service id
options.serviceNamestringthe service name
options.portnumberthe service port
options.consul.discoveryHoststringthe discovery ip
options.consul.healthCheck.timeoutnumberthe health check timeout, default 1s
options.consul.healthCheck.intervalnumberthe health check interval,default 10s
options.consul.healthCheck.deregisterCriticalServiceAfterstringtimeout after which to automatically deregister service if check remains in critical state
options.consul.healthCheck.protocolstringhttps or http, default is http.
options.consul.healthCheck.tcpstringhost:port to test, passes if connection is established, fails otherwise.
options.consul.healthCheck.scriptstringpath to check script, requires interval.
options.consul.healthCheck.dockerContainerIdstringDocker container ID to run script.
options.consul.healthCheck.shellstringshell in which to run script (currently only supported with Docker).
options.consul.healthCheck.ttlstringtime to live before check must be updated, instead of http/tcp/script and interval (ex: 60s).
options.consul.healthCheck.notesstringhuman readable description of check.
options.consul.healthCheck.statusstringinitial service status.
options.consul.healthCheck.routestringthe health check url, default is /health.
options.consul.maxRetrynumberthe max retry count when register service fail
options.consul.retryIntervalnumberthe 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

NestCloud is MIT licensed.

0.5.0-8

5 years ago

0.5.0-7

5 years ago

0.5.0-6

5 years ago

0.5.0-5

5 years ago

0.5.0-4

5 years ago

0.5.0-3

5 years ago

0.5.0-2

5 years ago

0.5.0-1

5 years ago

0.5.0-0

5 years ago

0.4.5-3

5 years ago

0.4.5-2

5 years ago

0.4.5-1

5 years ago

0.4.5-0

5 years ago

0.4.4

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.17

5 years ago

0.3.16

5 years ago

0.3.16-0

5 years ago

0.3.15

5 years ago

0.3.14

5 years ago

0.3.14-2

5 years ago

0.3.14-1

5 years ago

0.3.14-0

5 years ago

0.3.13

5 years ago

0.3.12

5 years ago

0.3.10

5 years ago

0.3.9

5 years ago

0.3.9-0

5 years ago

0.3.8

5 years ago

0.3.8-5

5 years ago

0.3.8-4

5 years ago

0.3.8-3

5 years ago

0.3.8-2

5 years ago

0.3.8-1

5 years ago

0.3.8-0

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.5-3

5 years ago

0.2.5-2

5 years ago

0.2.5-1

5 years ago

0.2.5-0

5 years ago

0.2.4

5 years ago

0.2.4-3

5 years ago

0.2.4-2

5 years ago

0.2.4-1

5 years ago

0.2.4-0

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.1-5

5 years ago

0.2.1-4

5 years ago

0.2.1-3

5 years ago

0.2.1-2

5 years ago

0.2.1-1

5 years ago

0.2.1-0

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago