node-http-proxy load balance websocket error



我刚刚开始评估node-http-proxy,因为我需要一个可扩展的web套接字服务器。

我已经测试了存储库中提供的"simple-balance -with-websockets"示例,但它在作为多个地址的代理时不起作用。它只能作为一个地址的代理!

当代理多个地址时,WebSocket挂起错误如下:

Error: socket hang up
    at createHangUpError (http.js:1472:15)
    at Socket.socketOnEnd [as onend] (http.js:1568:23)
    at Socket.g (events.js:180:16)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)

我正在使用:

0.10.26

节点Socket IO 1.0.6node-http-proxy 1.1.5平台OSX

下面是负载均衡器。它与提供的示例的唯一区别是使用的地址和侦听端口。

var http = require('http'),
    httpProxy = require('http-proxy');
//
// A simple round-robin load balancing strategy.
// 
// First, list the servers you want to use in your rotation.
//
var addresses = [
    {
        host: 'localhost',
        port: 8000
    },
    {
        host: 'localhost',
        port: 8001
    },
    {
        host: 'localhost',
        port: 8002
    }
];
//
// 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(9000);

作为上述负载平衡器的目标的基本http服务器是:

var http = require('http'),
    fs = require('fs'),
    io = require('socket.io');
var args = process.argv.splice(2);
var port = args[0] || 8000;
server = http.createServer(function(req, res) {
    var filePath = (__dirname + '/public/connect.html');
    fs.readFile(filePath,function (err, data){
        res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
        res.write(data);
        res.end();
    });
});
server.listen(port, function() {
    console.log('ws listening on: ' + port);
});
io = io(server);
io.on('connect', function(socket){
   console.log('socket connected');
    socket.emit('message', 'ws message from ' + port);
});

客户端html为:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io();
            socket.on('message', function (data) {
                console.log(data);
            });
    </script>
</head>
<body>
node-http-proxy basic load balance test with websockets
</body>
</html>

我认为这是一个基本的测试,但它不工作!谁能解释我做错了什么,并建议一个解决方案,请?

谢谢你的建议。

插座。IO 1.0需要粘性会话。看到插座。io/docs/using-multiple-nodes

第一引擎。IO发出一个XHR请求,然后发出一个websocket请求。两个请求都需要到达同一个套接字。io服务器。发动机更是如此。IO需要回退到长轮询等…

要修复它,您只需要使代理服务器会话感知。它仍然可以轮询新的连接,但只要它为套接字提供服务。

当IO请求时,需要将该会话的后续请求路由到同一后端。

最新更新