zhike-consul
a simple consul client
Demo
const Consul = require('zhike-consul');
const keys = ['orderPrivate', 'mq', 'userService'];
const host = '127.0.0.1';
const port = 8500;
const env = 'development';
const timeout = 3000;
const output = false;
let consul = new Consul(keys, host, port, global, { timeout, output });
consul.pull(env).then(function(data) {
let express = require('express');
let app = express();
let mq = amqplib.connect('amqp://' + config.mq.user + ':' + config.mq.pass + '@' + config.mq.host + ':' + config.mq.port);
app.listen(config.port);
})
API
1.consul(keys, host, port, global)
Initialize a new Consul client
Options
- keys(array), 想要获取的配置文件的key值, 如['db', 'redis']
- host(string), default: 127.0.0.1
- port(number), default: 8500
- global, global.CFG可以获取到相关的配置
- option.timeout, 超时时间,默认为3000,可以按需指定,最大不能超过30000,单位是毫秒
- option.output, 配置本地缓存文件绝对路径,设置成 false 可以禁用强制写到本地的特性。
Usage
var Consul = require('zhike-consul');
var consul = new Consul(keys, host, port, global, { timeout: 3000, output: false });
2.pull(env)
Get config values.
Options
- env(string), 指定拉取哪个环境的配置信息, 如development、test或production,默认development
Usage
consul.pull('development').then(function(data) {
console.log(data.CFG.redis.port);
console.log(data.config.redis.port);
console.log(config.redis.port);
console.log(CFG.redis.port);
})
3.register(data)
Register current service
Options
Usage
consul.register(data).then(function() {
console.log('success');
})
4.getNodeIp()
Get ip address of consul service is running
Usage
consul.getNodeIp().then(function(data) {
console.log(data);
})
Run Tests
npm test