配置中心 Apollo 的 Node.js 版本 SDK
使用说明
- npm install
npm install apollo-client-node --save
- require && init
const apollo = require('apollo-client-node');
apollo.init({
basePath: '[配置获取接口链接前缀部分]',
appId: '[项目AppId]',
clusterName: '[集群名称]',
namespaces: '[命名空间数组]',
cachePath: '[配置缓存文件的路径]',
});
- watch
const apollo = require('apollo-client-node');
apollo.setOptions({
basePath: '[配置获取接口链接前缀部分]',
appId: '[项目AppId]',
clusterName: '[集群名称]',
namespaces: '[命名空间数组]',
cachePath: '[配置缓存文件的路径]',
});
apollo.watch();
- use config
const apollo = require('apollo-client-node');
console.log(apollo.get('config-key'));
console.log(apollo.getAll());
集成到 eggjs 框架
- npm install
npm install apollo-client-node --save
- create init script
const apollo = require('apollo-client');
apollo.init({
basePath: process.env.APOLLO_BASE_PATH,
appId: process.env.APOLLO_APP_ID,
});
- run init-apollo.js before eggjs cluster (package.json)
{
...
"scripts": {
"start": "npm stop && APOLLO_BASE_PATH=https://apollo-config.example.com APOLLO_APP_ID=app-id-xxx node scripts/init-apollo.js && eggctl start --daemon --title=egg-server-example",
"start:release": "npm stop && APOLLO_BASE_PATH=https://apollo-config.example.com APOLLO_APP_ID=app-id-xxx node scripts/init-apollo.js && eggctl start --daemon --title=egg-server-example-release",
"stop": "egg-scripts stop --title=egg-server-example",
"stop:release": "egg-scripts stop --title=egg-server-example-release",
"dev": "APOLLO_BASE_PATH=https://apollo-config-dev.example.com APOLLO_APP_ID=app-id-xxx node scripts/init-apollo.js && egg-bin dev",
...
}
...
}
- load & use config
const apollo = require('apollo-client');
const cachePath = '';
apollo.loadLocalConfig(cachePath);
module.exports = {
logger: {
dir: apollo.get('LOGGER_DIR'),
},
};