diff --git a/README.md b/README.md index 9f967e5a9..2e5ecac12 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ 梦工厂服务端项目 ### 项目构架说明 -``` +```shell .vscode #vscode 编辑器环境配置项 包含服务调试运行配置 bin #服务运行和发布环境 包含服务的执行文件以及相关依赖配置 conf @@ -45,9 +45,114 @@ sys #公用系统工具集 utils #公用方法工具集 ``` + +### 如何快速开发模块 +- 定义模块类型名称 在comm/core.go 中定义定义自己的业务模块名称 + ```go + const ( + SM_GateModule core.M_Modules = "gateway" //gate模块 网关服务模块 + SM_WebModule core.M_Modules = "web" //web模块 + SM_UserModule core.M_Modules = "user" //用户模块 + SM_PackModule core.M_Modules = "pack" //背包模块 + SM_MailModule core.M_Modules = "mail" //邮件模块 + SM_FriendModule core.M_Modules = "friend" //好友模块 + //定义自己的模块名称 + ) + ``` +- 创建自己的模块目录 在modules/ 目录下创建自己的模块目录 +- 定义自己的模块对象 在modules/自己的模块目录 创建module.go 文件 + ```go + package pack //修改成自己的包名 + + import ( + "go_dreamfactory/comm" + "go_dreamfactory/modules" + + "go_dreamfactory/lego/core" + ) + + /* + 模块名:Pack + 描述:背包系统模块 + 开发:李伟 + */ + func NewModule() core.IModule { + m := new(Pack) + return m + } + + //换成自己的模块对象 + type Pack struct { + modules.ModuleBase + } + + func (this *Pack) GetType() core.M_Modules { + return comm.SM_PackModule //换成自己的模块名 + } + + //组装组件的地方 + func (this *Pack) OnInstallComp() { + this.ModuleBase.OnInstallComp() + } + + ``` +- 定义自己的用户请求处理组件 在modules/自己的模块目录 创建api_comp.go 文件 名字根据自己的情况定义, + ```go + //api_comp.go + //必须继承 modules.MComp_GateComp + type Api_Comp struct { + modules.MComp_GateComp + } + + //定义具体的消息处理函数 + func (this *Api_Comp) QueryUserPackReq(ctx context.Context, session comm.IUserSession, req *pb.QueryUserPackReq) (err error) { + + } + + + + //将组件装备到模块上 + //module.go + func (this *Pack) OnInstallComp() { + this.ModuleBase.OnInstallComp() + this.api_comp = this.RegisterComp(new(Api_Comp)).(*Api_Comp) + } + ``` +- 定义自己的协议 在pb/proto 下定义自己的proto协议文件,并将文件加入到pb_2.7.py编译工具中 + ```py + buildProto('./pb/proto','./pb','comm') + buildProto('./pb/proto','./pb','errorcode') + buildProto('./pb/proto','./pb','user_db') + buildProto('./pb/proto','./pb','user_msg') + buildProto('./pb/proto','./pb','pack_db') + buildProto('./pb/proto','./pb','pack_msg') + //加入知己的协议文件 + ``` +- 将模块装备到worker服务容器中 services/worker/main.go + ```go + func main() { + flag.Parse() + s := NewService( + rpcx.SetConfPath(*conf), + rpcx.SetVersion("1.0.0.0"), + ) + s.OnInstallComp( //装备组件 + services.NewGateRouteComp(), + ) + lego.Run(s, //运行模块 + // web.NewModule(), + user.NewModule(), + pack.NewModule(), + mail.NewModule(), + //在这里吧自己的模块注册进去 + ) + + } + ``` + ### 服务容器的定义说明 - service 容器定义 项目可更具情况组合拆分业务到具体的服务中 -``` +```go var ( conf = flag.String("conf", "./conf/worker.yaml", "获取需要启动的服务配置文件") //启动服务的Id ) @@ -101,7 +206,7 @@ func (this *Service) InitSys() { ### 模块容器的定义说明 pack/module.go 每个模块目录必须有一个模块对象 一般将相同类型的业务分为一个模块中实现, 模块也只是一个容器,模块中具体实现功能的单元为模块组件对象,模块启动是装备具体的业务组件集合实现相关的业务功能 -``` +```go func NewModule() core.IModule { m := new(Pack) return m @@ -133,7 +238,7 @@ func (this *Pack) OnInstallComp() { } ``` pack/api_comp.go 背包用户api组件 用于接收处理用具背包请求协议 -``` +```go //Api 组件继承于模块网关组件 modules.MComp_GateComp 内部通过反射机制将当前组件内处理协议的接口注册到 comp_gateroute.go 网关服务组件中,之后服务组件就可以把接收到的用户消息分发给当前api组件 type Api_Comp struct { modules.MComp_GateComp @@ -176,7 +281,7 @@ func (this *Api_Comp) QueryUserPackReq(ctx context.Context, session comm.IUserSe ### 客户端发送消息到服务器处理的流程介绍 - 前后端通用协议结构定义 pb/proto/comm.proto - ``` + ```go message UserMessage { string MainType =1; //消息的主id 默认为具体的业务模块名称 string SubType = 2; //消息的副id 默认为具体处理消息的函数名 @@ -185,7 +290,7 @@ func (this *Api_Comp) QueryUserPackReq(ctx context.Context, session comm.IUserSe } ``` - 网关服务接收用户协议的处理 modules/gateway/agent.go - ``` + ```go //用户代理对象 type Agent struct { gateway IGateway @@ -247,7 +352,7 @@ func (this *Api_Comp) QueryUserPackReq(ctx context.Context, session comm.IUserSe ``` - Worker 服务如何接受用户的消息 services/comp_gateroute.go - ``` + ```go //网关服务组件启动时会注册Rpc方法 接收网关服务那边发过来的用户消息 func (this *SComp_GateRouteComp) Start() (err error) { this.service.RegisterFunctionName(string(comm.Rpc_GatewayRoute), this.ReceiveMsg) //注册网关路由接收接口 @@ -278,7 +383,7 @@ func (this *Api_Comp) QueryUserPackReq(ctx context.Context, session comm.IUserSe } ``` - 业务模块 如何接受处理用户的消息 - ``` + ```go #模块需要接受处理用户发送过来的消息需要装备一个继承modules/gate_comp.go 的模块网关组件,此组件启动是会自动把自身定义的消息处理函数注册到上面提到的服务网关组件services/comp_gateroute.go 中去,实现可以参考pack/api_comp.go 的定义 /* 模块 网关组件 接收处理用户传递消息 @@ -350,7 +455,7 @@ func (this *Api_Comp) QueryUserPackReq(ctx context.Context, session comm.IUserSe } ``` - 消息处理函数如何给用户回复消息 - ``` + ```go //每一个消息处理函数我们都会传递一个 IUserSession的对象 开发人员可以使用此对象向当前用户回复消息以及关闭连接等操作 type IUserSession interface { GetSessionId() string @@ -391,7 +496,7 @@ func (this *Api_Comp) QueryUserPackReq(ctx context.Context, session comm.IUserSe } ``` - 消息处理函数如何给其他用户发送消息 - ``` + ```go modules/modulebase.go 是所有业务模块的基础模块,大家在定义业务模块时注意继承此模块,此模块封装有给指定用户发送消息以及给多个用户同时发送消息的接口,后续一些通用的接口都会封装到这一层 方便上层业务模块快速调用,组件可以在Init方法中通过断言的方式获取到模块对象,在调用底层接口即可 //向目标用户发送消息 @@ -436,6 +541,4 @@ func (this *Api_Comp) QueryUserPackReq(ctx context.Context, session comm.IUserSe } return } - - ``` \ No newline at end of file