Created
April 8, 2014 15:51
-
-
Save cleuton/10146531 to your computer and use it in GitHub Desktop.
Naive load balancer written in Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // balanceador de carga | |
| var http = require('http'); | |
| // npm install http-proxy | |
| var http_proxy = require('http-proxy'); | |
| var proxServer = 0; | |
| var servidores = [ | |
| {'url':'http://localhost:8085'}, | |
| {'url':'http://localhost:8090'}, | |
| {'url':'http://localhost:8095'}]; | |
| var proxy = http_proxy.createProxy(); | |
| require('http').createServer(function(req, res) { | |
| var servidor = servidores[proxServer]; | |
| proxy.web(req, res, { | |
| target: servidor.url | |
| }, function(e) { console.log(e); }); | |
| proxServer = proxServer == 2 ? 0 : proxServer + 1; | |
| }).listen(8080); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works! You have to install http-proxy ("[sudo] npm install http-proxy"). For future development, parametrize the server's table, and, perhaps, include a VM instantiation option, based on current load. You can also check the server's load before routing the request.