config.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package apollo
  2. import (
  3. "fmt"
  4. "git.bvbej.com/bvbej/base-golang/pkg/env"
  5. "github.com/spf13/viper"
  6. "github.com/zouyx/agollo/v4"
  7. "github.com/zouyx/agollo/v4/component/log"
  8. agolloConfig "github.com/zouyx/agollo/v4/env/config"
  9. )
  10. func GetApolloConfig(appId, secret string, config interface{}) {
  11. namespace := env.Active().Value() + ".yaml"
  12. ac := &agolloConfig.AppConfig{
  13. AppID: appId,
  14. Cluster: "dev",
  15. IP: "https://config.bvbej.com",
  16. NamespaceName: namespace,
  17. IsBackupConfig: true,
  18. Secret: secret,
  19. }
  20. agollo.SetLogger(&log.DefaultLogger{})
  21. client, err := agollo.StartWithConfig(func() (*agolloConfig.AppConfig, error) {
  22. return ac, nil
  23. })
  24. if err != nil {
  25. panic(fmt.Sprintf("get config error:[%s]", err))
  26. }
  27. viper.SetConfigType("yaml")
  28. client.GetConfigCache(ac.NamespaceName).Range(func(key, value interface{}) bool {
  29. viper.Set(key.(string), value)
  30. return true
  31. })
  32. if err = viper.Unmarshal(config); err != nil {
  33. panic(fmt.Sprintf("unmarshal config error:[%s]", err))
  34. }
  35. }