package apollo import ( "fmt" "git.bvbej.com/bvbej/base-golang/pkg/env" "github.com/apolloconfig/agollo/v4" "github.com/apolloconfig/agollo/v4/component/log" apolloConfig "github.com/apolloconfig/agollo/v4/env/config" "github.com/apolloconfig/agollo/v4/storage" "github.com/spf13/viper" ) type clientConfig struct { client agollo.Client ac *apolloConfig.AppConfig conf interface{} } func GetApolloConfig(appId, secret string, config interface{}) { var err error namespace := env.Active().Value() + ".yaml" c := new(clientConfig) c.conf = config c.ac = &apolloConfig.AppConfig{ AppID: appId, Cluster: "dev", IP: "https://config.bvbej.com", NamespaceName: namespace, IsBackupConfig: true, Secret: secret, } agollo.SetLogger(&log.DefaultLogger{}) c.client, err = agollo.StartWithConfig(func() (*apolloConfig.AppConfig, error) { return c.ac, nil }) if err != nil { panic(fmt.Sprintf("get config error:[%s]", err)) } c.client.AddChangeListener(c) err = c.serialization() if err != nil { panic(fmt.Sprintf("unmarshal config error:[%s]", err)) } } func (c *clientConfig) serialization() error { viper.SetConfigType("yaml") c.client.GetConfigCache(c.ac.NamespaceName).Range(func(key, value interface{}) bool { viper.Set(key.(string), value) return true }) return viper.Unmarshal(c.conf) } func (c *clientConfig) OnChange(*storage.ChangeEvent) { _ = c.serialization() } func (c *clientConfig) OnNewestChange(*storage.FullChangeEvent) { // }