@jiaxinjiang/nest-nacos
v1.0.12
Published
Nacos component for NestJs.
Downloads
10
Readme
Features
- Automatic registration and subscription services.
- Listen for configuration changes, and use
@jiaxinjiang/nest-remote-config
module to obtain remote configuration data.
Installation
Yarn
yarn add @jiaxinjiang/nest-nacos
NPM
npm install @jiaxinjiang/nest-nacos --save
Getting Started
You can use the @jiaxinjiang/nest-config
package to configure Nacos.
Directory structure:
├── env
│ ├── env
│ ├── env.dev
│ ├── env.prod
│ ├── env.test
├── src
│ ├── app.module.ts
│ ├── config
│ ├── nacos.config.ts
Nacos configuration file:
// .env.dev
NEST_NACOS_SERVER_LIST=192.168.0.102:8848
NEST_NACOS_NAMESPACE=d8bcbaad-f3d1-40d8-9d55-a03cafea5299
NEST_NACOS_GROUP_NAME=dev
NEST_NACOS_CONFIG_ID_BASIC=basic.config.yml
// nacos.config.ts
import {
NestNacosConfig,
ClientOptions,
ConfigOptions,
NacosInstanceOptions,
NacosNamingOptions,
NacosSubscribeOptions,
} from '@jiaxinjiang/nacos';
const {
NEST_NACOS_NAMESPACE,
NEST_NACOS_GROUP_NAME,
NEST_NACOS_SERVER_LIST,
NEST_NACOS_CONFIG_ID_BASIC,
} = process.env;
export default {
naming: {
serverList: NEST_NACOS_SERVER_LIST,
namespace: NEST_NACOS_NAMESPACE,
} as NacosNamingOptions,
instance: {
groupName: NEST_NACOS_GROUP_NAME,
} as NacosInstanceOptions,
configClient: {
leaderPort: Number(`45${Math.floor(Math.random() * 900) + 100}`),
serverAddr: NEST_NACOS_SERVER_LIST.split(',')[0],
namespace: NEST_NACOS_NAMESPACE,
} as ClientOptions,
configs: [{
dataId: NEST_NACOS_CONFIG_ID_BASIC,
groupName: NEST_NACOS_GROUP_NAME,
}] as ConfigOptions[],
subscribers: [
// Services to be monitored
{
serviceName: 'service-1',
groupName: 'nodejs',
},
{
serviceName: 'service-2',
groupName: 'nodejs',
},
] as NacosSubscribeOptions[],
} as NestNacosConfig;
Module introduction example:
import { Module } from '@nestjs/common';
import { ConfigService } from '@jiaxinjiang/nest-config';
import { LoggerProvider } from '@jiaxinjiang/nest-logger';
import {
NacosNamingModule,
NacosNamingOptions,
NacosInstanceModule,
NacosInstanceOptions,
NacosSubscribeOptions,
NacosConfigModule,
ConfigOptions,
ClientOptions,
} from '@jiaxinjiang/nest-nacos';
@Module({
imports: [
NacosNamingModule.forRootAsync({
useFactory: (configService: ConfigService, logger: LoggerProvider) => {
const nacosConfig = configService.get('nacos');
const subscribers =
(nacosConfig.subscribers as NacosSubscribeOptions[]) || [];
const naming = configService.get('nacos').naming as NacosNamingOptions;
naming.logger = logger;
naming.appName = naming.appName || configService.get('appName');
return {
naming,
subscribers,
};
},
inject: [ConfigService, LoggerProvider],
}),
NacosInstanceModule.forRootAsync({
useFactory: (configService: ConfigService) => {
const nacosConfig = configService.get('nacos');
const instance = nacosConfig.instance as NacosInstanceOptions;
const subscribers =
(nacosConfig.subscribers as NacosSubscribeOptions[]) || [];
instance.serviceName =
instance.serviceName || configService.get('appName');
instance.ip = instance.ip || configService.get('ip');
instance.port = instance.port || configService.get('port');
return {
instance,
subscribers,
};
},
inject: [ConfigService],
}),
NacosConfigModule.forRootAsync({
useFactory: (configService: ConfigService, logger: LoggerProvider) => {
const {
naming,
configs,
client,
}: {
naming: NacosNamingOptions;
configs: ConfigOptions[];
client: ClientOptions;
} = configService.get('nacos');
client.appName = client.appName || configService.get('appName');
client.serverAddr = client.serverAddr || naming.serverList[0];
client.namespace = client.namespace || naming.namespace;
return {
client,
configs,
logger,
};
},
inject: [ConfigService, LoggerProvider],
}),
],
providers: [],
exports: [],
})
export class NacosModule {}
API
NacosNamingService:
- nacosNamingService.registerInstance(serviceName: string, instance: NacosInstanceOptions, groupName?: string)
- nacosNamingService.deregisterInstance(serviceName: string, instance: NacosInstanceOptions, groupName?: string)
- nacosNamingService.getAllInstances(serviceName: string, groupName?: string, clusters?: string, subscribe?: boolean)
- nacosNamingService.selectInstances(serviceName: string, groupName?: string, clusters?: string, healthy?: boolean, subscribe?: boolean)
- nacosNamingService.getServerStatus()
- nacosNamingService.subscribe(info: string | { serviceName: string; groupName?: string; clusters?: string }, listener: (instances: NacosInstance[], logger: NacosLogger) => void = () => null)
- nacosNamingService.unSubscribe(info: string | { serviceName: string; groupName?: string; clusters?: string }, listener: (instances: NacosInstance[], logger: NacosLogger) => void = () => null)
- nacosNamingService.selectOneHealthyInstance(serviceName: string, groupName?: string, clusters?: string)
- nacosNamingService.axiosRequestInterceptor()
NacosConfigService:
- nacosConfigService.getListeners()
- nacosConfigService.getConfigClient()
- nacosConfigService.getConfigStore()
- nacosConfigService.getConfigKey(dataId: string, groupName = 'DEFAULT_GROUP')
ConfigStore:
- configStore.get(configKey: string, defaultConfigData?: T | any)
- configStore.set(configKey: string, configData?: any)
- configStore.on('change', (configKey: string, configData: any) => {})