Go game server framework to build from zero (a) - Architecture Design

         Wuyi Hidden Man, whose real name is Guan Jian Chang, 10-year career in the game, now seaside seclusion.

  This tutorial Go language partitions game server framework to build an example.

  Go is a static strongly typed language Google developed, compiled, and hair, with a programming language garbage collection function. Approximate C language grammar, to support the interface, by another concept struct struct manner comprising object-oriented inheritance. Performance comparable to C / C ++, compared to C / C ++ is more robust and easier to develop concurrent programs. I used to write C ++ server, after exposure to Go, is more inclined to do the game server developed by Go.

  The so-called partition games, refers to the game will be divided into a number of zones, players can not interact or little interaction between the different zones. Players enter the game need to select the partition, specify the partition into the game, a player can also have roles in different partitions. Currently on the market most severe online games have adopted this model, zoning does not require large DAU games for interactive games, such as cards, MMORPG, SLG and so on. From a technical level, the partitions which are a means of cluster expansion; the operation is conducive to partition fine operators, service operators have been rolling is more mature game operator means. Relative partitions game, there is no partition of social games, these games are matched against the core gameplay, such as COC. If the zoning district game, the game map for the class of service nodes, the broadcast service is mapped to match the clothes, the architecture of the partition of the game will not be well understood.

  This tutorial is mainly about the game server partition frame structures. The overall design is as follows:

  Partition game, players first log in the game, and then enter the specified partition. So first of all have a login server, provides global login service. Login core data server is the player account, the core business is the player to log in check, including the use of own-account system, third-party account login. Account long-term storage of data needs to fall, so supporting a MySQL database on the server that holds the player account information. There is a service to multiple login process, verification log in the game. Log service is a short connection service, using the http protocol, providing unified access to the external address by nginx, multiple sign-in service processes do load balancing. In general, the interface will return to the partition login information, role players each partition brief information, the latest version of the client number, configuration version, version resources, announcements and other information can be (will be introduced later) by gm serving dynamic updates, log on clothes in response obtain and returned to the client upon request. So log on the server supporting a redis service for caching data.

  After the success of players log in the game, the game will enter the selected partition. Zoning is a set of service process. Most games are using a long partitioning communication connection, if taking into account the now popular h5 game, small game micro-channel, consider using websocket do communications (previously used socket). Taking into account the needs of the game will be broadcast, broadcast data and game data partitions are hoping to return from the same connection to the client, it is necessary to provide a unified service partition ingress gateway, the gateway server provides a unified process of zoning and address of the client port, internal data forwarding do. Logical partitioning services can be concentrated in one service game in the process. Some of the previous games, online game service data will be stored in memory in the process, because the game data changes too frequently, MySQL frequently read and write performance is not high, it will be some time before every save to MySQL. But the sub-region游戏服是一个单点,一旦崩掉,游戏服的内存数据就会丢失,回档到上一次保存的时间。后来一些游戏为了减少这种风险,把在线数据保存到共享内存,再定时保存到MySQL。共享内存依赖系统和语言,目前发现Go没有直接支持。再后来有了memcached和redis,部分游戏选择用这种缓存系统做缓存,游戏服崩掉,数据还在缓存里不会丢失,可以快速启动游戏服恢复服务。我选用redis作为缓存,缓存活跃玩家数据。隔一定时间,把变化数据保存到MySQL。redis数据主要使用key-value方式保存数据,每次业务处理都需要读取、解析,再使用。对业务开发不是很友好。游戏服进程内存还是会保存在线玩家数据,玩家进入分区时从redis读取到游戏服内存,redis不命中则发布消息给数据服进行数据预热,预热成功后从redis读取。后面的请求可以直接通过内存数据做业务判断、处理,更改数据以事务方式保存到redis,成功后响应给客户端。这样内存数据跟redis数据一致,而且可以把玩家数据拆成更细的单元,减少跟redis间的通信。玩家下线后清除游戏服内存数据。所以分区内配套一个redis、一个MySQL。为了建立定时保存数据这个机制,且不会因游戏服崩溃而受影响,配备一个功能很简单的数据服,通过redis的发布订阅机制、消息队列,负责数据的定时落地固化、玩家注册、数据预热。

  前面提到广播服,广播服顾名思义主要负责广播,例如跑马灯广播、世界聊天、世界boss。广播服通过各个分区的网关服将数据广播给玩家,因此广播服将连接各个分区网关。广播任务通过消息队列进行缓存,这样每个分区的广播操作在写到队列后就可以响应客户端。消息队列采用redis实现。广播服是一个全局的服务,为了避免单点风险,可以做成主从,通过redis的订阅发布机制,启动时订阅redis,如果一定时间没有收到发布消息,认为主服务不存在,切换为主服务,取消消息订阅,连接各分区网关,定时向redis发布消息报活。

  除了业务相关的服务,需要对整个服务体系提供管理。例如开服、停服、更新配置/资源版本、发邮件、发公告、发放道具、踢人等。提供一个全局的gm服,各分区服务启动后,游戏服进程连接到gm服并保持心跳,以通知gm服开/停服。gm服将这些变更信息更新到登录服的redis,这样玩家登录游戏就知道各个服的状态。gm服还可以通过向redis发消息通知登录服进行封号等操作。由于各个游戏服都连接到gm服,这样就可以对各个分区发gm命令。gm服可以通过向广播服的消息队列写消息发全员广播。gm服的功能由运营人员进行操作,所以需要提供http服务,方便在网页上访问。gm服有道具发放的功能,所以第三方支付回调可以通过gm服的http接口请求发货。

  为了给运营提供决策,还需要提供统计后台,对游戏数据日志进行收集、统计。由于登录服、各分区的游戏服、gm服都会上报数据,数据来源广,数据量大,需要做消息队列。因此登录服、游戏服、gm服都通过redis的消息队列进行上报。统计服从redis读取消息,保存数据日志到MySQL。因此需要配套一个redis、一个MySQL。统计服的功能由运营人员使用,需要提供http服务,方便在网页上访问。统计服的http接口还支持客户端进行数据上报。

  为了合并运营人员的页面,gm服、统计服通过nginx提供统一的http地址。

  这样就得到了如前面设计图的整个服务框架。 

  本篇介绍到这里,接下来会详细介绍各个服务的实现。在此之前,下一篇先介绍一些通用的基础机制设计和实现。

Guess you like

Origin www.cnblogs.com/niudanshui/p/11864281.html