feat: config.

This commit is contained in:
Bobo
2025-06-02 15:12:30 +08:00
parent f7166c40b0
commit 13aa5042b4
12 changed files with 37 additions and 1306 deletions

View File

@@ -9,7 +9,6 @@ import (
kratosRegistry "github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/transport"
"github.com/tx7do/kratos-bootstrap/config"
"github.com/tx7do/kratos-bootstrap/logger"
"github.com/tx7do/kratos-bootstrap/tracer"
@@ -43,28 +42,28 @@ func NewApp(ll log.Logger, rr kratosRegistry.Registrar, srv ...transport.Server)
// DoBootstrap 执行引导
func DoBootstrap(serviceInfo *utils.ServiceInfo) (*conf.Bootstrap, log.Logger, kratosRegistry.Registrar) {
// inject command flags
Flags := config.NewCommandFlags()
Flags := NewCommandFlags()
Flags.Init()
var err error
// load configs
if err = config.LoadBootstrapConfig(Flags.Conf); err != nil {
if err = LoadBootstrapConfig(Flags.Conf); err != nil {
panic(fmt.Sprintf("load config failed: %v", err))
}
// init logger
ll := logger.NewLoggerProvider(config.GetBootstrapConfig().Logger, serviceInfo)
ll := logger.NewLoggerProvider(GetBootstrapConfig().Logger, serviceInfo)
// init registrar
reg := NewRegistry(config.GetBootstrapConfig().Registry)
reg := NewRegistry(GetBootstrapConfig().Registry)
// init tracer
if err = tracer.NewTracerProvider(config.GetBootstrapConfig().Trace, serviceInfo); err != nil {
if err = tracer.NewTracerProvider(GetBootstrapConfig().Trace, serviceInfo); err != nil {
panic(fmt.Sprintf("init tracer failed: %v", err))
}
return config.GetBootstrapConfig(), ll, reg
return GetBootstrapConfig(), ll, reg
}
type InitApp func(logger log.Logger, registrar kratosRegistry.Registrar, bootstrap *conf.Bootstrap) (*kratos.App, func(), error)

View File

@@ -0,0 +1,65 @@
package bootstrap
import conf "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
var configList []interface{}
var commonConfig = &conf.Bootstrap{}
func GetBootstrapConfig() *conf.Bootstrap {
return commonConfig
}
// RegisterConfig 注册配置
func RegisterConfig(c interface{}) {
initBootstrapConfig()
configList = append(configList, c)
}
func initBootstrapConfig() {
if len(configList) > 0 {
return
}
configList = append(configList, commonConfig)
if commonConfig.Server == nil {
commonConfig.Server = &conf.Server{}
configList = append(configList, commonConfig.Server)
}
if commonConfig.Client == nil {
commonConfig.Client = &conf.Client{}
configList = append(configList, commonConfig.Client)
}
if commonConfig.Data == nil {
commonConfig.Data = &conf.Data{}
configList = append(configList, commonConfig.Data)
}
if commonConfig.Trace == nil {
commonConfig.Trace = &conf.Tracer{}
configList = append(configList, commonConfig.Trace)
}
if commonConfig.Logger == nil {
commonConfig.Logger = &conf.Logger{}
configList = append(configList, commonConfig.Logger)
}
if commonConfig.Registry == nil {
commonConfig.Registry = &conf.Registry{}
configList = append(configList, commonConfig.Registry)
}
if commonConfig.Oss == nil {
commonConfig.Oss = &conf.OSS{}
configList = append(configList, commonConfig.Oss)
}
if commonConfig.Notify == nil {
commonConfig.Notify = &conf.Notification{}
configList = append(configList, commonConfig.Notify)
}
}

View File

@@ -8,7 +8,6 @@ import (
"github.com/go-kratos/kratos/v2/registry"
v1 "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
"github.com/tx7do/kratos-bootstrap/config"
)
func initApp(logger log.Logger, registrar registry.Registrar, _ *v1.Bootstrap) (*kratos.App, func(), error) {
@@ -35,7 +34,7 @@ func initAppEx(logger log.Logger, registrar registry.Registrar, _ *v1.Bootstrap,
func TestCustomBootstrap(t *testing.T) {
customCfg := &CustomConfig{}
config.RegisterConfig(customCfg)
RegisterConfig(customCfg)
Service.SetName("test")
Service.SetVersion("v0.0.1")

98
bootstrap/config.go Normal file
View File

@@ -0,0 +1,98 @@
package bootstrap
import (
"github.com/go-kratos/kratos/v2/config"
fileKratos "github.com/go-kratos/kratos/v2/config/file"
"github.com/go-kratos/kratos/v2/log"
conf "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
"github.com/tx7do/kratos-bootstrap/utils"
)
// NewFileConfigSource 创建一个本地文件配置源
func NewFileConfigSource(filePath string) config.Source {
return fileKratos.NewSource(filePath)
}
// NewConfigProvider 创建一个配置
func NewConfigProvider(configPath string) config.Config {
err, rc := LoadRemoteConfigSourceConfigs(configPath)
if err != nil {
log.Error("LoadRemoteConfigSourceConfigs: ", err.Error())
}
if rc != nil {
return config.New(
config.WithSource(
NewFileConfigSource(configPath),
NewRemoteConfigSource(rc),
),
)
} else {
return config.New(
config.WithSource(
NewFileConfigSource(configPath),
),
)
}
}
// LoadBootstrapConfig 加载程序引导配置
func LoadBootstrapConfig(configPath string) error {
cfg := NewConfigProvider(configPath)
var err error
if err = cfg.Load(); err != nil {
return err
}
initBootstrapConfig()
if err = scanConfigs(cfg); err != nil {
return err
}
return nil
}
func scanConfigs(cfg config.Config) error {
initBootstrapConfig()
for _, c := range configList {
if err := cfg.Scan(c); err != nil {
return err
}
}
return nil
}
// LoadRemoteConfigSourceConfigs 加载远程配置源的本地配置
func LoadRemoteConfigSourceConfigs(configPath string) (error, *conf.RemoteConfig) {
configPath = configPath + "/" + remoteConfigSourceConfigFile
if !utils.PathExists(configPath) {
return nil, nil
}
cfg := config.New(
config.WithSource(
NewFileConfigSource(configPath),
),
)
defer func(cfg config.Config) {
if err := cfg.Close(); err != nil {
panic(err)
}
}(cfg)
var err error
if err = cfg.Load(); err != nil {
return err, nil
}
if err = scanConfigs(cfg); err != nil {
return err, nil
}
return nil, GetBootstrapConfig().Config
}

12
bootstrap/config_test.go Normal file
View File

@@ -0,0 +1,12 @@
package bootstrap
import (
"testing"
)
func TestRegisterConfig(t *testing.T) {
var cfg struct {
Test string
}
RegisterConfig(&cfg)
}

46
bootstrap/flag.go Normal file
View File

@@ -0,0 +1,46 @@
package bootstrap
import (
"flag"
"github.com/tx7do/kratos-bootstrap/utils"
)
// CommandFlags 命令传参
type CommandFlags struct {
Conf string // 引导配置文件路径,默认为:../../configs
Env string // 开发环境dev、debug……
ConfigHost string // 远程配置服务端地址
ConfigType string // 远程配置服务端类型
Daemon bool // 是否转为守护进程
}
func NewCommandFlags() *CommandFlags {
f := &CommandFlags{
Conf: "",
Env: "",
ConfigHost: "",
ConfigType: "",
Daemon: false,
}
f.defineFlag()
return f
}
func (f *CommandFlags) defineFlag() {
flag.StringVar(&f.Conf, "conf", "../../configs", "config path, eg: -conf ../../configs")
flag.StringVar(&f.Env, "env", "dev", "runtime environment, eg: -env dev")
flag.StringVar(&f.ConfigHost, "chost", "127.0.0.1:8500", "config server host, eg: -chost 127.0.0.1:8500")
flag.StringVar(&f.ConfigType, "ctype", "consul", "config server host, eg: -ctype consul")
flag.BoolVar(&f.Daemon, "d", false, "run app as a daemon with -d=true.")
}
func (f *CommandFlags) Init() {
flag.Parse()
if f.Daemon {
utils.BeDaemon("-d")
}
}

View File

@@ -8,10 +8,24 @@ replace (
github.com/armon/go-metrics => github.com/hashicorp/go-metrics v0.4.1
github.com/tx7do/kratos-bootstrap/api => ../api
github.com/tx7do/kratos-bootstrap/config => ../config
github.com/tx7do/kratos-bootstrap/logger => ../logger
github.com/tx7do/kratos-bootstrap/registry => ../registry
github.com/tx7do/kratos-bootstrap/registry/consul => ../registry/consul
github.com/tx7do/kratos-bootstrap/registry/etcd => ../registry/etcd
github.com/tx7do/kratos-bootstrap/registry/eureka => ../registry/eureka
github.com/tx7do/kratos-bootstrap/registry/kubernetes => ../registry/kubernetes
github.com/tx7do/kratos-bootstrap/registry/nacos => ../registry/nacos
github.com/tx7do/kratos-bootstrap/registry/polaris => ../registry/polaris
github.com/tx7do/kratos-bootstrap/registry/servicecomb => ../registry/servicecomb
github.com/tx7do/kratos-bootstrap/registry/zookeeper => ../registry/zookeeper
github.com/tx7do/kratos-bootstrap/remoteconfig/apollo => ../remoteconfig/apollo
github.com/tx7do/kratos-bootstrap/remoteconfig/consul => ../remoteconfig/consul
github.com/tx7do/kratos-bootstrap/remoteconfig/etcd => ../remoteconfig/etcd
github.com/tx7do/kratos-bootstrap/remoteconfig/kubernetes => ../remoteconfig/kubernetes
github.com/tx7do/kratos-bootstrap/remoteconfig/nacos => ../remoteconfig/nacos
github.com/tx7do/kratos-bootstrap/remoteconfig/polaris => ../remoteconfig/polaris
github.com/tx7do/kratos-bootstrap/tracer => ../tracer
github.com/tx7do/kratos-bootstrap/utils => ../utils
)
@@ -22,7 +36,6 @@ require (
github.com/olekukonko/tablewriter v1.0.7
github.com/spf13/cobra v1.9.1
github.com/tx7do/kratos-bootstrap/api v0.0.21
github.com/tx7do/kratos-bootstrap/config v0.0.10
github.com/tx7do/kratos-bootstrap/logger v0.0.10
github.com/tx7do/kratos-bootstrap/registry v0.1.0
github.com/tx7do/kratos-bootstrap/registry/consul v0.1.0
@@ -32,6 +45,12 @@ require (
github.com/tx7do/kratos-bootstrap/registry/nacos v0.1.0
github.com/tx7do/kratos-bootstrap/registry/servicecomb v0.1.0
github.com/tx7do/kratos-bootstrap/registry/zookeeper v0.1.0
github.com/tx7do/kratos-bootstrap/remoteconfig/apollo v0.1.0
github.com/tx7do/kratos-bootstrap/remoteconfig/consul v0.1.0
github.com/tx7do/kratos-bootstrap/remoteconfig/etcd v0.1.0
github.com/tx7do/kratos-bootstrap/remoteconfig/kubernetes v0.1.0
github.com/tx7do/kratos-bootstrap/remoteconfig/nacos v0.1.1
github.com/tx7do/kratos-bootstrap/remoteconfig/polaris v0.1.0
github.com/tx7do/kratos-bootstrap/tracer v0.0.10
github.com/tx7do/kratos-bootstrap/utils v0.1.3
golang.org/x/tools v0.33.0
@@ -154,12 +173,6 @@ require (
github.com/tencentcloud/tencentcloud-cls-sdk-go v1.0.11 // indirect
github.com/tinylib/msgp v1.3.0 // indirect
github.com/tjfoc/gmsm v1.4.1 // indirect
github.com/tx7do/kratos-bootstrap/remoteconfig/apollo v0.1.0 // indirect
github.com/tx7do/kratos-bootstrap/remoteconfig/consul v0.1.0 // indirect
github.com/tx7do/kratos-bootstrap/remoteconfig/etcd v0.1.0 // indirect
github.com/tx7do/kratos-bootstrap/remoteconfig/kubernetes v0.1.0 // indirect
github.com/tx7do/kratos-bootstrap/remoteconfig/nacos v0.1.0 // indirect
github.com/tx7do/kratos-bootstrap/remoteconfig/polaris v0.1.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.etcd.io/etcd/api/v3 v3.6.0 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.6.0 // indirect

View File

@@ -628,32 +628,6 @@ github.com/tjfoc/gmsm v1.3.2/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn
github.com/tjfoc/gmsm v1.4.1 h1:aMe1GlZb+0bLjn+cKTPEvvn9oUEBlJitaZiiBwsbgho=
github.com/tjfoc/gmsm v1.4.1/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/tx7do/kratos-bootstrap/registry/consul v0.1.0 h1:O25Fb6c7pK2wQcaCsek3Rr5aWFwhyd493onVPwMKCd0=
github.com/tx7do/kratos-bootstrap/registry/consul v0.1.0/go.mod h1:sQeHz3LTab9nai22ID++nY1Epixljh/XbXWG84lHskY=
github.com/tx7do/kratos-bootstrap/registry/etcd v0.1.0 h1:hRjBltNtwxo6Tq10Xg4VJa3PQXKpmLgIqk3+Pkc/Q20=
github.com/tx7do/kratos-bootstrap/registry/etcd v0.1.0/go.mod h1:KlKs9hUA0rLr3oXddz/Svp+O83YDSNBSrMtNj/8fUHE=
github.com/tx7do/kratos-bootstrap/registry/eureka v0.1.0 h1:nF7O5At2lkYDOJnYDupvF84nXFYdHoIIOnk2SltFtT4=
github.com/tx7do/kratos-bootstrap/registry/eureka v0.1.0/go.mod h1:7DrS4kWWyjPGYLU6ZK66yixCsVVrLBokJM/zVRhWsBU=
github.com/tx7do/kratos-bootstrap/registry/kubernetes v0.1.0 h1:g9WKluXphQKn8+Cqf2zaQ6MZ/CKfuWHAOFUWMYhSJ8A=
github.com/tx7do/kratos-bootstrap/registry/kubernetes v0.1.0/go.mod h1:cnTOKn2L1zhySWTyA8zIpOyKryqCzH9jj0x+dFSF7s0=
github.com/tx7do/kratos-bootstrap/registry/nacos v0.1.0 h1:u/mJlI5CdPsHrfPOhzcS1fT72yLRkLu50lhYwkXYZtM=
github.com/tx7do/kratos-bootstrap/registry/nacos v0.1.0/go.mod h1:/sBbHxWAcCAj4trjmguwViS/za2CelOTQPnDKExbo74=
github.com/tx7do/kratos-bootstrap/registry/servicecomb v0.1.0 h1:sJZwmEzdUzC4qn5+tyTjujIl/EP4pq4Ve9RhXAp2T2M=
github.com/tx7do/kratos-bootstrap/registry/servicecomb v0.1.0/go.mod h1:3J4HMpqYKaYMaHeQ2JGm9LiLCj0HEpbJEMNTfi/5yps=
github.com/tx7do/kratos-bootstrap/registry/zookeeper v0.1.0 h1:jnb4mKxhHY3fjofEsuT47YC6id98ePGqeOvwsqjo4Pw=
github.com/tx7do/kratos-bootstrap/registry/zookeeper v0.1.0/go.mod h1:J1djr1sRj0bgYqweR+lm4xADePt6oC2HhAr4xNyYep4=
github.com/tx7do/kratos-bootstrap/remoteconfig/apollo v0.1.0 h1:gEi8ukkZ7+9QWi5yvc3u2LX4Tmcro5QSrqrt1dhGidY=
github.com/tx7do/kratos-bootstrap/remoteconfig/apollo v0.1.0/go.mod h1:No4jfTwG6yGPaxUkbRPOQg8rrwiAsoYPEIvajfT5TBw=
github.com/tx7do/kratos-bootstrap/remoteconfig/consul v0.1.0 h1:QaZq/fbIEdPzsG5PsxLLwxFz8K4EwJsb+drZedjRxmo=
github.com/tx7do/kratos-bootstrap/remoteconfig/consul v0.1.0/go.mod h1:DfV9MOzBhQbjrcJJo4yzBLl2e87gw4pxG0rrp6aN/Fk=
github.com/tx7do/kratos-bootstrap/remoteconfig/etcd v0.1.0 h1:XRMZlAmgebrsx4pJJNR7XqNiPNEb//1Ovmj8M2xJGO8=
github.com/tx7do/kratos-bootstrap/remoteconfig/etcd v0.1.0/go.mod h1:bnpvKsNputwma0ddQ8b5IEPlNy2MYFmYi6Xf0F7m18o=
github.com/tx7do/kratos-bootstrap/remoteconfig/kubernetes v0.1.0 h1:yPjm22Iu/szERlog8KIwDm1DWidij4U8/cpFN63Ei4A=
github.com/tx7do/kratos-bootstrap/remoteconfig/kubernetes v0.1.0/go.mod h1:53m4kOfvXfyrIjUq/ZK7hXY5LraJFbWrR4y9Qqkjlp0=
github.com/tx7do/kratos-bootstrap/remoteconfig/nacos v0.1.0 h1:clGiOIkrV+5ICVxo2UXzLofbHgSNeQQ3ktmnSBtC6mI=
github.com/tx7do/kratos-bootstrap/remoteconfig/nacos v0.1.0/go.mod h1:RcCVpBOkRkpVdX8m5EdevwFy91EzXA+TocLncNRG4j4=
github.com/tx7do/kratos-bootstrap/remoteconfig/polaris v0.1.0 h1:mmJL9YXmspyRfKr7LbYE0e5cNpRiVpzZppE/QHOzVM0=
github.com/tx7do/kratos-bootstrap/remoteconfig/polaris v0.1.0/go.mod h1:43O4L+Xe6fItWsBMSYAmrGjxRXVKzreOxD1H01U5uwE=
github.com/uber/jaeger-client-go v2.30.0+incompatible h1:D6wyKGCecFaSRUpo8lCVbaOOb6ThwMmTEbhRwtKR97o=
github.com/uber/jaeger-client-go v2.30.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg=

View File

@@ -0,0 +1,49 @@
package bootstrap
import (
"github.com/go-kratos/kratos/v2/config"
conf "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
"github.com/tx7do/kratos-bootstrap/remoteconfig/apollo"
"github.com/tx7do/kratos-bootstrap/remoteconfig/consul"
"github.com/tx7do/kratos-bootstrap/remoteconfig/etcd"
"github.com/tx7do/kratos-bootstrap/remoteconfig/kubernetes"
"github.com/tx7do/kratos-bootstrap/remoteconfig/nacos"
"github.com/tx7do/kratos-bootstrap/remoteconfig/polaris"
)
const remoteConfigSourceConfigFile = "remote.yaml"
type Type string
const (
LocalFile Type = "file"
Nacos Type = "nacos"
Consul Type = "consul"
Etcd Type = "etcd"
Apollo Type = "apollo"
Kubernetes Type = "kubernetes"
Polaris Type = "polaris"
)
// NewRemoteConfigSource 创建一个远程配置源
func NewRemoteConfigSource(c *conf.RemoteConfig) config.Source {
switch Type(c.Type) {
default:
fallthrough
case LocalFile:
return nil
case Nacos:
return nacos.NewConfigSource(c)
case Consul:
return consul.NewConfigSource(c)
case Etcd:
return etcd.NewConfigSource(c)
case Apollo:
return apollo.NewConfigSource(c)
case Kubernetes:
return kubernetes.NewConfigSource(c)
case Polaris:
return polaris.NewConfigSource(c)
}
}