Skip to content

Commit

Permalink
Finish v4.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Dec 10, 2020
2 parents 708a7de + 3eb83c4 commit e35bcef
Show file tree
Hide file tree
Showing 27 changed files with 403 additions and 237 deletions.
19 changes: 14 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ import (
"strconv"
)

var (
//next try connect period - 60 second
nextTryConnectPeriod int64 = 60
)

func init() {
extension.SetCacheFactory(&memory.DefaultCacheFactory{})
extension.SetLoadBalance(&roundrobin.RoundRobin{})
Expand All @@ -64,6 +69,10 @@ type Client struct {
cache *storage.Cache
}

func (c *Client) getAppConfig() config.AppConfig {
return *c.appConfig
}

func create() *Client {

appConfig := env.InitFileConfig()
Expand Down Expand Up @@ -93,21 +102,21 @@ func StartWithConfig(loadAppConfig func() (*config.AppConfig, error)) (*Client,
c.cache = storage.CreateNamespaceConfig(appConfig.NamespaceName)
appConfig.Init()

serverlist.InitSyncServerIPList(c.appConfig)
serverlist.InitSyncServerIPList(c.getAppConfig)

//first sync
configs := syncApolloConfig.Sync(c.appConfig)
configs := syncApolloConfig.Sync(c.getAppConfig)
if len(configs) > 0 {
for _, apolloConfig := range configs {
c.cache.UpdateApolloConfig(apolloConfig, c.appConfig, true)
c.cache.UpdateApolloConfig(apolloConfig, c.getAppConfig, true)
}
}

log.Debug("init notifySyncConfigServices finished")

//start long poll sync config
configComponent := &notify.ConfigComponent{}
configComponent.SetAppConfig(c.appConfig)
configComponent.SetAppConfig(c.getAppConfig)
configComponent.SetCache(c.cache)
go component.StartRefreshConfig(configComponent)

Expand All @@ -134,7 +143,7 @@ func (c *Client) GetConfigAndInit(namespace string) *storage.Config {
storage.CreateNamespaceConfig(namespace)

//sync config
syncApolloConfig.SyncWithNamespace(namespace, c.appConfig)
syncApolloConfig.SyncWithNamespace(namespace, c.getAppConfig)
}

config = c.cache.GetConfig(namespace)
Expand Down
11 changes: 7 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"github.com/zouyx/agollo/v4/agcache/memory"
"github.com/zouyx/agollo/v4/env/config"
"github.com/zouyx/agollo/v4/env/server"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -197,15 +198,17 @@ func TestGetStringValue(t *testing.T) {

func TestAutoSyncConfigServicesNormal2NotModified(t *testing.T) {
client := createMockApolloConfig(120)
server := runLongNotmodifiedConfigResponse()
serverResponse := runLongNotmodifiedConfigResponse()
newAppConfig := getTestAppConfig()
newAppConfig.IP = server.URL
newAppConfig.IP = serverResponse.URL
time.Sleep(1 * time.Second)
newAppConfig.NextTryConnTime = 0
server.SetServers(newAppConfig.GetHost(), nil)
client.appConfig = newAppConfig

apolloConfig, _ := createApolloConfigWithJSON([]byte(configResponseStr))
client.cache.UpdateApolloConfig(apolloConfig.(*config.ApolloConfig), newAppConfig, true)
client.cache.UpdateApolloConfig(apolloConfig.(*config.ApolloConfig), func() config.AppConfig {
return *newAppConfig
}, true)

config := newAppConfig.GetCurrentApolloConfig().Get()[newAppConfig.NamespaceName]

Expand Down
4 changes: 1 addition & 3 deletions cluster/load_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
package cluster

import (
"sync"

"github.com/zouyx/agollo/v4/env/config"
)

//LoadBalance 负载均衡器
type LoadBalance interface {
//Load 负载均衡,获取对应服务信息
Load(servers sync.Map) *config.ServerInfo
Load(servers map[string]*config.ServerInfo) *config.ServerInfo
}
13 changes: 5 additions & 8 deletions cluster/roundrobin/round_robin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
package roundrobin

import (
"sync"

"github.com/zouyx/agollo/v4/env/config"
)

Expand All @@ -28,16 +26,15 @@ type RoundRobin struct {
}

//Load 负载均衡
func (r *RoundRobin) Load(servers sync.Map) *config.ServerInfo {
func (r *RoundRobin) Load(servers map[string]*config.ServerInfo) *config.ServerInfo {
var returnServer *config.ServerInfo
servers.Range(func(k, v interface{}) bool {
server := v.(*config.ServerInfo)
for _, server := range servers {
// if some node has down then select next node
if server.IsDown {
return true
continue
}
returnServer = server
return false
})
break
}
return returnServer
}
53 changes: 27 additions & 26 deletions cluster/roundrobin/round_robin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package roundrobin
import (
"github.com/zouyx/agollo/v4/component/serverlist"
"github.com/zouyx/agollo/v4/env/config"
"github.com/zouyx/agollo/v4/env/server"
"github.com/zouyx/agollo/v4/protocol/http"
"testing"

Expand Down Expand Up @@ -84,57 +85,57 @@ func TestSelectHost(t *testing.T) {

appConfig := env.InitFileConfig()
//mock ip data
trySyncServerIPList(appConfig)
trySyncServerIPList(*appConfig)

servers := appConfig.GetServers()
t.Log("appconfig host:" + appConfig.GetHost())
t.Log("appconfig select host:", balanace.Load(*appConfig.GetServers()).HomepageURL)
t.Log("appconfig select host:", balanace.Load(server.GetServers(appConfig.GetHost())).HomepageURL)

host := "http://localhost:8888/"
Assert(t, host, Equal(appConfig.GetHost()))
Assert(t, host, NotEqual(balanace.Load(*appConfig.GetServers()).HomepageURL))
Assert(t, host, NotEqual(balanace.Load(server.GetServers(appConfig.GetHost())).HomepageURL))

//check select next time
appConfig.SetNextTryConnTime(5)
Assert(t, host, NotEqual(balanace.Load(*appConfig.GetServers()).HomepageURL))
server.SetNextTryConnTime(appConfig.GetHost(), 5)
Assert(t, host, NotEqual(balanace.Load(server.GetServers(appConfig.GetHost())).HomepageURL))

//check servers
appConfig.SetNextTryConnTime(5)
firstHost := balanace.Load(*appConfig.GetServers())
Assert(t, host, NotEqual(firstHost.HomepageURL))
appConfig.SetDownNode(firstHost.HomepageURL)
server.SetNextTryConnTime(appConfig.GetHost(), 5)
firstHost := balanace.Load(server.GetServers(appConfig.GetHost())).HomepageURL
Assert(t, host, NotEqual(firstHost))
server.SetDownNode(appConfig.GetHost(), firstHost)

secondHost := balanace.Load(*appConfig.GetServers()).HomepageURL
secondHost := balanace.Load(server.GetServers(appConfig.GetHost())).HomepageURL
Assert(t, host, NotEqual(secondHost))
Assert(t, firstHost, NotEqual(secondHost))
appConfig.SetDownNode(secondHost)
server.SetDownNode(appConfig.GetHost(), secondHost)

thirdHost := balanace.Load(*appConfig.GetServers()).HomepageURL
thirdHost := balanace.Load(server.GetServers(appConfig.GetHost())).HomepageURL
Assert(t, host, NotEqual(thirdHost))
Assert(t, firstHost, NotEqual(thirdHost))
Assert(t, secondHost, NotEqual(thirdHost))

servers.Range(func(k, v interface{}) bool {
appConfig.SetDownNode(k.(string))
return true
})
for _, info := range server.GetServers(appConfig.GetHost()) {
info.IsDown = true
}

Assert(t, balanace.Load(*appConfig.GetServers()), NilVal())
Assert(t, balanace.Load(server.GetServers(appConfig.GetHost())), NilVal())

//no servers
//servers = make(map[string]*serverInfo, 0)
deleteServers(appConfig)
Assert(t, balanace.Load(*appConfig.GetServers()), NilVal())
Assert(t, balanace.Load(server.GetServers(appConfig.GetHost())), NilVal())
}

func deleteServers(appConfig *config.AppConfig) {
servers := appConfig.GetServers()
servers.Range(func(k, v interface{}) bool {
servers.Delete(k)
return true
})
servers := make(map[string]*config.ServerInfo)
server.SetServers(appConfig.GetHost(), servers)
}

func trySyncServerIPList(appConfig *config.AppConfig) {
serverlist.SyncServerIPListSuccessCallBack([]byte(servicesConfigResponseStr), http.CallBack{AppConfig: appConfig})
func trySyncServerIPList(appConfig config.AppConfig) {
// 里面已经设置了
serverMap, _ := serverlist.SyncServerIPListSuccessCallBack([]byte(servicesConfigResponseStr), http.CallBack{AppConfigFunc: func() config.AppConfig {
return appConfig
}})
m := serverMap.(map[string]*config.ServerInfo)
server.SetServers(appConfig.GetHost(), m)
}
28 changes: 17 additions & 11 deletions component/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package component

import (
"github.com/zouyx/agollo/v4/component/log"
"github.com/zouyx/agollo/v4/env/server"
"github.com/zouyx/agollo/v4/protocol/http"
"testing"

Expand Down Expand Up @@ -94,25 +95,26 @@ var (

func TestSelectOnlyOneHost(t *testing.T) {
appConfig := env.InitFileConfig()
trySyncServerIPList(appConfig)
trySyncServerIPList(func() config.AppConfig {
return *appConfig
})
host := "http://localhost:8888/"
Assert(t, host, Equal(appConfig.GetHost()))
load := extension.GetLoadBalance().Load(*appConfig.GetServers())
load := extension.GetLoadBalance().Load(server.GetServers(appConfig.GetHost()))
Assert(t, load, NotNilVal())
Assert(t, host, NotEqual(load.HomepageURL))

appConfig.IP = host
Assert(t, host, Equal(appConfig.GetHost()))
load = extension.GetLoadBalance().Load(*appConfig.GetServers())
load = extension.GetLoadBalance().Load(server.GetServers(appConfig.GetHost()))
Assert(t, load, NotNilVal())
Assert(t, host, NotEqual(load.HomepageURL))

appConfig.IP = "https://localhost:8888"
https := "https://localhost:8888/"
Assert(t, https, Equal(appConfig.GetHost()))
load = extension.GetLoadBalance().Load(*appConfig.GetServers())
Assert(t, load, NotNilVal())
Assert(t, host, NotEqual(load.HomepageURL))
load = extension.GetLoadBalance().Load(server.GetServers(appConfig.GetHost()))
Assert(t, load, NilVal())
}

type testComponent struct {
Expand All @@ -130,8 +132,8 @@ func TestName(t *testing.T) {

}

func trySyncServerIPList(appConfig *config.AppConfig) {
SyncServerIPListSuccessCallBack([]byte(servicesConfigResponseStr), http.CallBack{AppConfig: appConfig})
func trySyncServerIPList(appConfigFunc func() config.AppConfig) {
SyncServerIPListSuccessCallBack([]byte(servicesConfigResponseStr), http.CallBack{AppConfigFunc: appConfigFunc})
}

//SyncServerIPListSuccessCallBack 同步服务器列表成功后的回调
Expand All @@ -152,11 +154,15 @@ func SyncServerIPListSuccessCallBack(responseBody []byte, callback http.CallBack
return
}

for _, server := range tmpServerInfo {
if server == nil {
serverMap := make(map[string]*config.ServerInfo)
for _, server1 := range tmpServerInfo {
if server1 == nil {
continue
}
callback.AppConfig.GetServers().Store(server.HomepageURL, server)
serverMap[server1.HomepageURL] = server1
}
configFunc := callback.AppConfigFunc()
c := &configFunc
server.SetServers(c.GetHost(), serverMap)
return
}
9 changes: 7 additions & 2 deletions component/notify/change_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/zouyx/agollo/v4/agcache/memory"
"github.com/zouyx/agollo/v4/cluster/roundrobin"
"github.com/zouyx/agollo/v4/component/remote"
"github.com/zouyx/agollo/v4/env/config"
jsonFile "github.com/zouyx/agollo/v4/env/file/json"
"github.com/zouyx/agollo/v4/extension"
"sync"
Expand Down Expand Up @@ -83,8 +84,12 @@ func buildNotifyResult(t *testing.T) {
newAppConfig.IP = server.URL

syncApolloConfig := remote.CreateSyncApolloConfig()
apolloConfigs := syncApolloConfig.Sync(newAppConfig)
apolloConfigs = syncApolloConfig.Sync(newAppConfig)
apolloConfigs := syncApolloConfig.Sync(func() config.AppConfig {
return *newAppConfig
})
apolloConfigs = syncApolloConfig.Sync(func() config.AppConfig {
return *newAppConfig
})

Assert(t, apolloConfigs, NotNilVal())
Assert(t, len(apolloConfigs), Equal(1))
Expand Down
12 changes: 6 additions & 6 deletions component/notify/componet_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ const (

//ConfigComponent 配置组件
type ConfigComponent struct {
appConfig *config.AppConfig
cache *storage.Cache
appConfigFunc func() config.AppConfig
cache *storage.Cache
}

// SetAppConfig nolint
func (c *ConfigComponent) SetAppConfig(appConfig *config.AppConfig) {
c.appConfig = appConfig
func (c *ConfigComponent) SetAppConfig(appConfigFunc func() config.AppConfig) {
c.appConfigFunc = appConfigFunc
}

// SetCache nolint
Expand All @@ -53,9 +53,9 @@ func (c *ConfigComponent) Start() {
for {
select {
case <-t2.C:
configs := instance.Sync(c.appConfig)
configs := instance.Sync(c.appConfigFunc)
for _, apolloConfig := range configs {
c.cache.UpdateApolloConfig(apolloConfig, c.appConfig, true)
c.cache.UpdateApolloConfig(apolloConfig, c.appConfigFunc, true)
}
t2.Reset(longPollInterval)
}
Expand Down
8 changes: 4 additions & 4 deletions component/remote/abs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ type AbsApolloConfig struct {
remoteApollo ApolloConfig
}

func (a *AbsApolloConfig) SyncWithNamespace(namespace string, appConfig *config.AppConfig) *config.ApolloConfig {
if appConfig == nil {
func (a *AbsApolloConfig) SyncWithNamespace(namespace string, appConfigFunc func() config.AppConfig) *config.ApolloConfig {
if appConfigFunc == nil {
panic("can not find apollo config!please confirm!")
}

urlSuffix := a.remoteApollo.GetSyncURI(*appConfig, namespace)
appConfig := appConfigFunc()
urlSuffix := a.remoteApollo.GetSyncURI(appConfig, namespace)

callback := a.remoteApollo.CallBack(namespace)
apolloConfig, err := http.RequestRecovery(appConfig, &env.ConnectConfig{
Expand Down
Loading

0 comments on commit e35bcef

Please sign in to comment.