2012-09-14 56 views
4

到目前为止,我已经有了一个node.js应用程序。运行socket.io。随着用户数量的增长,它大部分时间都会达到100%CPU,因此我决定将用户分成多个node.js进程。我已经拆分了我的node.js应用程序逻辑,以允许不同子域上的用户分片。我还将会话代码提取到通过URL传递的令牌中,因此Cookie不重要。使用HAProxy缩放socket.io

我想使用我的8核机器的至少4个核心,所以我想运行多个node.js进程,每个进程在子域上提供应用程序。为了通过端口80访问所有node.js,我决定使用HAProxy。设置是这样的:

 domain.com -> haproxy -> node on 127.0.0.1:5000 
sub1.domain.com -> haproxy -> node on 127.0.0.1:5001 
sub2.domain.com -> haproxy -> node on 127.0.0.1:5002 
sub3.domain.com -> haproxy -> node on 127.0.0.1:5003 

现在一切正常,但应用程序的一部分reqular(不使用socket.io)是很慢的。它使用Express.js编写,当我直接打开页面时(即不通过HAProxy),它的运行速度非常快。此外,连接到socket.io可以快速与XHR传输协同工作,但对于Websocket传输,建立连接也需要很长时间。一旦建立连接,它就可以很好地,快速地工作。

我从来没有使用过HAProxy,所以我可能会错误地配置一些东西。这里是我的HAProxy配置:

global 
    maxconn 50000 
    daemon 

defaults 
    mode http 
    retries 1 
    contimeout 8000 
    clitimeout 120000 
    srvtimeout 120000 

frontend http-in 
    bind *:80 
    acl is_l1 hdr_end(host) -i sub1.domain.com 
    acl is_l2 hdr_end(host) -i sub2.domain.com 
    acl is_l3 hdr_end(host) -i sub3.domain.com 
    acl is_l0 hdr_end(host) -i domain.com 
    use_backend b1 if is_l1 
    use_backend b2 if is_l2 
    use_backend b3 if is_l3 
    use_backend b0 if is_l0 
    default_backend b0 

backend b0 
    balance source 
    option forwardfor except 127.0.0.1 # stunnel already adds the header 
    server s1 127.0.0.1:5000 

backend b1 
    balance source 
    option forwardfor except 127.0.0.1 # stunnel already adds the header 
    server s2 127.0.0.1:5001 

backend b2 
    balance source 
    option forwardfor except 127.0.0.1 # stunnel already adds the header 
    server s2 127.0.0.1:5002 

backend b3 
    balance source 
    option forwardfor except 127.0.0.1 # stunnel already adds the header 
    server s2 127.0.0.1:5003 

回答

3

我想通了。我没有在文档中找到它,但全局maxconn设置不适用于前端。前端具有2000个并发连接的默认值,并且所有内容都已排队。由于我有长期socket.io连接,这就产生了问题。

解决方案是在前端部分显式设置maxconn。

+1

你是对的。或者,您可以在默认部分设置此maxconn,它将应用于所有前端。请记住,您总是希望前端的maxconn低于全局部分,以避免一个前端饱和整个过程的风险。 –