diff --git a/registry/refreshConfig.go b/registry/refreshConfig.go index 0f1903f..90d0838 100644 --- a/registry/refreshConfig.go +++ b/registry/refreshConfig.go @@ -1,15 +1,17 @@ package registry import ( + "bytes" "encoding/base64" "encoding/json" "io/ioutil" "net/http" + "os" "github.com/smallnest/rpcx/log" "github.com/soheilhy/cmux" "github.com/xurwxj/gtils/base" - "github.com/xurwxj/rpcx_consul/remoteconfig" + "github.com/xurwxj/viper" ) type CmuxPluginConfig struct { @@ -41,7 +43,7 @@ func (s *CmuxPluginConfig) MuxMatch(m cmux.CMux) { if len(c) > 0 { v := c[0].Value newValue, _ := base64.StdEncoding.DecodeString(v) - remoteconfig.MergeConfig(string(newValue)) + MergeConfig(string(newValue)) log.Debugf("consulConfigUpdate kvKey=%v value=%v ", c[0].Key, string(newValue)) } resByte, err := base.GetByteArrayFromInterface(map[string]interface{}{ @@ -63,3 +65,20 @@ func (s *CmuxPluginConfig) MuxMatch(m cmux.CMux) { } go httpS.Serve(listener) } + +func MergeConfig(data string) { + if data == "" { + return + } + base.CheckPathExistOrCreate("nacos") + err := os.WriteFile("nacos/remote.json", []byte(data), os.ModePerm) + if err != nil { + log.Errorf("mergeConfig:WriteFile config=%v err=%v ", data, err) + } + err = viper.MergeConfig(bytes.NewReader([]byte(data))) + if err != nil { + log.Errorf("mergeConfig config=%v err=%v ", data, err) + } else { + log.Infof("mergeConfig done success ") + } +} diff --git a/remoteconfig/consul.go b/remoteconfig/consul.go deleted file mode 100644 index 181daee..0000000 --- a/remoteconfig/consul.go +++ /dev/null @@ -1,64 +0,0 @@ -package remoteconfig - -import ( - "bytes" - "fmt" - "os" - "time" - - "github.com/smallnest/rpcx/log" - "github.com/xurwxj/gtils/base" - "github.com/xurwxj/rpcx_consul/client" - "github.com/xurwxj/viper" -) - -func initConsulConfig() { - remoteF := base.CheckFileExistBackInfo("nacos/remote.json", true) - cacheExist := false - if remoteF != nil { - remoteByte, err := os.ReadFile("nacos/remote.json") - if err != nil { - log.Errorf("initConsulConfig:ReadFile err %v", err) - } else { - err = viper.MergeConfig(bytes.NewReader(remoteByte)) - if err != nil { - log.Errorf("initConsulConfig:MergeConfig err %v", err) - } else { - cacheExist = true - go initConfig() - } - } - } - if !cacheExist { - log.Infof("starting init consul config....") - initConfig() - } - -} -func initConfig() { - client, err := client.ConsulClient(configServerHostPort, configDataGroup, configAuth) - if err != nil { - log.Errorf("initConsulConfig configServerHostPort %v err %v", configServerHostPort, err) - time.Sleep(10 * time.Second) - initConsulConfig() - return - } - // fmt.Println("client: ", client) - c := client.KV() - kvPath := fmt.Sprintf("%s/%s", configNameSpace, configDataID) - kv, _, err := c.Get(kvPath, nil) - if err != nil { - log.Errorf("initConsulConfig kvPath =%v configServerHostPort=%v err %v", kvPath, configServerHostPort, err) - time.Sleep(10 * time.Second) - initConsulConfig() - return - } - // fmt.Println("kv: ", kv) - if kv == nil { - log.Errorf("initConsulConfig notFound kvPath=%v configServerHostPort=%v ", kvPath, configServerHostPort) - time.Sleep(10 * time.Second) - initConsulConfig() - return - } - MergeConfig(string(kv.Value)) -} diff --git a/remoteconfig/index.go b/remoteconfig/index.go deleted file mode 100644 index 65ba125..0000000 --- a/remoteconfig/index.go +++ /dev/null @@ -1,64 +0,0 @@ -package remoteconfig - -import ( - "bytes" - "os" - - "github.com/smallnest/rpcx/log" - "github.com/xurwxj/gtils/base" - "github.com/xurwxj/viper" -) - -var ( - configServerType string - configServerHostPort string - configServerContextPath string - configNameSpace string - configDataID string - configDataGroup string - configAuth string - configCacheDir string - notLoadCacheAtStart bool - timeoutMs uint64 - listenIntervalMs uint64 -) - -// InitRemoteConfig init remote config and watch update, will override config value in config file -func InitRemoteConfig() { - configServerType = viper.GetString("server.config.type") - configServerHostPort = viper.GetString("server.config.hostPort") - configServerContextPath = viper.GetString("server.config.contextPath") - configNameSpace = viper.GetString("server.config.nameSpace") - configDataID = viper.GetString("server.config.dataID") - configDataGroup = viper.GetString("server.config.dataGroup") - if configServerType == "" || configServerHostPort == "" || configNameSpace == "" || configDataID == "" || configDataGroup == "" { - log.Errorf("please check config or cmd params configLost func initRemoteConfig") - os.Exit(1) - } - configAuth = viper.GetString("server.config.auth") - configCacheDir = viper.GetString("server.config.cacheDir") - notLoadCacheAtStart = viper.GetBool("server.config.notLoadCacheAtStart") - timeoutMs = viper.GetUint64("server.config.timeoutMs") - listenIntervalMs = viper.GetUint64("server.config.listenIntervalMs") - switch configServerType { - case "consul": - initConsulConfig() - } -} - -func MergeConfig(data string) { - if data == "" { - return - } - base.CheckPathExistOrCreate("nacos") - err := os.WriteFile("nacos/remote.json", []byte(data), os.ModePerm) - if err != nil { - log.Errorf("mergeConfig:WriteFile config=%v configServer=%v err=%v ", data, configServerType, err) - } - err = viper.MergeConfig(bytes.NewReader([]byte(data))) - if err != nil { - log.Errorf("mergeConfig config=%v configServer=%v err=%v ", data, configServerType, err) - } else { - log.Infof("mergeConfig done success configServer=%v", configServerType) - } -}