Commit ab204945 authored by Quinton.Xu's avatar Quinton.Xu

add remoteConfigServiceSikpCache & remoteConfigServiceSikpCache

parent 7fe8f241
...@@ -7,6 +7,7 @@ const helper = require('./helper'); ...@@ -7,6 +7,7 @@ const helper = require('./helper');
const config = require('../config.js'); const config = require('../config.js');
module.exports = { module.exports = {
// Apollo开放平台接入方式
remoteConfigService: async (config) => { remoteConfigService: async (config) => {
assert(config, 'param config is required'); assert(config, 'param config is required');
assert(config.token, 'param token is required'); assert(config.token, 'param token is required');
...@@ -24,6 +25,47 @@ module.exports = { ...@@ -24,6 +25,47 @@ module.exports = {
assert(res.status === 200, 'apollo host unavailable, please contact your administrtor'); assert(res.status === 200, 'apollo host unavailable, please contact your administrtor');
return helper.mergeConfig(res.data); return helper.mergeConfig(res.data);
}, },
// 通过不带缓存的Http接口从Apollo读取配置
remoteConfigServiceSikpCache: async (config) => {
assert(config, 'param config is required');
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json;charset=UTF-8',
},
rejectUnauthorized: false,
contentType: 'json',
dataType: 'json',
};
const URIs = helper.getConfigSkipCacheUri(config);
const bundle = await Promise.all(URIs.map(uri => urllib.request(uri, options)));
for (let res of bundle) {
assert(res.status === 200, 'apollo host unavailable, please contact your administrtor');
}
return helper.mergeConfigurations(bundle);
},
//通过带缓存的Http接口从Apollo读取配置
remoteConfigServiceFromCache: async (config) => {
assert(config, 'param config is required');
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json;charset=UTF-8',
},
rejectUnauthorized: false,
contentType: 'json',
dataType: 'json',
};
const URIs = helper.getConfigFromCacheUri(config);
const bundle = await Promise.all(URIs.map(uri => urllib.request(uri, options)));
for (let res of bundle) {
assert(res.status === 200, 'apollo host unavailable, please contact your administrtor');
}
return helper.mergeConfigurations(bundle);
},
// 生成default.env // 生成default.env
createEnvFile: (envConfig) => { createEnvFile: (envConfig) => {
if (fs.existsSync(config.ENV_FILE_PATH)) { if (fs.existsSync(config.ENV_FILE_PATH)) {
...@@ -33,6 +75,7 @@ module.exports = { ...@@ -33,6 +75,7 @@ module.exports = {
fs.appendFileSync(config.ENV_FILE_PATH, `${key}=${envConfig[key]}\n`); fs.appendFileSync(config.ENV_FILE_PATH, `${key}=${envConfig[key]}\n`);
} }
}, },
// 注入到process.env // 注入到process.env
setEnv: () => { setEnv: () => {
try { try {
......
...@@ -32,6 +32,17 @@ module.exports = { ...@@ -32,6 +32,17 @@ module.exports = {
return Object.assign({}, ...privatePayload, ...publicPayload); return Object.assign({}, ...privatePayload, ...publicPayload);
}, },
// 合并配置
mergeConfigurations(payload) {
assert(Array.isArray(payload), 'Apollo config should be an array');
try {
const confs = payload.map(pl => pl.data.configurations);
return Object.assign({}, ...confs);
} catch(err) {
assert(err, 'Apollo configs not be merged');
}
},
_itemsPick(items, keys) { _itemsPick(items, keys) {
const ret = []; const ret = [];
for (let item of items) { for (let item of items) {
...@@ -41,6 +52,7 @@ module.exports = { ...@@ -41,6 +52,7 @@ module.exports = {
} }
return ret; return ret;
}, },
// clientIp这个参数是可选的,用来实现灰度发布。 如果不想传这个参数,请注意URL中从?号开始的query parameters整个都不要出现。 // clientIp这个参数是可选的,用来实现灰度发布。 如果不想传这个参数,请注意URL中从?号开始的query parameters整个都不要出现。
getConfigFromApolloUri(config) { getConfigFromApolloUri(config) {
// 读取环境变量 // 读取环境变量
...@@ -58,6 +70,7 @@ module.exports = { ...@@ -58,6 +70,7 @@ module.exports = {
return apolloString; return apolloString;
}, },
// 获取集群下所有Namespace信息接口 // 获取集群下所有Namespace信息接口
getAllConfigFromApolloUri(config) { getAllConfigFromApolloUri(config) {
const { configServerUrl, appId, clusterName, apolloEnv } = config; const { configServerUrl, appId, clusterName, apolloEnv } = config;
...@@ -68,4 +81,32 @@ module.exports = { ...@@ -68,4 +81,32 @@ module.exports = {
return apolloString; return apolloString;
}, },
// 通过带缓存的Http接口从Apollo读取配置
getConfigFromCacheUri(config) {
const { configServerUrl, appId, clusterName, namespaceName, clientIp } = config;
assert(configServerUrl, 'configServerUrl is required');
assert(namespaceName, 'namespaceName is required');
assert(appId, 'appId is required');
assert(clusterName, 'clusterName is required');
if (Array.isArray(namespaceName)) {
return namespaceName.map(n => `${configServerUrl}/configfiles/json/${appId}/${clusterName}/${namespaceName}?ip=${clientIp}`);
} else {
return Arrary.from(`${configServerUrl}/configfiles/json/${appId}/${clusterName}/${namespaceName}?ip=${clientIp}`);
}
},
// 通过不带缓存的Http接口从Apollo读取配置
getConfigSkipCacheUri(config) {
const { configServerUrl, appId, clusterName, namespaceName, releaseKey, clientIp } = config;
assert(configServerUrl, 'configServerUrl is required');
assert(namespaceName, 'namespaceName is required');
assert(appId, 'appId is required');
assert(clusterName, 'clusterName is required');
if (Array.isArray(namespaceName)) {
return namespaceName.map(n => `${configServerUrl}/configs/${appId}/${clusterName}/${n}?releaseKey=${releaseKey}&ip=${clientIp}`);
} else {
return Arrary.from(`${configServerUrl}/configs/${appId}/${clusterName}/${namespaceName}?releaseKey=${releaseKey}&ip=${clientIp}`);
}
}
}; };
\ No newline at end of file
...@@ -19,6 +19,32 @@ describe('#index', function () { ...@@ -19,6 +19,32 @@ describe('#index', function () {
const result = await apollo.remoteConfigService(eggConfig); const result = await apollo.remoteConfigService(eggConfig);
assert(Object.keys(result).length > 0, 'Read config failed'); assert(Object.keys(result).length > 0, 'Read config failed');
}); });
// 通过带缓存的Http接口从Apollo读取配置
it('index.remoteConfigServiceSikpCache', async () => {
const eggConfig = {
configServerUrl: 'http://example.com',
appId: '<appId>',
clusterName: 'default',
namespaceName: [ 'namespaceName1', 'namespaceName1' ],
// clientIp: '',
};
const result = await apollo.remoteConfigServiceSikpCache(eggConfig);
console.log(result)
assert(Object.keys(result).length > 0, 'Read config failed');
});
// 通过不带缓存的Http接口从Apollo读取配置
it('index.remoteConfigServiceSikpCache', async () => {
const eggConfig = {
configServerUrl: 'http://example.com',
appId: '<appId>',
clusterName: 'default',
namespaceName: [ 'namespaceName1', 'namespaceName1' ],
// clientIp: '',
};
const result = await apollo.remoteConfigServiceSikpCache(eggConfig);
assert(Object.keys(result).length > 0, 'Read config failed');
});
it('index.createEnvFile', async () => { it('index.createEnvFile', async () => {
const eggConfig = { const eggConfig = {
configServerUrl: 'http://example.com', configServerUrl: 'http://example.com',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment