Files
utility_go/config/config.go
2025-05-14 17:52:41 +08:00

71 lines
1.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package config
import (
"github.com/apolloconfig/agollo/v4/storage"
"github.com/ayflying/utility_go/pkg"
"github.com/gogf/gf/contrib/config/apollo/v2"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"sync"
)
var (
//ApolloCfg *apolloConfig.AppConfig
ApolloCfg *apollo.Config
ApolloListener []string
Item2Obj = map[string]Load{}
)
// load接口定义了Load方法用于加载数据
type Load interface {
Load(cfg ...string)
}
type Cfg struct {
Lock sync.Mutex
}
// Deprecated : pkg.Config().GetDbFile(name)
func (c *Cfg) GetDbFile(name string) (res *g.Var, err error) {
res, err = pkg.Config().GetDbFile(name)
return
}
// Deprecated : pkg.Config().GetFile(name, obj...)
func (c *Cfg) GetFile(filename string, obj ...Load) (jsonObj *gjson.Json, err error) {
jsonObj, err = pkg.Config().GetFile(filename)
return
}
// getUrlFile 获取远程配置
// Deprecated : pkg.Config().GetUrlFile(name)
func (c *Cfg) GetUrlFile(name string) (jsonObj *gjson.Json, err error) {
jsonObj, err = pkg.Config().GetUrlFile(name)
return
}
// Deprecated : pkg.Config().GetApollo(name, obj)
func (c *Cfg) GetApollo(name string, obj Load) (jsonObj *gjson.Json, err error) {
jsonObj, err = pkg.Config().GetApollo(name, obj)
return
}
// 阿波罗监听器
type CustomChangeListener struct {
wg sync.WaitGroup
}
func (c *CustomChangeListener) OnChange(changeEvent *storage.ChangeEvent) {
g.Log().Debugf(nil, "当前Namespace变化了%v", changeEvent.Namespace)
filename := changeEvent.Namespace
if obj, ok := Item2Obj[filename]; ok {
//重载配置文件
obj.Load(changeEvent.Changes["content"].NewValue.(string))
}
}
func (c *CustomChangeListener) OnNewestChange(event *storage.FullChangeEvent) {
//write your code here
}