A NestCloud component for service registration and service discovery.
$ npm i --save @nestcloud/consul-service @nestcloud/consul consul
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 {}
consul:
host: localhost
port: 8500
discoveryHost: localhost
service:
id: your-service-id
name: your-service-name
port: 3000
healthCheck:
timeout: 1s
interval: 10s
route: /health
# when register / deregister the service to consul fail, it will retry five times.
maxRetry: 5
retryInterval: 5000
import { Component } from '@nestjs/common';
import { InjectConsulService, ConsulService } from '@nestcloud/consul-service';
@Component()
export class TestService {
constructor(@InjectConsulService() private readonly service: ConsulService) {}
getServiceNodes() {
const nodes = this.service.getServiceNodes('user-service', {passing: true});
this.service.watch('user-service', nodes => {
console.log(nodes);
});
console.log(nodes);
}
}
consul:
healthCheck:
timeout: 1s
interval: 10s
script: /root/script/check.sh
consul:
healthCheck:
timeout: 1s
interval: 10s
protocol: http
route: /health
consul:
healthCheck:
timeout: 1s
interval: 10s
tcp: localhost:3000
consul:
healthCheck:
ttl: 60s
consul:
healthCheck:
dockerContainerId: 2ddd99fd268c
Import nest consul service module.
field | type | description |
---|---|---|
options.dependencies | string[] | if you are using @nestcloud/boot module, please set [NEST_BOOT] |
options.service.id | string | the service id |
options.service.name | string | the service name |
options.service.port | number | the service port |
options.service.includes | string[] | sync services from consul, if not set, it will sync all services |
options.consul.discoveryHost | string | the discovery ip |
options.consul.healthCheck.timeout | number | the health check timeout, default 1s |
options.consul.healthCheck.interval | number | the health check interval,default 10s |
options.consul.healthCheck.deregisterCriticalServiceAfter | string | timeout after which to automatically deregister service if check remains in critical state |
options.consul.healthCheck.protocol | string | https or http, default is http. |
options.consul.healthCheck.tcp | string | host:port to test, passes if connection is established, fails otherwise. |
options.consul.healthCheck.script | string | path to check script, requires interval. |
options.consul.healthCheck.dockerContainerId | string | Docker container ID to run script. |
options.consul.healthCheck.shell | string | shell in which to run script (currently only supported with Docker). |
options.consul.healthCheck.ttl | string | time to live before check must be updated, instead of http/tcp/script and interval (ex: 60s). |
options.consul.healthCheck.notes | string | human readable description of check. |
options.consul.healthCheck.status | string | initial service status. |
options.consul.healthCheck.route | string | the health check url, default is /health. |
options.consul.maxRetry | number | the max retry count when register service fail |
options.consul.retryInterval | number | the retry interval when register service fail |
Get all services with nodes.
Get all service names
watch service nodes change
watch service name list change
- Author - NestCloud
NestCloud is MIT licensed.