Documentation
¶
Index ¶
- type ClientRPCHook
- type FileNameHandler
- type IApp
- type IModule
- type IModuleServerSession
- type IRPCModule
- type Option
- func BILogDir(v string) Option
- func ConfigKey(v string) Option
- func ConsulAddr(v ...string) Option
- func Debug(t bool) Option
- func KillWaitTTL(t time.Duration) Option
- func LogDir(v string) Option
- func Nats(nc *nats.Conn) Option
- func Parse(t bool) Option
- func ProcessID(v string) Option
- func RPCExpired(t time.Duration) Option
- func RPCMaxCoroutine(t int) Option
- func RegisterInterval(t time.Duration) Option
- func RegisterTTL(t time.Duration) Option
- func Registry(r registry.Registry) Option
- func Selector(r selector.Selector) Option
- func SetClientRPChandler(t ClientRPCHook) Option
- func SetServerRPCHandler(t ServerRPCHook) Option
- func Version(v string) Option
- func WithBIFile(name FileNameHandler) Option
- func WithLogFile(name FileNameHandler) Option
- func WorkDir(v string) Option
- type Options
- type ServerRPCHook
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ClientRPCHook ¶ added in v1.2.0
type ClientRPCHook func(server registry.Node, rpcinfo *core.RPCInfo, result any, err error, exec_time int64)
ClientRPCHook 调用方RPC钩子回调
type FileNameHandler ¶
FileNameHandler 自定义日志文件名字
type IApp ¶
type IApp interface {
OnInit() error
OnDestroy() error
Run(mods ...IModule) error
// Config 获取启动配置
Config() conf.Config
// Options 获取应用配置
Options() Options
// Transporter 获取消息传输对象
Transporter() *nats.Conn
// Registrar 获取服务注册对象
Registrar() registry.Registry
// WorkDir 获取进程工作目录
WorkDir() string
// GetProcessEnv 获取应用进程分组ID(dev,test,...)
GetProcessEnv() string
// UpdateOptions 允许再次更新应用配置(before app.Run)
UpdateOptions(opts ...Option) error
// 设置服务路由器(动态转换service名称)
SetServiceRoute(fn func(route string) string) error
// 获取服务实例(通过服务ID|服务类型,可设置selector.WithFilter和selector.WithStrategy)
GetRouteServer(service string, opts ...selector.SelectOption) (IModuleServerSession, error)
// 获取服务实例(通过服务ID(moduleType@id))
GetServerByID(serverID string) (IModuleServerSession, error)
// 获取服务实例(通过服务类型(moduleType),可设置可设置selector.WithFilter和selector.WithStrategy)
GetServerBySelector(serviceName string, opts ...selector.SelectOption) (IModuleServerSession, error)
// 获取多个服务实例(通过服务类型(moduleType))
GetServersByType(serviceName string) []IModuleServerSession
// Call RPC调用(需要等待结果)
Call(ctx context.Context, moduleServer, _func string, param mqrpc.ParamOption, opts ...selector.SelectOption) (any, error)
// Call RPC调用(无需等待结果)
CallNR(ctx context.Context, moduleServer, _func string, params ...any) error
// Call RPC调用(群发,无需等待结果)
CallBroadcast(ctx context.Context, moduleType, _func string, params ...any)
// 回调(hook)
OnConfigurationLoaded(func()) error // 设置应用启动配置初始化完成后回调
OnModuleInited(func(module IModule)) error // 设置每个模块初始化完成后回调
GetModuleInited() func(module IModule) // 获取每个模块初始化完成后回调函数
OnStartup(func()) error // 设置应用启动完成后回调
OnServiceBreak(_func func(moduleName, serverId string)) error // 设置当模块服务断开删除时回调
}
IApp 应用定义
type IModule ¶
type IModule interface {
GetType() string // 模块类型
Version() string // 模块版本
Run(closeSig chan bool) // Run 由派生类实现(不可调用基类Run)
OnInit(settings *conf.ModuleSettings) // 所有初始化逻辑都放到Init中, 重载OnInit不可调用基类!(由Init层层调用base.Init)即可
OnDestroy()
OnAppConfigurationLoaded() // 当App初始化时调用,这个接口不管这个模块是否在这个进程运行都会调用
OnConfChanged(settings *conf.ModuleSettings) // 为以后动态服务发现做准备(目前没用)
}
IModule 基本模块定义
type IModuleServerSession ¶ added in v1.2.0
type IModuleServerSession interface {
// 服务ID
GetID() string
// 服务名称(moduleType)
GetName() string
// RPC客户端
GetRPC() mqrpc.IRPCClient
// 服务节点信息
GetNode() *registry.Node
SetNode(node *registry.Node) (err error)
}
IModuleServerSession Module服务会话代理
type IRPCModule ¶
type IRPCModule interface {
IModule // 需要自行在Run方法中调用StartTimer方法
timer.ITimer
// 模块服务ID
GetServerID() string
GetModuleSettings() (settings *conf.ModuleSettings)
// 注册RPC方法(f的第一个参数必须是context.Context,返回参数(最多两个)最后一个必须是error)
Register(msg string, f interface{}) // 同步
RegisterGO(msg string, f interface{}) // 并发
// 获取服务实例(通过服务ID|服务类型,可设置选择器过滤)
GetRouteServer(service string, opts ...selector.SelectOption) (IModuleServerSession, error) //获取经过筛选过的服务
// 通过服务ID(moduleType@id)获取服务实例
GetServerByID(serverID string) (IModuleServerSession, error)
// 通过服务类型(moduleType)获取服务实例列表
GetServersByType(serviceName string) []IModuleServerSession
// 通过服务类型(moduleType)获取服务实例(可设置选择器)
GetServerBySelector(serviceName string, opts ...selector.SelectOption) (IModuleServerSession, error)
// RPC方法
Call(ctx context.Context, moduleServer, _func string, params mqrpc.ParamOption, opts ...selector.SelectOption) (any, error)
CallNR(ctx context.Context, moduleServer, _func string, params ...any) error
CallBroadcast(ctx context.Context, moduleType, _func string, params ...any)
}
IRPCModule RPC模块定义
type Option ¶
type Option func(*Options)
Option 应用级别配置项
func KillWaitTTL ¶
KillWaitTTL specifies the interval on which to re-register
func RegisterInterval ¶
RegisterInterval specifies the interval on which to re-register
func RegisterTTL ¶
RegisterTTL specifies the TTL to use when registering the service
func SetClientRPChandler ¶
func SetClientRPChandler(t ClientRPCHook) Option
SetClientRPChandler 配置调用者监控器
func SetServerRPCHandler ¶
func SetServerRPCHandler(t ServerRPCHook) Option
SetServerRPCHandler 配置服务方监控器
type Options ¶
type Options struct {
Version string // app的版本
Debug bool // 是否打印日志到控制台(true)
Parse bool // 是否由框架解析启动环境变量(true)
WorkDir string // 工作目录(from startUpArgs.WordDir)
ProcessEnv string // 进程分组名称(from startUpArgs.ProcessEnv)
ConfigKey string // consul configKey(default: config/{env}/server)
ConsulAddr []string // consul addr(from startUpArgs.ConsulAddr)
LogDir string // Log目录(from startUpArgs.LogPath default: ./logs)
BIDir string // BI目录(from startUpArgs.BiPath default: ./logBI)
PProfAddr string
KillWaitTTL time.Duration // 服务关闭超时强杀(60s)
Nats *nats.Conn
Registry registry.Registry // 注册服务发现(registry.DefaultRegistry)
Selector selector.Selector // 节点选择器(在Registry基础上)(cache.NewSelector())
RegisterInterval time.Duration // 服务注册发现续约频率(10s)
RegisterTTL time.Duration // 服务注册发现续约生命周期(20s)
RPCExpired time.Duration // RPC调用超时(10s)
RPCMaxCoroutine int // 默认0(不限制)
ClientRPCHandler ClientRPCHook // 配置全局的RPC调用方监控器(nil)
ServerRPCHandler ServerRPCHook // 配置全局的RPC服务方监控器(nil)
// 自定义日志文件名字(主要作用方便k8s映射日志不会被冲突,建议使用k8s pod实现)
LogFileName FileNameHandler // 日志文件名称(默认):fmt.Sprintf("%s/%v%s%s", logdir, prefix, processID, suffix)
// 自定义BI日志名字
BIFileName FileNameHandler // BI文件名称(默认):fmt.Sprintf("%s/%v%s%s", logdir, prefix, processID, suffix)
}
Options 应用级别配置
type ServerRPCHook ¶ added in v1.2.0
ServerRPCHook 服务方RPC钩子回调
Click to show internal directories.
Click to hide internal directories.