Skip to content

Commit

Permalink
Merge 4c5d407 into 101b68e
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Oct 10, 2019
2 parents 101b68e + 4c5d407 commit 85e118b
Show file tree
Hide file tree
Showing 15 changed files with 468 additions and 128 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
.glide/
/.idea/
/apolloConfig.json
/application.json
44 changes: 44 additions & 0 deletions agcache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package agcache

import "github.com/coocood/freecache"

//CacheInterface 自定义缓存组件接口
type CacheInterface interface {
Set(key, value []byte, expireSeconds int) (err error)

EntryCount() (entryCount int64)

Get(key []byte) (value []byte, err error)

Del(key []byte) (affected bool)

NewIterator() *freecache.Iterator

TTL(key []byte) (timeLeft uint32, err error)

Clear()
}

const (
//50m
apolloConfigCacheSize = 50 * 1024 * 1024

//1 minute
configCacheExpireTime = 120
)

//CacheFactory 缓存组件工厂接口
type CacheFactory interface {
//Create 创建缓存组件
Create() CacheInterface
}

//DefaultCacheFactory 构造默认缓存组件工厂类
type DefaultCacheFactory struct {

}

//Create 创建默认缓存组件
func (d *DefaultCacheFactory) Create()CacheInterface {
return freecache.NewCache(apolloConfigCacheSize)
}
30 changes: 14 additions & 16 deletions app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ var (
nofity_connect_timeout = 10 * time.Minute //10m
//for on error retry
on_error_retry_interval = 1 * time.Second //1s
//for typed config cache of parser result, e.g. integer, double, long, etc.
//max_config_cache_size = 500 //500 cache key
//for typed config agcache of parser result, e.g. integer, double, long, etc.
//max_config_cache_size = 500 //500 agcache key
//config_cache_expire_time = 1 * time.Minute //1 minute

//max retries connect apollo
Expand All @@ -33,7 +33,7 @@ var (
appConfig *AppConfig

//real servers ip
servers map[string]*serverInfo = make(map[string]*serverInfo, 0)
servers = make(map[string]*serverInfo, 0)

//next try connect period - 60 second
next_try_connect_period int64 = 60
Expand Down Expand Up @@ -145,12 +145,12 @@ func initConfig(loadAppConfig func() (*AppConfig, error)) {
}

func(appConfig *AppConfig) {
apolloConfig := &ApolloConfig{}
apolloConfig.AppId = appConfig.AppId
apolloConfig.Cluster = appConfig.Cluster
apolloConfig.NamespaceName = appConfig.NamespaceName
splitNamespaces(appConfig.NamespaceName, func(namespace string) {
apolloConfig := &ApolloConfig{}
apolloConfig.init(appConfig,namespace)

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

Expand Down Expand Up @@ -215,7 +215,7 @@ func syncServerIpListSuccessCallBack(responseBody []byte) (o interface{}, err er

//sync ip list from server
//then
//1.update cache
//1.update agcache
//2.store in disk
func syncServerIpList(newAppConfig *AppConfig) error {
appConfig := GetAppConfig(newAppConfig)
Expand All @@ -242,26 +242,24 @@ func getConfigUrl(config *AppConfig) string {
}

func getConfigUrlByHost(config *AppConfig, host string) string {
current := GetCurrentApolloConfig()
return fmt.Sprintf("%sconfigs/%s/%s/%s?releaseKey=%s&ip=%s",
host,
url.QueryEscape(config.AppId),
url.QueryEscape(config.Cluster),
url.QueryEscape(config.NamespaceName),
url.QueryEscape(current.ReleaseKey),
url.QueryEscape(getCurrentApolloConfigReleaseKey(config.NamespaceName)),
getInternal())
}

func getConfigUrlSuffix(config *AppConfig, newConfig *AppConfig) string {
if newConfig != nil {
func getConfigURLSuffix(config *AppConfig,namespaceName string) string {
if config == nil {
return ""
}
current := GetCurrentApolloConfig()
return fmt.Sprintf("configs/%s/%s/%s?releaseKey=%s&ip=%s",
url.QueryEscape(config.AppId),
url.QueryEscape(config.Cluster),
url.QueryEscape(config.NamespaceName),
url.QueryEscape(current.ReleaseKey),
url.QueryEscape(namespaceName),
url.QueryEscape(getCurrentApolloConfigReleaseKey(namespaceName)),
getInternal())
}

Expand Down
4 changes: 2 additions & 2 deletions app_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestInit(t *testing.T) {
Assert(t, "application", Equal(config.NamespaceName))
Assert(t, "localhost:8888", Equal(config.Ip))

apolloConfig := GetCurrentApolloConfig()
apolloConfig := GetCurrentApolloConfig()[config.NamespaceName]
Assert(t, "test", Equal(apolloConfig.AppId))
Assert(t, "dev", Equal(apolloConfig.Cluster))
Assert(t, "application", Equal(apolloConfig.NamespaceName))
Expand All @@ -42,7 +42,7 @@ func TestStructInit(t *testing.T) {
Assert(t, "application1", Equal(config.NamespaceName))
Assert(t, "localhost:8889", Equal(config.Ip))

apolloConfig := GetCurrentApolloConfig()
apolloConfig := GetCurrentApolloConfig()[config.NamespaceName]
Assert(t, "test1", Equal(apolloConfig.AppId))
Assert(t, "dev1", Equal(apolloConfig.Cluster))
Assert(t, "application1", Equal(apolloConfig.NamespaceName))
Expand Down
2 changes: 1 addition & 1 deletion change_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func buildNotifyResult(t *testing.T) {

Assert(t, err,NilVal())

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

Assert(t, "100004458", Equal(config.AppId))
Assert(t, "default", Equal(config.Cluster))
Expand Down
21 changes: 21 additions & 0 deletions component_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package agollo

import (
"encoding/json"
"strings"
"sync"
)

const (
comma = ","
)

type AbsComponent interface {
Start()
}
Expand All @@ -26,6 +31,22 @@ type ApolloConfig struct {
Configurations map[string]string `json:"configurations"`
}

func splitNamespaces(namespacesStr string,callback func(namespace string))map[string]int64{
namespaces:=make(map[string]int64,1)
split := strings.Split(namespacesStr, comma)
for _, namespace := range split {
callback(namespace)
namespaces[namespace]=default_notification_id
}
return namespaces
}

func (a *ApolloConfig) init(appConfig *AppConfig,namespace string) {
a.AppId = appConfig.AppId
a.Cluster = appConfig.Cluster
a.NamespaceName = namespace
}

func createApolloConfigWithJson(b []byte) (*ApolloConfig, error) {
apolloConfig := &ApolloConfig{}
err := json.Unmarshal(b, apolloConfig)
Expand Down
57 changes: 37 additions & 20 deletions componet_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ type apolloNotify struct {
NamespaceName string `json:"namespaceName"`
}

func (this *notificationsMap) setNotify(namespaceName string, notificationId int64) {
this.Lock()
defer this.Unlock()
this.notifications[namespaceName] = notificationId
func (n *notificationsMap) setNotify(namespaceName string, notificationId int64) {
n.Lock()
defer n.Unlock()
n.notifications[namespaceName] = notificationId
}
func (this *notificationsMap) getNotifies() string {
this.RLock()
defer this.RUnlock()

func (n *notificationsMap) getNotify(namespace string) int64 {
n.RLock()
defer n.RUnlock()
return n.notifications[namespace]
}

func (n *notificationsMap) getNotifies() string {
n.RLock()
defer n.RUnlock()

notificationArr := make([]*notification, 0)
for namespaceName, notificationId := range this.notifications {
for namespaceName, notificationId := range n.notifications {
notificationArr = append(notificationArr,
&notification{
NamespaceName: namespaceName,
Expand All @@ -64,11 +71,12 @@ func initAllNotifications() {
appConfig := GetAppConfig(nil)

if appConfig != nil {
namespaces := splitNamespaces(appConfig.NamespaceName,
func(namespace string) {})

allNotifications = &notificationsMap{
notifications: make(map[string]int64, 1),
notifications: namespaces,
}

allNotifications.setNotify(appConfig.NamespaceName, default_notification_id)
}
}

Expand Down Expand Up @@ -146,6 +154,9 @@ func updateAllNotifications(remoteConfigs []*apolloNotify) {
if remoteConfig.NamespaceName == "" {
continue
}
if allNotifications.getNotify(remoteConfig.NamespaceName)==0{
continue
}

allNotifications.setNotify(remoteConfig.NamespaceName, remoteConfig.NotificationId)
}
Expand All @@ -170,14 +181,20 @@ func autoSyncConfigServices(newAppConfig *AppConfig) error {
panic("can not find apollo config!please confirm!")
}

urlSuffix := getConfigUrlSuffix(appConfig, newAppConfig)

_, err := requestRecovery(appConfig, &ConnectConfig{
Uri: urlSuffix,
}, &CallBack{
SuccessCallBack: autoSyncConfigServicesSuccessCallBack,
NotModifyCallBack: touchApolloConfigCache,
})
var err error
for namespace := range allNotifications.notifications {
urlSuffix := getConfigURLSuffix(appConfig, namespace)

_, err = requestRecovery(appConfig, &ConnectConfig{
Uri: urlSuffix,
}, &CallBack{
SuccessCallBack: autoSyncConfigServicesSuccessCallBack,
NotModifyCallBack: touchApolloConfigCache,
})
if err!=nil{
return err
}
}

return err
return nil
}

0 comments on commit 85e118b

Please sign in to comment.