bootstrap

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 30 Imported by: 0

README

应用程序引导

概述

此包负责程序的引导配置管理。提供一个线程安全的初始化流程和配置注册机制,用于在应用启动阶段集中管理各类配置结构体(例如服务器、客户端、数据、日志等)。

设计要点

  • 延迟初始化:使用 sync.Once 确保引导配置仅初始化一次。
  • 并发安全:读写操作通过 sync.RWMutex 保护。
  • 配置注册:通过 RegisterConfig 注册任意非空指针类型配置(例如 &conf.SomeConfig{}),内部对同一指针地址做去重。
  • 主配置访问:使用 GetBootstrapConfig 获取共享的 *conf.Bootstrap 实例。

使用示例

package main

import (
    "github.com/codedongking/kratos-bootstrap/bootstrap"
    "github.com/codedongking/kratos-bootstrap/bootstrap/api/gen/go/conf/v1"

	//_ "github.com/codedongking/kratos-bootstrap/config/apollo"
	//_ "github.com/codedongking/kratos-bootstrap/config/consul"
	_ "github.com/codedongking/kratos-bootstrap/config/etcd"
	//_ "github.com/codedongking/kratos-bootstrap/config/kubernetes"
	//_ "github.com/codedongking/kratos-bootstrap/config/nacos"
	//_ "github.com/codedongking/kratos-bootstrap/config/polaris"

	//_ "github.com/codedongking/kratos-bootstrap/logger/aliyun"
	//_ "github.com/codedongking/kratos-bootstrap/logger/fluent"
	//_ "github.com/codedongking/kratos-bootstrap/logger/logrus"
	//_ "github.com/codedongking/kratos-bootstrap/logger/tencent"
	//_ "github.com/codedongking/kratos-bootstrap/logger/zap"
	//_ "github.com/codedongking/kratos-bootstrap/logger/zerolog"
	
	//_ "github.com/codedongking/kratos-bootstrap/registry/consul"
	_ "github.com/codedongking/kratos-bootstrap/registry/etcd"
	//_ "github.com/codedongking/kratos-bootstrap/registry/eureka"
	//_ "github.com/codedongking/kratos-bootstrap/registry/kubernetes"
	//_ "github.com/codedongking/kratos-bootstrap/registry/nacos"
	//_ "github.com/codedongking/kratos-bootstrap/registry/polaris"
	//_ "github.com/codedongking/kratos-bootstrap/registry/servicecomb"
	//_ "github.com/codedongking/kratos-bootstrap/registry/zookeeper"
)

var version string

// go build -ldflags "-X main.version=x.y.z"

func newApp(
	lg log.Logger,
	re registry.Registrar,
	hs *http.Server,
) *kratos.App {
	return bootstrap.NewApp(
		lg,
		re,
		hs,
	)
}

