lego development framework use
Introduction to lego development framework
After I finished the front-end framework, I always wanted to enter the back-end framework design, just as golang is rising, So I started to study its golang, and looked at the source code and ideas of the two open-source frameworks, leaf and mqant in detail. The previous versions were basically modified with the framework of others. Later, I gradually modified them to add my own ideas and understanding. It can be seen that the core design of lego began to be consistent with the design of the front-end U3D FW There is only one more concept of service, and this framework also inherits the basic idea of my framework design, namely horizontal and vertical extensibility and business independence
Import of lego
go get https://github.com/liwei1dao/lego.git
lego framework directory structure description
- logo directory
- Base / / framework basic service encapsulates two types of service base classes: single (independent) and cluster (cluster)
- Core / / framework core interface definition, service and module as well as definition of various components and data types
- lib / / the user code accumulation of the framework code base inherits the module (gate,http), service component (COMP gateway routing component) and module component (COMP gateway business component). In the later stage, more functional modules and components will be integrated
- sys / / framework system library is similar to tool library, but because of its strong functional type and its own data structure management and external expansion, it has opened up a system library including (monodb,redis,log,rpc )This kind of independent function set will continue to expand later
- utils / / toolset encapsulates all kinds of practical tools and objects such as data containers
base service encapsulation
- IService all services must inherit from it (under the core directory)
type IService interface { GetId() string //Get service id GetType() string //Get service type GetVersion() int32 //Get service version GetWorkPath() string //Get service working directory GetSettings() ServiceSttings //Get service configuration table information Init(service IService) (err error) //Initialize interface InitSys() //Initialize system OnInstallComp(cops ...IServiceComp) //Assemble service components Start() (err error) //Startup service Run(mods ...IModule) //Operation service Close(closemsg string) //Shut down service Destroy() (err error) //Destruction service GetComp(CompName S_Comps) (comp IServiceComp, err error) //Get components GetModule(ModuleName M_Modules) (module IModule, err error) //Acquisition module }
- single independent server
- This service is a stand-alone service, which is generally used for the development of functional services that provide separate functions without the assistance of other services, such as simple web servers and some micro and small business servers
type ISingleService interface { core.IService }
- Cluster cluster server
- This service is used for the development and use of large-scale service clusters, involving inter service communication, service discovery, update, loss, and inter service messaging mechanisms such as point-to-point, one to many, and message subscription and publishing,
type IClusterService interface { core.IService GetTag() string //Get cluster label GetCategory() core.S_Category //Service categories such as game clothes GetRpcId() string //Get rpc communication id GetPreWeight() int32 //The cluster service load value can be ignored temporarily GetSessionsByCategory(category core.S_Category) (ss []core.IServiceSession) //Get service list by service category DefauleRpcRouteRules(stype string) (ss core.IServiceSession, err error) //Default rpc routing rules RpcInvokeById(sId string, rkey core.Rpc_Key, iscall bool, arg ...interface{}) (result interface{}, err error) //Execute remote service Rpc method RpcInvokeByType(sType string, rkey core.Rpc_Key, iscall bool, arg ...interface{}) (result interface{}, err error) //Execute remote methods according to routing rules ReleaseRpc(rkey core.Rpc_Key, arg ...interface{}) //Release Rpc Register(id core.Rpc_Key, f interface{}) (err error) //Register RPC remote methods RegisterGO(id core.Rpc_Key, f interface{}) (err error) //Register RPC remote methods Subscribe(id core.Rpc_Key, f interface{}) (err error) //Subscribe to Rpc UnSubscribe(id core.Rpc_Key, f interface{}) (err error) //Subscribe to Rpc }
core interface structure definition
-
Custom data structure·
type S_Category string / / service category e.g. gateway service game service business service is mainly used for service function classification
Type M? Modules string / / module type
Type s? Comps string / / server component type
type ErrorCode int32 / / error code
type Event_Key string / / event Key name
Type rpc_key string / / RPC interface name
Type redis? Key string / / redis cache key
type SqlTable string / / database table name -
Service interface
//Service configuration data structure type ServiceSttings struct { Settings map[string]interface{} Modules map[string]map[string]interface{} } //Service interface type IService interface { GetId() string //Get service id GetType() string //Get service type GetVersion() int32 //Get service version GetWorkPath() string //Get service working directory GetSettings() ServiceSttings //Get service configuration table information Init(service IService) (err error) //Initialize interface InitSys() //Initialize system OnInstallComp(cops ...IServiceComp) //Assemble service components Start() (err error) //Startup service Run(mods ...IModule) //Operation service Close(closemsg string) //Shut down service Destroy() (err error) //Destruction service GetComp(CompName S_Comps) (comp IServiceComp, err error) //Get components GetModule(ModuleName M_Modules) (module IModule, err error) //Acquisition module } //Server component interface type IServiceComp interface { GetName() S_Comps Init(service IService, comp IServiceComp) (err error) Start() (err error) Destroy() (err error) }
-
Module interface definition
//Module interface type IModule interface { GetType() M_Modules Init(service IService, module IModule, setting map[string]interface{}) (err error) OnInstallComp() Start() (err error) Run(closeSig chan bool) (err error) Destroy() (err error) } //Module component interface type IModuleComp interface { Init(service IService, module IModule, comp IModuleComp, setting map[string]interface{}) (err error) Start() (err error) Destroy() (err error) }
-
Session object
- Service session
//Service session service refers to the object of communication and basic information transmission type IServiceSession interface { GetId() string GetRpcId() string GetType() string GetVersion() int32 SetVersion(v int32) GetPreWeight() int32 SetPreWeight(p int32) Done() CallNR(_func Rpc_Key, params ...interface{}) (err error) Call(_func Rpc_Key, params ...interface{}) (interface{}, error) }
- User session client object message sending and basic information passing objects
type IUserSession interface { GetSessionId() string GetIP() string GetGateId() string SendMsg(comdId uint16, msgId uint16, msg interface{}) (err error) Close() (err error) }
- Service session
lib integrated function modules and components
- s_comps
- Comp gateway provides services with messages to receive gateway services. Only services with this component can receive message push from themselves or other gateway services
- modules
- The gate gateway service module provides tcp and websocket services to the client module, which needs to expand and implement the necessary interfaces by itself. Refer to the real example of gate module in demo
- The http web service module provides http or https services. You can refer to the implementation instance of demo as well
- module-comps
- Comp gate cooperates with the gateway service module to integrate the business components of this component, which can process messages from the gateway allocation
sys integrated system
- The registry system cooperates with the consumer service to realize the discovery, update and loss events of the service cluster as well as the saving of rpc publish subscription data under the cluster
- rpc rpc message system provides services under cluster service. See message communication
- workerpools work pool system cuts high-performance processing flows for high concurrency services and businesses through security
- The log log system integrates the zap system. This system can extend the log interface. The user-defined and other third-party log systems can
- The proto protocol system supports gate and rpc message serialization. The default message structure is encapsulated inside. You can customize the message structure by setting the startup parameters
- Event event system provides the service with the function of event registration, listening and processing
- mgo mogodb database system, encapsulating the official driver library, simplifying the system call interface
- sqls sqlserver database system, encapsulating the official driver library, simplifying the system call interface
- redis redis cache data system provides various data storage and reading interfaces
- timer system encapsulates an efficient time wheel system
- SDK integrates various third-party service codes, such as Alibaba cloud's SMS and email message push implementation
demo download address
demo project address:
https://github.com/liwei1dao/lego_demo.git
https://gitee.com/liwei1dao/lego_demo.git