tcp-balance
A simple TCP load balancer.
一个简单的TCP负载均衡器。
Installation
$ npm install -g tcp-balance
Usage
$ tcp-balance [-b [host:]port] [-a single|robin|random|weighted] host:port[%weight] [host:port[%weight]...]
-b, --bind
Bind to the specified address and port. Default: 127.0.0.1:8080.
绑定到指定的地址和端口。默认:127.0.0.1:8080。
-a, --algorithm
Load balancing algorithm. Algorithms: single, robin, random, weighted. Default: robin.
负载均衡算法。可选值:single, robin, random, weighted。默认:robin。
host:port[%weight]
Upstream server address and port. Weight is a optional numeric leading with percentage sign. If not specified, the default weight is 100. It's only effective when algorithm is weighted.
上游服务的地址和端口。权重是一个可选的数值,以百分号开头。如果未指定,默认权重为100。仅在weighted算法下有效。
Examples
Single. Load balance to the first available server only. For example: Failover.
Single. 只负载到第一个可用的服务器。例如:故障转移。
$ tcp-balance -b 0.0.0.0:1883 -a single s1.example.com:1883 s2.example.com:1883
Weighted round robin. For example: HTTP load balancing.
权重轮询。例如:HTTP负载均衡。
$ tcp-balance -b 0.0.0.0:80 -a weighted s1.example.com:80%30 s2.example.com:80%70
Programing
Install tcp-balance as npm dependency.
安装tcp-balance为npm依赖。
$ npm i tcp-balance
Import and use it.
引入并使用。
import { Agent } from 'tcp-balance/bin/agent';
import { SingleBalancer } from 'tcp-balance/bin/balancer';
import { Logger } from 'tcp-balance/bin/logger';
Logger.setChannel(console);
const balancer = new SingleBalancer();
balancer.putUpstream('s1.example.com', 80);
balancer.putUpstream('s2.example.com', 80);
const agent = new Agent(balancer);
agent.start(0.0.0.0, 80);
Build
$ npm i -g typescript
$ git clone https://github.com/iwares/tcp-balance.git
$ cd tcp-balance
$ npm i
$ npm run build
License
MIT