From aef31d8b55eca9c653e0b76be07e49ec0b1e0866 Mon Sep 17 00:00:00 2001 From: tangxuanzhao Date: Fri, 18 Feb 2022 14:26:05 +0800 Subject: [PATCH] support config center --- common/env.go | 6 +++++ config/config.go | 32 ++++++++++++++++++---- config_manager/config_center.go | 33 +++++++++++++++++++++++ config_manager/const.go | 5 ++++ config_manager/init.go | 47 +++++++++++++++++++++++++++++++++ config_manager/interface.go | 12 +++++++++ 6 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 common/env.go create mode 100644 config_manager/config_center.go create mode 100644 config_manager/const.go create mode 100644 config_manager/init.go create mode 100644 config_manager/interface.go diff --git a/common/env.go b/common/env.go new file mode 100644 index 0000000..d0cdd90 --- /dev/null +++ b/common/env.go @@ -0,0 +1,6 @@ +package common + +// EnvKeyGloryEnv if is set to dev,then: +// 1. choose config center with namespace dev +// 2. choose config path like "config/glory_dev.yaml +const EnvKeyGloryEnv = "GLORY_ENV" // diff --git a/config/config.go b/config/config.go index 51bc783..9f989c4 100644 --- a/config/config.go +++ b/config/config.go @@ -15,6 +15,8 @@ import ( // EnvKeyGloryConfigPath is absolute/relate path to glory.yaml const EnvKeyGloryConfigPath = "GLORY_CONFIG_PATH" // default val is "config/glory.yaml" +const EnvKeyGloryConfigCenterPath = "GLORY_CONFIG_CENTER_PATH" // default val is "config/config_center.yaml" + // EnvKeyGloryEnv if is set to dev,then: // 1. choose config center with namespace dev // 2. choose config path like "config/glory_dev.yaml @@ -54,6 +56,28 @@ func GetConfigPath() string { return configPath } +func GetConfigCenterPath() string { + configPath := "" + env := os.Getenv(EnvKeyGloryEnv) + + configFilePath := DefaultConfigPath + if os.Getenv(EnvKeyGloryConfigCenterPath) != "" { + configFilePath = os.Getenv(EnvKeyGloryConfigCenterPath) + } + prefix := strings.Split(configFilePath, ".yaml") + // prefix == ["config/glory", ""] + if len(prefix) != 2 { + panic("Invalid config file path = " + configFilePath) + } + // get target env yaml file + if env != "" { + configPath = prefix[0] + "_" + env + ".yaml" + } else { + configPath = configFilePath + } + return configPath +} + func loadFileConfig() error { conf := NewServerConfig() configPath := GetConfigPath() @@ -79,11 +103,8 @@ func loadFileConfig() error { // oadConfigCenterConfig load config center's config from file, default is config/config_center_config.yaml func loadConfigCenterConfig() bool { - configCenterConfigFilePath := DefaultConfigCenterConfigPath - if path := os.Getenv(EnvKeyGloryConfigCenterConfigPath); path != "" { - configCenterConfigFilePath = path - } - yamlFile, err := ioutil.ReadFile(configCenterConfigFilePath) + configCenterPath := GetConfigCenterPath() + yamlFile, err := ioutil.ReadFile(configCenterPath) if err != nil { fmt.Println("config center info: can't load config center config at " + DefaultConfigCenterConfigPath) return false @@ -95,6 +116,7 @@ func loadConfigCenterConfig() bool { return false } configCenterConfig.checkAndFix() + // TODO: 扩展支持更多的配置中心 if configCenterConfig.Name != "nacos" && configCenterConfig.Name != "" { fmt.Println("error: config center name ", configCenterConfig.Name, "not support") return false diff --git a/config_manager/config_center.go b/config_manager/config_center.go new file mode 100644 index 0000000..389aa12 --- /dev/null +++ b/config_manager/config_center.go @@ -0,0 +1,33 @@ +package configmanager + +import ( + "fmt" + + "github.com/spf13/viper" +) + +var ( + configCenter ConfigCenter + configCenterViperInstance *viper.Viper +) + +func RegisterConfigBuilder(builder ConfigCenterBuilder) error { + if configCenter != nil { + return fmt.Errorf("only allow one config center builder") + } + // 初始化配置中心 + var err error + configCenter, err = builder(configCenterViperInstance) + if err != nil { + return err + } + + return nil +} + +func GetConfigCenter() (ConfigCenter, error) { + if configCenter == nil { + return configCenter, fmt.Errorf("config center not register") + } + return configCenter, nil +} diff --git a/config_manager/const.go b/config_manager/const.go new file mode 100644 index 0000000..d8ce590 --- /dev/null +++ b/config_manager/const.go @@ -0,0 +1,5 @@ +package configmanager + +const DefaultConfigCenterConfigPath = "config/config_center.yaml" + +const EnvKeyGloryConfigCenterPath = "GLORY_CONFIG_CENTER_PATH" // default val is "config/config_center.yaml" diff --git a/config_manager/init.go b/config_manager/init.go new file mode 100644 index 0000000..8d97204 --- /dev/null +++ b/config_manager/init.go @@ -0,0 +1,47 @@ +package configmanager + +import ( + "log" + "os" + "strings" + + "github.com/glory-go/glory/common" + "github.com/spf13/viper" +) + +func init() { + loadConfigCenterConfig() +} + +func GetConfigCenterPath() string { + configPath := "" + env := os.Getenv(common.EnvKeyGloryEnv) + + configFilePath := DefaultConfigCenterConfigPath + if os.Getenv(EnvKeyGloryConfigCenterPath) != "" { + configFilePath = os.Getenv(EnvKeyGloryConfigCenterPath) + } + prefix := strings.Split(configFilePath, ".yaml") + // prefix == ["config/glory", ""] + if len(prefix) != 2 { + panic("Invalid config file path = " + configFilePath) + } + // get target env yaml file + if env != "" { + configPath = prefix[0] + "_" + env + ".yaml" + } else { + configPath = configFilePath + } + return configPath +} + +func loadConfigCenterConfig() { + path := GetConfigCenterPath() + + viperInstance := viper.GetViper() + viperInstance.SetConfigFile(path) + if err := viper.ReadInConfig(); err != nil { + log.Panic("load config center meets error", err) + } + configCenterViperInstance = viperInstance +} diff --git a/config_manager/interface.go b/config_manager/interface.go new file mode 100644 index 0000000..bcbf38c --- /dev/null +++ b/config_manager/interface.go @@ -0,0 +1,12 @@ +package configmanager + +import "github.com/spf13/viper" + +type ConfigCenter interface { + // LoadConfig 从配置中心获取配置 + LoadConfig(key string) (string, error) + // SyncConfig 持续从配置中心获取配置,当配置发生变更时,将会修改value指向的值 + SyncConfig(key string, value *string) error +} + +type ConfigCenterBuilder func(conf *viper.Viper) (ConfigCenter, error)