dawn

package module
v1.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2026 License: MIT Imports: 21 Imported by: 0

README

dawn 基于Go语言开发的高性能分布式游戏服务器框架

1.介绍

是一款基于Go语言开发的轻量级、高性能分布式游戏服务器框架。 其中,模块设计方面借鉴了kratos的模块设计思路,旨在为游戏服务器开发提供完善、高效、优雅、标准化的解决方案。 框架自创建至今已在多个企业级游戏项目中上线实践过,稳定性有充分的保障。

架构图

2.优势
  • 💰 免费性:框架遵循MIT协议,完全开源免费。
  • 💡 简单性:架构简单,源码简洁易理解。
  • 🚠 便捷性:仅暴露必要的调用接口,减轻开发者的心智负担。
  • 🚀 高性能:框架原生实现集群通信方案,普通机器单线程也能轻松实现20W的TPS。
  • 🧊 标准化:框架原生提供标准化的开发规范,无论多么复杂的项目也能轻松应对。
  • ✈️ 高效性:框架原生提供tcp、kcp、ws等服务器,方便开发者快速构建各种类型的网关服务器。
  • ⚖️ 稳定性:所有发布的正式版本均已通过内部真实业务的严格测试,具备较高的稳定性。
  • 🎟️ 扩展性:采用良好的接口设计,方便开发者设计实现自有功能。
  • 🔑 平滑性:引入信号量,通过控制服务注册中心来实现优雅地滚动更新。
  • 🔩 扩容性:通过优雅的路由分发机制,理论上可实现无限扩容。
  • 🔧 易调试:框架原生提供了tcp、kcp、ws等客户端,方便开发者进行独立的调试全流程调试。
  • 🧰 可管理:提供完善的后台管理接口,方便开发者快速实现自定义的后台管理功能。
3.功能
  • 网关:支持tcp、kcp、ws等协议的网关服务器。
  • 日志:支持console、file、aliyun、tencent等多种日志组件。
  • 注册:支持consul、etcd、nacos等多种服务注册中心。
  • 协议:支持json、protobuf、msgpack等多种通信协议。
  • 配置:支持consul、etcd、nacos等多种配置中心;并支持json、yaml、toml、xml等多种文件格式。
  • 通信:支持grpc、rpcx等多种高性能通信方案。
  • 重启:支持服务器的平滑重启。
  • 事件:支持redis、nats、kafka、rabbitMQ等事件总线实现方案。
  • 加密:支持rsa、ecc等多种加密方案。
  • 服务:支持grpc、rpcx等多种微服务解决方案。
  • 灵活:支持单体、分布式等多种架构方案。
  • Web:提供http协议的fiber服务器及swagger文档解决方案。
  • 工具:提供脚手架工具箱,可快速构建集群项目。
  • 缓存:支持redis、memcache等多种常用的缓存方案。
  • Actor:提供完善actor模型解决方案。
  • 分布式锁:支持redis、memcache等多种分布式锁解决方案。
4.下一期新功能规划
  • 分布式任务调度系统
5.特殊说明

在dawn交流群中经常有小伙伴提及到Gate、Node、Mesh之间到底是个什么关系,这里就做一个统一的解答

  • Gate:网关服,主要用于管理客户端连接,接收客户端的路由消息,并分发路由消息到不同的的Node节点服。
  • Node: 节点服,作为整个集群系统的核心组件,主要用于核心逻辑业务的编写。Node节点服务可以根据业务需要做成有状态或无状态的节点,当作为无状态的节点时,Node节点与Mesh微服务基本无异;但当Node节点作为有状态节点时,Node节点便不能随意更新进行重启操作。故而Node与Mesh分离的业务场景的价值就体现出来了。
  • Mesh:微服务,主要用于无状态的业务逻辑编写。Mesh能做的功能Node一样可以完成,如何选择完全取决于自身业务场景,开发者可以根据自身业务场景灵活搭配。
6.通信协议

在dawn框架中,通信协议统一采用size+header+route+seq+message的格式:

1.数据包

 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
+---------------------------------------------------------------+-+-------------+-------------------------------+-------------------------------+
|                              size                             |h|   extcode   |             route             |              seq              |
+---------------------------------------------------------------+-+-------------+-------------------------------+-------------------------------+
|                                                                message data ...                                                               |
+-----------------------------------------------------------------------------------------------------------------------------------------------+

2.心跳包

 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
