配置多次循环获取

This commit is contained in:
ayflying
2024-12-25 12:11:29 +08:00
parent 41dd5e4db9
commit 05d88a89f4

View File

@@ -11,7 +11,9 @@ import (
"github.com/gogf/gf/v2/os/gfile" "github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gres" "github.com/gogf/gf/v2/os/gres"
"github.com/gogf/gf/v2/text/gstr" "github.com/gogf/gf/v2/text/gstr"
"path"
"sync" "sync"
"time"
) )
var ( var (
@@ -22,10 +24,11 @@ var (
// load接口定义了Load方法用于加载数据 // load接口定义了Load方法用于加载数据
type Load interface { type Load interface {
Load() Load(cfg ...string)
} }
type Cfg struct { type Cfg struct {
Lock sync.Mutex
} }
func (c *Cfg) GetDbFile(name string) (res *g.Var, err error) { func (c *Cfg) GetDbFile(name string) (res *g.Var, err error) {
@@ -69,25 +72,49 @@ func (c *Cfg) GetUrlFile(name string) (jsonObj *gjson.Json, err error) {
// 获取阿波罗 // 获取阿波罗
func (c *Cfg) GetApollo(name string, obj Load) (jsonObj *gjson.Json, err error) { func (c *Cfg) GetApollo(name string, obj Load) (jsonObj *gjson.Json, err error) {
Item2Obj[name] = obj c.Lock.Lock()
defer c.Lock.Unlock()
//c2 := &config.AppConfig{ Item2Obj[name+".json"] = obj
// AppID: consts.ApolloAppID, var cfg = config.AppConfig{
// Cluster: consts.ApolloCluster, AppID: ApolloCfg.AppID,
// IP: consts.ApolloIP, Cluster: ApolloCfg.Cluster,
// NamespaceName: consts.ApolloNamespaceName, IP: ApolloCfg.IP,
// IsBackupConfig: consts.ApolloIsBackupConfig, NamespaceName: name + ".json",
// Secret: consts.ApolloSecret, Secret: ApolloCfg.Secret,
//} IsBackupConfig: ApolloCfg.IsBackupConfig,
BackupConfigPath: ApolloCfg.BackupConfigPath,
SyncServerTimeout: 60,
MustStart: true,
}
//cfg.NamespaceName = name + ".json"
client, err := agollo.StartWithConfig(func() (*config.AppConfig, error) { client, err := agollo.StartWithConfig(func() (*config.AppConfig, error) {
return ApolloCfg, nil return ApolloCfg, nil
}) })
if client == nil {
return
}
var getStr string
var getApollo *storage.Config
for range 10 {
getApollo = client.GetConfig(cfg.NamespaceName)
if getApollo != nil {
break
}
time.Sleep(time.Second * 5)
}
get := client.GetConfigCache(ApolloCfg.NamespaceName) if getApollo != nil {
getStr, err := get.Get("content") getStr = getApollo.GetValue("content")
if getStr != "" {
//写入配置
gfile.PutContents(path.Join("manifest", "game", name+".json"), getStr)
}
} else {
jsonObj, err = c.GetFile(name)
}
jsonObj, err = gjson.DecodeToJson(getStr) jsonObj, err = gjson.DecodeToJson(getStr)
//首次运行加入监听器 //首次运行加入监听器
if !gstr.InArray(ApolloListener, name) { if !gstr.InArray(ApolloListener, name) {
c2 := &CustomChangeListener{} c2 := &CustomChangeListener{}
@@ -115,16 +142,9 @@ func (c *CustomChangeListener) OnChange(changeEvent *storage.ChangeEvent) {
g.Log().Debugf(nil, "当前Namespace变化了%v", changeEvent.Namespace) g.Log().Debugf(nil, "当前Namespace变化了%v", changeEvent.Namespace)
filename := changeEvent.Namespace filename := changeEvent.Namespace
if obj, ok := Item2Obj[filename]; ok { if obj, ok := Item2Obj[filename]; ok {
obj.Load() //重载配置文件
obj.Load(changeEvent.Changes["content"].NewValue.(string))
} }
//cfgList := consts.ExcelFileList
//for _, v := range cfgList {
// if v.Name == changeEvent.Namespace {
//
// }
//}
} }
func (c *CustomChangeListener) OnNewestChange(event *storage.FullChangeEvent) { func (c *CustomChangeListener) OnNewestChange(event *storage.FullChangeEvent) {