初始化
服务运行时
xMain.Runner 统一启动 HTTP 服务并支持优雅关闭
服务运行时
xMain.Runner 用于统一启动 HTTP 服务、托管后台协程并在信号到达时优雅关闭。
import xMain "github.com/bamboo-services/bamboo-base-go/major/main"Runner
func Runner(
reg *xReg.Reg,
log *xLog.LogNamedLogger,
opts []xOption.Option,
goroutineFunc ...func(ctx context.Context, extra ...any),
)功能
- 从环境变量读取
XLF_HOST与XLF_PORT作为监听地址,默认localhost:1118。 - 接收
opts []xOption.Option,自动装配缓存、数据库等内置组件(推荐通过Register传入 opts,业务节点立即可用) - 自动监听
SIGINT/SIGTERM,触发优雅关闭。 - 支持注册路由函数(传统方式)或通过 Option 声明式注册路由
- 支持启动多个后台协程并统一等待退出。
- HTTP 关闭超时时间为 30 秒。
参数说明
字段
类型
快速使用
package main
import (
"context"
xCtx "github.com/bamboo-services/bamboo-base-go/defined/context"
xLog "github.com/bamboo-services/bamboo-base-go/common/log"
xMain "github.com/bamboo-services/bamboo-base-go/major/main"
xOption "github.com/bamboo-services/bamboo-base-go/major/option"
xOptCache "github.com/bamboo-services/bamboo-base-go/major/option/cache"
xOptDatabase "github.com/bamboo-services/bamboo-base-go/major/option/database"
xReg "github.com/bamboo-services/bamboo-base-go/major/register"
xRegNode "github.com/bamboo-services/bamboo-base-go/major/register/node"
// v1.2.0 起驱动插件化:空白导入 MySQL 驱动触发注册
_ "github.com/bamboo-services/bamboo-base-go/plugins/database/mysql"
)
func main() {
// 业务自定义节点(注意:DatabaseKey/RedisClientKey/CacheManagerKey/SnowflakeNodeKey 是框架保留键,
// 不能出现在 nodeList 中,否则 Register 会因重复注册 panic)
reg := xReg.Register(context.Background(),
[]xRegNode.RegNodeList{
{Key: xCtx.CustomClientKey, Node: initCustomSDK},
},
// v1.0.5 起:opts 作为变参直接传给 Register,统一装配内置组件
xOption.WithCache(xOptCache.WithRedis("localhost:6379")),
// v1.2.0 起:用 WithDriver 替代 MySQL() 构造函数
xOption.WithDatabase(xOptDatabase.WithDriver(xOptDatabase.DriverMySQL, "user:pass@tcp(localhost:3306)/bamboo")),
xOption.WithRoute(registerRoute),
)
log := xLog.WithName(xLog.NamedMAIN)
// Runner 启动 HTTP 服务并优雅关闭
xMain.Runner(reg, log, nil)
}仅路由(无内置组件)
// opts 传给 Register(方式 A,推荐)
reg := xReg.Register(context.Background(), nil,
xOption.WithRoute(registerRoute),
)
xMain.Runner(reg, log, nil)带后台任务示例
func runConsumer(ctx context.Context, extra ...any) {
for {
select {
case <-ctx.Done():
return
default:
// 消费任务
}
}
}
func main() {
// ... reg, log 初始化省略
// opts 传给 Register(方式 A,推荐)
reg := xReg.Register(context.Background(), nil,
xOption.WithRoute(registerRoute),
)
xMain.Runner(reg, log, nil, runConsumer)
}如需接入 gRPC(可选),请前往独立模块文档:
gRPC 运行时
Option 声明式配置
历史演进:
- v1.0.3:
Runner接受[]xOption.Option,开始支持声明式装配内置组件 - v1.0.4:缓存与数据库改为两层调用形态,构造函数收敛到
xOptCache/xOptDatabase子包 - v1.0.5:
Register同样接受opts ...xOption.Option变参,opts 直接在注册阶段装配,业务节点可立即从ctx取到 DB / 缓存 - v1.2.0:数据库驱动插件化,
MySQL()/Postgres()等构造函数移除,改用WithDriver/WithDialector并导入对应驱动插件
两种传参方式等价,二选一即可:
import (
xOption "github.com/bamboo-services/bamboo-base-go/major/option"
xOptCache "github.com/bamboo-services/bamboo-base-go/major/option/cache"
xOptDatabase "github.com/bamboo-services/bamboo-base-go/major/option/database"
_ "github.com/bamboo-services/bamboo-base-go/plugins/database/mysql" // v1.2.0 起需空白导入驱动插件
)
reg := xReg.Register(context.Background(), nil,
xOption.WithCache(xOptCache.WithRedis("localhost:6379")),
xOption.WithDatabase(xOptDatabase.WithDriver(xOptDatabase.DriverMySQL, "user:pass@tcp(localhost:3306)/bamboo")),
xOption.WithRoute(registerRoute),
)
// 此时 reg.Init.Ctx 已含 *gorm.DB / *xCache.Manager,业务节点可用
xMain.Runner(reg, log, nil)import _ "github.com/bamboo-services/bamboo-base-go/plugins/database/mysql" // v1.2.0 起需空白导入驱动插件
reg := xReg.Register(context.Background(), nil) // 仅业务自定义节点
opts := []xOption.Option{
xOption.WithCache(xOptCache.WithRedis("localhost:6379")),
xOption.WithDatabase(xOptDatabase.WithDriver(xOptDatabase.DriverMySQL, "user:pass@tcp(localhost:3306)/bamboo")),
xOption.WithRoute(registerRoute),
}
xMain.Runner(reg, log, opts)nil 或空
opts表示不启用任何内置实现,仅以 reg 现有状态启动。