+---------------------------------------------------------------+-+-------------+---------------------------------------------------------------+
|                              size                             |h|   extcode   |                      heartbeat time (ns)                      |
+---------------------------------------------------------------+-+-------------+---------------------------------------------------------------+

size: 4 bytes

  • 包长度位
  • 固定长度为4字节,且不可修改

header: 1 bytes

h: 1 bit

  • 心跳标识位
  • %x0 表示数据包
  • %x1 表示心跳包

extcode: 7 bit

  • 扩展操作码
  • 暂未明确定义具体操作码

route: 1 bytes | 2 bytes | 4 bytes

  • 消息路由
  • 默认采用2字节,可通过打包器配置packet.routeBytes进行修改
  • 不同的路由对应不同的业务处理流程
  • 心跳包无消息路由位
  • 此参数由业务打包器打包,服务器开发者和客户端开发者均要关心此参数

seq: 0 bytes | 1 bytes | 2 bytes | 4 bytes

  • 消息序列号
  • 默认采用2字节,可通过打包器配置packet.seqBytes进行修改
  • 可通过将打包器配置packet.seqBytes设置为0来屏蔽使用序列号
  • 消息序列号常用于请求/响应模型的消息对儿的确认
  • 心跳包无消息序列号位
  • 此参数由业务打包器packet.Packer打包,服务器开发者和客户端开发者均要关心此参数

message data: n bytes

  • 消息数据
  • 心跳包无消息数据
  • 此参数由业务打包器packet.Packer打包,服务器开发者和客户端开发者均要关心此参数

heartbeat time: 8 bytes

  • 心跳数据
  • 数据包无心跳数据
  • 上行心跳包无需携带心跳数据,下行心跳包默认携带8 bytes的服务器时间(ns),可通过网络库配置进行设置是否携带下行包时间信息
  • 此参数由网络框架层自动打包,服务端开发者不关注此参数,客户端开发者需关注此参数
7.相关工具链

1.安装protobuf编译器(使用场景:开发mesh微服务)

  • Linux, using apt or apt-get, for example:
$ apt install -y protobuf-compiler
$ protoc --version  # Ensure compiler version is 3+
  • MacOS, using Homebrew:
$ brew install protobuf
$ protoc --version  # Ensure compiler version is 3+
  • Windows, download from Github:

2.安装protobuf go代码生成工具(使用场景:开发mesh微服务)

go install google.golang.org/protobuf/cmd/protoc-gen-go@latest

3.安装grpc代码生成工具(使用场景:使用GRPC组件开发mesh微服务)

go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

4.安装rpcx代码生成工具(使用场景:使用RPCX组件开发mesh微服务)

go install github.com/rpcxio/protoc-gen-rpcx@latest

5.安装gorm dao代码生成工具(使用场景:使用GORM作为数据库orm)

go install github.com/dawnsgo/gorm_dao_generator@latest

6.安装mongo dao代码生成工具(使用场景:使用MongoDB作为数据库orm)

go install github.com/dawnsgo/mongo_dao_generator@latest
8.配置中心

1.功能介绍

配置中心主要定位于业务的配置管理,提供快捷灵活的配置方案。支持完善的读取、修改、删除、热更新等功能。

2.支持组件

9.注册中心

1.功能介绍

注册中心用于集群实例的服务注册和发现。支撑整个集群的无感知停服、重启、动态扩容等功能。

2.支持组件

10.网络模块

1.功能介绍

网络模块主要以组件的形式集成于网关模块,为网关提供灵活的网络通信支持。

2.支持组件

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCache added in v1.0.9

func GetCache() cache.Cache

GetCache 获取缓存

func GetConfigurator added in v1.0.9

func GetConfigurator() config.Configurator

GetConfigurator 获取配置器

func GetEventbus added in v1.0.9

func GetEventbus() eventbus.Eventbus

GetEventbus 获取事件总线

func GetLockMaker added in v1.0.9

func GetLockMaker() lock.Maker

GetLockMaker 获取分布式锁制造器

func GetLogger added in v1.0.9

func GetLogger() log.Logger

GetLogger 获取日志记录器

func GetTaskPool added in v1.0.9

func GetTaskPool() task.Pool

GetTaskPool 获取任务池

func SetCache added in v1.0.9

func SetCache(ca cache.Cache)

SetCache 设置缓存

func SetConfigurator added in v1.0.9

func SetConfigurator(configurator config.Configurator)

SetConfigurator 设置配置器

func SetEventbus added in v1.0.9

