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

eggjs-nacos

v1.1.9

Published

Egg.js nacos插件,服务注册发现,获取配置中心

Downloads

15

Readme

安装插件

$ npm i eggjs-nacos --save

开启插件

  1. 服务注册

开启插件后,启动时服务会注册到 Nacos, 请确保 config 中 nacos.serverList 和 nacos.client 已填写。多进程下单一注册服务

  1. 服务发现

配置 nacos.subscribers 后, 启动服务时会自动监听配置的服务。多进程下单一发现服务

// {app_root}/config/plugin.js
exports.nacos = {
  enable: true,
  package: "eggjs-nacos",
};

配置

// {app_root}/config/config.default.js
exports.nacos = {
  serverList: "", // 必填,  nacos 服务url
  isRegisterInstance: true, // 非必填,  是否注册实例, 默认 true
  client: {
    namespace: "", // 必填,  命名空间ID,用于后续服务注册、服务发现
    serviceName: "", // 非必填, 服务名称, 默认自动获取package.json中的name
    groupName: "", // 非必填, 分组名称 默认 DEFAULT_GROUP
    username: 'user', // 非必填
    password: 'pass', // 非必填
  },
  subscribers: {
    test01Service: {
      serviceName: "egg-test", // 服务名称
      groupName: "", // 默认 DEFAULT_GROUP
      clusters: "", // 默认 DEFAULT
      subscribe: true, // 是否订阅服务  默认 true
    },
    test02Service: {
      serviceName: "egg-test02", // 服务名称
      subscribe: false,
    },
  },
};

使用

test01Service 服务已订阅的话将根据权重轮训向实例发起请求

const result = await this.ctx.nacos.test01Service.request("/tab/list"); // 默认 GET 请求
if (result.status !== 200) throw Error("Error ...");
console.log(result.data);

test02Service 服务未订阅的话每次调用会向 Nacos 服务器获取实例,然后在这些实例中随机挑选一个发起请求

const options = {
  method: "POST",
  dataType: "json",
  timeout: 30000,
  data: { a: "11", b: "22" },
};
const result = await this.ctx.nacos.test02Service.request(
  "/tab/create",
  options
);
if (result.status !== 200) throw Error("Error ...");
console.log(result.data);

获取配置中心

具体用法和 node-apollo 使用一样

{app_root}/nacos.js

const { fetchRemoteNacosConfig } = require("eggjs-nacos");
fetchRemoteNacosConfig(
  {
    serverAddr: "127.0.0.1:8848",
    namespace: "dev", // 命名空间ID
  },
  [
    {
      namespace: "public", // 命名空间ID, 优先使用这里配置的命名空间ID
      configs: [
        {
          dataId: "com.dq.redis",
          group: "DEFAULT_GROUP",
        },
      ],
    },
    {
      // 未配置命名空间的话默认使用上面的 dev
      configs: [
        {
          dataId: "com.dq.test",
          group: "DEFAULT_GROUP",
        },
      ],
    },
  ]
).then((data) => {
  console.log("env:", data);
});

{app_root}/package.json

...
"scripts": {
  "dev": "node nacos.js && egg-bin dev",
  "start": "node nacos.js && egg-scripts start"
}
...

读取配置设置到环境变量的两种方式

// {app_root}/config/plugin.js
...
exports.nacos = {
  enable: true,
  package: "eggjs-nacos",
};
...

// 或者 {app_root}/config/config.default.ts
...
require('eggjs-nacos').setEnv();
...

从环境变量中读取配置使用 {app_root}/config/config.default.ts

...
config.redis = {
  client: {
    host: process.env["redis.host"], // 从环境变量中读取配置
    port: process.env["redis.port"],
    password: process.env["redis.password"],
    db: process.env["redis.db"],
  },
};
...

APIs

service

  • request(url, options) 向实例发起请求.
  • pick() 挑选一个实例.
  • selectInstances() 获取所有实例.

config

  • fetchRemoteNacosConfig(clientOptions, configOptions) 获取远程 Nacos 配置信息
    • clientOptions {ClientOptions}
    • configOptions {Array}
      • namespace {String} 命名空间 ID, 优先使用
      • configs {Array}
        • dataId {String}
        • group {String} 分组
  • createEnvFile(config) 将配置信息写入到文件
    • config {Object}
  • setEnv() 注入到环境变量

License

BSD-2-Clause