feat: refactor.
This commit is contained in:
12
bootstrap.go
12
bootstrap.go
@@ -2,10 +2,12 @@ package bootstrap
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/go-kratos/kratos/v2/log"
|
"github.com/go-kratos/kratos/v2/log"
|
||||||
"github.com/go-kratos/kratos/v2/registry"
|
"github.com/go-kratos/kratos/v2/registry"
|
||||||
|
|
||||||
conf "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
|
conf "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
|
||||||
|
"github.com/tx7do/kratos-bootstrap/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Bootstrap 应用引导启动
|
// Bootstrap 应用引导启动
|
||||||
@@ -17,20 +19,20 @@ func Bootstrap(serviceInfo *ServiceInfo) (*conf.Bootstrap, log.Logger, registry.
|
|||||||
var err error
|
var err error
|
||||||
|
|
||||||
// load configs
|
// load configs
|
||||||
if err = LoadBootstrapConfig(Flags.Conf); err != nil {
|
if err = config.LoadBootstrapConfig(Flags.Conf); err != nil {
|
||||||
panic(fmt.Sprintf("load config failed: %v", err))
|
panic(fmt.Sprintf("load config failed: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// init logger
|
// init logger
|
||||||
ll := NewLoggerProvider(commonConfig.Logger, serviceInfo)
|
ll := NewLoggerProvider(config.GetBootstrapConfig().Logger, serviceInfo)
|
||||||
|
|
||||||
// init registrar
|
// init registrar
|
||||||
reg := NewRegistry(commonConfig.Registry)
|
reg := NewRegistry(config.GetBootstrapConfig().Registry)
|
||||||
|
|
||||||
// init tracer
|
// init tracer
|
||||||
if err = NewTracerProvider(commonConfig.Trace, serviceInfo); err != nil {
|
if err = NewTracerProvider(config.GetBootstrapConfig().Trace, serviceInfo); err != nil {
|
||||||
panic(fmt.Sprintf("init tracer failed: %v", err))
|
panic(fmt.Sprintf("init tracer failed: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
return commonConfig, ll, reg
|
return config.GetBootstrapConfig(), ll, reg
|
||||||
}
|
}
|
||||||
|
|||||||
65
config/bootstrap_config.go
Normal file
65
config/bootstrap_config.go
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
package bootstrap
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/go-kratos/kratos/v2/config"
|
"github.com/go-kratos/kratos/v2/config"
|
||||||
"github.com/go-kratos/kratos/v2/log"
|
"github.com/go-kratos/kratos/v2/log"
|
||||||
|
|
||||||
|
fileKratos "github.com/go-kratos/kratos/v2/config/file"
|
||||||
|
|
||||||
"github.com/tx7do/kratos-bootstrap/config/apollo"
|
"github.com/tx7do/kratos-bootstrap/config/apollo"
|
||||||
"github.com/tx7do/kratos-bootstrap/config/consul"
|
"github.com/tx7do/kratos-bootstrap/config/consul"
|
||||||
"github.com/tx7do/kratos-bootstrap/config/etcd"
|
"github.com/tx7do/kratos-bootstrap/config/etcd"
|
||||||
@@ -11,69 +13,48 @@ import (
|
|||||||
"github.com/tx7do/kratos-bootstrap/config/nacos"
|
"github.com/tx7do/kratos-bootstrap/config/nacos"
|
||||||
"github.com/tx7do/kratos-bootstrap/config/polaris"
|
"github.com/tx7do/kratos-bootstrap/config/polaris"
|
||||||
|
|
||||||
// file
|
|
||||||
fileKratos "github.com/go-kratos/kratos/v2/config/file"
|
|
||||||
|
|
||||||
conf "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
|
conf "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
var commonConfig = &conf.Bootstrap{}
|
|
||||||
var configList []interface{}
|
|
||||||
|
|
||||||
const remoteConfigSourceConfigFile = "remote.yaml"
|
const remoteConfigSourceConfigFile = "remote.yaml"
|
||||||
|
|
||||||
// RegisterConfig 注册配置
|
type Type string
|
||||||
func RegisterConfig(c interface{}) {
|
|
||||||
initBootstrapConfig()
|
const (
|
||||||
configList = append(configList, c)
|
LocalFileType Type = "file"
|
||||||
|
NacosType Type = "nacos"
|
||||||
|
ConsulType Type = "consul"
|
||||||
|
EtcdType Type = "etcd"
|
||||||
|
ApolloType Type = "apollo"
|
||||||
|
KubernetesType Type = "kubernetes"
|
||||||
|
PolarisType Type = "polaris"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewRemoteConfigSource 创建一个远程配置源
|
||||||
|
func NewRemoteConfigSource(c *conf.RemoteConfig) config.Source {
|
||||||
|
switch Type(c.Type) {
|
||||||
|
default:
|
||||||
|
fallthrough
|
||||||
|
case LocalFileType:
|
||||||
|
return nil
|
||||||
|
case NacosType:
|
||||||
|
return nacos.NewConfigSource(c)
|
||||||
|
case ConsulType:
|
||||||
|
return consul.NewConfigSource(c)
|
||||||
|
case EtcdType:
|
||||||
|
return etcd.NewConfigSource(c)
|
||||||
|
case ApolloType:
|
||||||
|
return apollo.NewConfigSource(c)
|
||||||
|
case KubernetesType:
|
||||||
|
return kubernetes.NewConfigSource(c)
|
||||||
|
case PolarisType:
|
||||||
|
return polaris.NewConfigSource(c)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func initBootstrapConfig() {
|
// NewFileConfigSource 创建一个本地文件配置源
|
||||||
if len(configList) > 0 {
|
func NewFileConfigSource(filePath string) config.Source {
|
||||||
return
|
return fileKratos.NewSource(filePath)
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConfigProvider 创建一个配置
|
// NewConfigProvider 创建一个配置
|
||||||
@@ -156,44 +137,5 @@ func LoadRemoteConfigSourceConfigs(configPath string) (error, *conf.RemoteConfig
|
|||||||
return err, nil
|
return err, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, commonConfig.Config
|
return nil, GetBootstrapConfig().Config
|
||||||
}
|
|
||||||
|
|
||||||
type ConfigType string
|
|
||||||
|
|
||||||
const (
|
|
||||||
ConfigTypeLocalFile ConfigType = "file"
|
|
||||||
ConfigTypeNacos ConfigType = "nacos"
|
|
||||||
ConfigTypeConsul ConfigType = "consul"
|
|
||||||
ConfigTypeEtcd ConfigType = "etcd"
|
|
||||||
ConfigTypeApollo ConfigType = "apollo"
|
|
||||||
ConfigTypeKubernetes ConfigType = "kubernetes"
|
|
||||||
ConfigTypePolaris ConfigType = "polaris"
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewRemoteConfigSource 创建一个远程配置源
|
|
||||||
func NewRemoteConfigSource(c *conf.RemoteConfig) config.Source {
|
|
||||||
switch ConfigType(c.Type) {
|
|
||||||
default:
|
|
||||||
fallthrough
|
|
||||||
case ConfigTypeLocalFile:
|
|
||||||
return nil
|
|
||||||
case ConfigTypeNacos:
|
|
||||||
return nacos.NewConfigSource(c)
|
|
||||||
case ConfigTypeConsul:
|
|
||||||
return consul.NewConfigSource(c)
|
|
||||||
case ConfigTypeEtcd:
|
|
||||||
return etcd.NewConfigSource(c)
|
|
||||||
case ConfigTypeApollo:
|
|
||||||
return apollo.NewConfigSource(c)
|
|
||||||
case ConfigTypeKubernetes:
|
|
||||||
return kubernetes.NewConfigSource(c)
|
|
||||||
case ConfigTypePolaris:
|
|
||||||
return polaris.NewConfigSource(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewFileConfigSource 创建一个本地文件配置源
|
|
||||||
func NewFileConfigSource(filePath string) config.Source {
|
|
||||||
return fileKratos.NewSource(filePath)
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package bootstrap
|
package config
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package bootstrap
|
package config
|
||||||
|
|
||||||
import "os"
|
import "os"
|
||||||
|
|
||||||
19
go.mod
19
go.mod
@@ -6,16 +6,18 @@ toolchain go1.22.1
|
|||||||
|
|
||||||
replace (
|
replace (
|
||||||
github.com/tx7do/kratos-bootstrap/api => ./api
|
github.com/tx7do/kratos-bootstrap/api => ./api
|
||||||
|
|
||||||
|
github.com/tx7do/kratos-bootstrap/config/apollo => ./config/apollo
|
||||||
github.com/tx7do/kratos-bootstrap/config/consul => ./config/consul
|
github.com/tx7do/kratos-bootstrap/config/consul => ./config/consul
|
||||||
|
github.com/tx7do/kratos-bootstrap/config/etcd => ./config/etcd
|
||||||
|
github.com/tx7do/kratos-bootstrap/config/kubernetes => ./config/kubernetes
|
||||||
|
github.com/tx7do/kratos-bootstrap/config/nacos => ./config/nacos
|
||||||
|
github.com/tx7do/kratos-bootstrap/config/polaris => ./config/polaris
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-chassis/sc-client v0.7.0
|
github.com/go-chassis/sc-client v0.7.0
|
||||||
github.com/go-kratos/aegis v0.2.0
|
github.com/go-kratos/aegis v0.2.0
|
||||||
github.com/go-kratos/kratos/contrib/config/apollo/v2 v2.0.0-20240504101732-d0d5761f9ca8
|
|
||||||
github.com/go-kratos/kratos/contrib/config/etcd/v2 v2.0.0-20240504101732-d0d5761f9ca8
|
|
||||||
github.com/go-kratos/kratos/contrib/config/kubernetes/v2 v2.0.0-20240504101732-d0d5761f9ca8
|
|
||||||
github.com/go-kratos/kratos/contrib/config/nacos/v2 v2.0.0-20240504101732-d0d5761f9ca8
|
|
||||||
github.com/go-kratos/kratos/contrib/log/aliyun/v2 v2.0.0-20240504101732-d0d5761f9ca8
|
github.com/go-kratos/kratos/contrib/log/aliyun/v2 v2.0.0-20240504101732-d0d5761f9ca8
|
||||||
github.com/go-kratos/kratos/contrib/log/fluent/v2 v2.0.0-20240504101732-d0d5761f9ca8
|
github.com/go-kratos/kratos/contrib/log/fluent/v2 v2.0.0-20240504101732-d0d5761f9ca8
|
||||||
github.com/go-kratos/kratos/contrib/log/logrus/v2 v2.0.0-20240504101732-d0d5761f9ca8
|
github.com/go-kratos/kratos/contrib/log/logrus/v2 v2.0.0-20240504101732-d0d5761f9ca8
|
||||||
@@ -39,7 +41,12 @@ require (
|
|||||||
github.com/spf13/cobra v1.8.0
|
github.com/spf13/cobra v1.8.0
|
||||||
github.com/stretchr/testify v1.9.0
|
github.com/stretchr/testify v1.9.0
|
||||||
github.com/tx7do/kratos-bootstrap/api v0.0.1
|
github.com/tx7do/kratos-bootstrap/api v0.0.1
|
||||||
|
github.com/tx7do/kratos-bootstrap/config/apollo v0.0.1
|
||||||
github.com/tx7do/kratos-bootstrap/config/consul v0.0.1
|
github.com/tx7do/kratos-bootstrap/config/consul v0.0.1
|
||||||
|
github.com/tx7do/kratos-bootstrap/config/etcd v0.0.1
|
||||||
|
github.com/tx7do/kratos-bootstrap/config/kubernetes v0.0.1
|
||||||
|
github.com/tx7do/kratos-bootstrap/config/nacos v0.0.1
|
||||||
|
github.com/tx7do/kratos-bootstrap/config/polaris v0.0.1
|
||||||
go.etcd.io/etcd/client/v3 v3.5.13
|
go.etcd.io/etcd/client/v3 v3.5.13
|
||||||
go.opentelemetry.io/otel v1.26.0
|
go.opentelemetry.io/otel v1.26.0
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.26.0
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.26.0
|
||||||
@@ -77,6 +84,10 @@ require (
|
|||||||
github.com/go-chassis/openlog v1.1.3 // indirect
|
github.com/go-chassis/openlog v1.1.3 // indirect
|
||||||
github.com/go-errors/errors v1.0.1 // indirect
|
github.com/go-errors/errors v1.0.1 // indirect
|
||||||
github.com/go-kit/kit v0.10.0 // indirect
|
github.com/go-kit/kit v0.10.0 // indirect
|
||||||
|
github.com/go-kratos/kratos/contrib/config/apollo/v2 v2.0.0-20240504101732-d0d5761f9ca8 // indirect
|
||||||
|
github.com/go-kratos/kratos/contrib/config/etcd/v2 v2.0.0-20240504101732-d0d5761f9ca8 // indirect
|
||||||
|
github.com/go-kratos/kratos/contrib/config/kubernetes/v2 v2.0.0-20240504101732-d0d5761f9ca8 // indirect
|
||||||
|
github.com/go-kratos/kratos/contrib/config/nacos/v2 v2.0.0-20240504101732-d0d5761f9ca8 // indirect
|
||||||
github.com/go-logfmt/logfmt v0.5.0 // indirect
|
github.com/go-logfmt/logfmt v0.5.0 // indirect
|
||||||
github.com/go-logr/logr v1.4.1 // indirect
|
github.com/go-logr/logr v1.4.1 // indirect
|
||||||
github.com/go-logr/stdr v1.2.2 // indirect
|
github.com/go-logr/stdr v1.2.2 // indirect
|
||||||
|
|||||||
Reference in New Issue
Block a user