func SetEventbus(eb eventbus.Eventbus)

SetEventbus 设置事件总线

func SetLockMaker added in v1.0.9

func SetLockMaker(maker lock.Maker)

SetLockMaker 设置分布式锁制造器

func SetLogger added in v1.0.9

func SetLogger(logger log.Logger)

SetLogger 设置日志记录器

func SetTaskPool added in v1.0.9

func SetTaskPool(pool task.Pool)

SetTaskPool 设置任务池

Types

type Container

type Container struct {
	// contains filtered or unexported fields
}

Container 服务容器

func NewContainer

func NewContainer(opts ...ContainerOption) *Container

NewContainer 创建一个容器

func (*Container) Add

func (c *Container) Add(components ...component.Component)

Add 添加组件

func (*Container) Context added in v1.0.9

func (c *Container) Context() *Context

Context 获取容器的上下文

func (*Container) Serve

func (c *Container) Serve(once ...bool)

Serve 启动容器

func (*Container) ServeWithError added in v1.0.9

func (c *Container) ServeWithError(once ...bool) error

ServeWithError 启动容器并返回错误(不会调用 log.Fatal) 适用于测试或需要自定义错误处理的场景

type ContainerError added in v1.0.9

type ContainerError struct {
	Phase     string // 错误发生的阶段: init, start, close, destroy
	Component string // 出错的组件名称
	Err       error  // 原始错误
	Retryable bool   // 是否可重试
}

ContainerError 容器错误类型

func (*ContainerError) Error added in v1.0.9

func (e *ContainerError) Error() string

func (*ContainerError) Unwrap added in v1.0.9

func (e *ContainerError) Unwrap() error

type ContainerOption added in v1.0.9

type ContainerOption func(*Container)

ContainerOption 容器配置选项

func WithCloseTimeout added in v1.0.9

func WithCloseTimeout(timeout time.Duration) ContainerOption

WithCloseTimeout 设置组件关闭超时时间

func WithContext added in v1.0.9

func WithContext(ctx *Context) ContainerOption

WithContext 使用指定的上下文

func WithDestroyTimeout added in v1.0.9

func WithDestroyTimeout(timeout time.Duration) ContainerOption

WithDestroyTimeout 设置组件销毁超时时间

func WithErrorHandler added in v1.0.9

func WithErrorHandler(handler ErrorHandler) ContainerOption

WithErrorHandler 设置自定义错误处理器

func WithErrorRecoveryStrategy added in v1.0.9

func WithErrorRecoveryStrategy(strategy ErrorRecoveryStrategy) ContainerOption

WithErrorRecoveryStrategy 设置错误恢复策略

func WithExitOnError added in v1.0.9

func WithExitOnError(exit bool) ContainerOption

WithExitOnError 设置发生错误时是否退出程序(默认 true)

func WithOrderedClose added in v1.0.9

func WithOrderedClose(ordered bool) ContainerOption

WithOrderedClose 设置是否按顺序关闭组件(根据优先级)

func WithRetryConfig added in v1.0.9

func WithRetryConfig(config *RetryConfig) ContainerOption

WithRetryConfig 设置重试配置

type Context added in v1.0.9

type Context struct {
	// contains filtered or unexported fields
}

Context 框架上下文,统一管理核心依赖 支持多实例场景(如测试),同时保持向后兼容

并发安全设计:

  • Getter 方法使用 atomic.Value,无锁读取,高性能
  • Setter 方法使用 sync.Mutex,保证写入原子性
  • 适合读多写少的场景(Setter 只在初始化时调用)

func Default added in v1.0.9

func Default() *Context

Default 获取默认上下文

func NewContext added in v1.0.9

func NewContext() *Context

NewContext 创建新的上下文

func (*Context) AttachToPackages added in v1.0.9

func (c *Context) AttachToPackages()

AttachToPackages 将 Context 关联到各包的全局变量 调用后,各包的 Get 函数会优先从 Context 读取

func (*Context) Cache added in v1.0.9

func (c *Context) Cache() cache.Cache

Cache 获取缓存(实现 cache.ContextProvider 接口)

func (*Context) Close added in v1.0.9

func (c *Context) Close() error

Close 关闭上下文中的所有资源

func (*Context) Configurator added in v1.0.9

func (c *Context) Configurator() config.Configurator

Configurator 获取配置器(实现 config.ContextProvider 接口)

func (*Context) DetachFromPackages added in v1.0.9

