4 Star 5 Fork 4

Plato / Service-Box-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
config.go 2.29 KB
一键复制 编辑 原始数据 按行查看 历史
package config
import (
"gitee.com/dennis-kk/service-box-go/util/config/reader"
"gitee.com/dennis-kk/service-box-go/util/config/source"
"gitee.com/dennis-kk/service-box-go/util/config/source/apollo"
"gitee.com/dennis-kk/service-box-go/util/config/source/file"
"gitee.com/dennis-kk/service-box-go/util/config/source/memory"
"strings"
)
type (
OnReloadCallBack func(Config)
)
// Config is an interface abstraction for dynamic configuration
type Config interface {
// Values provide the reader.Values interface
reader.Values
// Init the config
Init(opts ...Option) error
// Start config main loop
Start() error
// Options in the config
Options() *Options
// Close Stop the config loader
Close() error
// Watch start reload watch
Watch(OnReloadCallBack)
// Load config sources
Load(source ...source.Source) error
}
var (
//DefaultConfig Default Config Manager
DefaultConfig Config
)
// NewConfig returns new config
func NewConfig(opts ...Option) (Config, error) {
return newConfig(opts...)
}
// Load config sources
func Load(source ...source.Source) error {
return DefaultConfig.Load(source...)
}
// LoadFile is shorthand for creating a file source and loading it
func LoadFile(path string) (Config, error) {
cfg, err := newConfig()
if err != nil {
return nil, err
}
err = cfg.Load(file.NewSource(
file.WithPath(path),
))
if err != nil {
return nil, err
}
return cfg, nil
}
func LoadYaml(cfgStr string) (Config, error) {
cfg, err := newConfig()
if err != nil {
return nil, err
}
err = cfg.Load(memory.NewSource(
memory.WithYaml([]byte(cfgStr)),
))
return cfg, err
}
func LoadApollo(apolloCfg *apollo.Config) (Config, error) {
cfg, err := newConfig()
if err != nil {
return nil, err
}
// 传入配置需要分割name space
spaces := strings.Split(apolloCfg.NameSpace, ",")
var sources []source.Source
for _, space := range spaces {
cfg := &apollo.Config{
Host: apolloCfg.Host,
AppId: apolloCfg.AppId,
NameSpace: space,
Cluster: apolloCfg.Cluster,
Secret: apolloCfg.Secret,
Timeout: apolloCfg.Timeout,
IsRetry: apolloCfg.IsRetry,
Auth: apolloCfg.Auth,
ConfigExt: apolloCfg.ConfigExt,
}
sources = append(sources, apollo.NewSource(
apollo.WithApolloHost(cfg),
))
}
err = cfg.Load(sources...)
return cfg, err
}
Go
1
https://gitee.com/dennis-kk/service-box-go.git
git@gitee.com:dennis-kk/service-box-go.git
dennis-kk
service-box-go
Service-Box-go
v0.5.16

搜索帮助

53164aa7 5694891 3bd8fe86 5694891