├── LICENSE ├── README.md ├── config.json ├── load-balance.js ├── package-lock.json └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 maskodingku 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # load-balancer-nodejs 2 | nodejs load balancing app to distribute http requests evenly across multiple servers. 3 | 4 | ![1M requests routed to 10 proxies](https://raw.githubusercontent.com/borzaresearch/node-load-balancers/master/docs/comparison.png) 5 | 6 | How to use ? 7 | 8 | 1. Please edit the file 'config.json'. 9 | 2. enter the value 'host' and 'port' with the following format: 10 | 11 |
12 | {
13 |   "host" : "your-ip-server",
14 |   "port" : port-server
15 | }
16 | 
17 | 18 | 3. Perform package installation 19 | 20 |
npm install
21 | 22 | 4. For a test run: 23 | 24 |
npm test
25 | 26 | 5. If you want to run in production mode, please install the 'pm2' module. 27 | 28 |
npm install pm2 -g
29 | 30 | 6. Then run the following command: 31 | 32 |
npm start
33 | 34 | ------------------------------------ **** ------------------------------ 35 | 36 | Thanks to : 37 | 38 | 1. PM2 : https://www.npmjs.com/package/pm2 39 | 2. http-proxy : https://www.npmjs.com/package/http-proxy 40 | 3. load-balancers : https://www.npmjs.com/package/load-balancers 41 | -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "port-listener":80, 3 | "timeout-waitting-proxy":10000, 4 | "proxy-balance":[ 5 | { 6 | "host":"10.104.0.3", 7 | "port":8081 8 | }, 9 | { 10 | "host":"10.104.0.4", 11 | "port":8082 12 | }, 13 | { 14 | "host":"10.104.0.5", 15 | "port":8083 16 | }, 17 | { 18 | "host":"10.104.0.7", 19 | "port":8084 20 | }, 21 | { 22 | "host":"10.104.0.8", 23 | "port":8085 24 | }, 25 | { 26 | "host":"10.104.0.9", 27 | "port":8086 28 | }, 29 | { 30 | "host":"10.104.0.11", 31 | "port":8087 32 | } 33 | ] 34 | } 35 | -------------------------------------------------------------------------------- /load-balance.js: -------------------------------------------------------------------------------- 1 | const http = require("http"); 2 | const fs = require("fs"); 3 | const httpProxy = require('http-proxy'); 4 | const { 5 | P2cBalancer, 6 | RandomBalancer, 7 | } = require('load-balancers'); 8 | const dir = process.cwd(); 9 | const config = require(dir+"/config.json"); 10 | const proxy = httpProxy.createProxyServer({ 11 | proxyTimeout:config["timeout-waitting-proxy"] 12 | }); 13 | const proxies = config["proxy-balance"]; 14 | const balancer = new P2cBalancer(proxies.length); 15 | const port = config["port-listener"]; 16 | 17 | proxy.on('error', function (err, req, res) { 18 | res.writeHead(500, { 19 | 'Content-Type': 'text/plain' 20 | }); 21 | res.end('Something went wrong. And we are reporting a custom error message.'); 22 | }); 23 | 24 | //----------- main ---------------------------------------------------- 25 | http.createServer((req,res)=>{ 26 | proxy.web(req, res, { 27 | target: proxies[balancer.pick()] 28 | }); 29 | }).listen(port,()=>{ 30 | console.log("server load balancer running on port",port); 31 | }); -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "load-balance", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "load-balance", 9 | "version": "1.0.0", 10 | "license": "MIT", 11 | "dependencies": { 12 | "http-proxy": "^1.18.1", 13 | "load-balancers": "^1.3.52" 14 | } 15 | }, 16 | "node_modules/eventemitter3": { 17 | "version": "4.0.7", 18 | "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz", 19 | "integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==" 20 | }, 21 | "node_modules/follow-redirects": { 22 | "version": "1.14.9", 23 | "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", 24 | "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==", 25 | "funding": [ 26 | { 27 | "type": "individual", 28 | "url": "https://github.com/sponsors/RubenVerborgh" 29 | } 30 | ], 31 | "engines": { 32 | "node": ">=4.0" 33 | }, 34 | "peerDependenciesMeta": { 35 | "debug": { 36 | "optional": true 37 | } 38 | } 39 | }, 40 | "node_modules/http-proxy": { 41 | "version": "1.18.1", 42 | "resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.18.1.tgz", 43 | "integrity": "sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==", 44 | "dependencies": { 45 | "eventemitter3": "^4.0.0", 46 | "follow-redirects": "^1.0.0", 47 | "requires-port": "^1.0.0" 48 | }, 49 | "engines": { 50 | "node": ">=8.0.0" 51 | } 52 | }, 53 | "node_modules/load-balancers": { 54 | "version": "1.3.52", 55 | "resolved": "https://registry.npmjs.org/load-balancers/-/load-balancers-1.3.52.tgz", 56 | "integrity": "sha512-MjumtTnTzq0CD8i+E4BZlAnW+Sr10bZeXo9L/mG8wsCFDEbVFscWStM+4J2UlDbhiOhwaXNqnyURViOspmtc9w==" 57 | }, 58 | "node_modules/requires-port": { 59 | "version": "1.0.0", 60 | "resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz", 61 | "integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=" 62 | } 63 | }, 64 | "dependencies": { 65 | "eventemitter3": { 66 | "version": "4.0.7", 67 | "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz", 68 | "integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==" 69 | }, 70 | "follow-redirects": { 71 | "version": "1.14.9", 72 | "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", 73 | "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==" 74 | }, 75 | "http-proxy": { 76 | "version": "1.18.1", 77 | "resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.18.1.tgz", 78 | "integrity": "sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==", 79 | "requires": { 80 | "eventemitter3": "^4.0.0", 81 | "follow-redirects": "^1.0.0", 82 | "requires-port": "^1.0.0" 83 | } 84 | }, 85 | "load-balancers": { 86 | "version": "1.3.52", 87 | "resolved": "https://registry.npmjs.org/load-balancers/-/load-balancers-1.3.52.tgz", 88 | "integrity": "sha512-MjumtTnTzq0CD8i+E4BZlAnW+Sr10bZeXo9L/mG8wsCFDEbVFscWStM+4J2UlDbhiOhwaXNqnyURViOspmtc9w==" 89 | }, 90 | "requires-port": { 91 | "version": "1.0.0", 92 | "resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz", 93 | "integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=" 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "load-balance", 3 | "version": "1.0.0", 4 | "description": "nodejs load balancing app to distribute http requests evenly across multiple servers.", 5 | "main": "load-balance.js", 6 | "scripts": { 7 | "test": "node load-balance.js", 8 | "start": "pm2 stop load-balance -f & sleep 3s && pm2 start load-balance.js --exp-backoff-restart-delay=1000 --max-memory-restart 512M -i max && pm2 save && pm2 startup && pm2 save" 9 | }, 10 | "author": "Abdi Syahputra Harahap", 11 | "license": "MIT", 12 | "dependencies": { 13 | "http-proxy": "^1.18.1", 14 | "load-balancers": "^1.3.52" 15 | } 16 | } 17 | --------------------------------------------------------------------------------