File tree 1 file changed +36
-0
lines changed
1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ var httpProxy = require ( '../../lib/node-http-proxy' ) ;
2
+ //
3
+ // A simple round-robin load balancing strategy.
4
+ //
5
+ // First, list the servers you want to use in your rotation.
6
+ //
7
+ var addresses = [
8
+ {
9
+ host : 'ws1.0.0.0' ,
10
+ port : 80
11
+ } ,
12
+ {
13
+ host : 'ws2.0.0.0' ,
14
+ port : 80
15
+ }
16
+ ] ;
17
+
18
+ httpProxy . createServer ( function ( req , res , proxy ) {
19
+ //
20
+ // On each request, get the first location from the list...
21
+ //
22
+ var target = addresses . shift ( ) ;
23
+
24
+ //
25
+ // ...then proxy to the server whose 'turn' it is...
26
+ //
27
+ console . log ( 'balancing request to: ' , target ) ;
28
+ proxy . proxyRequest ( req , res , target ) ;
29
+
30
+ //
31
+ // ...and then the server you just used becomes the last item in the list.
32
+ //
33
+ addresses . push ( target ) ;
34
+ } ) . listen ( 8000 ) ;
35
+
36
+ // Rinse; repeat; enjoy.
You can’t perform that action at this time.
0 commit comments