From 05d88a89f4fb10b4fda741f4c51821066188043a Mon Sep 17 00:00:00 2001 From: ayflying Date: Wed, 25 Dec 2024 12:11:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=A4=9A=E6=AC=A1=E5=BE=AA?= =?UTF-8?q?=E7=8E=AF=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 64 +++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/config/config.go b/config/config.go index 6b32916..e267a09 100644 --- a/config/config.go +++ b/config/config.go @@ -11,7 +11,9 @@ import ( "github.com/gogf/gf/v2/os/gfile" "github.com/gogf/gf/v2/os/gres" "github.com/gogf/gf/v2/text/gstr" + "path" "sync" + "time" ) var ( @@ -22,10 +24,11 @@ var ( // load接口定义了Load方法,用于加载数据 type Load interface { - Load() + Load(cfg ...string) } type Cfg struct { + Lock sync.Mutex } 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) { - Item2Obj[name] = obj + c.Lock.Lock() + defer c.Lock.Unlock() - //c2 := &config.AppConfig{ - // AppID: consts.ApolloAppID, - // Cluster: consts.ApolloCluster, - // IP: consts.ApolloIP, - // NamespaceName: consts.ApolloNamespaceName, - // IsBackupConfig: consts.ApolloIsBackupConfig, - // Secret: consts.ApolloSecret, - //} + Item2Obj[name+".json"] = obj + var cfg = config.AppConfig{ + AppID: ApolloCfg.AppID, + Cluster: ApolloCfg.Cluster, + IP: ApolloCfg.IP, + NamespaceName: name + ".json", + 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) { 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) - getStr, err := get.Get("content") + if getApollo != nil { + 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) - //首次运行加入监听器 if !gstr.InArray(ApolloListener, name) { c2 := &CustomChangeListener{} @@ -115,16 +142,9 @@ 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() + //重载配置文件 + 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) {