Node.js Client for Apollo
$ npm install apollo-node-client --save
const { ConfigService } = require('apollo-node-client');
const service = new ConfigService({
configServerUrl: 'http://localhost:8080/',
appId: 'SampleApp',
clusterName: 'default',
secret: 'cf86d564d10a46d4a5989dfdeed3a3a2'
});
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
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
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
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
<Object>configServerUrl
<string> Apollo 配置服务的地址appId
<string> 应用的 appId[clusterName]
<string> 集群名[secret]
<string> 服务端密钥 access key
-
Returns: ConfigService
-
- configService.getAppConfig( [ ip ] )
-
[ip]
<string> 应用部署的机器ip -
Returns: Promise<PropertiesConfig> 默认的
namespace
为application
-
- configService.getConfig( namespaceName, [ ip ] )
-
namespaceName
<string> Namespace的名字,以后缀名判断是什么类型格式的Config
。如果没有后缀名,默认为properties
,目前只支持.json
,.properties
-
[ip]
<string> 应用部署的机器ip -
Returns: Promise<PropertiesConfig | JSONConfig>
-
-
propertiesConfig.getAllConfig()
- Returns: Map<string, string>
- propertiesConfig.getProperty( key, [ defaultValue ] )
-
key
<string> 要获取的配置的key
-
[defaultValue]
<string> 默认值,当传入的key
不存在时,会返回defaultValue
-
Returns: undefined | string
-
- propertiesConfig.addChangeListener( handle )
-
handle
( changeEvent: ConfigChangeEvent<string> ) => void 监听配置变化事件的回调函数 -
Returns: void
-
-
jsonConfig.getAllConfig()
- Returns: JSONValueType
- jsonConfig.getProperty( key, [ defaultValue ] )
-
key
<string> 要获取的配置的key
-
[defaultValue]
<string> 默认值,当传入的key
不存在时,会返回defaultValue
-
Returns: undefined | JSONValueType
-
- jsonConfig.addChangeListener( handle )
-
handle
( changeEvent: ConfigChangeEvent<JSONValueType> ) => void 监听配置变化事件的回调函数 -
Returns: void
-
-
configChangeEvent.getNamespace()
- Returns: string
-
configChangeEvent.changedKeys()
- Returns: string[]
-
configChangeEvent.getChange()
- Returns: undefined | ConfigChange<T>
-
configChange.getNamespace()
- Returns: string
-
configChange.getPropertyName()
- Returns: string
-
configChange.getOldValues()
- Returns: undefined | T
-
configChange.getNewValue()
- Returns: undefined | T
-
configChange.getChangeType()
- Returns: PropertyChangeType
- propertyChangeType.ADDED
- propertyChangeType.MODIFIED
- propertyChangeType.DELETED