具有多个端点的HA代理上的节点Socket.io

时间:2016-07-19 06:26:33

标签: node.js sockets load-balancing haproxy

我想在两个盒子上部署我的节点websocket服务,并使用haproxy对其进行屏蔽,但它不起作用。

frontend http-in
  mode http
  bind *:80

  acl is_websocket path_beg /prodSocket                                                                     
  acl is_websocket hdr(Upgrade) -i WebSocket
  acl is_websocket hdr_beg(Host) -i ws
  use_backend prodSocket if is_websocket

  acl is_websocket path_beg /demoSocket                                                                     
  acl is_websocket hdr(Upgrade) -i WebSocket
  acl is_websocket hdr_beg(Host) -i ws
  use_backend demoSocket if is_websocket

backend demoSocket
  timeout server 180s
  server 148.251.76.84 148.251.76.84:9000 weight 1 maxconn 1024 check


backend prodSocket
  timeout server 180s
  server 148.251.76.85 148.251.76.85:9000 weight 1 maxconn 1024 check

客户代码 -

 var socket = io('http://localhost/prodSocket', {                                                            
      'force new connection': false,
      'reconnection delay': 500,
      'max reconnection attempts': 10,
 });
 socket.emit('client', { my: 'data' });
 socket.on('news', function (data) {
     console.log(data);
});

上述代码不起作用,但如果我进行了以下更改,则可以正常工作 -

frontend http-in
  mode http
  bind *:80

  acl is_websocket path_beg /socket.io                                                                     
  acl is_websocket hdr(Upgrade) -i WebSocket
  acl is_websocket hdr_beg(Host) -i ws
  use_backend socket if is_websocket

backend socket
  timeout server 180s
  server 148.251.76.84 148.251.76.84:9000 weight 1 maxconn 1024 check

客户代码 -

 var socket = io('http://localhost:9090', {                                                            
      'force new connection': false,
      'reconnection delay': 500,
      'max reconnection attempts': 10,
 });
 socket.emit('client', { my: 'data' });
 socket.on('news', function (data) {
     console.log(data);
});

我知道socket io正在调用/socket.io端点来创建套接字连接但是如何在两个不同的端点上部署我的服务呢?

版本 -

  1. Socket.io - 1.4.5
  2. 节点 - v5.6.0
  3. HAproxy - 1.4.24
  4. Ubuntu - 14.04
  5. 使用/socket.io端点的HAProxy日志 -

    config -

    frontend http-in
      mode http
      bind *:9090
    
      acl is_websocket path_beg /socket.io
      acl is_websocket hdr(Upgrade) -i WebSocket                                                               
      acl is_websocket hdr_beg(Host) -i ws
      use_backend socket if is_websocket
    

    CLient脚本 -

     var socket = io('http://localhost:9090', {                                                 
          'force new connection': false,
          'reconnection delay': 500,
          'max reconnection attempts': 10,
     });
    
    
    Jul 21 10:50:04 localhost haproxy[11981]: 127.0.0.1:48571 [21/Jul/2016:10:49:51.830] http-in socket/148.251.76.84 0/0/171/676/12725 101 187 - - ---- 2/2/2/2/0 0/0 "GET /socket.io/?EIO=3&transport=websocket&sid=PvK2vnQO1_IepDHOAAAJ HTTP/1.1"
    Jul 21 10:50:55 localhost haproxy[11981]: 127.0.0.1:48573 [21/Jul/2016:10:49:51.832] http-in socket/148.251.76.84 0/0/123/126/63531 200 1551 - - cD-- 2/2/2/2/0 0/0 "GET /socket.io/?EIO=3&transport=polling&t=LOBvKmM&sid=PvK2vnQO1_IepDHOAAAJ HTTP/1.1"
    Jul 21 10:50:55 localhost haproxy[11981]: 127.0.0.1:48569 [21/Jul/2016:10:49:51.505] http-in socket/148.251.76.84 0/0/152/159/64144 200 1199 - - cD-- 1/1/1/1/0 0/0 "GET /socket.io/?EIO=3&transport=polling&t=LOBvKhF HTTP/1.1"
    

    使用/ prodSocket端点的HAProxy日志 -

    配置 -

    frontend http-in
    mode http
    bind *:9090
    
    acl is_websocket path_beg /prodSocket
    acl is_websocket hdr(Upgrade) -i WebSocket                                                               
    acl is_websocket hdr_beg(Host) -i ws
    use_backend socket if is_websocket
    

    CLient脚本 -

     var socket = io('http://localhost:9090/prodSocket', {                                                 
          'force new connection': false,
          'reconnection delay': 500,
          'max reconnection attempts': 10,
     });
    
    
    Jul 21 10:55:11 localhost haproxy[12361]: Proxy socket started.
    Jul 21 10:55:11 localhost haproxy[12361]: 127.0.0.1:48856 [21/Jul/2016:10:55:11.767] http-in http-in/<NOSRV> -1/-1/-1/-1/0 503 213 - - SC-- 0/0/0/0/0 0/0 "GET /socket.io/?EIO=3&transport=polling&t=LOBwYtK HTTP/1.1"
    Jul 21 10:55:16 localhost haproxy[12362]: 127.0.0.1:48859 [21/Jul/2016:10:55:16.229] http-in http-in/<NOSRV> -1/-1/-1/-1/0 503 213 - - SC-- 0/0/0/0/0 0/0 "GET /socket.io/?EIO=3&transport=polling&t=LOBwZz3 HTTP/1.1"
    Jul 21 10:55:17 localhost haproxy[12362]: 127.0.0.1:48860 [21/Jul/2016:10:55:17.364] http-in http-in/<NOSRV> -1/-1/-1/-1/0 503 213 - - SC-- 0/0/0/0/0 0/0 "GET /socket.io/?EIO=3&transport=polling&t=LOBwaEn HTTP/1.1"
    Jul 21 10:55:19 localhost haproxy[12362]: 127.0.0.1:48862 [21/Jul/2016:10:55:19.075] http-in http-in/<NOSRV> -1/-1/-1/-1/0 503 213 - - SC-- 0/0/0/0/0 0/0 "GET /socket.io/?EIO=3&transport=polling&t=LOBwafV HTTP/1.1"
    Jul 21 10:55:22 localhost haproxy[12362]: 127.0.0.1:48865 [21/Jul/2016:10:55:22.262] http-in http-in/<NOSRV> -1/-1/-1/-1/0 503 213 - - SC-- 0/0/0/0/0 0/0 "GET /socket.io/?EIO=3&transport=polling&t=LOBwbRI HTTP/1.1"
    Jul 21 10:55:27 localhost haproxy[12362]: 127.0.0.1:48869 [21/Jul/2016:10:55:27.271] http-in http-in/<NOSRV> -1/-1/-1/-1/0 503 213 - - SC-- 0/0/0/0/0 0/0 "GET /socket.io/?EIO=3&transport=polling&t=LOBwcfa HTTP/1.1"
    

0 个答案:

没有答案