A Javascript module for Node.js to connect Disconf service.
A Javascript module for Node.js to connect Disconf service.
You can install it using npm:
$ npm install node-disconf-client
var os = require('os');
var path = require('path');
var disconf = require('node-disconf-client');
// 可以配合config模块使用
var config = require('config');
var configDir = config.util.getEnv('NODE_CONFIG_DIR');
disconf.init({
// 配置disconf的本地配置文件路径
path: configDir,
filename: 'disconf.properties'
}, {
// 配置远程下载的配置保存哪个文件(所有配置聚合后的文件)
// 这里以 hostname 命名,使能被 config 模块读取
dist_file: path.join(configDir, os.hostname() + '.properties'),
// 配置远程下载的配置保存哪个目录(配置源文件)
user_define_download_dir: path.join(configDir, 'download')
});
// 错误事件
disconf.on('error', function (err) {
console.log('error:', err.stack);
});
// 准备事件,此时重新加载config模块,使配置生效
disconf.on('ready', function (data) {
console.log('ready:', data);
var conf = disconf.util.reloadConfig();
console.log('conf:', conf);
});
// 配置在远程被修改,此时重新加载config模块,使配置生效
disconf.on('change', function (event, data) {
console.log('change:', event.name, data);
var conf = disconf.util.reloadConfig();
console.log('conf:', conf);
});
This is the only external interface that executes instance initialization. The argument file
can set the directory and file name of the option file, that can configure the client itself. The argument option
can set option manually. Its priority is higher than the file. Part of option with the same name is same as the Java client. You can refer Java Disconf-Client
.
Arguments
file {Object} - The file of the option. Currently available options are:
path
{String} - The directory path of option file.filename
{String} - The name of option file, defaults to disconf.properties
.option {Object} - The client's own configuration file. Currently available options are:
dist_file
: {String} - 获取的配置写入的目标文件路径.conf_file_name
: {String} - 需要远程获取的配置文件,用逗号分隔,例如: demo.properties,system.propertiesconf_item_name
: {String} - 需要远程获取的配置项,用逗号分隔,例如: test,demo以下配置同 java 客户端一致:
enable: {remote: {conf: true}}
: {Boolean} - 是否使用远程配置文件,true(默认)会从远程获取配置 false则直接获取本地配置conf_server_host
: {String} - 配置服务器的 HOST,用逗号分隔 127.0.0.1:8000,127.0.0.1:8000app
: {String} - APP,请采用 产品线_服务名 格式version
: {String} - 版本,请采用 X_X_X_X 格式env
: {String} - 部署环境debug
: {Boolean} - 调试ignore
: {String} - 忽略哪些分布式配置,用逗号分隔conf_server_url_retry_times
: {Number} - 获取远程配置 重试次数,默认是3次conf_server_url_retry_sleep_seconds
: {Number} - 获取远程配置 重试时休眠时间,默认是5秒user_define_download_dir
: {String} - 用户定义的下载文件夹, 远程文件下载后会放在这里。注意,此文件夹必须有有权限,否则无法下载到这里callback(err, zk) {Function} - The callback function.
err
{Error} - Error during initialization.zk
{{}} - The node-zookeeper-client instance.Example
var disconf = require('node-disconf-client');
disconf.init({
path: './config',
filename: 'disconf.properties'
}, {
dist_file: './config/remote.properties',
user_define_download_dir: './config/download'),
conf_item_name: 'demo',
conf_server_host: '127.0.0.1:8000',
app: 'DEFAULT_APP',
version: 'DEFAULT_VERSION',
env: 'DEFAULT_ENV',
});
Optionally, you can register watcher functions after calling
init
methods.
ready
After the first time to downloaded all remote configuration, this event will be emit. It will return the data that contains all the configuration.
Callback
Example
disconf.on('ready', function (data) {
console.log('ready:', data);
});
change
At any time when remote configuration changes, this event will be emit. It will return the event and the data that contains all the configuration.
Callback
Example
disconf.on('change', function (event, data) {
console.log('change:', event.name, data);
});
error
At any time when a error appears, this event will be emit.
Callback
Example
disconf.on('error', function (err) {
console.log('error:', err.stack);
});
It provides utility tools include in util
.
This function can reload the config
module, make the changes of the configuration files to take effect. The function returns the config
instance. But you can still perform again require
to get the config
instance.
Before using this function you must have installed the config
module. Such as like the following:
$ npm install config
Example
var disconf = require('node-disconf-client');
var conf = disconf.util.reloadConfig();
.js
和json
后缀类型文件支持,值类型为列表的配置项同时支持json
数组和以,
逗号分隔的字符串Licensed under the MIT license.