npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

26

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();