1.3.25 • Published 2 years ago

whale-nest-nacos v1.3.25

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

功能

  • 基于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();
1.2.0

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.3.10

2 years ago

1.3.13

2 years ago

1.3.14

2 years ago

1.3.11

2 years ago

1.3.12

2 years ago

1.3.17

2 years ago

1.3.15

2 years ago

1.3.16

2 years ago

1.3.19

2 years ago

1.3.20

2 years ago

1.3.21

2 years ago

1.3.9

2 years ago

1.3.8

2 years ago

1.3.24

2 years ago

1.3.25

2 years ago

1.3.22

2 years ago

1.2.10

2 years ago

1.3.23

2 years ago

1.2.11

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.1.0

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.3.7

2 years ago

1.3.6

2 years ago

1.3.5

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.0.40

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.49

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.2.9

2 years ago

1.0.34

2 years ago

1.0.33

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago