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

@zijin-m/egg-apollo

v1.0.8

Published

apollo plugin for egg base on ctrip-apollo

Downloads

13

Readme

egg-apollo

NPM version build status Test coverage David deps Known Vulnerabilities npm download

使用场景

用于接入 apollo 配置中心,实现启动前动态加载配置进行初始化,与运行时获取最新配置来实现配置热更新,基于ctrip-apollo扩展。

启动前动态加载配置

通过配置 mergeNamespace: application 可以在 egg 启动前(目前在configWillLoad生命周期钩子),将 apollo 的 application 命名空间的配置合入本地文件配置 app.config 中,实现启动前的配置文件加载。

运行时获取配置

可以通过像获取 egg 配置一样的调用方式直接获取 apollo 配置,如 app.config.application 来获得 application 命名空间下的配置信息,也可以通过 app.apollo 来获取 apollo 实例,自行调用 api 获取配置信息。

配置获取在默认配置的情况下都是热更新的。详见后续说明。

安装

npm i @zijin-m/egg-apollo

依赖说明

依赖的 egg 版本

| egg-apollo 版本 | egg 1.x | | --------------- | ------- | | 1.x | 😁 | | 0.x | ❌ |

开启插件

// config/plugin.js
exports.apollo = {
  enable: true,
  package: '@zijin-m/egg-apollo'
};

详细配置

  host: '', // 配置中心地址
  appId: '', // appId
  cluster: 'default', // 默认集群
  namespaces: [ 'application' ], // 默认命名空间
  cachePath: '/tmp/apollo_cache', // 默认缓存目录
  enableUpdateNotification: true, // 默认开启推送更新
  enableFetch: true, // 默认开启定时拉取
  fetchInterval: 5 * 60 * 1000, // 定时拉取间隔
  retry: 10, // 初始化重试次数
  mergeNamespace: '', // 将特定namespace合入本地配置 默认不做
  mountConfig: true, // 将namespaces挂载到本地配置 默认开启

请到 config/config.default.js 查看详细配置项说明。

使用参考

apollo的某个appId下的application命名空间配置如下时

| key | value | | ------ | ---------------- | | name | apollo | | config | {"key": "value"} | | a.b | 1 | | a.c.d | 2 |

通过app.conig直接获取配置信息。

console.log(app.config.application);
// {
//   name: "apollo",
//   config: {
//     key: "value"
//   },
//   a: {
//     b: 1,
//     c: {
//       d: 2
//     }
//   }
// }

通过这种方式获得配置数据会默认进行 key 的对象合并和 value 的 JSON 格式化,所以推荐使用这种方式,具体的合并方式可以参考properties对 namespaces 合并的规则,但是一般来说不推荐使用 key 合并,而是直接把 value 设置为 JSON 对象更好。

第二种是直接通过app.apollo获取,这种获取方式拿到的配置不会对数据进行任何处理,所以得到的是原始字符串。

app.apollo.namespace().config(); // 获取默认namespace下的全部配置
app.apollo.namespace().get(key); // 获取默认namespace下的某个配置

关于热更新

对于以上两种获取配置的方式,在启用enableUpdateNotification: true后可以获得近实时更新配置,或者启用enableFetch: true(fetchInterval)后,在fetchInterval周期后更新配置。

配置说明

常见配置可以参考ctrip-apollo

mountConfig配置 默认开启

开启后会将namespaces挂载到config下,比如配置namespaces: [ 'application','otherNameSpace' ]的时候你可以通过app.config.application,app.config.otherNameSpace获取 apollo 配置。

mergeNamespace配置

如果你希望将特定namespace的配置合并到config中,可以设置mergeNamespace,如mergeNamespace: 'application',常见的使用场景是在 egg 启动前更改配置,如将启动的配置放在 apollo 配置中心,否则使用mountConfig进行配置挂载就能满足日常需求。

提问交流

请到 egg issues 异步交流。

License

MIT