Strategy ONE

负载均衡器和集群 Library:示例设置工作流程

安装并配置 Redis 服务器

  1. 要安装 Redis,请参阅 Redis 入门
  2. 运行 Redis 服务器。
  3. 通过发送“PING”命令来检查 Redis 是否正常工作 redis-cli

    如果 Redis 服务器正常,将会响应‘PONG’。

    复制
    127.0.0.1:6379> ping
    PONG
    127.0.0.1:6379>

    协作服务器需要一个 URL 来连接到正在运行的 Redis 服务器,以实现水平集群目的。

    例子redis://redis_host_machine:6379

    修改Redis配置文件,使其可以接受外部流量。

    • 在 Redis 配置文件中(redis.conf 或者 redis.windows-service.conf)添加/更改 bind 进入 bind 0.0.0.0
    • 保存修改并重启Redis Server

配置协作服务器

  1. 对于安装在不同机器上的所有协作服务器,在配置文件中设置 Redis 服务器 URL、Web 负载均衡器 URL、集群模式:

    复制
    {
    "port": 3000,
    "dataSource": {"username": "mstr_collab","password": "xxx"},
    "logging": true,
    "authorizationServerUrl":"<web_load_balancer_url>/api",
    "scaling": "horizontal","redisServerUrl": 
    "<redis_server_url>",
    .......
    }

     

  2. 保存配置文件并重新启动协作服务器。
  3. 通过访问各自的 /status URL 确保所有协作服务器都在正常运行。它将告诉协作服务器实例的实际状态。例如,下面的协作服务器实际上由于无法访问 Postgres 和 Redis 服务器而暂停。

    复制
    {
    "version":"11.0.0.720",
    "enableTls":false,
    "scaling":"horizontal",
    "logging":true,
    "state":"paused",
    "reason":"The following dependent services are unavailable: DB Service, Redis Server",
    "main":
    {
    "cpu":{"current":0,"max":17},
    "memory":{"current":62758912,"max":85626880},
    "connections":{"total":12,"current":4,"max":11},
    "messages":{"processed":5},
    "restCalls":{"received":3},
    "up_since":"2018-4-18 02:12:42","pid":900
    }
    }

     

配置 Web 负载均衡器服务器

负载均衡器必须利用“粘性会话”来确保客户端被定向到建立初始会话的实例。它还应该支持依赖于 HTTP 1.1 升级协议的 Web Socket 协议(wss:)。

例如,当使用 NGINX 作为 Web 负载均衡器时,以下是水平集群协作服务器的配置。

复制
......

http {
    ......

    upstream collabServer {
    ip_hash;             # this is a must, ensures that we are sticky to the IP to enable a correct socket.io handshake
    server collabServer1:3000;    # provide the list of the Collaboration servers from here
    server collabServer2:3000;
    server collabServer3:3000;
    }
    
    location /socket.io/ {                    # API Pattern for socket.io handshake
        proxy_http_version 1.1;                # Required for proper HTTP upgrade protocol
        proxy_set_header Upgrade $http_upgrade;     # This is a Hop-by-Hop header–must pass through manually
        proxy_set_header Connection "upgrade";      # Another hop-by-hop header
        proxy_pass http://collabServer/socket.io/;      # Directs the request to a server in upstream "collabServer"
    }

    location /status {                    # forward the /status API to one server from the CollabServer list
        proxy_pass http://collabServer/status;
    }

    ......

}

......