whale-nest-nacos
v1.3.25
Published
If you are a nest micro service architecture, you can use the SDK, so as to realize service discovery based on nacos, distributed load balancing strategy
Downloads
114
Maintainers
Readme
功能
- 基于nacos实现服务发现,订阅实例变更,配置变更,下发负载均衡策略
- 基于nacos-sdk-nodejs提供服务注册、发现、订阅等功能
下载
Yarn
yarn add whale-nest-nacos
NPM
npm install whale-nest-nacos --save
起步
在项目根目录创建config文件夹,目录如下:
├── config
│ ├── localhost.json
│ ├── develop.json
│ ├── stage.json
│ ├── production.json
├── src
│ ├── app.module.ts
│ ├── config
│ ├── nacos.config.ts
Nacos 配置文件:
// develop.json
{
"nacosServerAddress": "10.168.0.61:8848", // nacos服务器地址
"namespace": "develop", // 命名空间,一般以命名空间区别环境
}
注册Grpc连接,有两种方式,如下:
1、通过 nacosClientProvider 方法注入
// chameleon.module.ts
import { Module } from '@nestjs/common';
import { nacosClientProvider } from 'whale-nest-nacos'
import { getInfraProtoPath } from '../utils';
import {MiddleWareService} from './chameleon.service';
import {MiddleWareServiceResolver} from './chameleon.resolver';
@Module({
imports: [],
providers: [
nacosClientProvider({
serviceName: 'chameleon',
packageName: 'chameleon_proto',
protoPath: getInfraProtoPath('/chameleon/chameleon.proto'),
}),
MiddleWareService,
MiddleWareServiceResolver
],
exports: [MiddleWareService],
})
export class ChameleonModule { }
// chameleon.service.ts
import { Injectable, OnModuleInit, Inject } from '@nestjs/common';
import { NacosClientGrpc } from 'whale-nest-nacos'
import {IMiddleWareService} from './chameleon.d';
@Injectable()
export class MiddleWareService implements OnModuleInit {
private middleWareService: IMiddleWareService;
constructor(@Inject('chameleon') private readonly client: NacosClientGrpc) { }
async onModuleInit() {
this.middleWareService = this.client.getService('MiddleWareService');
}
public async create(req) {
return await this.middleWareService.create(req).toPromise();
}
public async getMiddleWareList(req) {
const { getMiddleWareList } = this.middleWareService
return await getMiddleWareList(req).toPromise();
}
}
2、通过 getNacosClientGrpc 方法同步获取
// schedule.service.ts
import { Injectable, OnModuleInit } from '@nestjs/common';
import { getNacosClientGrpc,NacosClientGrpc } from 'whale-nest-nacos'
import { getInfraProtoPath } from '../utils';
import { IScheduleService } from './schedule.d';
@Injectable()
export class ScheduleService implements OnModuleInit {
private client: NacosClientGrpc;
private scheduleService: IScheduleService;
async onModuleInit() {
this.client = await getNacosClientGrpc({
serviceName: 'core-infra-schedule',
packageName: 'openplatform_schedule_proto',
protoPath: getInfraProtoPath('/schedule/schedule.proto')
})
this.scheduleService = this.client.getService<IScheduleService>('ScheduleService');
}
public async listServerResource(req) {
return await this.scheduleService.listServerResource(req).toPromise();
}
public async listScheduleService(req) {
return await this.scheduleService.listScheduleService(req).toPromise();
}
}
获取后端服务nacos配置
import { WhaleNacosConfigService } from 'whale-nest-nacos';
const nacosConfig = new WhaleNacosConfigService({
group: 'HTTP',
serviceName: 'sitevar'
});
const serviceAddress = nacosConfig.getServiceConfig();