{"id":21061507,"url":"https://github.com/khronosleung/egg-consul-client","last_synced_at":"2026-04-27T09:31:59.695Z","repository":{"id":43014566,"uuid":"383533166","full_name":"khronosleung/egg-consul-client","owner":"khronosleung","description":"egg 框架的 consul-client 插件","archived":false,"fork":false,"pushed_at":"2022-03-23T06:50:12.000Z","size":1195,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-30T16:23:42.366Z","etag":null,"topics":["consul","egg","egg-consul","egg-plugin","node-consul","nodejs-consul"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/khronosleung.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-06T16:28:20.000Z","updated_at":"2022-03-23T06:50:15.000Z","dependencies_parsed_at":"2022-09-16T06:50:55.810Z","dependency_job_id":null,"html_url":"https://github.com/khronosleung/egg-consul-client","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/khronosleung/egg-consul-client","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khronosleung%2Fegg-consul-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khronosleung%2Fegg-consul-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khronosleung%2Fegg-consul-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khronosleung%2Fegg-consul-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/khronosleung","download_url":"https://codeload.github.com/khronosleung/egg-consul-client/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khronosleung%2Fegg-consul-client/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32331305,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T23:26:28.701Z","status":"online","status_checked_at":"2026-04-27T02:00:06.769Z","response_time":128,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["consul","egg","egg-consul","egg-plugin","node-consul","nodejs-consul"],"created_at":"2024-11-19T17:25:15.990Z","updated_at":"2026-04-27T09:31:59.664Z","avatar_url":"https://github.com/khronosleung.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# egg-consul-client\n\n[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][codecov-image]][codecov-url]\n[![David deps][david-image]][david-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/egg-consul-client.svg?style=flat-square\n[travis-image]: https://img.shields.io/travis/khronosleung/egg-consul-client.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/egg-consul-client\n[travis-url]: https://travis-ci.com/khronosleung/egg-consul-client\n[codecov-image]: https://img.shields.io/codecov/c/github/khronosleung/egg-consul-client.svg?style=flat-square\n[codecov-url]: https://codecov.io/github/khronosleung/egg-consul-client?branch=master\n[david-image]: https://img.shields.io/david/khronosleung/egg-consul-client.svg?style=flat-square\n[david-url]: https://david-dm.org/khronosleung/egg-consul-client\n[snyk-image]: https://snyk.io/test/npm/egg-consul-client/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/egg-consul-client\n[download-image]: https://img.shields.io/npm/dm/egg-consul-client.svg?style=flat-square\n[download-url]: https://npmjs.org/package/egg-consul-client\n\negg 框架的 consul-client 插件\n\n此插件基于 [consul](https://github.com/silas/node-consul) 实现简单的配置封装，并增加了一些功能 hook api。\n\n\n## 安装\n\n```bash\n$ npm i egg-consul-client --save\n```\n\n## 开启插件\n\n```js\n// config/plugin.js\nexports.consul = {\n  enable: true,\n  package: 'egg-consul-client',\n};\n```\n\n## 使用场景\n\n### Why and What\n\nEgg其中特性是能在一个应用实例里管理多个Worker进程，从而提高CPU使用率，但是如果把整个Consul服务注册流程放在 Worker 上实现，会导致注册了多个services，此框架由 `agent` 实现服务注册，由 `agent` 代表整个应用实例，并可以沿用 `consul` 的API，例如：服务注册、健康检查、KV 等功能，`agent` 和 `worker` 之间通过 `IPC` 进行通信。\n\n更多信息请移步阅读：\n- [多进程研发模式增强](https://eggjs.org/zh-cn/advanced/cluster-client.html)\n- [多进程模型和进程间通讯](https://eggjs.org/zh-cn/core/cluster-and-ipc.html)\n- [Egg加载器及生命周期](https://eggjs.org/zh-cn/advanced/loader.html#%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F)\n\n### 用例\n\n### trigger(apiName, param)\n\n代理调用 [node-consul API](https://github.com/silas/node-consul#documentation) Api。\n\n参数\n - apiName(String): api名\n - param(Any, optional): api参数\n\n\n用例：\n```javascript\nawait app.consul.trigger('agent.self');\n```\n\n结果：\n```json\n{\n  \"Config\": {\n    \"Bootstrap\": true,\n    \"Server\": true,\n    \"Datacenter\": \"dc1\",\n    \"DataDir\": \"/tmp/node1/data\",\n    \"DNSRecursor\": \"\",\n    \"DNSConfig\": {\n      \"NodeTTL\": 0,\n      \"ServiceTTL\": null,\n      \"AllowStale\": false,\n      \"MaxStale\": 5000000000\n    },\n    \"Domain\": \"consul.\",\n    \"LogLevel\": \"INFO\",\n    \"NodeName\": \"node1\",\n    \"ClientAddr\": \"127.0.0.1\",\n    \"BindAddr\": \"127.0.0.1\",\n    \"AdvertiseAddr\": \"127.0.0.1\",\n    \"Ports\": {\n      \"DNS\": 8600,\n      \"HTTP\": 8500,\n      \"RPC\": 8400,\n      \"SerfLan\": 8301,\n      \"SerfWan\": 8302,\n      \"Server\": 8300\n    },\n    \"LeaveOnTerm\": false,\n    \"SkipLeaveOnInt\": false,\n    \"StatsiteAddr\": \"\",\n    \"Protocol\": 2,\n    \"EnableDebug\": false,\n    \"VerifyIncoming\": false,\n    \"VerifyOutgoing\": false,\n    \"CAFile\": \"\",\n    \"CertFile\": \"\",\n    \"KeyFile\": \"\",\n    \"ServerName\": \"\",\n    \"StartJoin\": [],\n    \"UiDir\": \"\",\n    \"PidFile\": \"/tmp/node1/pid\",\n    \"EnableSyslog\": false,\n    \"SyslogFacility\": \"LOCAL0\",\n    \"RejoinAfterLeave\": false,\n    \"CheckUpdateInterval\": 300000000000,\n    \"Revision\": \"441d613e1bd96254c78c46ee7c1b35c161fc7295+CHANGES\",\n    \"Version\": \"0.3.0\",\n    \"VersionPrerelease\": \"\"\n  },\n  \"Member\": {\n    \"Name\": \"node1\",\n    \"Addr\": \"127.0.0.1\",\n    \"Port\": 8301,\n    \"Tags\": {\n      \"bootstrap\": \"1\",\n      \"build\": \"0.3.0:441d613e\",\n      \"dc\": \"dc1\",\n      \"port\": \"8300\",\n      \"role\": \"consul\",\n      \"vsn\": \"2\",\n      \"vsn_max\": \"2\",\n      \"vsn_min\": \"1\"\n    },\n    \"Status\": 1,\n    \"ProtocolMin\": 1,\n    \"ProtocolMax\": 2,\n    \"ProtocolCur\": 2,\n    \"DelegateMin\": 2,\n    \"DelegateMax\": 4,\n    \"DelegateCur\": 4\n  }\n}\n```\n\n### Hook\n\n基于 consul api，将一些常用api组合，进行打包封装。\n\n\n#### hook.registerService\n\n注册服务。\n\n内部关联的Api：\n - `agent.service.register`\n\n用例：\n```javascript\nawait app.consul.trigger('hook.registerService');\n```\n\n\n#### hook.deRegisterService\n\n注销已注册服务。\n\n内部关联的Api：\n - `agent.service.deregister`\n - `agent.check.deregister`\n\n用例：\n```javascript\nawait app.consul.trigger('hook.deRegisterService');\n```\n\n## 服务发现\n\n#### getServiceHost\n\n通过服务名称获取服务host，如果是目标服务是集群多服务，内置负载均衡算法（轮询算法）\n\n用例：\n```javascript\n// 第一次调用\napp.consul.getServiceHost('serviceName-1'); // return 192.168.1.123:7001\n\n// 第二次调用\napp.consul.getServiceHost('serviceName-1'); // return 192.168.1.124:7001\n\n// 第三次调用\napp.consul.getServiceHost('serviceName-1'); // return 192.168.1.123:7001\n```\n\n服务列表获取和维护，是通过定时任务获取，间隔频率更改 `config.consul.syncInterval` 配置。\n\n\n## 详细配置\n\n请到 [config/config.default.js](config/config.default.js) 查看详细配置项说明。\n\n```javascript\n// config/config.[env].js\nconfig.consul = {\n  autoRegister: true,                                  // 是否自动注册服务\n  timeout: 3000,                                       // consul api调用超时时间，单位：毫秒    \n  syncInterval: '1m',                                  // 服务列表同步更新间隔，默认为1分钟\n  server: {                                            // required, consul agent 服务配置\n    host: '127.0.0.1',                                 // consul agent服务IP（String, default: 127.0.0.1）\n    port: 8500,                                        // consul agent服务端口（Integer, default: 8500）\n    secure: false,                                     // 启用 HTTPS（Boolean, default: false）\n    promisify: true,                                   // 启动 Promise 风格，默认为 Callback（Boolean|Function, optional）\n  },\n  client: {                                            // required, consul service 配置\n    name: serviceName,                                 // 注册的服务名称（String）\n    id: '',                                            // 服务注册标识（String, optional）\n    tags: ['serviceTag1', 'serviceTag2'],              // 服务标签（String[], optional）\n    address: '10.4.146.241',                           // 注册的服务地址（String, optional）\n    port: 7001,                                        // 注册的服务端口（Integer, optional）\n    check: {                                           // 健康检查配置（Object, optional）\n      http: 'http://10.4.146.241:7001/healthcheck',    // 健康检查URL\n      interval: '5s',                                  // 健康检查频率\n      timeout: '5s',                                   // 健康检查超时时间\n      status: 'critical',                              // 初始化服务状态（String, optional）\n    },\n    checks: [],                                        // 有多个检查的路径，可采用对象数组形式，参数参照check的（Object[], optional）\n  },\n};\n```\n\n## 提问交流\n\n请到 [egg-consul-client issues](https://github.com/khronosleung/egg-consul-client/issues) 交流。\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhronosleung%2Fegg-consul-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkhronosleung%2Fegg-consul-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhronosleung%2Fegg-consul-client/lists"}