3 Star 1 Fork 1

NightTC/Gobige

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Gobige Framework

Gobige 是一个高性能、模块化的分布式游戏服务器框架,旨在简化游戏服务器的开发和部署。它支持多种服务器类型的协同工作,提供了灵活的消息路由和高效的 RPC 调用机制。

功能特点

  • 模块化设计:支持按需加载模块,方便扩展和维护。
  • 高性能消息处理:基于 MsgHandler 的消息分发机制,支持普通消息、RPC 消息和服务器间消息。
  • 分布式架构:支持多服务器协作,提供负载均衡和服务发现功能。
  • 灵活的协议支持:基于 Protobuf 定义协议,支持客户端与服务器、服务器之间的通信。
  • 易于扩展:通过注册消息处理器和自定义逻辑,快速实现业务需求。

目录结构

gobige/
├── dbmanager/          # 数据库管理模块
├── entity/             # 实体管理模块
├── global/             # 全局配置和常量
├── logger/             # 日志模块
├── msgdef/             # 消息定义和 Protobuf 文件
├── msghandler/         # 消息处理模块
├── service/            # 服务管理模块
├── sess/               # 会话管理模块
└── tools/              # 工具和辅助脚本

快速开始

1. 克隆代码库

git clone https://github.com/your-repo/gobige.git
cd gobige

2. 安装依赖

确保已安装 Go 环境,运行以下命令安装依赖:

go mod tidy

3. 编译和运行

使用以下命令编译并运行服务器:

go build -o gobige-server main.go
./gobige-server

4. 配置文件

配置文件说明

server.json 是 Gobige 框架的核心配置文件,包含以下主要字段:

  • Config:全局配置部分。

    • Recover:是否启用 recover 机制,默认为 true
    • Debug:是否启用调试模式,默认为 false
    • IsCreateDB:是否创建数据库表,默认为 false
    • EncryptEnabled:是否启用加密,默认为 false
    • SessTicker:服务器连接心跳时间(秒)。
    • SessTimeout:服务器连接超时时间(秒)。
    • LogFile:日志配置文件路径。
    • Etcd:ETCD 配置。
      • Addrs:ETCD 集群地址列表。
      • DialTimeout:连接超时时间(秒)。
      • LeaseTTL:租约时间(秒)。
  • [ServerName]:服务器实例配置部分。

    • ServerID:服务器唯一标识符。
    • TickCount:服务器帧间隔时间(毫秒)。
    • DebugAddr:调试模式监听地址。
    • MysqlConfig:MySQL 数据库配置。
      • MySQLUser:用户名。
      • MySQLPwd:密码。
      • MySQLAddr:数据库地址。
      • MySQLDB:数据库名称。
      • MySQLMaxIdleConns:最大空闲连接数。
      • MySQLMaxOpenConns:最大打开连接数。
      • MySQLMaxLifetime:连接最大存活时间(秒)。
    • Listen:监听配置。
      • Inner:内部监听地址。
      • Outer:外部监听地址。
      • Http:HTTP 监听地址。
    • RedisList:Redis 配置列表。

Redis 配置说明

Redis 配置在 server.json 文件的 RedisList 字段中定义,每个 Redis 配置包含以下字段:

  • Name:Redis 实例的名称,用于标识。
  • Addr:Redis 服务器地址。
  • Index:Redis 数据库索引。
  • MaxIdle:最大空闲连接数。
  • IdleTimeout:空闲连接超时时间(秒)。
  • MaxActive:最大活跃连接数。
  • Password:Redis 访问密码。

