如何在服务器之间平衡地分配(纸牌游戏桌)发牌人? [英] How to distribute (card game table) dealers across servers in a balanced way?

查看:110
本文介绍了如何在服务器之间平衡地分配(纸牌游戏桌)发牌人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从事类似于二十一点的在线纸牌游戏,该游戏将由一系列桌子组成,其中每个桌子都有一个经销商"和多个人类玩家.发牌人(计算机机器人)负责发牌和洗牌.将存储在PostgreSQL数据库表中,管理员可以添加/删除/编辑表.

游戏将由一个Web前端和一个REST/websocket API后端组成.我可能会使用Kubernetes和Nginx作为后端服务器的负载平衡器.

我的问题.假设我有一台服务器,我可以简单地让它从数据库中读取表列表,然后为每个表启动一个经销商进程/线程.但是,如果我有2个或更多服务器,事情就会变得更加混乱.

如何确保在所有服务器上均衡地分配(例如,如果有10个表和3个服务器,则分配应该大致为3-3-4)?

如何确保如果服务器发生故障,其会重新分配给活动服务器?

当新服务器联机时,如何确保将某些现有重新分配给它,以减少其他服务器的工作量?

我假设每个状态更改都通过websocket转发给玩家,并存储在数据库中以保持持久性(仅用于故障恢复和某些统计).因此,如果由于某种原因server1停止工作并且nginx开始将table1的客户端连接到server2,则它(server2)在恢复故障之前由server1服务的游戏状态上将没有问题.

您可以使用URI或参数的哈希将与同一表相关的所有请求转发到特定服务器.

当nginx看到该服务器脱机时,它将停止将其用作池成员,并以方便的方式将所有请求迁移到另一台服务器.

当然,当新服务器上线时,在游戏过程中不会将现有表重新分配给新服务器(因为您需要保持websocket连接处于工作状态).

但是当游戏结束时,您可以告诉客户端(通过现有的websocket连接)使用稍微修改的URL重新打开新的websocket连接,从而迫使nginx重新平衡服务器之间的连接.

 上游后端{哈希$ request_uri一致;#hash $ arg_table一致;服务器bj-1.card-games.com max_fails = 2 fail_timeout = 2s;服务器bj-2.card-games.com max_fails = 2 fail_timeout = 2s;服务器bj-3.card-games.com max_fails = 2 fail_timeout = 2s;}服务器 {地点/{proxy_pass http://$ backend;}} 

I am currently working on an online card game, similar to blackjack, which will consist of a series of tables where each table has a "dealer" and multiple human players. The dealer (a computer bot) is responsible for dealing and shuffling cards. The tables will be stored in a PostgreSQL database table and it will be possible for a human admin to add/remove/edit tables.

The game will consist of a web front end and a REST/websocket API backend. I will probably use Kubernetes and Nginx as a load balancer for the backend servers.

Onto my question. Let's say I have a single server, I could simply have it read the table list from the database and start a dealer process/thread per table. However, if I have 2 or more servers things start to get more messy.

How do I ensure that the tables are assigned in a balanced way across all servers (e.g. if there is 10 tables and 3 servers, the distribution should be roughly 3-3-4)?

How do I ensure that if a server fails, its tables get reassigned to a live server?

How do I make sure that, when a new server comes online, some existing tables get re-assigned to it so to reduce the workload of the other servers?

解决方案

I assume each state change is forwarded to players via websockets and stored into the database for persistence (just for failure recovery and some stats). So if for some reason server1 stops working and nginx starts connecting clients of table1 to server2 then it(server2) will have no problems with picking up the state of the game which was serviced by server1 before failure.

You can use hash of URI or parameter to forward all requests related to the same table to specific server.

When nginx sees that server went offline it will stop using it as a pool member and migrate all requests to another server in a consitent way.

Of course when new server comes online existing table will NOT be reassigned to the new server during the game (because you need to keep websocket connection in working state).

But when game ends you can tell clients (via existing websocket connection) to reopen new websocket connection with slightly modified URL thus forcing nginx to re-balance connections between servers.

upstream backend {
    hash $request_uri consistent;
#    hash $arg_table consistent;
    server bj-1.card-games.com max_fails=2 fail_timeout=2s;
    server bj-2.card-games.com max_fails=2 fail_timeout=2s;
    server bj-3.card-games.com max_fails=2 fail_timeout=2s;
}

server {
 location / {
  proxy_pass http://$backend;
 }
}

这篇关于如何在服务器之间平衡地分配(纸牌游戏桌)发牌人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