1 Star 0 Fork 0

bugsmo/kratos-bootstrap

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
bootstrap.go 2.21 KB
一键复制 编辑 原始数据 按行查看 历史
bugsmo 提交于 2024-06-01 21:56 . v1.0.0
package bootstrap
import (
"fmt"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/log"
kratosRegistry "github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/transport"
"gitee.com/ssxlt/kratos-bootstrap/config"
"gitee.com/ssxlt/kratos-bootstrap/logger"
"gitee.com/ssxlt/kratos-bootstrap/registry"
"gitee.com/ssxlt/kratos-bootstrap/tracer"
conf "gitee.com/ssxlt/kratos-bootstrap/api/gen/go/conf/v1"
)
var (
Service = config.NewServiceInfo(
"",
"1.0.0",
"",
)
)
// NewApp 创建应用程序
func NewApp(ll log.Logger, rr kratosRegistry.Registrar, srv ...transport.Server) *kratos.App {
return kratos.New(
kratos.ID(Service.GetInstanceId()),
kratos.Name(Service.Name),
kratos.Version(Service.Version),
kratos.Metadata(Service.Metadata),
kratos.Logger(ll),
kratos.Server(
srv...,
),
kratos.Registrar(rr),
)
}
// DoBootstrap 执行引导
func DoBootstrap(serviceInfo *config.ServiceInfo) (*conf.Bootstrap, log.Logger, kratosRegistry.Registrar) {
// inject command flags
Flags := config.NewCommandFlags()
Flags.Init()
var err error
// load configs
if err = config.LoadBootstrapConfig(Flags.Conf); err != nil {
panic(fmt.Sprintf("load config failed: %v", err))
}
// init logger
ll := logger.NewLoggerProvider(config.GetBootstrapConfig().Logger, serviceInfo)
// init registrar
reg := registry.NewRegistry(config.GetBootstrapConfig().Registry)
// init tracer
if err = tracer.NewTracerProvider(config.GetBootstrapConfig().Trace, serviceInfo); err != nil {
panic(fmt.Sprintf("init tracer failed: %v", err))
}
return config.GetBootstrapConfig(), ll, reg
}
type InitApp func(logger log.Logger, registrar kratosRegistry.Registrar, bootstrap *conf.Bootstrap) (*kratos.App, func(), error)
// Bootstrap 应用引导启动
func Bootstrap(initApp InitApp, serviceName, version *string) {
if serviceName != nil && len(*serviceName) != 0 {
Service.Name = *serviceName
}
if version != nil && len(*version) != 0 {
Service.Version = *version
}
// bootstrap
cfg, ll, reg := DoBootstrap(Service)
// init app
app, cleanup, err := initApp(ll, reg, cfg)
if err != nil {
panic(err)
}
defer cleanup()
// run the app.
if err = app.Run(); err != nil {
panic(err)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ssxlt/kratos-bootstrap.git
[email protected]:ssxlt/kratos-bootstrap.git
ssxlt
kratos-bootstrap
kratos-bootstrap
main

搜索帮助