Documentation
¶
Index ¶
- Constants
- Variables
- func CloseClients() error
- func Env() string
- func GetClient(name string) (*grpc.ClientConn, error)
- func InjectGrpcClient(v any)
- func IsLocal() bool
- func IsProd() bool
- func LoadUserConfig(cfg any) error
- func LoggerLevel() string
- func RegisterClient(serverKind string, newFunc interface{})
- type Application
- type ApplicationOption
- func WithAppName(name string) ApplicationOption
- func WithCmdline(cmdline ...*cobra.Command) ApplicationOption
- func WithGrpcServer(gs *grpc.Server) ApplicationOptiondeprecated
- func WithHttpServer(hs *http.Server) ApplicationOptiondeprecated
- func WithLifecycleConfig(cfg *Config) ApplicationOption
- func WithMigrations(fsys fs.FS) ApplicationOption
- func WithPluginTimeouts(ready, drain, close time.Duration) ApplicationOption
- func WithPlugins(plugins ...Plugin) ApplicationOption
- func WithStartPreHook(hook func(context.Context) error) ApplicationOption
- func WithState(state *State) ApplicationOption
- type Config
- type Logger
- type Plugin
- type PluginBase
- type ServerType
- type State
Constants ¶
const (
// Version 当前框架的版本
Version = "v0.5.1"
)
Variables ¶
var ( // ErrNotStarted 表示应用还没有完成启动,不应接收流量。 ErrNotStarted = errors.New("ezboot: not started") // ErrDraining 表示应用正在排空,不应接收新流量。 ErrDraining = errors.New("ezboot: draining") // ErrNilState 表示调用方传入了一个 nil 的就绪状态对象。 ErrNilState = errors.New("ezboot: state is nil") )
var DiscardLogger = zap.NewNop().Sugar()
DiscardLogger 是一个默认的空输出的日志记录器
var ErrNilConfig = errors.New("ezboot: config is nil")
ErrNilConfig 表示调用方传入了一个 nil 的框架配置。
var ErrNilMigrationFS = errors.New("ezboot: migrations fs is nil")
ErrNilMigrationFS 表示调用方传入了一个 nil 的迁移文件系统。
var ProviderSet = wire.NewSet( ProvideConfig, ProvideLogger, NewState, tracing.NewOTEL, grpcclient.NewGRPCClient, )
ProviderSet 提供所有应用都会用到、且不需要定制的依赖与插件:
配置 + 日志 + 就绪状态 + tracing + gRPC 客户端连接池
http 与 grpc 插件刻意不在这里:它们需要路由、service 注册、网关等 应用特有的参数,必须由应用自己的 provider 构造。把 grpc.New / http.New 放进这个集合会与应用的构造器产生同类型冲突(wire: multiple bindings)。
infra 插件(MySQL / Redis / Kafka / RabbitMQ / Elasticsearch / Etcd) 也不在这里:只用 HTTP 的服务不应该因为引入框架就打开 Redis。
Functions ¶
func CloseClients ¶ added in v0.5.0
func CloseClients() error
CloseClients closes every cached gRPC client connection.
func GetClient ¶ added in v0.5.0
func GetClient(name string) (*grpc.ClientConn, error)
GetClient returns the cached gRPC connection configured under name.
func InjectGrpcClient ¶
func InjectGrpcClient(v any)
func LoggerLevel ¶
func LoggerLevel() string
func RegisterClient ¶
func RegisterClient(serverKind string, newFunc interface{})
Types ¶
type Application ¶
type Application struct {
// contains filtered or unexported fields
}
Application 是一个gin-boot应用程序
type ApplicationOption ¶
type ApplicationOption func(app *Application) error
ApplicationOption 创建应用的额外选项
func WithCmdline ¶
func WithCmdline(cmdline ...*cobra.Command) ApplicationOption
WithCmdline 增加一个额外命令行配置项
func WithGrpcServer
deprecated
func WithGrpcServer(gs *grpc.Server) ApplicationOption
WithGrpcServer 设置启动Grpc服务。
Deprecated: 使用 WithPlugins 显式注册插件。
func WithHttpServer
deprecated
func WithHttpServer(hs *http.Server) ApplicationOption
WithHttpServer 设置启动Http服务。
Deprecated: 使用 WithPlugins 显式注册插件。
func WithLifecycleConfig ¶ added in v0.5.0
func WithLifecycleConfig(cfg *Config) ApplicationOption
WithLifecycleConfig 从框架配置读取生命周期各阶段的预算 (app.lifecycle.*),零值使用默认值。
func WithMigrations ¶ added in v0.5.2
func WithMigrations(fsys fs.FS) ApplicationOption
WithMigrations 注入应用内嵌的迁移文件系统(通常来自 //go:embed)。 文件系统的根目录必须直接包含 *.up.sql / *.down.sql 迁移文件。 开启自动迁移但没有调用本选项时,启动会失败。
func WithPluginTimeouts ¶ added in v0.5.0
func WithPluginTimeouts(ready, drain, close time.Duration) ApplicationOption
WithPluginTimeouts 覆盖生命周期各阶段的默认预算。
func WithPlugins ¶ added in v0.5.0
func WithPlugins(plugins ...Plugin) ApplicationOption
WithPlugins 注册参与生命周期的插件。列表顺序决定 Close 的逆序: 越靠前的插件越晚关闭(例如 OTEL 应放在最前)。
func WithStartPreHook ¶
func WithStartPreHook(hook func(context.Context) error) ApplicationOption
WithStartPreHook 在服务启动之前的任务
func WithState ¶ added in v0.5.0
func WithState(state *State) ApplicationOption
WithState 设置就绪状态对象。它与 HTTP 插件共享,由 lifecycle 写入。
type Config ¶
Config 配置别名
func ProvideConfig ¶
func ProvideConfig() *Config
ProvideConfig 加载框架全局配置。
它同时完成需要早于插件构造的初始化(tracing 拦截器、gRPC 客户端池), 因为 gRPC driver 在构造时就会读取拦截器列表。
type Logger ¶
type Logger = zap.SugaredLogger
Logger 日志实现的别名
func ProvideLogger ¶
func ProvideLogger() *Logger
ProvideLogger 加载日志记录器。它先确保配置已加载,因此和 ProvideConfig 之间没有隐式的构造顺序要求。
type Plugin ¶ added in v0.5.0
type Plugin interface {
Name() string
Start(ctx context.Context) error
Ready(ctx context.Context) error
Drain(ctx context.Context) error
Close(ctx context.Context) error
}
Plugin 是应用生命周期中一个可插拔的组件。
阶段调用顺序:Start → 轮询 Ready → Drain → Close。
契约:
- Start 必须阻塞到插件停止(覆盖它时记得自己写 <-ctx.Done())。 Start 返回即代表插件已经停止。
- Ready 回答“现在能不能接流量”,只在启动期被调用。它必须便宜、 可重复调用、并发安全,不得自己重试或长时间阻塞。
- Drain 与 Close 收到的是 lifecycle 新建的、带超时的 ctx,不是 Start 时的那个。Drain 负责停止接收新工作并等待在途工作结束, Close 负责释放资源。两者不可合并。
- Name 只用于日志与错误信息,不作为查找键。
type PluginBase ¶ added in v0.5.0
type PluginBase struct{}
PluginBase 提供四个阶段的默认实现。嵌入它之后,插件只需要实现 Name,以及自己真正关心的那几个阶段。
func (PluginBase) Close ¶ added in v0.5.0
func (PluginBase) Close(context.Context) error
Close 默认什么都不做。
func (PluginBase) Drain ¶ added in v0.5.0
func (PluginBase) Drain(context.Context) error
Drain 默认什么都不做。
type ServerType ¶
type ServerType = grpc_boot.ServerType
type State ¶ added in v0.5.0
type State struct {
// contains filtered or unexported fields
}
State 是就绪状态的唯一数据源:由 lifecycle 写入,由就绪端点读取。
运行期的 /readyz 只读它,不去探测任何插件。依赖故障交给超时、重试 与熔断处理;readiness 只回答“这个实例该不该接流量”。
func (*State) MarkDraining ¶ added in v0.5.0
func (s *State) MarkDraining()
MarkDraining 标记开始排空。调用后 /readyz 立即返回 503。
func (*State) MarkStarted ¶ added in v0.5.0
func (s *State) MarkStarted()
MarkStarted 标记启动完成。由 lifecycle 在全部插件 Ready 之后调用。
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
snowflake
command
|
|
|
ezboot
module
|
|
|
protoc-gen-go-ezboot
module
|
|
|
protoc-gen-go-rbac
module
|
|
|
client
Package client 把进程级 gRPC 客户端连接池暴露成一个插件。
|
Package client 把进程级 gRPC 客户端连接池暴露成一个插件。 |
|
Package infra 把外部依赖封装成插件:数据库、Redis、消息队列、搜索 和 etcd。
|
Package infra 把外部依赖封装成插件:数据库、Redis、消息队列、搜索 和 etcd。 |
|
internal
|
|
|
gin-boot/middleware
Package middleware contains HTTP and gRPC request-pipeline middleware only: logging, recovery, validation, metadata helpers and tracing interceptors.
|
Package middleware contains HTTP and gRPC request-pipeline middleware only: logging, recovery, validation, metadata helpers and tracing interceptors. |
|
logging
Package logging 1.
|
Package logging 1. |
|
Package snowflake provides a very simple Twitter snowflake generator and parser.
|
Package snowflake provides a very simple Twitter snowflake generator and parser. |
|
tools
|
|
|
reflect
module
|
|
|
Package tracing 把 OpenTelemetry 的 tracer provider 暴露成一个插件。
|
Package tracing 把 OpenTelemetry 的 tracer provider 暴露成一个插件。 |