Skip to content

Commit

Permalink
Finish v1.6.0
Browse files Browse the repository at this point in the history
Finish #29
  • Loading branch information
zouyx committed Sep 20, 2018
2 parents 078ee87 + 18f712d commit cf38b89
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
/.idea/
/apolloConfig.json
3 changes: 2 additions & 1 deletion app.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"appId": "test",
"cluster": "dev",
"namespaceName": "application",
"ip": "localhost:8888"
"ip": "localhost:8888",
"backupConfigPath":""
}
8 changes: 7 additions & 1 deletion app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ type AppConfig struct {
NamespaceName string `json:"namespaceName"`
Ip string `json:"ip"`
NextTryConnTime int64 `json:"-"`
BackupConfigPath string `json:"backupConfigPath"`
}

func (this *AppConfig) getBackupConfigPath() string{
return this.BackupConfigPath
}


func (this *AppConfig) getHost() string{
if strings.HasPrefix(this.Ip,"http"){
return this.Ip
Expand Down Expand Up @@ -160,7 +166,7 @@ func initConfig(loadAppConfig func()(*AppConfig,error)) {
apolloConfig.Cluster=appConfig.Cluster
apolloConfig.NamespaceName=appConfig.NamespaceName

updateApolloConfig(apolloConfig)
updateApolloConfig(apolloConfig,false)
}(appConfig)
}

Expand Down
2 changes: 1 addition & 1 deletion componet_timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func autoSyncConfigServicesSuccessCallBack(responseBody []byte)(o interface{},er
return nil,err
}

updateApolloConfig(apolloConfig)
updateApolloConfig(apolloConfig,true)

return nil,nil
}
Expand Down
17 changes: 16 additions & 1 deletion componet_timer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,31 @@ func TestAutoSyncConfigServicesNormal2NotModified(t *testing.T) {
}

fmt.Println(err)

//sleep for async
time.Sleep(1 *time.Second)
checkBackupFile(t)
}

func checkBackupFile(t *testing.T){
newConfig,e := loadConfigFile(appConfig.getBackupConfigPath())
t.Log(newConfig.Configurations)
isNil(e)
isNotNil(newConfig.Configurations)
for k,v :=range newConfig.Configurations {
test.Equal(t,getValue(k),v)
}
}


