mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[examples] updated balancer examples
This commit is contained in:
parent
ed8c9eeba9
commit
831a44b3c8
58
examples/balancer/simple-balancer-with-websockets.js
Normal file
58
examples/balancer/simple-balancer-with-websockets.js
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
var http = require('http'),
|
||||||
|
httpProxy = require('../../lib/http-proxy');
|
||||||
|
|
||||||
|
//
|
||||||
|
// A simple round-robin load balancing strategy.
|
||||||
|
//
|
||||||
|
// First, list the servers you want to use in your rotation.
|
||||||
|
//
|
||||||
|
var addresses = [
|
||||||
|
{
|
||||||
|
host: 'ws1.0.0.0',
|
||||||
|
port: 80
|
||||||
|
},
|
||||||
|
{
|
||||||
|
host: 'ws2.0.0.0',
|
||||||
|
port: 80
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
//
|
||||||
|
// Create a HttpProxy object for each target
|
||||||
|
//
|
||||||
|
|
||||||
|
var proxies = addresses.map(function (target) {
|
||||||
|
return new httpProxy.createProxyServer({
|
||||||
|
target: target
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
//
|
||||||
|
// Get the proxy at the front of the array, put it at the end and return it
|
||||||
|
// If you want a fancier balancer, put your code here
|
||||||
|
//
|
||||||
|
|
||||||
|
function nextProxy() {
|
||||||
|
var proxy = proxies.shift();
|
||||||
|
proxies.push(proxy);
|
||||||
|
return proxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Get the 'next' proxy and send the http request
|
||||||
|
//
|
||||||
|
|
||||||
|
var server = http.createServer(function (req, res) {
|
||||||
|
nextProxy().web(req, res);
|
||||||
|
});
|
||||||
|
|
||||||
|
//
|
||||||
|
// Get the 'next' proxy and send the upgrade request
|
||||||
|
//
|
||||||
|
|
||||||
|
server.on('upgrade', function (req, socket, head) {
|
||||||
|
nextProxy().ws(req, socket, head);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen(8080);
|
||||||
|
|
||||||
38
examples/balancer/simple-balancer.js
Normal file
38
examples/balancer/simple-balancer.js
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
var http = require('http'),
|
||||||
|
httpProxy = require('../../lib/http-proxy');
|
||||||
|
//
|
||||||
|
// A simple round-robin load balancing strategy.
|
||||||
|
//
|
||||||
|
// First, list the servers you want to use in your rotation.
|
||||||
|
//
|
||||||
|
var addresses = [
|
||||||
|
{
|
||||||
|
host: 'ws1.0.0.0',
|
||||||
|
port: 80
|
||||||
|
},
|
||||||
|
{
|
||||||
|
host: 'ws2.0.0.0',
|
||||||
|
port: 80
|
||||||
|
}
|
||||||
|
];
|
||||||
|
var proxy = httpProxy.createServer();
|
||||||
|
|
||||||
|
http.createServer(function (req, res) {
|
||||||
|
//
|
||||||
|
// On each request, get the first location from the list...
|
||||||
|
//
|
||||||
|
var target = { target: addresses.shift() };
|
||||||
|
|
||||||
|
//
|
||||||
|
// ...then proxy to the server whose 'turn' it is...
|
||||||
|
//
|
||||||
|
console.log('balancing request to: ', target);
|
||||||
|
proxy.web(req, res, target);
|
||||||
|
|
||||||
|
//
|
||||||
|
// ...and then the server you just used becomes the last item in the list.
|
||||||
|
//
|
||||||
|
addresses.push(target);
|
||||||
|
}).listen(8000);
|
||||||
|
|
||||||
|
// Rinse; repeat; enjoy.
|
||||||
Loading…
x
Reference in New Issue
Block a user