补充项目接口注释说明

This commit is contained in:
liwei1dao 2022-06-15 13:35:36 +08:00
parent ca0a418b4f
commit 35be79ffc7
8 changed files with 89 additions and 39 deletions

View File

@ -71,22 +71,27 @@ func (this *RPCXService) Init(service core.IService) (err error) {
return this.ServiceBase.Init(service)
}
//初始化内置系统
func (this *RPCXService) InitSys() {
//初始化日志系统
if err := log.OnInit(this.opts.Setting.Sys["log"]); err != nil {
panic(fmt.Sprintf("Sys log Init err:%v", err))
} else {
log.Infof("Sys log Init success !")
}
//初始化事件系统
if err := event.OnInit(this.opts.Setting.Sys["event"]); err != nil {
log.Panicf(fmt.Sprintf("Sys event Init err:%v", err))
} else {
log.Infof("Sys event Init success !")
}
//初始化rpcx系统
if err := rpcx.OnInit(this.opts.Setting.Sys["rpcx"], rpcx.SetServiceTag(this.GetTag()), rpcx.SetServiceId(this.GetId()), rpcx.SetServiceType(this.GetType()), rpcx.SetServiceVersion(this.GetVersion()), rpcx.SetServiceAddr(fmt.Sprintf("%s:%d", this.GetIp(), this.GetPort()))); err != nil {
log.Panicf(fmt.Sprintf("Sys rpcx Init err:%v", err))
} else {
log.Infof("Sys rpcx Init success !")
}
//服务启动完毕后 启动rpcx系统
event.Register(core.Event_ServiceStartEnd, func() { //阻塞 先注册服务集群 保证其他服务能及时发现
if err := rpcx.Start(); err != nil {
log.Panicf(fmt.Sprintf("Sys rpcx Start err:%v", err))

View File

@ -4,14 +4,18 @@ import (
"go_dreamfactory/lego/core"
)
//基础模块对象
type ModuleBase struct {
comps []core.IModuleComp
}
//此方法可上层重构 返回自定义的参数载体接口对象 用于底层自动序列化参数使用
func (this *ModuleBase) NewOptions() (options core.IModuleOptions) {
return new(ModuleOptions)
}
//模块初始化接口 上层实现可以更具需求重构扩展接口 通过此接口模块可以拿到 当前运行的服务对象以及实现的模块对象和模块参数对象
//此函数底层实现 初始化模块的组件系统
func (this *ModuleBase) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
module.OnInstallComp()
for _, v := range this.comps {
@ -23,10 +27,12 @@ func (this *ModuleBase) Init(service core.IService, module core.IModule, options
return
}
//暗转组件接口 上层模块实现 徐涛重构此方法 注册模块组件
func (this *ModuleBase) OnInstallComp() {
}
//模块启动接口 启动时可以处理一些外部调用相关的事情
func (this *ModuleBase) Start() (err error) {
for _, v := range this.comps {
err = v.Start()
@ -37,11 +43,13 @@ func (this *ModuleBase) Start() (err error) {
return
}
//模块独立协程 模块可以更具需要自行重构
func (this *ModuleBase) Run(closeSig chan bool) (err error) {
return
}
//模块消息接口
func (this *ModuleBase) Destroy() (err error) {
for _, v := range this.comps {
err = v.Destroy()
@ -52,6 +60,7 @@ func (this *ModuleBase) Destroy() (err error) {
return
}
//注册模块组件
func (this *ModuleBase) RegisterComp(comp core.IModuleComp) interface{} {
this.comps = append(this.comps, comp)
return comp

View File

@ -4,16 +4,20 @@ import (
"go_dreamfactory/lego/core"
)
//模块基础组件
type ModuleCompBase struct{}
//模块基础组件 初始化接口 组件可以通过此接口 获取到当前运行的服务对象以及组件的所属模块对象和上层组件的实现对象以及 模块的配置参数对象
func (this *ModuleCompBase) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
return
}
//组件启动接口
func (this *ModuleCompBase) Start() (err error) {
return
}
//组件销毁接口
func (this *ModuleCompBase) Destroy() (err error) {
return
}

View File

@ -2,6 +2,7 @@ package cbase
import "go_dreamfactory/lego/utils/mapstructure"
//模块的基础参数对象设计 主要需要实现 LoadConfig 序列化参数接口
type ModuleOptions struct {
}

View File

@ -13,6 +13,7 @@ import (
"go_dreamfactory/lego/sys/log"
)
//模块的运行载体对象 管理模块的配置以及协程对象
type defaultModule struct {
seetring map[string]interface{}
mi core.IModule
@ -20,42 +21,49 @@ type defaultModule struct {
wg sync.WaitGroup
}
//模块的独立协程
func (this *defaultModule) run() {
this.wg.Add(1)
this.mi.Run(this.closeSig)
this.wg.Done()
}
//模块的销毁逻辑
func (this *defaultModule) destroy() (err error) {
defer lego.Recover(fmt.Sprintf("Module :%s destroy", this.mi.GetType()))
err = this.mi.Destroy()
if err != nil {
err = fmt.Errorf("关闭模块【%s】失败 err:%s", this.mi.GetType(), err.Error())
}
this.wg.Wait()
return
}
///基础服务对象
type ServiceBase struct {
closesig chan string
Service core.IService
comps map[core.S_Comps]core.IServiceComp
modules map[core.M_Modules]*defaultModule
closesig chan string //服务关闭信号管道
service core.IService //服务的上层实现对象
comps map[core.S_Comps]core.IServiceComp //组件列表
modules map[core.M_Modules]*defaultModule //模块列表
}
//服务初始化过程 完成 系统 和 服务组件的 初始化过程
func (this *ServiceBase) Init(service core.IService) (err error) {
this.closesig = make(chan string, 1)
this.Service = service
this.service = service
this.modules = make(map[core.M_Modules]*defaultModule)
this.Service.InitSys()
this.service.InitSys()
for _, v := range this.comps {
options := v.NewOptions()
if o, ok := service.GetSettings().Comps[string(v.GetName())]; ok {
options.LoadConfig(o)
}
err = v.Init(this.Service, v, options)
err = v.Init(this.service, v, options)
if err != nil {
return
}
}
log.Infof("服务[%s] 初始化完成!", this.Service.GetId())
log.Infof("服务[%s] 初始化完成!", this.service.GetId())
return nil
}
@ -70,6 +78,7 @@ func (this *ServiceBase) OnInstallComp(cops ...core.IServiceComp) {
}
}
//启动服务 完成服务组件的启动过程
func (this *ServiceBase) Start() (err error) {
for _, v := range this.comps {
err = v.Start()
@ -77,13 +86,14 @@ func (this *ServiceBase) Start() (err error) {
return
}
}
log.Infof("服务[%s:%s] 启动完成!", this.Service.GetId(), this.Service.GetVersion())
log.Infof("服务[%s:%s] 启动完成!", this.service.GetId(), this.service.GetVersion())
return
}
//运行模块
func (this *ServiceBase) Run(mod ...core.IModule) {
for _, v := range mod {
if sf, ok := this.Service.GetSettings().Modules[string(v.GetType())]; ok {
for _, v := range mod { //初始化模块对象 分配配置参数
if sf, ok := this.service.GetSettings().Modules[string(v.GetType())]; ok {
this.modules[v.GetType()] = &defaultModule{
seetring: sf,
mi: v,
@ -98,10 +108,10 @@ func (this *ServiceBase) Run(mod ...core.IModule) {
log.Warnf("注册模块【%s】 没有对应的配置信息", v.GetType())
}
}
for _, v := range this.modules {
for _, v := range this.modules { //序列化每一个模块的参数对象 完成模块的初始化 过程
options := v.mi.NewOptions()
if err := options.LoadConfig(v.seetring); err == nil {
err := v.mi.Init(this.Service, v.mi, options)
err := v.mi.Init(this.service, v.mi, options)
if err != nil {
log.Panicf(fmt.Sprintf("初始化模块【%s】错误 err:%v", v.mi.GetType(), err))
}
@ -109,17 +119,16 @@ func (this *ServiceBase) Run(mod ...core.IModule) {
log.Panicf(fmt.Sprintf("模块【%s】 Options:%v 配置错误 err:%v", v.mi.GetType(), v.seetring, err))
}
}
for _, v := range this.modules {
for _, v := range this.modules { //完成模块的启动过程
err := v.mi.Start()
if err != nil {
log.Panicf(fmt.Sprintf("启动模块【%s】错误 err:%v", v.mi.GetType(), err))
}
}
for _, v := range this.modules {
v.wg.Add(1)
for _, v := range this.modules { //启动模块的独立协程
go v.run()
}
event.TriggerEvent(core.Event_ServiceStartEnd) //广播事件
event.TriggerEvent(core.Event_ServiceStartEnd) //广播服务启动完毕事件
//监听外部关闭服务信号
c := make(chan os.Signal, 1)
//添加进程结束信号
@ -132,16 +141,18 @@ func (this *ServiceBase) Run(mod ...core.IModule) {
syscall.SIGQUIT) //用户发送QUIT字符(Ctrl+/)触发
select {
case sig := <-c:
log.Errorf("服务[%s] 关闭 signal = %v\n", this.Service.GetId(), sig)
log.Errorf("服务[%s] 关闭 signal = %v\n", this.service.GetId(), sig)
case <-this.closesig:
log.Errorf("服务[%s] 关闭\n", this.Service.GetId())
log.Errorf("服务[%s] 关闭\n", this.service.GetId())
}
}
//完毕服务模块接口
func (this *ServiceBase) Close(closemsg string) {
this.closesig <- closemsg
}
//服务关闭时的消息调用过程
func (this *ServiceBase) Destroy() (err error) {
for _, v := range this.modules {
v.closeSig <- true
@ -160,6 +171,7 @@ func (this *ServiceBase) Destroy() (err error) {
return
}
//获取当前服务下指定模块对象
func (this *ServiceBase) GetModule(ModuleName core.M_Modules) (module core.IModule, err error) {
if v, ok := this.modules[ModuleName]; ok {
return v.mi, nil
@ -168,6 +180,7 @@ func (this *ServiceBase) GetModule(ModuleName core.M_Modules) (module core.IModu
}
}
//获取当前服务下指定组件对象
func (this *ServiceBase) GetComp(CompName core.S_Comps) (comp core.IServiceComp, err error) {
if v, ok := this.comps[CompName]; ok {
return v, nil

View File

@ -4,21 +4,26 @@ import (
"go_dreamfactory/lego/core"
)
//服务基础组件
type ServiceCompBase struct {
}
//组件参数对象 更具服务组件的需求 可以重构此接口实现自定义参数对象的返回
func (this *ServiceCompBase) NewOptions() (options core.ICompOptions) {
return new(ServiceCompOptions)
}
//组件初始化接口对象
func (this *ServiceCompBase) Init(service core.IService, comp core.IServiceComp, options core.ICompOptions) (err error) {
return
}
//组件启动接口
func (this *ServiceCompBase) Start() (err error) {
return
}
//组件销毁接口
func (this *ServiceCompBase) Destroy() (err error) {
return
}

View File

@ -11,8 +11,8 @@ type SqlTable string //数据库表定义
type CustomRoute uint8 //自定义网关
const (
AutoIp = "0.0.0.0"
AllIp = "255.255.255.255"
AutoIp = "0.0.0.0" //自动ip 可以匹配任意ip地址
AllIp = "255.255.255.255" //多选ip 匹配全部ip
)
const ( //默认事件
@ -29,6 +29,7 @@ const (
S_Category_BusinessService S_Category = "BusinessService" //业务服务器
)
//服务配置数据接口定义
type ServiceSttings struct {
Id string //服务Id
Type string //服务类型 (相同的服务可以启动多个)
@ -41,6 +42,7 @@ type ServiceSttings struct {
Modules map[string]map[string]interface{} //服务模块配置
}
//基础服务的接口设计
type IService interface {
GetId() string //获取服务id
GetType() string //获取服务类型
@ -58,6 +60,8 @@ type IService interface {
GetComp(CompName S_Comps) (comp IServiceComp, err error) //获取组件
GetModule(ModuleName M_Modules) (module IModule, err error) //获取模块
}
//基础服务组件的接口设计
type IServiceComp interface {
GetName() S_Comps
NewOptions() (options ICompOptions)
@ -65,44 +69,52 @@ type IServiceComp interface {
Start() (err error)
Destroy() (err error)
}
//基础模块的接口设计
type IModule interface {
GetType() M_Modules
NewOptions() (options IModuleOptions)
Init(service IService, module IModule, options IModuleOptions) (err error)
OnInstallComp()
Start() (err error)
Run(closeSig chan bool) (err error)
Destroy() (err error)
GetType() M_Modules //模块类型
NewOptions() (options IModuleOptions) //返回模块的参数类型 用于自动序列化模块参数对象
Init(service IService, module IModule, options IModuleOptions) (err error) //服务运行时 优先执行完所有模块的Init 函数
OnInstallComp() //模块启动时在Init中有限执行模块的组件转载函数 初始化组件列表
Start() (err error) //服务初始化所有模块后在同一执行各个模块的启动函数
Run(closeSig chan bool) (err error) //模块需要独立携程运行时可以重构此接口
Destroy() (err error) //模块销毁接口
}
//服务组件参数对象接口
type ICompOptions interface {
LoadConfig(settings map[string]interface{}) (err error)
LoadConfig(settings map[string]interface{}) (err error) //序列化参数接口
}
//模块参数对象接口
type IModuleOptions interface {
LoadConfig(settings map[string]interface{}) (err error)
LoadConfig(settings map[string]interface{}) (err error) //序列化参数接口
}
//模块组件接口设计
type IModuleComp interface {
Init(service IService, module IModule, comp IModuleComp, options IModuleOptions) (err error)
Start() (err error)
Destroy() (err error)
Init(service IService, module IModule, comp IModuleComp, options IModuleOptions) (err error) //组件初始化函数
Start() (err error) //组件启动函数
Destroy() (err error) //组件销毁函数
}
//用户基础会话对象设计接口
type IUserSession interface {
GetSessionId() string
GetIP() string
GetGateId() string
SendMsg(comdId uint16, msgId uint16, msg interface{}) (err error)
Close() (err error)
GetSessionId() string //用户会话连接id 标识唯一的socket对象
GetIP() string //用户的ip信息
GetGateId() string //用户所在的网关服务地址
SendMsg(comdId uint16, msgId uint16, msg interface{}) (err error) //向用户发送消息
Close() (err error) //断开用户的连接
}
//服务后台运维接口 动态修改服务或模块配置参数
type IServiceMonitor interface {
IModule
RegisterServiceSettingItem(comp S_Comps, name string, iswrite bool, value interface{}, f func(newvalue string) (err error)) //注册服务级别的Setting
RegisterModuleSettingItem(module M_Modules, name string, iswrite bool, value interface{}, f func(newvalue string) (err error)) //注册模块级别的Setting
}
//Monitor 数据
//服务监控 Monitor 数据
type (
SettingItem struct {
ItemName string

View File

@ -9,6 +9,7 @@ import (
//参数校验
func (this *Api_Comp) Getlist_Check(session comm.IUserSession, req *pb.GetlistReq) (result map[string]interface{}, code pb.ErrorCode) {
result = map[string]interface{}{"ce": 123}
return
}