Git Product home page Git Product logo

egg-apollo-client's Introduction

EGG-APOLLO-CLIENT

NPM version

******************************************************************
******************************************************************
**********              代码千万行,注释第一行              **********
**********              编码不规范,同事泪两行              **********
******************************************************************
******************************************************************

携程 Apollo 配置中心 egg 客户端版本

FIRST

使用时,如果遇到编译失败,请确保你的系统安装有 curl 命令行工具

Installation

npm i @gaoding/egg-apollo-client [--save]

Usage

add plugin

// config/plugin.js or config/plugin.ts
exports.apollo = {
    enable: true,
    package: '@gaoding/egg-apollo-client'
}

add apollo plugin config

// config/config.[env].js
config.apollo = {
    config_server_url: 'http[s]://xxxxxxx', // required, 配置中心服务地址
    app_id: 'xxx',                          // required, 需要加载的配置
    init_on_start: true,                    // optional, 在 app 启动时同时加载配置,加载的配置会在插件加载前被加载
    cluster_name: 'xxx',                    // optional, 加载配置的集群名称, default: 'default'
    namespace_name: 'xxx',                  // optional, 加载配置的命名空间, default: 'application'
    release_key: 'xxx',                     // optional, 加载配置的版本 key, default: ''
    ip: 'xxx'                               // optional,

    set_env_file: false,                    // optional, 是否写入到 env 文件, default: false
    env_file_path: 'xxxx',                  // optional, 写入的 env 文件路径, default: ${os.tmpdir()}/.env.apollo
    watch: false,                           // optional, 长轮询查看配置是否更新, default: false
    timeout: 50000,                         // optional, 长轮询 timeout 设置,默认 50000
}

在 config 目录下添加新文件 config.apollo.js

// config.apollo.js
module.exports = (apollo, appConfig) => {
    // 直接提取配置
    const env = apollo.get('${namespace}.NODE_ENV');
    // 不添加 namespace 前缀的时候,默认使用 application 的配置
    // 如果 application 不包含该配置,则提取 process.env 里面的配置
    // const env = apollo.get('NODE_ENV');
    // 提取类型配置
    apollo.getString('${namespace}.${string_config}');
    apollo.getNumber('${namespace}.${number_config}');
    apollo.getBoolean('${namespace}.${boolean_config}');
    apollo.getJSON('${namespace}.${json_config}');
    apollo.getDate('${namespace}.${date_config}');


    // 提取指定 namespace 内容
    const application = apollo.getNamespace('application');
    // 提取配置
    const config1 = application.get('config1');
    // 提取 string 类型配置
    const str = application.getString('config2');
    // 提取 number 类型配置
    const num = application.getNumber('config3');
    // 提取 boolean 类型配置
    const bool = application.getBoolean('config4');
    // 提取 json
    const json = application.getJSON('config5');
    // 提取 date
    const date = application.getDate('config6');

    // 提取所有配置内容
    const all = apollo.getAll();
    // 可以从 all 中提取需要的 namespace 配置
    // const application = all.application
    // const config1 = application.get('config1');
    // const str = application.getString('config2');
    // const num = application.getNumber('config3');
    // const bool = application.getBoolean('config4');
    // const json = application.getJSON('config5');
    // const date = application.getDate('config6');

    return {
        logger: {
            ...appConfig.logger,
            level: apollo.get('LOGGER_LEVEL')
        }
        ....
    }
}

启动自定义

egg-apollo-client 没有特殊配置只加载符合配置项(config.apollo)的配置信息,如果有需要其他的额外配置,可以另外通过启动自定义来配置

// app.js
class AppBootHook {

  constructor(app) {
    this.app = app;
  }

  configWillLoad() {
      // configWillLoad 是最后一次修改插件配置的时机,此方法内只能使用同步方法
      // 插件在实例化之后不能再修改配置,所以如果有需要加载插件配置的内容,需要在这里加载
      // apollo.init 结合了 http.request 的同步方法,该方法会阻塞知道拿到数据或请求超时,可以使用该方法在这里加载配置
      this.app.apollo.init({...});
  }
}

Tips

  • ✅ 支持初始化的同步加载配置,解决远程加载配置是异步的问题
  • ✅ config.apollo.js 是 apollo 的配置文件,会在所有配置加载之后覆盖原有配置
  • ✅ 支持将配置写入到本地文件,需要开启 set_env_file
  • ✅ 当读取远程配置出错时,兼容本地 env 文件读取, 需要开启 set_env_file

Todo

  • ✅ 支持配置订阅模式,暂时没想到已有项目的实用性,因为插件的加载是不可修改的,更新配置要让插件生效就要重启进程
  • 🔥 支持多集群加载

egg-apollo-client's People

Contributors

dependabot[bot] avatar xuezier avatar yuu2lee4 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

egg-apollo-client's Issues

set_env_file not work

apollo: {
      config_server_url: 'server_url',
      app_id: 'appId',
      init_on_start: true,
      cluster_name: 'default',
      namespace_name: 'application',
      release_key: '',
      set_env_file: true,
      watch: true,
      timeout: 5000,
},

this is my config, built .env.apollo file under the app.baseDir yet.
set_env_file not work, what is the problem?

Cannot read property 'content-type' of undefined

hi,请教下
我在egg 项目的中引用的版本入下
"@gaoding/egg-apollo-client": "^2.0.3",
typescript版本为"^3.0.0"
运行 yarn run dev 会报TypeError: Cannot read property 'content-type' of undefined错误,

但是如果"@gaoding/egg-apollo-client":"^1.4.0"则是可以运行
我们最新的egg-apollo-client版本在ts环境下该如何解决上面的问题吖

how to receive watch result

i config watch:true, timeout:5.
but i dont know whether they worked.
how can i receive the long polling result?

watch not work

startNotification->remoteConfigServiceSkipCache

 if (response.isJSON() || response.statusCode === 304) {
            if (response.data) {
                this.setEnv(response.data);
                // 这里没有触发 this.emit('config.updated', response.data);
            }
            return response.data;
        }

覆盖配置

可以设置时间间隔不断更新配置文件(config.default.ts)里的对象字段么,如何配置

config.apollo = {
config_server_url: 'http://106.54.227.205:8080', // required, 配置中心服务地址
app_id: 'appid', // required, 需要加载的配置
init_on_start: true, // optional, 在 app 启动时同时加载配置,加载的配置会在插件加载前被加载
cluster_name: 'default', // optional, 加载配置的集群名称, default: 'default'
namespace_name: 'application', // optional, 加载配置的命名空间, default: 'application'
release_key: '', // optional, 加载配置的版本 key, default: ''
ip: '', // optional,
set_env_file: true, // optional, 是否写入到 env 文件, default: false
env_file_path: '', // optional, 写入的 env 文件路径, default: ${app.baseDir}/.env.apollo
watch: true, // optional, 长轮询查看配置是否更新, default: false
timeout: 61000, // optional, 长轮询 timeout 设置,默认 50000
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.