@qiwi/nestjs-enterprise-consul

@qiwi/nestjs-enterprise-consul

Nestjs module for working with Consul

Installation

Requires following packages to be installed

yarn add @qiwi/nestjs-enterprise-consul

Configuration

import { Module } from '@nestjs/common'
import { ConfigModule } from '@qiwi/nestjs-enterprise-config'
import { LoggerModule } from '@qiwi/nestjs-enterprise-logger'
import { ConsulModule } from '@qiwi/nestjs-enterprise-consul'

@Module({
imports: [
ConfigModule,
LoggerModule,
ConsulModule,
// and so on
],
controllers: [],
providers: [],
})
export class AppModule {}

Config

{
"data": {
"name": "APP_NAME",
"server": {
"port": 8080,
"host": "$host:"
},
"consul": {
"host": "CONSUL_AGENT_HOST",
"port": "CONSUL_AGENT_PORT",
"token": "consul token",
"tags": ["tag"]
}
},
"sources": {
"host": {
"pipeline": "ip"
}
}
}

Usage

// main.ts
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'

async function bootstrap() {

const app = await NestFactory.create(AppModule)
//...

const consul = app.get('IConsul')
const local = config.get('local')
//...

if (!local) {
consul.register()
}
}
//...
// service.ts
@Injectable()
export class ConnectionProviderService implements IConnectionProvider {
constructor(
@Inject('IConsul')
private consul: IConsul,
) {}

async getConnectionParams(serviceName: string) {
return this.consul.getConnectionParams(serviceName)
}
}

API

ConsulModule class

Exports ConsulService with token IConsul

ConsulService class

register(opts: any): IPromise

getConnectionParams(opts: any): Promise<IConnectionParams | undefined>

field type description
IConnectionParams.host string host
IConnectionParams.port number port

getKv(opts: any): Promise <INormalizedConsulKvValue>

field type description
INormalizedConsulKvValue.createIndex number createIndex
INormalizedConsulKvValue.modifyIndex number modifyIndex
INormalizedConsulKvValue.lockIndex number lockIndex
INormalizedConsulKvValue.key string key
INormalizedConsulKvValue.flags number flags
INormalizedConsulKvValue.value string value

Docs