1.3.25 • Published 3 years ago

whale-nest-nacos v1.3.25

Weekly downloads
-
License
-
Repository
github
Last release
3 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

3 years ago

1.2.8

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.3.10

3 years ago

1.3.13

3 years ago

1.3.14

3 years ago

1.3.11

3 years ago

1.3.12

3 years ago

1.3.17

3 years ago

1.3.15

3 years ago

1.3.16

3 years ago

1.3.19

3 years ago

1.3.20

3 years ago

1.3.21

3 years ago

1.3.9

3 years ago

1.3.8

3 years ago

1.3.24

3 years ago

1.3.25

3 years ago

1.3.22

3 years ago

1.2.10

3 years ago

1.3.23

3 years ago

1.2.11

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.1.0

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.3.7

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.0.40

3 years ago

1.0.44

3 years ago

1.0.43

3 years ago

1.0.42

3 years ago

1.0.41

3 years ago

1.0.48

3 years ago

1.0.47

3 years ago

1.0.46

3 years ago

1.0.45

3 years ago

1.0.49

3 years ago

1.0.51

3 years ago

1.0.50

3 years ago

1.2.9

3 years ago

1.0.34

3 years ago

1.0.33

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago