{"id":19774353,"url":"https://github.com/vinks/nest-consul-loadbalance","last_synced_at":"2026-06-09T04:31:11.781Z","repository":{"id":141672427,"uuid":"167543907","full_name":"vinks/nest-consul-loadbalance","owner":"vinks","description":"A Nest framework (node.js) module  for getting service from consul and sending http request by loadbalance.","archived":false,"fork":false,"pushed_at":"2019-01-11T08:56:07.000Z","size":75,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-28T11:51:50.673Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vinks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-25T12:27:02.000Z","updated_at":"2020-03-05T04:49:54.000Z","dependencies_parsed_at":"2024-07-14T16:19:27.725Z","dependency_job_id":null,"html_url":"https://github.com/vinks/nest-consul-loadbalance","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/vinks/nest-consul-loadbalance","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinks%2Fnest-consul-loadbalance","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinks%2Fnest-consul-loadbalance/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinks%2Fnest-consul-loadbalance/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinks%2Fnest-consul-loadbalance/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vinks","download_url":"https://codeload.github.com/vinks/nest-consul-loadbalance/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vinks%2Fnest-consul-loadbalance/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34092253,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-09T02:00:06.510Z","response_time":63,"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":[],"created_at":"2024-11-12T05:12:42.109Z","updated_at":"2026-06-09T04:31:11.764Z","avatar_url":"https://github.com/vinks.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"http://nestjs.com/\" target=\"blank\"\u003e\u003cimg src=\"https://nestjs.com/img/logo_text.svg\" width=\"320\" alt=\"Nest Logo\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## Description\n\nA component of [nestcloud](http://github.com/nest-cloud/nestcloud). NestCloud is a nest framework micro-service solution.\n  \n[中文文档](https://nestcloud.org/solutions/fu-zai-jun-heng)\n\nThis is a software load balancers primary for rest calls.\n\n## Installation\n\n```bash\n$ npm i --save nest-consul consul nest-consul-loadbalance\n```\n\n## Quick Start\n\n#### Import Module\n\n```typescript\nimport { Module } from '@nestjs/common';\nimport { ConsulModule } from 'nest-consul';\nimport { LoadbalanceModule } from 'nest-consul-loadbalance';\n\n@Module({\n  imports: [\n      ConsulModule.register({\n        host: '127.0.0.1',\n        port: 8500\n      }),\n      LoadbalanceModule.register({\n        rules: [\n            {service: 'test-service', ruleCls: 'RandomRule'},\n            {service: 'user-service', ruleCls: '../rules/CustomRule'}\n        ]\n      })\n  ],\n})\nexport class ApplicationModule {}\n```\n\nIf you use [nest-boot](https://github.com/miaowing/nest-boot) module.\n\n```typescript\nimport { Module } from '@nestjs/common';\nimport { ConsulModule, CONSUL_ADAPTER } from 'nest-consul';\nimport { LoadbalanceModule } from 'nest-consul-loadbalance';\nimport { BootModule, BOOT_ADAPTER } from 'nest-boot';\n\n@Module({\n  imports: [\n      ConsulModule.register({adapter: BOOT_ADAPTER}),\n      BootModule.register(__dirname, 'bootstrap.yml'),\n      LoadbalanceModule.register({adapter: BOOT_ADAPTER})\n  ],\n})\nexport class ApplicationModule {}\n```\n\n##### Nest-boot config file\n\n```yaml\nconsul:\n  host: localhost\n  port: 8500\nloadbalance:\n  rules:\n    - {service: 'test-service', ruleCls: 'RandomRule'}\n    - {service: 'user-service', ruleCls: '../rules/CustomRule'}\n```\n\n#### Usage\n\n```typescript\nimport { Component } from '@nestjs/common';\nimport { InjectLoadbalancee, Loadbalance } from 'nest-consul-loadbalance';\n\n@Component()\nexport class TestService {\n  constructor(@InjectLoadbalancee() private readonly lb: Loadbalance) {}\n\n  async chooseOneNode() {\n      const node = this.lb.choose('user-service');\n  }\n}\n```\n\n### Custom Loadbalance Rule\n\n```typescript\nimport { Rule, Loadbalancer } from 'nest-consul-loadbalance';\n\nexport class CustomRule implements Rule {\n    private loadbalancer: Loadbalancer;\n    \n    init(loadbalancer: Loadbalancer) {\n        this.loadbalancer = loadbalancer;\n    }\n\n    choose() {\n        const servers = this.loadbalancer.servers;\n        return servers[0];\n    }\n}\n```\n\n## API\n\n### class LoadbalanceModule\n\n#### static register\\(options\\): DynamicModule\n\nImport nest consul loadbalance module.\n\n| field | type | description |\n| :--- | :--- | :--- |\n| options.adapter | string | if you are using nest-boot module, please set BOOT_ADAPTER |\n| options.ruleCls | string \\| class | lb rule，support：RandomRule, RoundRobinRule, WeightedResponseTimeRule or custom lb rule, use relative path |\n| options.rules | RuleOption | one service use one rule, eg：\\[{service: '', ruleCls: ''}\\] |\n\n### class Loadbalance\n\n#### choose\\(service: string\\): Server\n\nChoose a node that running the specific service.\n\n| field | type | description |\n| :--- | :--- | :--- |\n| service | string | the service name |\n\n#### chooseLoadbalancer\\(service: string\\): Loadbalancer\n\nGet loadbalancer.\n\n| field | type | description |\n| :--- | :--- | :--- |\n| service | string | the service name |\n\n## Stay in touch\n\n- Author - [Miaowing](https://github.com/miaowing)\n\n## License\n\n  Nest is [MIT licensed](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvinks%2Fnest-consul-loadbalance","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvinks%2Fnest-consul-loadbalance","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvinks%2Fnest-consul-loadbalance/lists"}