Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support config center #8

Open
wants to merge 1 commit into
base: di
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions common/env.go
Original file line number Diff line number Diff line change
@@ -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" //
32 changes: 27 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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
Expand Down
33 changes: 33 additions & 0 deletions config_manager/config_center.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package configmanager

import (
"fmt"

"github.com/spf13/viper"
)

var (
configCenter ConfigCenter
configCenterViperInstance *viper.Viper
)

func RegisterConfigBuilder(builder ConfigCenterBuilder) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全局只能注册一个?

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
}
5 changes: 5 additions & 0 deletions config_manager/const.go
Original file line number Diff line number Diff line change
@@ -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"
47 changes: 47 additions & 0 deletions config_manager/init.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions config_manager/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package configmanager

import "github.com/spf13/viper"

type ConfigCenter interface {
// LoadConfig 从配置中心获取配置
LoadConfig(key string) (string, error)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只有key么?如果考虑横向扩展,可以增加一个metadata map[string]string。

// SyncConfig 持续从配置中心获取配置,当配置发生变更时,将会修改value指向的值
SyncConfig(key string, value *string) error
}

type ConfigCenterBuilder func(conf *viper.Viper) (ConfigCenter, error)