提交 96b082ab 编写于 作者: Q Quinton.Xu

initial commit

上级
node_modules
package-lock.json
# node-apollo
携程Apollo配置中心node SDK.
## Install
```bash
$ npm i node-apollo --save-dev --registry=http://http://116.62.161.40:7002
```
## Usage
see test example
## License
[MIT](LICENSE)
\ No newline at end of file
'use strict';
module.exports = require('./lib/apollo');
'use strict';
const urllib = require('urllib');
const helper = require('./helper');
module.exports = {
remoteConfigService: async (token, config) => {
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json;charset=UTF-8',
authorization: token,
},
rejectUnauthorized: false,
contentType: 'json',
dataType: 'json',
};
const res = await urllib.request(helper.getAllConfigFromApolloUri(config), options);
console.log(res.data)
return helper.mergeConfig(res.data);
// ctx.app.config = 2
},
};
\ No newline at end of file
'use strict';
const assert = require('assert');
module.exports = {
// https://stackoverflow.com/questions/3710204/how-to-check-if-a-string-is-a-valid-json-string-in-javascript-without-using-try
// 先判断是否是JSON String 格式,不是则直接返回原始string
toJSON(str) {
if (/^[\],:{}\s]*$/.test(str.replace(/\\["\\\/bfnrtu]/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
return JSON.parse(str);
} else {
return str;
}
},
mergeConfig(payload) {
assert(Array.isArray(payload), 'Apollo config should be an array');
const publicPayload = [];
const privatePayload = [];
for (let meta of payload) {
if (meta.isPublic) {
publicPayload.push(...this._itemsPick(meta.items, ['key', 'value']));
} else {
privatePayload.push(...this._itemsPick(meta.items, ['key', 'value']));
}
}
// Apollo配置加载顺序如下,后加载的会覆盖前面的同名配置
// -> 公开配置
// -> 私有配置
return Object.assign({}, ...privatePayload, ...publicPayload);
},
_itemsPick(items, keys) {
const ret = [];
for (let item of items) {
let obj = {};
obj[item.key] = this.toJSON(item.value);
ret.push(obj);
}
return ret;
},
// clientIp这个参数是可选的,用来实现灰度发布。 如果不想传这个参数,请注意URL中从?号开始的query parameters整个都不要出现。
getConfigFromApolloUri(config) {
// 读取环境变量
const { configServerUrl, appId, clusterName, namespaceName, clientIp } = config;
assert(configServerUrl, 'configServerUrl is required');
assert(appId, 'appId is required');
assert(clusterName, 'clusterName is required');
assert(namespaceName, 'namespaceName is required');
let apolloString;
if (clientIp) {
apolloString = `${configServerUrl}/configfiles/json/${appId}/${clusterName}/${namespaceName}?ip=${clientIp}`;
} else {
apolloString = `${configServerUrl}/configfiles/json/${appId}/${clusterName}/${namespaceName}`;
}
return apolloString;
},
// 获取集群下所有Namespace信息接口
getAllConfigFromApolloUri(config) {
const { configServerUrl, appId, clusterName, apolloEnv } = config;
assert(configServerUrl, 'configServerUrl is required');
assert(appId, 'appId is required');
assert(clusterName, 'clusterName is required');
let apolloString = `${configServerUrl}/openapi/v1/envs/${apolloEnv}/apps/${appId}/clusters/${clusterName}/namespaces`;
return apolloString;
},
};
\ No newline at end of file
{
"name": "node-apollo",
"version": "1.0.0",
"description": "携程Apollo配置中心SDK。",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git@code.aliyun.com:ecarx-web/node-apollo.git"
},
"keywords": [
"nodejs",
"apollo",
"config"
],
"author": "Quinton.xu@gmail.com",
"license": "ISC",
"devDependencies": {
"mocha": "^3.5.3"
},
"dependencies": {
"urllib": "^2.25.0"
}
}
'use strict';
const assert = require('assert');
const apollo = require('../index.js');
describe('#index', function () {
it('index.remoteConfigService', async () => {
const config = {
configServerUrl: 'http://116.62.161.40:8070',
appId: 'gnetlink-cms-api',
clusterName: 'default',
namespaceName: '',
apolloEnv: 'dev',
// clientIp: '',
};
const token = '9037a4e8c14c3d61ac5dbbdfa379fc56f0e098ff';
const result = await apollo.remoteConfigService(token, config);
assert(Object.keys(result).length > 0, 'Read config failed');
});
});
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册