go_dreamfactory/lego/base/rpcx/service.go
2022-06-10 19:22:02 +08:00

146 lines
4.1 KiB
Go

package rpcx
import (
"context"
"fmt"
"runtime"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"go_dreamfactory/lego/sys/cron"
"go_dreamfactory/lego/sys/event"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/sys/rpcx"
"github.com/smallnest/rpcx/client"
)
type RPCXService struct {
cbase.ServiceBase //继承服务基类+
opts *Options //服务启动的配置信息数据
rpcxService base.IRPCXService //服务自身 通过接口可以实现上层服务类重构底层接口
}
func (this *RPCXService) GetTag() string {
return this.opts.Setting.Tag
}
func (this *RPCXService) GetId() string {
return this.opts.Setting.Id
}
func (this *RPCXService) GetType() string {
return this.opts.Setting.Type
}
func (this *RPCXService) GetIp() string {
return this.opts.Setting.Ip
}
func (this *RPCXService) GetPort() int {
return this.opts.Setting.Port
}
func (this *RPCXService) GetCategory() core.S_Category {
return this.opts.Setting.Category
}
func (this *RPCXService) GetVersion() string {
return this.opts.Version
}
func (this *RPCXService) GetSettings() core.ServiceSttings {
return this.opts.Setting
}
func (this *RPCXService) GetRpcId() string {
return ""
}
func (this *RPCXService) GetPreWeight() float64 {
return float64(1) / float64(runtime.NumGoroutine())
}
func (this *RPCXService) SetPreWeight(weight int32) {
}
func (this *RPCXService) Options() *Options {
return this.opts
}
func (this *RPCXService) Configure(opts ...Option) {
this.opts = newOptions(opts...)
}
func (this *RPCXService) Init(service core.IService) (err error) {
this.rpcxService = service.(base.IRPCXService)
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 !")
}
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 !")
}
event.Register(core.Event_ServiceStartEnd, func() { //阻塞 先注册服务集群 保证其他服务能及时发现
if err := rpcx.Start(); err != nil {
log.Panicf(fmt.Sprintf("Sys rpcx Start err:%v", err))
}
})
}
func (this *RPCXService) Start() (err error) {
if err = this.ServiceBase.Start(); err != nil {
return
}
return
}
func (this *RPCXService) Run(mod ...core.IModule) {
modules := make([]core.IModule, 0)
modules = append(modules, mod...)
this.ServiceBase.Run(modules...)
}
func (this *RPCXService) Destroy() (err error) {
if err = rpcx.Stop(); err != nil {
return
}
cron.Stop()
err = this.ServiceBase.Destroy()
return
}
//注册服务对象
func (this *RPCXService) Register(rcvr interface{}) (err error) {
err = rpcx.Register(rcvr)
return
}
//注册服务方法
func (this *RPCXService) RegisterFunction(fn interface{}) (err error) {
err = rpcx.RegisterFunction(fn)
return
}
//注册服务方法 自定义方法名称
func (this *RPCXService) RegisterFunctionName(name string, fn interface{}) (err error) {
err = rpcx.RegisterFunctionName(name, fn)
return
}
//同步 执行目标远程服务方法
func (this *RPCXService) RpcCall(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) {
return rpcx.Call(ctx, servicePath, serviceMethod, args, reply)
}
//异步 执行目标远程服务方法
func (this *RPCXService) RpcGo(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (call *client.Call, err error) {
return rpcx.Go(ctx, servicePath, serviceMethod, args, reply, nil)
}