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

egg-nacos-ts

v1.0.2

Published

Nacos plugin for egg

Downloads

14

Readme

egg-nacos-ts

基于nacos-sdk-nodejs开发的nacos插件。cluster模式下支持单实例注册,以及通过配置自动注册和监听服务。

安装步骤:

npm install egg-nacos-ts
yarn add egg-nacos-ts

配置方式:

修改eggjs项目中config/plugin.ts文件,开启egg-nacos-ts插件。

// 示例代码
import { EggPlugin } from 'egg';

const plugin: EggPlugin = {
  nacos: {
    enable: true,
    package: 'egg-nacos-ts',
  },
};
export default plugin;

配置config/config.default.ts文件

// 示例代码,具体配置项参考ts声明文件
config.nacos = {
  serverList: ['127.0.0.1:8848'],
  namespace: 'public',
  subscribers: {  // 需要监听的服务,不配置不监听
    messageService: {
      serviceName: 'message-service',
    },
  },
  configCenter: {  // 配置中心相关配置
    clientOptions: {},
    configList: {
      baseConfig: {
        dataId: 'test-config.yml',
        groupName: 'DEFAULT_GROUP',
      },
    },
  },
  providers: {
    accountService: {
      serviceName: 'account-service',
      instance: {
        ip: '127.0.0.1',
        port: 8874,
      },
      groupName: 'DEFAULT_GROUP',
    },
  },
};

使用方式:

从nacos配置中心远程读取配置示例:

// agent.ts
import { Agent } from 'egg';
import { saveNacosRemoteConfig } from './lib/configHandler';

export default class AgentBootHook {
  constructor(private readonly agent: Agent) {}

  async didReady() {
    // 把nacos远程配置保存到临时文件,供appWorker调用。
    await saveNacosRemoteConfig(this.agent);
  }
}
// app.ts
import { Application, IBoot } from 'egg';
import { initNacosConfig } from './lib/configHandler';

export default class AppBoot implements IBoot {
  constructor(private app: Application) {}

  configWillLoad() {
    initNacosConfig(this.app);
  }

  async didReady() {}
  async beforeClose() {}
}
// lib/configHandler.ts
import { Agent, EggApplication } from 'egg';
import * as fs from 'fs';
import * as path from 'path';
import * as assert from 'assert';
import * as YAML from 'yamljs';
import * as is from 'is-type-of';

// 临时写入的远程配置文件
export const nacosRuntimeFile = './run/nacos-remote-config.json';

/**
 * 使用字符串深度获取对象属性
 * @param object
 * @param path
 * @param defaultValue
 */
export const deepGet = (object, path, defaultValue?) => {
  return (
    (!Array.isArray(path)
      ? path
          .replace(/\[/g, '.')
          .replace(/\]/g, '')
          .split('.')
      : path
    ).reduce((o, k) => (o || {})[k], object) || defaultValue
  );
};

/**
 * 深度遍历配置文件,检查模板字段,并替换。
 * @param obj
 * @param cb
 */
export const depthSearch = (obj, config) => {
  const regular = /^\$\{(.*)+\}$/;
  for (const index in obj) {
    const item = obj[index];
    if (is.object(item)) {
      depthSearch(item, config);
    } else if (is.string(item) && regular.test(item)) {
      // console.log(item,  deepGet(config, temp[1], ''));
      const temp = item.match(regular);
      obj[index] = deepGet(config, temp[1], '');
    }
  }
};

/**
 * agentWorker获取nacos配置数据,
 * 写入到运行时文件中,供appWorker调用。
 * @param agent
 */
export const saveNacosRemoteConfig = async (agent: Agent) => {
  const { nacosConfig, config } = agent;
  try {
    const configs = await nacosConfig.getAllConfigs();
    if (configs.length < 1) {
      throw new Error('Nacos配置信息未找到');
    }
    const configData = YAML.parse(Object.values(configs[0])[0]);
    const tempConfigFile = path.join(agent.baseDir, nacosRuntimeFile);
    fs.writeFileSync(tempConfigFile, JSON.stringify(configData));
  } catch (err) {
    agent.logger.error(err);
    throw new Error('Nacos配置信息获取失败!');
  }
};

/**
 * 从临时文件中读取agentWorker保存的远程配置文件,
 * 并修改当前项目中的config文件。
 * @param app
 */
export const initNacosConfig = (app: EggApplication) => {
  const tempConfigFile = path.join(app.baseDir, nacosRuntimeFile);
  try {
    const content = fs.readFileSync(tempConfigFile);
    const remoteConfig = JSON.parse(content.toString());
    depthSearch(app.config, remoteConfig);
  } catch (err) {
    app.logger.error('nacos配置文件读取失败!');
    throw err;
  }
};
// 示例配置 config.default.ts
config.typeorm = {
  clients: {
    accountDB: {
      type: 'postgres',
      host: '${database.host}', // 这里会用远程拿到的配置信息替换
      port: '${database.port}', // 这里会用远程拿到的配置信息替换
      username: '${database.username}', // 这里会用远程拿到的配置信息替换
      password: '${database.password}', // 这里会用远程拿到的配置信息替换
    },
  },
};