func main() {
	bootstrap.Bootstrap(initApp, trans.Ptr(service.AdminService), trans.Ptr(version))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AdjustAppInfo

func AdjustAppInfo(ai *conf.AppInfo)

AdjustAppInfo 调整应用信息,设置默认值

func BeDaemon

func BeDaemon(arg string)

BeDaemon 将当前进程转为守护进程(尝试启动脱离的子进程并退出父进程)

func NewApp

func NewApp(ctx *Context, srv ...transport.Server) *kratos.App

NewApp 创建应用程序

func NewAppInfo

func NewAppInfo(appId, version, appName *string) *conf.AppInfo

NewAppInfo 创建应用信息

func NewAppName

func NewAppName(project, appId string) string

NewAppName 生成应用名称

func NewInstanceId

func NewInstanceId(project, appId, version, host string) string

NewInstanceId 生成实例ID 格式:project-appId-version@host@xid

func NewRootCmd

func NewRootCmd(f *CommandFlags, runE func(cmd *cobra.Command, args []string) error) *cobra.Command

NewRootCmd 创建根命令并绑定命令行参数和执行函数。

func ResolveHost

func ResolveHost() string

ResolveHost 返回优先级选择的 host 标识:POD_NAME -> HOSTNAME env -> os.Hostname() -> 首个非 loopback IPv4 -> "unknown-host"

func RunApp

func RunApp(ctx *Context, initApp InitAppFunc, opts ...func(root *cobra.Command)) error

RunApp 运行应用程序并允许在执行前对 root 命令做定制。 opts 可用于注册子命令、对 root 添加 flag 或其他修改。

Types

type CommandFlags

type CommandFlags struct {
	Conf       string // 引导配置文件路径,默认为:../../configs
	Env        string // 开发环境:dev、debug……
	ConfigHost string // 远程配置服务端地址
	ConfigType string // 远程配置服务端类型
	Daemon     bool   // 是否转为守护进程
}

CommandFlags 命令传参

func NewCommandFlags

func NewCommandFlags() *CommandFlags

func (*CommandFlags) AddFlags

func (f *CommandFlags) AddFlags(cmd *cobra.Command)

AddFlags 将 flags 绑定到传入的 cobra.Command(通常是 root command)。

func (*CommandFlags) Init

func (f *CommandFlags) Init()

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context 引导上下文

func NewContext

func NewContext(parent context.Context, ai *conf.AppInfo) *Context

NewContext 创建带 cancel 的应用级 Context(传 nil 使用 Background)

func NewContextWithParam

func NewContextWithParam(parent context.Context, ai *conf.AppInfo, cfg *conf.Bootstrap, log kratosLog.Logger) *Context

func (*Context) CancelContext

func (c *Context) CancelContext()

CancelContext 触发取消(幂等)

func (*Context) Context

func (c *Context) Context() context.Context

Context 返回应用级根 context(保证非 nil)

func (*Context) DeleteCustomConfig

func (c *Context) DeleteCustomConfig(key string)

DeleteCustomConfig 删除自定义配置

func (*Context) GetAppInfo

func (c *Context) GetAppInfo() *conf.AppInfo

func (*Context) GetConfig

func (c *Context) GetConfig() *conf.Bootstrap

GetConfig 返回当前的 *conf.bootstrap(并发安全)

func (*Context) GetCustomConfig

func (c *Context) GetCustomConfig(key string) (any, bool)

GetCustomConfig 获取自定义配置(原始类型)

func (*Context) GetLogger

func (c *Context) GetLogger() kratosLog.Logger

func (*Context) GetRegistrar

func (c *Context) GetRegistrar() kratosRegistry.Registrar

func (*Context) GetValue

func (c *Context) GetValue(key string) (interface{}, bool)

GetValue 从通用存储读取值

func (*Context) NewLoggerHelper

func (c *Context) NewLoggerHelper(moduleName string) *kratosLog.Helper

func (*Context) PrintAppInfo

func (c *Context) PrintAppInfo()

func (*Context) RangeCustomConfig

func (c *Context) RangeCustomConfig(fn func(key string, val any) bool)

RangeCustomConfig 遍历自定义配置,回调返回 false 可停止遍历

func (*Context) RegisterCustomConfig

func (c *Context) RegisterCustomConfig(key string, cfg proto.Message)

RegisterCustomConfig 注册自定义配置

func (*Context) SetCustomConfig

func (c *Context) SetCustomConfig(key string, cfg proto.Message)

SetCustomConfig 存入自定义配置

func (*Context) SetValue

func (c *Context) SetValue(key string, val interface{})

SetValue 将任意值放入通用存储

func (*Context) UpTime

func (c *Context) UpTime() time.Duration

UpTime 返回应用已运行时间

type InitAppFunc

type InitAppFunc func(ctx *Context) (app *kratos.App, cleanup func(), err error)

InitAppFunc 应用初始化函数类型

Jump to

Keyboard shortcuts

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