0.7.1 • Published 4 years ago

@dimensionfourcloud/service v0.7.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

NestCloud - Service

Description

A NestCloud component for service registration and service discovery.

中文文档

Installation

# consul backend
$ npm install @dimensionfourcloud/service @dimensionfourcloud/consul consul --save
# etcd backend
$ npm install @dimensionfourcloud/service @dimensionfourcloud/etcd etcd3 --save

Quick Start

Import Module

Consul Backend

service:
  discoveryHost: localhost
  id: your-service-id
  name: your-service-name
  port: 3000
  tags: ['v1.0.1']
  healthCheck:
    timeout: 1s
    interval: 10s
    route: /health
  maxRetry: 5
  retryInterval: 5000
import { Module } from '@nestjs/common';
import { ConsulModule } from '@dimensionfourcloud/consul';
import { ServiceModule } from '@dimensionfourcloud/service';
import { BootModule } from '@dimensionfourcloud/boot';
import { NEST_BOOT, NEST_CONSUL } from '@dimensionfourcloud/common';

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

Etcd Backend

service:
  discoveryHost: localhost
  id: your-service-id
  name: your-service-name
  port: 3000
  tags: ['v1.0.1']
  healthCheck:
    ttl: 20
  maxRetry: 5
  retryInterval: 5000
import { Module } from '@nestjs/common';
import { EtcdModule } from '@dimensionfourcloud/etcd';
import { ServiceModule } from '@dimensionfourcloud/service';
import { BootModule } from '@dimensionfourcloud/boot';
import { NEST_BOOT, NEST_ETCD } from '@dimensionfourcloud/common';

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

Usage

import { Injectable } from '@nestjs/common';
import { InjectService, Service } from '@dimensionfourcloud/service';

@Injectable()
export class TestService {
  constructor(@InjectService() private readonly service: Service) {}

  getServiceNodes() {
      const nodes = this.service.getServiceNodes('user-service', {passing: true});
      this.service.watch('user-service', nodes => {
          console.log(nodes);
      });
      console.log(nodes);
  }
}

Checks

Script + Interval

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

Http + Interval

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

Tcp + Interval

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

Time To Live

service:
  healthCheck:
    ttl: 60s

Docker + Interval

service:
  healthCheck:
    dockerContainerId: 2ddd99fd268c

API

class ServiceModule

static register(options: RegisterOptions): DynamicModule

Import nest consul service module.

fieldtypedescription
options.dependenciesstring[]if you are using @dimensionfourcloud/boot module, please set NEST_BOOT
options.idstringthe service id
options.namestringthe service name
options.portnumberthe service port, if not set, it will use random port
options.tagsnumberthe service tags
options.includesstring[]sync services from consul, if not set, it will sync all services
options.discoveryHoststringthe discovery ip
options.healthCheck.timeoutnumberthe health check timeout, default 1s
options.healthCheck.intervalnumberthe health check interval,default 10s
options.healthCheck.deregisterCriticalServiceAfterstringtimeout after which to automatically deregister service if check remains in critical state
options.healthCheck.protocolstringhttps or http, default is http.
options.healthCheck.tcpstringhost:port to test, passes if connection is established, fails otherwise.
options.healthCheck.scriptstringpath to check script, requires interval.
options.healthCheck.dockerContainerIdstringDocker container ID to run script.
options.healthCheck.shellstringshell in which to run script (currently only supported with Docker).
options.healthCheck.ttlstringtime to live before check must be updated, instead of http/tcp/script and interval (ex: 60s).
options.healthCheck.notesstringhuman readable description of check.
options.healthCheck.statusstringinitial service status.
options.healthCheck.routestringthe health check url, default is /health.
options.maxRetrynumberthe max retry count when register service fail
options.retryIntervalnumberthe retry interval when register service fail

class Service

getServices(): ServiceNode[]

Get all services with nodes.

getServiceNames(): string[]

Get all service names

watch(service: string, callback: (nodes: IServiceNode[]) => void): void

watch service nodes change

watchServiceList(callback: (services: string[]) => void): void

watch service name list change

Stay in touch

License

NestCloud is MIT licensed.