go_dreamfactory/lego/base/rpcx/service.go

208 lines
8.4 KiB
Go

package rpcx
import (
"context"
"fmt"
"runtime"
"time"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"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) GetOpentime() time.Time {
t, err := time.ParseInLocation("2006-01-02 15:04:05", this.GetSettings().Opentime, time.Local)
if nil == err && !t.IsZero() {
return t
}
return time.Time{}
}
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 !")
}
//初始化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))
}
})
}
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
}
err = this.ServiceBase.Destroy()
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
}
///同步 执行目标远程服务方法
///servicePath = worker 表示采用负载的方式调用 worker类型服务执行rpc方法
///servicePath = worker/worker_1 表示寻找目标服务节点调用rpc方法
///servicePath = worker/!worker_1 表示选择非worker_1的节点随机选择节点执行rpc方法
///servicePath = worker/[worker_1,worker_2] 表示随机选择[]里面的服务节点执行rpc方法
///servicePath = worker/![worker_1,worker_2] 表示随机选择非[]里面的服务节点执行rpc方法
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)
}
///广播 执行目标远程服务方法
///servicePath = worker 表示采用负载的方式调用 worker类型服务执行rpc方法
func (this *RPCXService) RpcBroadcast(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) {
return rpcx.Broadcast(ctx, servicePath, serviceMethod, args, reply)
}
///异步 执行目标远程服务方法
///servicePath = worker 表示采用负载的方式调用 worker类型服务执行rpc方法
///servicePath = worker/worker_1 表示寻找目标服务节点调用rpc方法
///servicePath = worker/!worker_1 表示选择非worker_1的节点随机选择节点执行rpc方法
///servicePath = worker/[worker_1,worker_2] 表示随机选择[]里面的服务节点执行rpc方法
///servicePath = worker/![worker_1,worker_2] 表示随机选择非[]里面的服务节点执行rpc方法
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)
}
///跨集群 同步 执行目标远程服务方法
//clusterTag 集群标签
///servicePath = worker 表示采用负载的方式调用 worker类型服务执行rpc方法
///servicePath = worker/worker_1 表示寻找目标服务节点调用rpc方法
///servicePath = worker/!worker_1 表示选择非worker_1的节点随机选择节点执行rpc方法
///servicePath = worker/[worker_1,worker_2] 表示随机选择[]里面的服务节点执行rpc方法
///servicePath = worker/![worker_1,worker_2] 表示随机选择非[]里面的服务节点执行rpc方法
func (this *RPCXService) AcrossClusterRpcCall(ctx context.Context, clusterTag string, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) {
return rpcx.AcrossClusterCall(ctx, clusterTag, servicePath, serviceMethod, args, reply)
}
///跨集群 异步 执行目标远程服务方法
//clusterTag 集群标签
///servicePath = worker 表示采用负载的方式调用 worker类型服务执行rpc方法
///servicePath = worker/worker_1 表示寻找目标服务节点调用rpc方法
///servicePath = worker/!worker_1 表示选择非worker_1的节点随机选择节点执行rpc方法
///servicePath = worker/[worker_1,worker_2] 表示随机选择[]里面的服务节点执行rpc方法
///servicePath = worker/![worker_1,worker_2] 表示随机选择非[]里面的服务节点执行rpc方法
func (this *RPCXService) AcrossClusterRpcGo(ctx context.Context, clusterTag string, servicePath string, serviceMethod string, args interface{}, reply interface{}) (call *client.Call, err error) {
return rpcx.AcrossClusterGo(ctx, clusterTag, servicePath, serviceMethod, args, reply, nil)
}
//目标集群广播消息
//clusterTag 集群标签
///servicePath = worker 表示采用负载的方式调用 worker类型服务执行rpc方法
///servicePath = worker/worker_1 表示寻找目标服务节点调用rpc方法
///servicePath = worker/!worker_1 表示选择非worker_1的节点随机选择节点执行rpc方法
///servicePath = worker/[worker_1,worker_2] 表示随机选择[]里面的服务节点执行rpc方法
///servicePath = worker/![worker_1,worker_2] 表示随机选择非[]里面的服务节点执行rpc方法
func (this *RPCXService) AcrossClusterBroadcast(ctx context.Context, clusterTag string, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) {
return rpcx.AcrossClusterBroadcast(ctx, clusterTag, servicePath, serviceMethod, args, reply)
}
///全集群广播 执行目标远程服务方法
///servicePath = worker 表示采用负载的方式调用 worker类型服务执行rpc方法
func (this *RPCXService) ClusterBroadcast(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) {
return rpcx.ClusterBroadcast(ctx, servicePath, serviceMethod, args, reply)
}