npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zhike-consul

v1.0.12

Published

a simple consul client

Downloads

39

Readme

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;

// 1.初始化consul
let consul = new Consul(keys, host, port, global, { timeout, output });

// 2.加载相关配置
consul.pull(env).then(function(data) {
  /**
    data = {
      CFG: {
        orderPrivate: {port: 6002, timeout: 10},
        mq: {user: 'test', pass: 'test'},
        userService: {url: 'http://api.dev.smartstudy.com/user'}
      },
      config: {
        port: 6002,
        timeout: 10,
        mq: {user: 'test', pass: 'test'},
        userService: {url: 'http://api.dev.smartstudy.com/user'}
      }
    }
  */
  /** 每个key的配置信息会默认挂载在global的CFG对象下
    global.CFG = data.CFG
  */

  /** global的config对象会以私有配置为准,把其余的key的配置信息平铺在一起
    global.config = data.config;
  */

  // 3.启动HTTP Server
  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); // 6379
  console.log(data.config.redis.port); // 6379
  // 或直接用global对象CFG和config
  console.log(config.redis.port); // 6379
  console.log(CFG.redis.port);  // 6379
})

3.register(data)

Register current service

Options

  • data, 服务注册的数据

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);  // 172.16.3.2
})

Run Tests

npm test