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

egg-consul-client

v1.3.2

Published

Consul-Client plugin for Egg

Downloads

12

Readme

egg-consul-client

NPM version build status Test coverage David deps Known Vulnerabilities npm download

egg 框架的 consul-client 插件

此插件基于 consul 实现简单的配置封装,并增加了一些功能 hook api。

安装

$ npm i egg-consul-client --save

开启插件

// config/plugin.js
exports.consul = {
  enable: true,
  package: 'egg-consul-client',
};

使用场景

Why and What

Egg其中特性是能在一个应用实例里管理多个Worker进程,从而提高CPU使用率,但是如果把整个Consul服务注册流程放在 Worker 上实现,会导致注册了多个services,此框架由 agent 实现服务注册,由 agent 代表整个应用实例,并可以沿用 consul 的API,例如:服务注册、健康检查、KV 等功能,agentworker 之间通过 IPC 进行通信。

更多信息请移步阅读:

用例

trigger(apiName, param)

代理调用 node-consul API Api。

参数

  • apiName(String): api名
  • param(Any, optional): api参数

用例:

await app.consul.trigger('agent.self');

结果:

{
  "Config": {
    "Bootstrap": true,
    "Server": true,
    "Datacenter": "dc1",
    "DataDir": "/tmp/node1/data",
    "DNSRecursor": "",
    "DNSConfig": {
      "NodeTTL": 0,
      "ServiceTTL": null,
      "AllowStale": false,
      "MaxStale": 5000000000
    },
    "Domain": "consul.",
    "LogLevel": "INFO",
    "NodeName": "node1",
    "ClientAddr": "127.0.0.1",
    "BindAddr": "127.0.0.1",
    "AdvertiseAddr": "127.0.0.1",
    "Ports": {
      "DNS": 8600,
      "HTTP": 8500,
      "RPC": 8400,
      "SerfLan": 8301,
      "SerfWan": 8302,
      "Server": 8300
    },
    "LeaveOnTerm": false,
    "SkipLeaveOnInt": false,
    "StatsiteAddr": "",
    "Protocol": 2,
    "EnableDebug": false,
    "VerifyIncoming": false,
    "VerifyOutgoing": false,
    "CAFile": "",
    "CertFile": "",
    "KeyFile": "",
    "ServerName": "",
    "StartJoin": [],
    "UiDir": "",
    "PidFile": "/tmp/node1/pid",
    "EnableSyslog": false,
    "SyslogFacility": "LOCAL0",
    "RejoinAfterLeave": false,
    "CheckUpdateInterval": 300000000000,
    "Revision": "441d613e1bd96254c78c46ee7c1b35c161fc7295+CHANGES",
    "Version": "0.3.0",
    "VersionPrerelease": ""
  },
  "Member": {
    "Name": "node1",
    "Addr": "127.0.0.1",
    "Port": 8301,
    "Tags": {
      "bootstrap": "1",
      "build": "0.3.0:441d613e",
      "dc": "dc1",
      "port": "8300",
      "role": "consul",
      "vsn": "2",
      "vsn_max": "2",
      "vsn_min": "1"
    },
    "Status": 1,
    "ProtocolMin": 1,
    "ProtocolMax": 2,
    "ProtocolCur": 2,
    "DelegateMin": 2,
    "DelegateMax": 4,
    "DelegateCur": 4
  }
}

Hook

基于 consul api,将一些常用api组合,进行打包封装。

hook.registerService

注册服务。

内部关联的Api:

  • agent.service.register

用例:

await app.consul.trigger('hook.registerService');

hook.deRegisterService

注销已注册服务。

内部关联的Api:

  • agent.service.deregister
  • agent.check.deregister

用例:

await app.consul.trigger('hook.deRegisterService');

服务发现

getServiceHost

通过服务名称获取服务host,如果是目标服务是集群多服务,内置负载均衡算法(轮询算法)

用例:

// 第一次调用
app.consul.getServiceHost('serviceName-1'); // return 192.168.1.123:7001

// 第二次调用
app.consul.getServiceHost('serviceName-1'); // return 192.168.1.124:7001

// 第三次调用
app.consul.getServiceHost('serviceName-1'); // return 192.168.1.123:7001

服务列表获取和维护,是通过定时任务获取,间隔频率更改 config.consul.syncInterval 配置。

详细配置

请到 config/config.default.js 查看详细配置项说明。

// config/config.[env].js
config.consul = {
  autoRegister: true,                                  // 是否自动注册服务
  timeout: 3000,                                       // consul api调用超时时间,单位:毫秒    
  syncInterval: '1m',                                  // 服务列表同步更新间隔,默认为1分钟
  server: {                                            // required, consul agent 服务配置
    host: '127.0.0.1',                                 // consul agent服务IP(String, default: 127.0.0.1)
    port: 8500,                                        // consul agent服务端口(Integer, default: 8500)
    secure: false,                                     // 启用 HTTPS(Boolean, default: false)
    promisify: true,                                   // 启动 Promise 风格,默认为 Callback(Boolean|Function, optional)
  },
  client: {                                            // required, consul service 配置
    name: serviceName,                                 // 注册的服务名称(String)
    id: '',                                            // 服务注册标识(String, optional)
    tags: ['serviceTag1', 'serviceTag2'],              // 服务标签(String[], optional)
    address: '10.4.146.241',                           // 注册的服务地址(String, optional)
    port: 7001,                                        // 注册的服务端口(Integer, optional)
    check: {                                           // 健康检查配置(Object, optional)
      http: 'http://10.4.146.241:7001/healthcheck',    // 健康检查URL
      interval: '5s',                                  // 健康检查频率
      timeout: '5s',                                   // 健康检查超时时间
      status: 'critical',                              // 初始化服务状态(String, optional)
    },
    checks: [],                                        // 有多个检查的路径,可采用对象数组形式,参数参照check的(Object[], optional)
  },
};

提问交流

请到 egg-consul-client issues 交流。

License

MIT