directory
Version:
v1.3.12
Opens a new window with list of versions in this module.
Published: Dec 20, 2023
License: MIT
Opens a new window with license information.
README
¶
分布式多节点示例
- 建议在windows环境进行调试(示例自带
nats-server.exe
),其他操作系统需自行搭建nats server
- 本示例没有使用数据库,进程重启会还原所有数据
- 客户端演示分为两种:
robot_client
为go实现的游戏压测客户端,使用tcp/protobuf
协议
nodes/web/view/
为h5实现的游戏客户端,使用websocket/protobuf
协议
- 欢迎开发者一起入群讨论,构建更好的demo
要求
- 安装GO版本 >= 1.17
- 安装nats.io >= 2.0
配置
- profile文件在
examples/config/profile-gc.json
- 策划配置文件在
examples/config/data/
操作步骤
克隆
git clone https://github.com/cherry-game/cherry.git
- 或者点击github.com页面的
code
按钮Download zip
下载源码包
调试
0x00 打开项目
打开项目源码,找到examples/demo_game_cluster
目录
0x01 启动nats server
nats为高性能的分布式消息中间件,详情可通过https://github.com/nats-io/nats-server
进行了解
本框架中所有节点都基于nats进行消息通信
单机版nats执行程序在examples/3rd/nats-server
目录中
正式环境请使用集群nats-streaming-server进行部署 https://github.com/nats-io/nats-streaming-server
- 操作步骤:
- 运行
examples/3rd/nats-server/run_nats.bat
单机版
- 窗口显示
Listening for client connections on 0.0.0.0:4222
代表nats启动成功,nats默认监听4222
端口
0x02 启动参数配置
找到exmaples/demo_game_cluster/nodes/main.go
,所有节点都从main.go
启动
启动参数请参考launch.json文件
0x03 启动master节点
master节点主要用于实现最基础的发现服务,基于nats构建
正式环境也可配置为etcd方式提供发现服务
相关的代码在examples/demo_game_cluster/master/
目录
- 启动参数:
master --path=./examples/config/profile-gc.json --node=gc-master
0x04 启动center节点
center节点目前主要用于处理帐号相关的业务或全局唯一的业务
- 启动参数:
center --path=./examples/config/profile-gc.json --node=gc-center
0x05 启动web节点
web节点主要对外提供一些http的接口,可横向扩展,多节点部署
目前用于开发者帐号注册、区服列表、sdk登陆/支付回调、验证token生成等业务
- 启动参数:
web --path=./examples/config/profile-gc.json --node=gc-web-1
0x06 启动gate节点
gate节点为游戏对外网关,可横向扩展,多节点部署
主要用于管理客户端的连接、消息路由与转发
- 启动参数:
gate --path=./examples/config/profile-gc.json --node=gc-gate-1
0x07 启动game节点
game节点为具体的游戏逻辑业务,根据业务需求可多节点部署
在分服的游戏中可提供游戏内的各种逻辑实现
- 启动参数:
game --path=./examples/config/profile-gc.json --node=10001
测试
- 使用go实现客户端,通过tcp协议连接gate网关进行压力测试
- 使用h5实现客户端,通过websocket协议连接gate网关进行功能的展示
启动压测机器人
- 找到
examples/demo_game_cluster/robot_client/main.go
文件,并执行
- 机器人执行逻辑为:
注册帐号
,登陆获取token
、连接网关
、用户登录游戏服
、查看角色
、创建角色
、进入角色
- 默认设定为创建1000个帐号,可自行调整
maxRobotNum
参数进行测试
- 执行完成后,从game节点的
Console
可以查看到onlineCount = 10000
字样,表示1万帐号已经进入游戏
启动h5客户端
- 直接访问
http://127.0.0.1
,按照界面步骤提示操作
源码讲解
运行截图
Directories
¶
internal
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Click to show internal directories.
Click to hide internal directories.