A component of nestcloud. NestCloud is a nest framework micro-service solution.
This is a Nest module provide service registration and service discovery.
$ npm i --save nest-consul-service nest-consul consul
import { Module } from '@nestjs/common';
import { ConsulModule } from 'nest-consul';
import { ConsulServiceModule, Check, PASSING, WARNING, FAILURE } 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',
},
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, Check, PASSING, WARNING, FAILURE } from 'nest-consul-service';
import { BootModule, BOOT_ADAPTER } from 'nest-boot';
@Module({
imports: [
ConsulModule.register({adapter: BOOT_ADAPTER}),
BootModule.register(__dirname, 'bootstrap.yml'),
ConsulServiceModule.register({adapter: BOOT_ADAPTER}),
],
})
export class ApplicationModule {}
web:
serviceId: node1
serviceName: user-service
port: 3001
consul:
host: localhost
port: 8500
discovery_host: localhost
health_check:
timeout: 1s
interval: 10s
# when register / deregister the service to consul fail, it will retry five times.
max_retry: 5
retry_interval: 5000
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);
}
}
The health check policy is a function, it will return an object that has status(PASSING, WARNING, CRITICAL) and message.
import { Module } from '@nestjs/common';
import { ConsulModule } from 'nest-consul';
import { ConsulServiceModule, PASSING } from 'nest-consul-service';
import { BootModule, BOOT_ADAPTER } from 'nest-boot';
@Module({
imports: [
ConsulModule.register({adapter: BOOT_ADAPTER}),
BootModule.register(__dirname, 'bootstrap.yml'),
ConsulServiceModule.register({
adapter: BOOT_ADAPTER,
checks: [() => {
return {status: PASSING, message: 'ok'}
}]
}),
],
})
export class ApplicationModule {}
Import nest consul service module.
field | type | description |
---|---|---|
options.adapter | string | if you are using nest-boot module, please set BOOT_ADAPTER |
options.serviceId | string | the service id |
options.serviceName | string | the service name |
options.port | number | the service port |
options.consul.discoveryHost | 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.max_retry | number | the max retry count when register service fail |
options.consul.retry_interval | number | the retry interval when register service fail |
options.checks | (()=> Check)[] | custom health check policies |
Get available services.
- Author - Miaowing
Nest is MIT licensed.