Redis 使用示例

  1. 定义 Redis 实例
    server.json 中定义 Redis 实例,例如:

    "RedisList": [
        {
            "Name": "LoginRedis",
            "Addr": "127.0.0.1:6379",
            "Index": 0,
            "MaxIdle": 10,
            "IdleTimeout": 300,
            "MaxActive": 20,
            "Password": ""
        }
    ]
    
  2. 初始化 Redis 实例
    在服务启动时,通过 global.SetRedisByName 注册 Redis 实例:

    rd := redislib.NewRedisAccess(
        redislib.RedisSetAddr("127.0.0.1:6379"),
        redislib.RedisSetIndexDB(0),
        redislib.RedisSetMaxIdle(10),
        redislib.RedisSetIdleTimeout(300*time.Second),
        redislib.RedisSetMaxActive(20),
    )
    global.SetRedisByName("LoginRedis", rd)
    
  3. 获取 Redis 连接
    使用 global.GetRedisAccess 获取 Redis 实例,并通过 GetConn 方法获取连接:

    conn := global.GetRedisAccess("LoginRedis").GetConn()
    defer conn.Close()
    
    // 使用 Redis 连接执行操作
    conn.Set(context.Background(), "key", "value", 0)
    
  4. 使用 Redis 工具类
    框架提供了工具类(如 LoginPlayerGateSession)封装了 Redis 操作。例如:

    player := redisutil.NewLoginPlayer(12345)
    player.SaveInfo(map[string]interface{}{
        "playerid": 12345,
        "name":     "test_player",
    })
    

使用 gen-gobige-json 工具生成配置文件

  1. 确保已安装 Go 环境,并克隆 gen-gobige-json 工具代码库:

    git clone https://gitee.com/night-tc/gen-gobige-json.git
    cd gen-gobige-json
    
  2. 修改 server.tmpl 模板文件,根据实际需求调整配置。

  3. 运行以下命令生成 server.json 文件:

    go run main.go -p ../res/config/server.tmpl -o ../res/config/server.json
    
    • -p 参数指定模板文件路径,例如 ../res/config/server.tmpl
    • -o 参数指定输出文件路径,例如 ../res/config/server.json
  4. 将生成的 server.json 文件放置到服务器运行目录下。

  5. 示例模板文件 server.tmpl 的内容如下:

    {
        "Config": {
            "Recover": true,
            "Debug": false,
            "IsCreateDB": false,
            "EncryptEnabled": false,
            "SessTicker": 60,
            "SessTimeout": 300,
            "LogFile": "log.toml",
            "Etcd": {
                "Addrs": ["127.0.0.1:2379"],
                "DialTimeout": 5,
                "LeaseTTL": 60
            }
        },
        "LoginServer": {
            "ServerID": 1,
            "TickCount": 100,
            "DebugAddr": "127.0.0.1:6060",
            "MysqlConfig": {
                "MySQLUser": "root",
                "MySQLPwd": "password",
                "MySQLAddr": "127.0.0.1:3306",
                "MySQLDB": "gobige",
                "MySQLMaxIdleConns": 10,
                "MySQLMaxOpenConns": 100,
                "MySQLMaxLifetime": 3600
            },
            "Listen": {
                "Inner": {
                    "Listen": "127.0.0.1:8000",
                    "Addr": "127.0.0.1",
                    "PortMin": 8000,
                    "PortMax": 8000,
                    "MaxConn": 1000
                },
                "Outer": {
                    "Listen": "0.0.0.0:9000",
                    "Addr": "127.0.0.1",
                    "PortMin": 9000,
                    "PortMax": 9000,
                    "MaxConn": 1000
                }
            },
            "RedisList": [
                {
                    "Name": "LoginRedis",
                    "Addr": "127.0.0.1:6379",
                    "Index": 0,
                    "MaxIdle": 10,
                    "IdleTimeout": 300,
                    "MaxActive": 20,
                    "Password": ""
                }
            ]
        }
    }
    

5. 定义协议

msgdef/protomsg 目录下定义 Protobuf 文件,运行工具生成对应的 Go 文件:

protoc --go_out=. --go-grpc_out=. your_proto_file.proto

6. 示例项目

可以参考 Gobige Demo 项目,了解如何使用 Gobige 框架构建分布式游戏服务器。

贡献

欢迎提交 Issue 和 Pull Request!如有任何问题,请联系维护者。

许可证

Gobige 使用 MIT License 进行许可。

MIT License Copyright (c) 2023 buguang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Gobige 是一个高性能、模块化的分布式游戏服务器框架,旨在简化游戏服务器的开发和部署。它支持多种服务器类型的协同工作,提供了灵活的消息路由和高效的 RPC 调用机制。 展开 收起
Go 等 6 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/night-tc/gobige.git
git@gitee.com:night-tc/gobige.git
night-tc
gobige
Gobige
master

搜索帮助