app

package
v1.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientRPCHandler

type ClientRPCHandler func(server registry.Node, rpcinfo *core.RPCInfo, result any, err error, exec_time int64)

ClientRPCHandler 调用方RPC监控

type FileNameHandler

type FileNameHandler func(logdir, prefix, processID, suffix string) string

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) (IServerSession, error)
	// 获取服务实例(通过服务ID(moduleType@id))
	GetServerByID(serverID string) (IServerSession, error)
	// 获取服务实例(通过服务类型(moduleType),可设置可设置selector.WithFilter和selector.WithStrategy)
	GetServerBySelector(serviceName string, opts ...selector.SelectOption) (IServerSession, error)
	// 获取多个服务实例(通过服务类型(moduleType))
	GetServersByType(serviceName string) []IServerSession

	// Call RPC调用(需要等待结果)
	Call(ctx context.Context, moduleType, _func string, param mqrpc.ParamOption, opts ...selector.SelectOption) (any, error)
	// Call RPC调用(无需等待结果)
	CallNR(ctx context.Context, moduleType, _func string, params ...any) error
	// Call RPC调用(群发,无需等待结果)
	CallBroadcast(ctx context.Context, moduleName, _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 mqant应用定义

var DefaultApp IApp = nil

default app instance

func App

func App() IApp

type ICtxTransSetApp

type ICtxTransSetApp interface {
	SetApp(IApp)
}

RPC传输时Context中的数据可能会需要赋值跨服务的app(为什么会有这个接口,会循环import)

type IModule

type IModule interface {
	GetType() string // 模块类型
	Version() string // 模块版本

	Run(closeSig chan bool)

	OnInit(settings *conf.ModuleSettings) // 所有初始化逻辑都放到Init中, 重载OnInit不可调用基类!(由Init层层调用base.Init)即可
	OnDestroy()
	OnAppConfigurationLoaded()                   // 当App初始化时调用,这个接口不管这个模块是否在这个进程运行都会调用
	OnConfChanged(settings *conf.ModuleSettings) // 为以后动态服务发现做准备(目前没用)
}

IModule 基本模块定义

type IRPCModule

type IRPCModule interface {
	IModule

	// 模块服务ID
	GetServerID() string
	GetModuleSettings() (settings *conf.ModuleSettings)

	// 获取服务实例(通过服务ID|服务类型,可设置选择器过滤)
	GetRouteServer(service string, opts ...selector.SelectOption) (IServerSession, error) //获取经过筛选过的服务
	// 通过服务ID(moduleType@id)获取服务实例
	GetServerByID(serverID string) (IServerSession, error)
	// 通过服务类型(moduleType)获取服务实例列表
	GetServersByType(serviceName string) []IServerSession
	// 通过服务类型(moduleType)获取服务实例(可设置选择器)
	GetServerBySelector(serviceName string, opts ...selector.SelectOption) (IServerSession, error)

	Call(ctx context.Context, moduleType, _func string, params mqrpc.ParamOption, opts ...selector.SelectOption) (any, error)
	CallNR(ctx context.Context, moduleType, _func string, params ...any) error
	CallBroadcast(ctx context.Context, moduleName, _func string, params ...any)
}

IRPCModule RPC模块定义

type IServerSession

type IServerSession interface {
	GetID() string
	GetName() string
	GetRPC() mqrpc.RPCClient

	GetNode() *registry.Node
	SetNode(node *registry.Node) (err error)

	Call(ctx context.Context, _func string, params ...any) (any, error)                             // 等待返回结果
	CallArgs(ctx context.Context, _func string, argTypes []string, argDatas [][]byte) (any, error)  // 内部使用(ctx参数必须装进args中)
	CallNR(ctx context.Context, _func string, params ...any) (err error)                            // 无需等待结果
	CallNRArgs(ctx context.Context, _func string, argTypes []string, argDatas [][]byte) (err error) // 内部使用(ctx参数必须装进args中)
}

IServerSession 服务代理

type Option

type Option func(*Options)

Option 应用级别配置项

func BILogDir

func BILogDir(v string) Option

BILogDir BI日志路径

func ConfigKey

func ConfigKey(v string) Option

Configure 配置key

func ConsulAddr

func ConsulAddr(v ...string) Option

Configure consule 地址

func Debug

func Debug(t bool) Option

Debug 只有是在调试模式下才会在控制台打印日志, 非调试模式下只在日志文件中输出日志

func KillWaitTTL

func KillWaitTTL(t time.Duration) Option

KillWaitTTL specifies the interval on which to re-register

func LogDir

func LogDir(v string) Option

LogDir 日志存储路径

func Nats

func Nats(nc *nats.Conn) Option

Nats nats配置

func Parse

func Parse(t bool) Option

Parse mqant框架是否解析环境参数

func ProcessID

func ProcessID(v string) Option

ProcessID 进程分组ID

func RPCExpired

func RPCExpired(t time.Duration) Option

RPC超时时间

func RPCMaxCoroutine

func RPCMaxCoroutine(t int) Option

单个节点RPC同时并发协程数

func RegisterInterval

func RegisterInterval(t time.Duration) Option

RegisterInterval specifies the interval on which to re-register

func RegisterTTL

func RegisterTTL(t time.Duration) Option

RegisterTTL specifies the TTL to use when registering the service

func Registry

func Registry(r registry.Registry) Option

Registry sets the registry for the service and the underlying components

func Selector

func Selector(r selector.Selector) Option

Selector 路由选择器

func SetClientRPChandler

func SetClientRPChandler(t ClientRPCHandler) Option

SetClientRPChandler 配置调用者监控器

func SetRpcCompleteHandler

func SetRpcCompleteHandler(t RpcCompleteHandler) Option

SetServerRPCCompleteHandler 服务RPC执行结果监控器

func SetServerRPCHandler

func SetServerRPCHandler(t ServerRPCHandler) Option

SetServerRPCHandler 配置服务方监控器

func Version

func Version(v string) Option

Version 应用版本

func WithBIFile

func WithBIFile(name FileNameHandler) Option

WithBIFile Bi日志名称

func WithLogFile

func WithLogFile(name FileNameHandler) Option

WithLogFile 日志文件名称

func WorkDir

func WorkDir(v string) Option

WorkDir 进程工作目录

type Options

type Options struct {
	Version     string // app的版本
	Debug       bool
	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: ./bin/logs)
	BIDir       string   // BI目录(from startUpArgs.BiPath default: ./bin/bi)
	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)

	ClientRPChandler   ClientRPCHandler   // 配置RPC调用方监控器(nil)
	ServerRPCHandler   ServerRPCHandler   // 配置RPC服务方监控器(nil)
	RpcCompleteHandler RpcCompleteHandler // 配置RPC执行结果监控器(nil)
	RPCExpired         time.Duration      // RPC调用超时(10s)
	RPCMaxCoroutine    int                // 默认0(不限制) 没用

	// 自定义日志文件名字(主要作用方便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 应用级别配置

func NewOptions

func NewOptions(opts ...Option) Options

APP选项

type RpcCompleteHandler

type RpcCompleteHandler func(module IModule, callInfo *mqrpc.CallInfo, input []any, out []any, execTime time.Duration)

ServerRPCHandler 服务方RPC监控

type ServerRPCHandler

type ServerRPCHandler func(module IModule, callInfo *mqrpc.CallInfo)

ServerRPCHandler 服务方RPC监控

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL