Skip to content

Commit

Permalink
Merge pull request apolloconfig#99 from lj99boy/withRawBackup
Browse files Browse the repository at this point in the history
增加备份原始namespace功能
  • Loading branch information
zouyx committed Apr 17, 2020
2 parents 485c4ec + 30155f8 commit 295a72a
Show file tree
Hide file tree
Showing 27 changed files with 268 additions and 106 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
application*.json
/abc1.json
/env_test.properties
/vendor/
3 changes: 1 addition & 2 deletions component/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"fmt"
"net/url"

"github.com/zouyx/agollo/v3/env/config"

"github.com/zouyx/agollo/v3/env"
"github.com/zouyx/agollo/v3/env/config"
"github.com/zouyx/agollo/v3/utils"
)

Expand Down
10 changes: 6 additions & 4 deletions component/notify/change_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"testing"
"time"

"github.com/zouyx/agollo/v3/storage"

"github.com/zouyx/agollo/v3/env"

. "github.com/tevid/gohamcrest"
"github.com/zouyx/agollo/v3/env"
jsonFile "github.com/zouyx/agollo/v3/env/file/json"
"github.com/zouyx/agollo/v3/extension"
"github.com/zouyx/agollo/v3/storage"
)

type CustomChangeListener struct {
Expand Down Expand Up @@ -58,6 +58,7 @@ func TestListenChangeEvent(t *testing.T) {
}

func buildNotifyResult(t *testing.T) {
extension.SetFileHandler(&jsonFile.JSONFileHandler{})
initNotifications()
server := runChangeConfigResponse()
defer server.Close()
Expand All @@ -81,6 +82,7 @@ func buildNotifyResult(t *testing.T) {
}

func TestRemoveChangeListener(t *testing.T) {
extension.SetFileHandler(&jsonFile.JSONFileHandler{})
go buildNotifyResult(t)

listener := &CustomChangeListener{}
Expand Down
6 changes: 3 additions & 3 deletions component/notify/componet_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"time"

"github.com/zouyx/agollo/v3/component"
"github.com/zouyx/agollo/v3/env/config"

"github.com/zouyx/agollo/v3/component/log"
"github.com/zouyx/agollo/v3/env"
"github.com/zouyx/agollo/v3/env/config"
"github.com/zouyx/agollo/v3/extension"
"github.com/zouyx/agollo/v3/protocol/http"
"github.com/zouyx/agollo/v3/storage"
"github.com/zouyx/agollo/v3/utils"
Expand Down Expand Up @@ -179,7 +179,7 @@ func syncConfigs(namespace string, isAsync bool) error {

func loadBackupConfig(namespace string, appConfig *config.AppConfig) {
env.SplitNamespaces(namespace, func(namespace string) {
config, _ := env.LoadConfigFile(appConfig.BackupConfigPath, namespace)
config, _ := extension.GetFileHandler().LoadConfigFile(appConfig.BackupConfigPath, namespace)
if config != nil {
storage.UpdateApolloConfig(config, false)
}
Expand Down
12 changes: 7 additions & 5 deletions component/notify/componet_notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"testing"
"time"

"github.com/zouyx/agollo/v3/env/config"
jsonConfig "github.com/zouyx/agollo/v3/env/config/json"

. "github.com/tevid/gohamcrest"
"github.com/zouyx/agollo/v3/env"
"github.com/zouyx/agollo/v3/env/config"
jsonConfig "github.com/zouyx/agollo/v3/env/config/json"
jsonFile "github.com/zouyx/agollo/v3/env/file/json"
"github.com/zouyx/agollo/v3/extension"
_ "github.com/zouyx/agollo/v3/loadbalance/roundrobin"
)

Expand Down Expand Up @@ -55,6 +56,7 @@ func initMockNotifyAndConfigServer() {
}

func TestSyncConfigServices(t *testing.T) {
extension.SetFileHandler(&jsonFile.JSONFileHandler{})
initMockNotifyAndConfigServer()

err := AsyncConfigs()
Expand Down Expand Up @@ -202,7 +204,7 @@ func TestAutoSyncConfigServicesNoBackupFile(t *testing.T) {
newAppConfig.IP = server.URL
appConfig := env.GetPlainAppConfig()
appConfig.IsBackupConfig = false
configFilePath := env.GetConfigFile(newAppConfig.GetBackupConfigPath(), "application")
configFilePath := extension.GetFileHandler().GetConfigFile(newAppConfig.GetBackupConfigPath(), "application")
err := os.Remove(configFilePath)

time.Sleep(1 * time.Second)
Expand All @@ -218,7 +220,7 @@ func TestAutoSyncConfigServicesNoBackupFile(t *testing.T) {

func checkNilBackupFile(t *testing.T) {
appConfig := env.GetPlainAppConfig()
newConfig, e := env.LoadConfigFile(appConfig.GetBackupConfigPath(), "application")
newConfig, e := extension.GetFileHandler().LoadConfigFile(appConfig.GetBackupConfigPath(), "application")
Assert(t, e, NotNilVal())
Assert(t, newConfig, NilVal())
}
Expand Down
2 changes: 1 addition & 1 deletion component/serverlist/sync.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package serverlist

import (
"github.com/zouyx/agollo/v3/component"
"time"

"github.com/zouyx/agollo/v3/component"
"github.com/zouyx/agollo/v3/component/log"
"github.com/zouyx/agollo/v3/env"
"github.com/zouyx/agollo/v3/env/config"
Expand Down
5 changes: 2 additions & 3 deletions env/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"fmt"
"net/url"
"os"
"strings"
"sync"

"github.com/zouyx/agollo/v3/component/log"
"github.com/zouyx/agollo/v3/env/config"
jsonConfig "github.com/zouyx/agollo/v3/env/config/json"
"github.com/zouyx/agollo/v3/utils"

"strings"
"sync"
)

const (
Expand Down
5 changes: 2 additions & 3 deletions env/app_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"encoding/json"
"os"
"sync"
"testing"
"time"

. "github.com/tevid/gohamcrest"
"github.com/zouyx/agollo/v3/env/config"
jsonConfig "github.com/zouyx/agollo/v3/env/config/json"
"github.com/zouyx/agollo/v3/utils"

"testing"
"time"
)

const servicesConfigResponseStr = `[{
Expand Down
6 changes: 2 additions & 4 deletions env/config/json/json_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package json
import (
"encoding/json"
"os"

"github.com/zouyx/agollo/v3/env/config"
"github.com/zouyx/agollo/v3/utils"

"testing"

. "github.com/tevid/gohamcrest"
"github.com/zouyx/agollo/v3/env/config"
"github.com/zouyx/agollo/v3/utils"
)

var (
Expand Down
54 changes: 0 additions & 54 deletions env/file.go

This file was deleted.

12 changes: 12 additions & 0 deletions env/file/file_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package file

import (
"github.com/zouyx/agollo/v3/env"
)

//FileHandler 备份文件读写
type FileHandler interface {
WriteConfigFile(config *env.ApolloConfig, configPath string) error
GetConfigFile(configDir string, namespace string) string
LoadConfigFile(configDir string, namespace string) (*env.ApolloConfig, error)
}
67 changes: 67 additions & 0 deletions env/file/json/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package json

import (
"bytes"
"encoding/json"
"fmt"

"github.com/zouyx/agollo/v3/component/log"
"github.com/zouyx/agollo/v3/env"
jsonConfig "github.com/zouyx/agollo/v3/env/config/json"
"github.com/zouyx/agollo/v3/extension"
)

//Suffix 默认文件保存类型
const Suffix = ".json"

func init() {
extension.SetFileHandler(&JSONFileHandler{})
}

var (
//jsonFileConfig 处理文件的json格式存取
jsonFileConfig = &jsonConfig.ConfigFile{}
//configFileMap 存取namespace文件地址
configFileMap = make(map[string]string, 1)
)

//JSONFileHandler 默认备份文件读写
type JSONFileHandler struct {
}

//WriteConfigFile write config to file
func (fileHandler *JSONFileHandler) WriteConfigFile(config *env.ApolloConfig, configPath string) error {
return jsonFileConfig.Write(config, fileHandler.GetConfigFile(configPath, config.NamespaceName))
}

//GetConfigFile get real config file
func (fileHandler *JSONFileHandler) GetConfigFile(configDir string, namespace string) string {
fullPath := configFileMap[namespace]
if fullPath == "" {
filePath := fmt.Sprintf("%s%s", namespace, Suffix)
if configDir != "" {
configFileMap[namespace] = fmt.Sprintf("%s/%s", configDir, filePath)
} else {
configFileMap[namespace] = filePath
}
}
return configFileMap[namespace]
}

//LoadConfigFile load config from file
func (fileHandler *JSONFileHandler) LoadConfigFile(configDir string, namespace string) (*env.ApolloConfig, error) {
configFilePath := fileHandler.GetConfigFile(configDir, namespace)
log.Info("load config file from :", configFilePath)
c, e := jsonFileConfig.Load(configFilePath, func(b []byte) (interface{}, error) {
config := &env.ApolloConfig{}
e := json.NewDecoder(bytes.NewBuffer(b)).Decode(config)
return config, e
})

if c == nil || e != nil {
log.Errorf("loadConfigFile fail,error:", e)
return nil, e
}

return c.(*env.ApolloConfig), e
}
20 changes: 12 additions & 8 deletions env/file_test.go → env/file/json/json_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package env
package json

import (
"os"
"testing"

. "github.com/tevid/gohamcrest"
"github.com/zouyx/agollo/v3/env"
"github.com/zouyx/agollo/v3/extension"
)

func TestWriteConfigFile(t *testing.T) {
func TestJSONFileHandler_WriteConfigFile(t *testing.T) {
extension.SetFileHandler(&JSONFileHandler{})
configPath := ""
jsonStr := `{
"appId": "100004458",
Expand All @@ -20,15 +23,16 @@ func TestWriteConfigFile(t *testing.T) {
"releaseKey": "20170430092936-dee2d58e74515ff3"
}`

config, err := CreateApolloConfigWithJSON([]byte(jsonStr))
os.Remove(GetConfigFile(configPath, config.NamespaceName))
config, err := env.CreateApolloConfigWithJSON([]byte(jsonStr))
os.Remove(extension.GetFileHandler().GetConfigFile(configPath, config.NamespaceName))

Assert(t, err, NilVal())
e := WriteConfigFile(config, configPath)
e := extension.GetFileHandler().WriteConfigFile(config, configPath)
Assert(t, e, NilVal())
}

func TestLoadConfigFile(t *testing.T) {
func TestJSONFileHandler_LoadConfigFile(t *testing.T) {
extension.SetFileHandler(&JSONFileHandler{})
jsonStr := `{
"appId": "100004458",
"cluster": "default",
Expand All @@ -40,10 +44,10 @@ func TestLoadConfigFile(t *testing.T) {
"releaseKey": "20170430092936-dee2d58e74515ff3"
}`

config, err := CreateApolloConfigWithJSON([]byte(jsonStr))
config, err := env.CreateApolloConfigWithJSON([]byte(jsonStr))

Assert(t, err, NilVal())
newConfig, e := LoadConfigFile("", config.NamespaceName)
newConfig, e := extension.GetFileHandler().LoadConfigFile("", config.NamespaceName)

t.Log(newConfig)
Assert(t, e, NilVal())
Expand Down

0 comments on commit 295a72a

Please sign in to comment.