//test if not modify
func TestAutoSyncConfigServicesNotModify(t *testing.T) {
server := runNotModifyConfigResponse()
newAppConfig:=getTestAppConfig()
newAppConfig.Ip=server.URL

apolloConfig,err:=createApolloConfigWithJson([]byte(configResponseStr))
updateApolloConfig(apolloConfig)
updateApolloConfig(apolloConfig,true)

time.Sleep(10*time.Second)
checkCacheLeft(t,configCacheExpireTime-10)
Expand Down
61 changes: 61 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package agollo

import (
"encoding/json"
"errors"
"fmt"
"os"
)

const FILE = "apolloConfig.json"
var configFile=""

//write config to file
func writeConfigFile(config *ApolloConfig,configPath string)error{
if config==nil{
logger.Error("apollo config is null can not write backup file")
return errors.New("apollo config is null can not write backup file")
}
file, e := os.Create(getConfigFile(configPath))
defer file.Close()
if e!=nil{
logger.Errorf("writeConfigFile fail,error:",e)
return e
}

return json.NewEncoder(file).Encode(config)
}

//get real config file
func getConfigFile(configDir string) string {
if configFile == "" {
if configDir!="" {
configFile=fmt.Sprintf("%s/%s",configDir,FILE)
}else{
configFile=FILE
}

}
return configFile
}

//load config from file
func loadConfigFile(configDir string) (*ApolloConfig,error){
configFilePath := getConfigFile(configDir)
logger.Info("load config file from :",configFilePath)
file, e := os.Open(configFilePath)
defer file.Close()
if e!=nil{
logger.Errorf("loadConfigFile fail,error:",e)
return nil,e
}
config:=&ApolloConfig{}
e=json.NewDecoder(file).Decode(config)

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

return config,e
}
53 changes: 53 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package agollo

import (
"github.com/zouyx/agollo/test"
"os"
"testing"
)

func TestWriteConfigFile(t *testing.T) {
configPath:=""
os.Remove(getConfigFile(configPath))
jsonStr := `{
"appId": "100004458",
"cluster": "default",
"namespaceName": "application",
"configurations": {
"key1":"value1",
"key2":"value2"
},
"releaseKey": "20170430092936-dee2d58e74515ff3"
}`

config, err := createApolloConfigWithJson([]byte(jsonStr))

isNil(err)
e := writeConfigFile(config,configPath)
isNil(e)
}

func TestLoadConfigFile(t *testing.T) {
jsonStr := `{
"appId": "100004458",
"cluster": "default",
"namespaceName": "application",
"configurations": {
"key1":"value1",
"key2":"value2"
},
"releaseKey": "20170430092936-dee2d58e74515ff3"
}`

config, err := createApolloConfigWithJson([]byte(jsonStr))

isNil(err)
newConfig,e := loadConfigFile("")

t.Log(newConfig)
isNil(e)
test.Equal(t,config.AppId,newConfig.AppId)
test.Equal(t,config.ReleaseKey,newConfig.ReleaseKey)
test.Equal(t,config.Cluster,newConfig.Cluster)
test.Equal(t,config.NamespaceName,newConfig.NamespaceName)
}
7 changes: 6 additions & 1 deletion repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type currentApolloConfig struct {
config *ApolloConnConfig
}

func updateApolloConfig(apolloConfig *ApolloConfig) {
func updateApolloConfig(apolloConfig *ApolloConfig,isBackupConfig bool) {
if apolloConfig == nil {
logger.Error("apolloConfig is null,can't update!")
return
Expand All @@ -50,6 +50,11 @@ func updateApolloConfig(apolloConfig *ApolloConfig) {
defer currentConnApolloConfig.l.Unlock()

currentConnApolloConfig.config = &apolloConfig.ApolloConnConfig

if isBackupConfig{
//write config file async
go writeConfigFile(apolloConfig,appConfig.getBackupConfigPath())
}
}

func updateApolloConfigCache(configurations map[string]string, expireTime int) map[string]*ConfigChange {
Expand Down
2 changes: 1 addition & 1 deletion repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestUpdateApolloConfigNull(t *testing.T) {

test.NotNil(t, currentConfig)

updateApolloConfig(nil)
updateApolloConfig(nil,true)

currentConnApolloConfig.l.RLock()
defer currentConnApolloConfig.l.RUnlock()
Expand Down
12 changes: 10 additions & 2 deletions start.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ func StartWithLogger(loggerInterface LoggerInterface) error {
}

//first sync
error := notifySyncConfigServices()
err := notifySyncConfigServices()

//first sync fail then load config file
if err !=nil{
config, _ := loadConfigFile(appConfig.BackupConfigPath)
if config!=nil{
updateApolloConfig(config,false)
}
}

//start auto refresh config
go StartRefreshConfig(&AutoRefreshConfigComponent{})

//start long poll sync config
go StartRefreshConfig(&NotifyConfigComponent{})

return error
return err
}
18 changes: 18 additions & 0 deletions start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package agollo
import (
"testing"
"github.com/zouyx/agollo/test"
"time"
)

func TestStart(t *testing.T) {
Expand All @@ -15,3 +16,20 @@ func TestStart(t *testing.T) {
value := getValue("key1")
test.Equal(t,"value1",value)
}

func TestErrorStart(t *testing.T) {
server:= runErrorResponse()
newAppConfig:=getTestAppConfig()
newAppConfig.Ip=server.URL

time.Sleep(1 * time.Second)

Start()

value := getValue("key1")
test.Equal(t,"value1",value)

value2 := getValue("key2")
test.Equal(t,"value2",value2)

}

0 comments on commit cf38b89

Please sign in to comment.