bootstrap
应用生命周期引导——统一骨架/资源注册/优雅关停/健康检查
🌐 项目主页:https://oekit.github.io/ · 名称由来:Open Easy Kit(开源 · 易用 · 工具集合)
🤖 AI Agent:本仓规范与硬规则见 AGENTS.md;机器可读索引见 llms.txt(接口签名以源码为准)。
安装
go get github.com/oekit/bootstrap@v0.4.1
这个仓库做什么
bootstrap 是应用生命周期引导:bootstrap.Run 提供「初始化 → 运行 → 优雅关停」
的统一骨架——资源注册、goroutine 托管、关停顺序、超时兜底全部内建。
快速使用
import (
"github.com/oekit/bootstrap/bootstrap"
ekithertz "github.com/oekit/hertz/hertz"
)
bootstrap.Run(cfg, func(app *bootstrap.App) error {
h := hertzserver.New(...)
ekithertz.RunServer(app, "my-service", h) // server 接入生命周期
app.AddResource("db", func(ctx context.Context) error { return dbPool.Close() })
app.GoBackground("worker", workerLoop) // 托管后台 goroutine(panic 兜底)
<-app.Context().Done() // ★ 长驻服务必须阻塞:返回即视为服务结束
return nil
})
健康检查
import "github.com/oekit/bootstrap/bootstrap/healthz"
healthz.RegisterCheck("db", pingDB) // 多 check 并发探测、总超时、panic 隔离
code, body := healthz.Handle(ctx) // 全通过 200,任一失败 503 + 详情
// Hertz 服务无需手写 handler:github.com/oekit/hertz 的 health 子包
// 提供 RegisterHealthRoutes(h),内部消费本包 Handle
语义要点
app.AddResource(name, closer):关停时按注册逆序释放
app.GoBackground(name, fn):托管后台 goroutine(panic 被 recover)
- 时间关系(K8s):发现延迟 < preStop < server 最大延迟 < ExitWaitTime < terminationGracePeriod
边界
- Hertz/Kitex 的
RunServer 接入本骨架;NNS 注册用 nnsc.RunWithLifecycle
矩阵总览(23 活跃模块,自动生成)
依赖严格自底向上;微库已合并为 pkg,旧微库仓已删除。
本仓在矩阵中的位置与依赖见上表「矩阵内依赖」列。历史沿革:源自 ekit 单体库
(git.enjoye.top/enjoydream/ekit v0.34.0,冻结维护),2026-10 拆分为独立 module。
License
MIT — 见 LICENSE