Skip to content

Commit

Permalink
Finish v1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Jul 12, 2018
2 parents f95d616 + 1f64a6c commit 27e7056
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 270 deletions.
33 changes: 26 additions & 7 deletions app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/url"
"encoding/json"
"strings"
)

const appConfigFileName ="app.properties"
Expand Down Expand Up @@ -53,6 +54,9 @@ type AppConfig struct {
}

func (this *AppConfig) getHost() string{
if strings.HasPrefix(this.Ip,"http"){
return this.Ip
}
return "http://"+this.Ip+"/"
}

Expand Down Expand Up @@ -175,7 +179,7 @@ func initServerIpList() {
for {
select {
case <-t2.C:
syncServerIpList()
syncServerIpList(nil)
t2.Reset(refresh_ip_list_interval)
}
}
Expand Down Expand Up @@ -209,12 +213,18 @@ func syncServerIpListSuccessCallBack(responseBody []byte)(o interface{},err erro
//then
//1.update cache
//2.store in disk
func syncServerIpList() error{
appConfig:=GetAppConfig()
func syncServerIpList(newAppConfig *AppConfig) error{
appConfig:=GetAppConfig(newAppConfig)
if appConfig==nil{
panic("can not find apollo config!please confirm!")
}
url:=getServicesConfigUrl(appConfig)

var url string
if newAppConfig ==nil{
getServicesConfigUrl(appConfig)
}else{
url=newAppConfig.Ip
}
logger.Debug("url:",url)

_,err:=request(url,&ConnectConfig{
Expand All @@ -226,7 +236,10 @@ func syncServerIpList() error{
return err
}

func GetAppConfig()*AppConfig {
func GetAppConfig(newAppConfig *AppConfig)*AppConfig {
if newAppConfig !=nil{
return newAppConfig
}
return appConfig
}

Expand Down Expand Up @@ -258,7 +271,10 @@ func getConfigUrlByHost(config *AppConfig,host string) string{
getInternal())
}

func getConfigUrlSuffix(config *AppConfig) string{
func getConfigUrlSuffix(config *AppConfig,newConfig *AppConfig) string{
if newConfig!=nil{
return ""
}
current:=GetCurrentApolloConfig()
return fmt.Sprintf("configs/%s/%s/%s?releaseKey=%s&ip=%s",
url.QueryEscape(config.AppId),
Expand All @@ -282,7 +298,10 @@ func getNotifyUrlByHost(notifications string,config *AppConfig,host string) stri
url.QueryEscape(notifications))
}

func getNotifyUrlSuffix(notifications string,config *AppConfig) string{
func getNotifyUrlSuffix(notifications string,config *AppConfig,newConfig *AppConfig) string{
if newConfig!=nil{
return ""
}
return fmt.Sprintf("notifications/v2?appId=%s&cluster=%s&notifications=%s",
url.QueryEscape(config.AppId),
url.QueryEscape(config.Cluster),
Expand Down
48 changes: 10 additions & 38 deletions app_config_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package agollo

import (
"net/http"
"fmt"
//"time"
)
//"time"
"net/http/httptest"
)

const servicesConfigResponseStr =`[{
"appName": "APOLLO-CONFIGSERVICE",
Expand Down Expand Up @@ -61,39 +61,11 @@ const servicesConfigResponseStr =`[{
//var server *http.Server

//run mock config server
func runMockServicesConfigServer(handler func(http.ResponseWriter, *http.Request)) {
uri:=fmt.Sprintf("/services/config")
http.HandleFunc(uri, handler)
func runMockServicesConfigServer() *httptest.Server{
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(servicesConfigResponseStr))
}))

//server = &http.Server{
// Addr: appConfig.Ip,
// Handler: http.DefaultServeMux,
//}
//
//server.ListenAndServe()


logger.Info("mock notify server:",appConfig.Ip)
err:=http.ListenAndServe(fmt.Sprintf("%s",appConfig.Ip), nil)
if err!=nil{
logger.Error("runMockConfigServer err:",err)
}
}

func closeMockServicesConfigServer() {
http.DefaultServeMux=http.NewServeMux()
//server.Close()
}


//Normal response
func normalServicesConfigResponse(rw http.ResponseWriter, req *http.Request) {
fmt.Fprintf(rw, servicesConfigResponseStr)
}

////Error response
////will hold 5s and keep response 404
//func errorConfigResponse(rw http.ResponseWriter, req *http.Request) {
// time.Sleep(500 * time.Microsecond)
// rw.WriteHeader(http.StatusNotFound)
//}
return ts
}
23 changes: 7 additions & 16 deletions app_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestInit(t *testing.T) {
config:=GetAppConfig()
config:=GetAppConfig(nil)

test.NotNil(t,config)
test.Equal(t,"test",config.AppId)
Expand Down Expand Up @@ -37,7 +37,7 @@ func TestStructInit(t *testing.T) {
return readyConfig,nil
})

config:=GetAppConfig()
config:=GetAppConfig(nil)
test.NotNil(t,config)
test.Equal(t,"test1",config.AppId)
test.Equal(t,"dev1",config.Cluster)
Expand All @@ -49,15 +49,6 @@ func TestStructInit(t *testing.T) {
test.Equal(t,"dev1",apolloConfig.Cluster)
test.Equal(t,"application1",apolloConfig.NamespaceName)

//go runMockConfigServer(onlyNormalConfigResponse)
//go runMockNotifyServer(onlyNormalResponse)
//defer closeMockConfigServer()
//
//Start()
//
//value := getValue("key1")
//test.Equal(t,"value1",value)

//revert file config
initFileConfig()
}
Expand Down Expand Up @@ -126,12 +117,12 @@ func TestSyncServerIpList(t *testing.T) {
}

func trySyncServerIpList(t *testing.T) {
go runMockServicesConfigServer(normalServicesConfigResponse)
defer closeMockServicesConfigServer()

time.Sleep(1*time.Second)
server := runMockServicesConfigServer()
defer server.Close()

err:=syncServerIpList()
newAppConfig:=getTestAppConfig()
newAppConfig.Ip=server.URL
err:=syncServerIpList(newAppConfig)

test.Nil(t,err)

Expand Down
28 changes: 8 additions & 20 deletions change_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,9 @@ func TestListenChangeEvent(t *testing.T) {

test.Equal(t,"application",changeEvent.Namespace)

//DELETED
test.Equal(t,"value",changeEvent.Changes["string"].OldValue)
test.Equal(t,"string",changeEvent.Changes["string"].NewValue)
test.Equal(t,MODIFIED,changeEvent.Changes["string"].ChangeType)

test.Equal(t,"true",changeEvent.Changes["bool"].OldValue)
test.Equal(t,"",changeEvent.Changes["bool"].NewValue)
test.Equal(t,DELETED,changeEvent.Changes["bool"].ChangeType)

test.Equal(t,"190.3",changeEvent.Changes["float"].OldValue)
test.Equal(t,"",changeEvent.Changes["float"].NewValue)
test.Equal(t,DELETED,changeEvent.Changes["float"].ChangeType)

test.Equal(t,"1",changeEvent.Changes["int"].OldValue)
test.Equal(t,"",changeEvent.Changes["int"].NewValue)
test.Equal(t,DELETED,changeEvent.Changes["int"].ChangeType)
test.Equal(t,"",changeEvent.Changes["string"].OldValue)
test.Equal(t,ADDED,changeEvent.Changes["string"].ChangeType)

test.Equal(t,"value1",changeEvent.Changes["key1"].NewValue)
test.Equal(t,"",changeEvent.Changes["key2"].OldValue)
Expand All @@ -51,15 +38,16 @@ func clearChannel() {
}

func buildNotifyResult(t *testing.T) {
go runMockConfigServer(changeConfigResponse)
defer closeMockConfigServer()
server := runChangeConfigResponse()
defer server.Close()

time.Sleep(1*time.Second)

appConfig.NextTryConnTime=0
newAppConfig:=getTestAppConfig()
newAppConfig.Ip=server.URL

err:=autoSyncConfigServices()
err=autoSyncConfigServices()
err:=autoSyncConfigServices(newAppConfig)
err=autoSyncConfigServices(newAppConfig)

test.Nil(t,err)

Expand Down
10 changes: 5 additions & 5 deletions componet_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ func getRemoteConfigSuccessCallBack(responseBody []byte)(o interface{},err error
return toApolloConfig(responseBody)
}

func getRemoteConfig() ([]*apolloNotify,error) {
appConfig:=GetAppConfig()
func getRemoteConfig(newAppConfig *AppConfig) ([]*apolloNotify,error) {
appConfig:=GetAppConfig(newAppConfig)
if appConfig==nil{
panic("can not find apollo config!please confirm!")
}
urlSuffix:=getNotifyUrlSuffix(allNotifications.getNotifies(),appConfig)
urlSuffix:=getNotifyUrlSuffix(allNotifications.getNotifies(),appConfig,newAppConfig)

//seelog.Debugf("allNotifications.getNotifies():%s",allNotifications.getNotifies())

Expand All @@ -79,7 +79,7 @@ func getRemoteConfig() ([]*apolloNotify,error) {

func notifySyncConfigServices() error {

remoteConfigs,err:=getRemoteConfig()
remoteConfigs,err:=getRemoteConfig(nil)

if err!=nil||len(remoteConfigs)==0{
return err
Expand Down Expand Up @@ -108,7 +108,7 @@ func init() {
}

func initAllNotifications() {
appConfig:=GetAppConfig()
appConfig:=GetAppConfig(nil)

if appConfig!=nil {
allNotifications = &notificationsMap{
Expand Down
32 changes: 9 additions & 23 deletions componet_notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
)

func TestSyncConfigServices(t *testing.T) {
defer closeMockNotifyServer()

notifySyncConfigServices()
}

func TestGetRemoteConfig(t *testing.T) {
go runMockNotifyServer(normalResponse)
defer closeMockNotifyServer()
server:= runNormalResponse()
newAppConfig:=getTestAppConfig()
newAppConfig.Ip=server.URL

time.Sleep(1*time.Second)

Expand All @@ -24,7 +23,7 @@ func TestGetRemoteConfig(t *testing.T) {
var err error
for{
count++
remoteConfigs,err=getRemoteConfig()
remoteConfigs,err=getRemoteConfig(newAppConfig)

//err keep nil
test.Nil(t,err)
Expand All @@ -49,14 +48,15 @@ func TestGetRemoteConfig(t *testing.T) {
}

func TestErrorGetRemoteConfig(t *testing.T) {
go runMockNotifyServer(errorResponse)
defer closeMockNotifyServer()
server:= runErrorResponse()
newAppConfig:=getTestAppConfig()
newAppConfig.Ip=server.URL

time.Sleep(1 * time.Second)

var remoteConfigs []*apolloNotify
var err error
remoteConfigs, err = getRemoteConfig()
remoteConfigs, err = getRemoteConfig(nil)

test.NotNil(t, err)
test.Nil(t, remoteConfigs)
Expand Down Expand Up @@ -116,18 +116,4 @@ func TestToApolloConfigError(t *testing.T) {
notified,err:=toApolloConfig([]byte("jaskldfjaskl"))
test.Nil(t,notified)
test.NotNil(t,err)
}
//
//func TestNotifyConfigComponent(t *testing.T) {
// go func() {
// for{
// time.Sleep(5*time.Second)
// fmt.Println(GetCurrentApolloConfig())
// }
// }()
//
//
// c:=&NotifyConfigComponent{}
// c.Start()
//
//}
}
8 changes: 4 additions & 4 deletions componet_timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (this *AutoRefreshConfigComponent) Start() {
}

func SyncConfig() error {
return autoSyncConfigServices()
return autoSyncConfigServices(nil)
}


Expand All @@ -37,13 +37,13 @@ func autoSyncConfigServicesSuccessCallBack(responseBody []byte)(o interface{},er
return nil,nil
}

func autoSyncConfigServices() error {
appConfig:=GetAppConfig()
func autoSyncConfigServices(newAppConfig *AppConfig) error {
appConfig:=GetAppConfig(newAppConfig)
if appConfig==nil{
panic("can not find apollo config!please confirm!")
}

urlSuffix:=getConfigUrlSuffix(appConfig)
urlSuffix:=getConfigUrlSuffix(appConfig,newAppConfig)

_,err:=requestRecovery(appConfig,&ConnectConfig{
Uri:urlSuffix,
Expand Down

0 comments on commit 27e7056

Please sign in to comment.