Node.js Client for Apollo
$ npm install apollo-node-client --save
ConfigService
const { ConfigService } = require('apollo-node-client');
const service = new ConfigService({
configServerUrl: 'http://localhost:8080/',
appId: 'SampleApp',
clusterName: 'default',
secret: 'cf86d564d10a46d4a5989dfdeed3a3a2'
});
namespace
的配置(application
)const config = await service.getAppConfig();
config.getAllConfig(); // Map(1) { 'mysql.user' => 'root' }
console.log(config.getProperty('mysql.user')); // root
console.log(config.getProperty('mysql.missing', 'default')); // default
properties
格式 namespace
的配置const config = await service.getConfig('application');
config.getAllConfig(); // Map(1) { 'mysql.user' => 'root' }
console.log(config.getProperty('mysql.user')); // root
console.log(config.getProperty('mysql.missing', 'default')); // default
json
格式 namespace
的配置const config = await service.getConfig('config.json');
config.getAllConfig(); // { mysql: { user: 'root' } }
console.log(config.getProperty('mysql.user')); // root
console.log(config.getProperty('mysql.missing', 'default')); // default
xml/yml/yaml/txt
格式 namespace
的配置const config = await service.getConfig('config.txt');
config.getAllConfig(); // txt config
console.log(config.getProperty('', 'default')); // txt config
console.log(config.getProperty()); // txt config
ip
const config = await service.getConfig('application', '192.168.3.4');
config.getAllConfig(); // Map(1) { 'mysql.user' => 'root' }
console.log(config.getProperty('mysql.user')); // root
console.log(config.getProperty('mysql.missing', 'default')); // default
config.addChangeListener((changeEvent) => {
for (const key of changeEvent.changedKeys()) {
const change = changeEvent.getChange(key);
if (change) {
console.log(`namespace: ${change.getNamespace()},
changeType: ${change.getChangeType()},
propertyName: ${change.getPropertyName()},
oldValue: ${change.getOldValue()},
newValue: ${change.getNewValue()}`);
}
}
});
new ConfigService( options )
options
\
configServerUrl
\
appId
\
[clusterName]
\
[secret]
\
Returns: ConfigService
configService.getAppConfig( [ ip ] )
[ip]
\
Returns: Promise\namespace
为 application
configService.getConfig( namespaceName, [ ip ] )
namespaceName
\Config
。如果没有后缀名,默认为 properties
,目前支持 .json
,.properties
,.xml
,.yml
, .yaml
,.txt
[ip]
\
Returns: Promise\<PropertiesConfig | JSONConfig | PlainConfig>
propertiesConfig.getAllConfig()
propertiesConfig.getProperty( key, [ defaultValue ] )
key
\key
[defaultValue]
\key
不存在时,会返回 defaultValue
Returns: undefined | string
propertiesConfig.addChangeListener( handle )
handle
( changeEvent: ConfigChangeEvent\
Returns: void
jsonConfig.getAllConfig()
jsonConfig.getProperty( key, [ defaultValue ] )
key
\key
[defaultValue]
\key
不存在时,会返回 defaultValue
Returns: undefined | JSONValueType
jsonConfig.addChangeListener( handle )
handle
( changeEvent: ConfigChangeEvent\
Returns: void
plainConfig.getAllConfig()
plainConfig.getProperty( key, [ defaultValue ] )
key
\key
都会返回整个配置文本内容
[defaultValue]
\defaultValue
Returns: undefined | string
configChangeEvent.getNamespace()
configChangeEvent.changedKeys()
configChangeEvent.getChange()
configChange.getNamespace()
configChange.getPropertyName()
configChange.getOldValues()
configChange.getNewValue()
configChange.getChangeType()
propertyChangeType.ADDED
propertyChangeType.MODIFIED
propertyChangeType.DELETED
Contributions are always welcome!