APOLLO-CLIENT
![NPM version](https://img.shields.io/npm/v/@gaoding/apollo-client.svg?style=flat-square)
******************************************************************
******************************************************************
********** 代码千万行,注释第一行 **********
********** 编码不规范,同事泪两行 **********
******************************************************************
******************************************************************
携程 Apollo 配置中心 node.js 客户端版本
Installation
npm i @gaoding/apollo-client [--save]
Usage
const Apollo = require('@gaoding/apollo-client').Apollo
const apollo = new Apollo({
config_server_url: 'http[s]://xxxxxxx',
app_id: 'xxx',
cluster_name: 'xxx',
namespace_name: 'xxx',
release_key: 'xxx',
ip: 'xxx'
set_env_file: false,
env_file_path: 'xxxx',
timeout: 50000,
});
apollo.init()
const env = apollo.get('${namespace}.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}');
const application = apollo.getNamespace('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');
const all = apollo.getAll();
apollo.startNotification(config?: IApolloRequestConfig);
启动自定义
apollo.init({...});
OpenApi
使用此 open api 需要先了解 Apollo 开放平台,详情请看链接:https://ctripcorp.github.io/apollo/#/zh/usage/apollo-open-api-platform
此插件是对 openApi 的 restful 接口调用进行整合打包,方便调用
初始化
具有两种初始化方案
- 跟随 Apollo 客户端初始化
const apollo = new Apollo({
...apolloConfig,
token: 'xxxxxxx',
portal_address: 'https://apollo.xxxx.com',
});
const openApi = apollo.openApi;
- 独立初始化
const { OpenApi } = require('@gaoding/apollo');
const openApi = new OpenApi({
token: 'xxxxxxx',
portal_address: 'https://apollo.xxxx.com',
app_id: 'xxx',
cluster_name: 'xxx',
namespace_name: 'xxx',
});
api
api 参数和详细文档请参照:https://ctripcorp.github.io/apollo/#/zh/usage/apollo-open-api-platform?id=%e4%b8%89%e3%80%81-%e6%8e%a5%e5%8f%a3%e6%96%87%e6%a1%a3
openApi Methods:
- getEnvclustersWithAppid(appId?: string)
- getApps()
- getClusterInfo(params)
- createCluster(params)
- getNamespaces(params)
- getNamespaceInfo(params)
- createNamespace(params)
- getNamespcaeLockInfo(params)
- getConfigByKey(params)
- addConfig(params)
- modifyConfig(params)
- deleteConfigByKey(params)
- releaseConfigs(params)
- getLatestRelease(params)
- rollbackRelease(params)
Tips
- ✅ 支持初始化的同步加载配置,解决远程加载配置是异步的问题
- ✅ 支持将配置写入到本地文件,需要开启 set_env_file
- ✅ 当读取远程配置出错时,兼容本地 env 文件读取, 需要开启 set_env_file
Todo