func (c *Context) DetachFromPackages()

DetachFromPackages 解除 Context 与各包的关联

func (*Context) Eventbus added in v1.0.9

func (c *Context) Eventbus() eventbus.Eventbus

Eventbus 获取事件总线(实现 eventbus.ContextProvider 接口)

func (*Context) IsAttached added in v1.0.9

func (c *Context) IsAttached() bool

IsAttached 检查是否已关联到各包

func (*Context) LockMaker added in v1.0.9

func (c *Context) LockMaker() lock.Maker

LockMaker 获取分布式锁制造器(实现 lock.ContextProvider 接口)

func (*Context) Logger added in v1.0.9

func (c *Context) Logger() log.Logger

Logger 获取日志记录器(实现 log.ContextProvider 接口)

func (*Context) SetCache added in v1.0.9

func (c *Context) SetCache(ca cache.Cache)

SetCache 设置缓存

func (*Context) SetConfigurator added in v1.0.9

func (c *Context) SetConfigurator(configurator config.Configurator)

SetConfigurator 设置配置器

func (*Context) SetEventbus added in v1.0.9

func (c *Context) SetEventbus(eb eventbus.Eventbus)

SetEventbus 设置事件总线

func (*Context) SetLockMaker added in v1.0.9

func (c *Context) SetLockMaker(maker lock.Maker)

SetLockMaker 设置分布式锁制造器

func (*Context) SetLogger added in v1.0.9

func (c *Context) SetLogger(logger log.Logger)

SetLogger 设置日志记录器

func (*Context) SetTaskPool added in v1.0.9

func (c *Context) SetTaskPool(pool task.Pool)

SetTaskPool 设置任务池

func (*Context) TaskPool added in v1.0.9

func (c *Context) TaskPool() task.Pool

TaskPool 获取任务池(实现 task.ContextProvider 接口)

type ErrorHandler added in v1.0.9

type ErrorHandler func(err error)

ErrorHandler 错误处理器类型

type ErrorRecoveryStrategy added in v1.0.9

type ErrorRecoveryStrategy int

ErrorRecoveryStrategy 错误恢复策略

const (
	// ErrorRecoveryNone 不进行恢复,直接失败
	ErrorRecoveryNone ErrorRecoveryStrategy = iota
	// ErrorRecoveryRetry 重试策略
	ErrorRecoveryRetry
	// ErrorRecoverySkip 跳过当前组件继续执行
	ErrorRecoverySkip
	// ErrorRecoveryCallback 回调自定义处理函数
	ErrorRecoveryCallback
)

type OrderedComponent added in v1.0.9

type OrderedComponent interface {
	component.Component
	// Priority 返回组件优先级,数值越小优先级越高(越先关闭)
	Priority() int
	// Dependencies 返回依赖的组件名称列表
	Dependencies() []string
}

OrderedComponent 有序组件接口,支持定义组件关闭顺序

type RetryConfig added in v1.0.9

type RetryConfig struct {
	MaxRetries int           // 最大重试次数
	RetryDelay time.Duration // 重试间隔
	Backoff    float64       // 退避系数(每次重试间隔乘以该系数)
}

RetryConfig 重试配置

func DefaultRetryConfig added in v1.0.9

func DefaultRetryConfig() *RetryConfig

DefaultRetryConfig 默认重试配置

Directories

Path Synopsis
redis module
gate
Package gate 提供网关组件,负责客户端连接管理和消息转发。
Package gate 提供网关组件,负责客户端连接管理和消息转发。
node
Package node 提供节点组件,负责业务逻辑处理和消息路由。
Package node 提供节点组件,负责业务逻辑处理和消息路由。
http module
Package config 提供统一的配置管理接口和实现。
Package config 提供统一的配置管理接口和实现。
nacos module
core
net
tls
Package encoding 提供统一的编解码器接口和多种编码格式支持。
Package encoding 提供统一的编解码器接口和多种编码格式支持。
proto
Package proto 提供 Protocol Buffers 格式的编解码器实现。
Package proto 提供 Protocol Buffers 格式的编解码器实现。
xml
redis module
internal
redis module
redis module
log
Package network 定义了网络通信的核心接口,包括客户端、服务器和连接。
Package network 定义了网络通信的核心接口,包括客户端、服务器和连接。
ws module
Package registry 提供服务注册发现的统一接口和实现。
Package registry 提供服务注册发现的统一接口和实现。
nacos module
grpc module
utils
xos

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL