whale-nest-nacos
Version:
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
157 lines (115 loc) • 3.67 kB
Markdown
<h1 align="center">whale-nest-nacos</h1>
- 基于nacos实现服务发现,订阅实例变更,配置变更,下发负载均衡策略
- 基于nacos-sdk-nodejs提供服务注册、发现、订阅等功能
**Yarn**
```bash
yarn add whale-nest-nacos
```
**NPM**
```bash
npm install whale-nest-nacos --save
```
```bash
├── config
│ ├── localhost.json
│ ├── develop.json
│ ├── stage.json
│ ├── production.json
├── src
│ ├── app.module.ts
│ ├── config
│ ├── nacos.config.ts
```
Nacos 配置文件:
```bash
// develop.json
{
"nacosServerAddress": "10.168.0.61:8848", // nacos服务器地址
"namespace": "develop", // 命名空间,一般以命名空间区别环境
}
```
```ts
// 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 { }
```
```ts
// 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();
}
}
```
```ts
// 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();
}
}
```
```ts
import { WhaleNacosConfigService } from 'whale-nest-nacos';
const nacosConfig = new WhaleNacosConfigService({
group: 'HTTP',
serviceName: 'sitevar'
});
const serviceAddress = nacosConfig.getServiceConfig();
```