问题描述
我目前正在开发一种类似于二十一点的在线纸牌游戏,它将由一系列牌桌组成,其中每张牌桌都有一个庄家"和多个人类玩家.经销商(计算机机器人)负责发牌和洗牌.表 将存储在 PostgreSQL 数据库表中,管理员可以添加/删除/编辑表.
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.
游戏将由一个 Web 前端和一个 REST/websocket API 后端组成.我可能会使用 Kubernetes 和 Nginx 作为后端服务器的负载均衡器.
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.
我的问题.假设我有一台服务器,我可以简单地让它从数据库中读取表列表并为每个表启动一个经销商进程/线程.但是,如果我有 2 台或更多服务器,事情就会变得更加混乱.
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.
我如何确保在所有服务器之间以平衡的方式分配表(例如,如果有 10 个表和 3 个服务器,分布应该大致为 3-3-4)?
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?
推荐答案
我假设每个状态变化都通过 websockets 转发给玩家并存储到数据库中以进行持久化(仅用于故障恢复和一些统计数据).因此,如果由于某种原因 server1 停止工作并且 nginx 开始将 table1 的客户端连接到 server2,那么它(server2)在获取失败前由 server1 提供服务的游戏状态方面将没有问题.
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.
您可以使用 URI 或参数的哈希值将与同一表相关的所有请求转发到特定服务器.
You can use hash of URI or parameter to forward all requests related to the same table to specific server.
当 nginx 看到该服务器离线时,它将停止将其用作池成员,并以一致的方式将所有请求迁移到另一台服务器.
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.
当然,当新服务器上线时,现有表不会在游戏过程中重新分配给新服务器(因为您需要保持 websocket 连接处于工作状态).
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).
但是当游戏结束时,您可以告诉客户端(通过现有的 websocket 连接)重新打开新的 websocket 连接,并稍微修改 URL,从而迫使 nginx 重新平衡服务器之间的连接.
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;
}
}
这篇关于如何以平衡的方式跨服务器分配(纸牌游戏桌)经销商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!