Skip to content

Commit

Permalink
Finish v2.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Nov 4, 2019
2 parents 4bd44bb + c50ba28 commit 2d89707
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 33 deletions.
23 changes: 1 addition & 22 deletions app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,6 @@ type serverInfo struct {
IsDown bool `json:"-"`
}

func init() {
//init config
initFileConfig()

//init common
initCommon()
}

func initCommon() {

}

func initFileConfig() {
// default use application.properties
initConfig(nil)
Expand All @@ -166,16 +154,6 @@ func initConfig(loadAppConfig func() (*AppConfig, error)) {
}(appConfig)
}

//init config by custom
func InitCustomConfig(loadAppConfig func() (*AppConfig, error)) {

initConfig(loadAppConfig)

//init all notification
initAllNotifications()

}

// set load app config's function
func getLoadAppConfig(loadAppConfig func() (*AppConfig, error)) (*AppConfig, error) {
if loadAppConfig != nil {
Expand All @@ -192,6 +170,7 @@ func getLoadAppConfig(loadAppConfig func() (*AppConfig, error)) (*AppConfig, err
//interval : 20m
func initServerIpList() {
syncServerIpList(nil)
logger.Debug("syncServerIpList started")

t2 := time.NewTimer(refresh_ip_list_interval)
for {
Expand Down
4 changes: 0 additions & 4 deletions componet_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func (n *notificationsMap) getNotifies() string {
return string(j)
}

func init() {
initAllNotifications()
}

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

Expand Down
48 changes: 48 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package agollo

import (
"fmt"
"github.com/zouyx/agollo/agcache"
)

const propertiesFormat ="%s=%s\n"

//ContentParser 内容转换
type ContentParser interface {
parse(cache agcache.CacheInterface) (string,error)
}

//DefaultParser 默认内容转换器
type DefaultParser struct {

}

func (d *DefaultParser)parse(cache agcache.CacheInterface) (string,error){
value, err := cache.Get(defaultContentKey)
if err!=nil{
return "",err
}
return string(value),nil
}

//PropertiesParser properties转换器
type PropertiesParser struct {

}

func (d *PropertiesParser)parse(cache agcache.CacheInterface) (string,error){
properties := convertToProperties(cache)
return properties,nil
}

func convertToProperties(cache agcache.CacheInterface) string {
properties:=""
if cache==nil {
return properties
}
cache.Range(func(key, value interface{}) bool {
properties+=fmt.Sprintf(propertiesFormat,key,string(value.([]byte)))
return true
})
return properties
}
40 changes: 38 additions & 2 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,33 @@ import (
"sync"
)


//ConfigFileFormat 配置文件类型
type ConfigFileFormat string

const (
//Properties
Properties ConfigFileFormat = "Properties"
//XML
XML ConfigFileFormat = "xml"
//JSON
JSON ConfigFileFormat = "json"
//YML
YML ConfigFileFormat = "yml"
//YAML
YAML ConfigFileFormat = "yaml"
)

const (
apolloConfigFormat = "%s.%s"

empty = ""

//1 minute
configCacheExpireTime = 120

defaultNamespace="application"
defaultContentKey="content"
)


Expand All @@ -23,10 +43,13 @@ var (

//config from apollo
apolloConfigCache = make(map[string]*Config,0)

formatParser = make(map[ConfigFileFormat]ContentParser,0)
defaultFormatParser =&DefaultParser{}
)

func init() {
initDefaultConfig()
func init(){
formatParser[Properties]=&PropertiesParser{}
}

func initDefaultConfig() {
Expand Down Expand Up @@ -335,4 +358,17 @@ func GetBoolValue(key string, defaultValue bool) bool {
}

return b
}

//GetContent 获取配置文件内容
func (c *Config)GetContent(format ConfigFileFormat) string {
parser := formatParser[format]
if parser==nil{
parser=defaultFormatParser
}
s, err:= parser.parse(c.cache)
if err!=nil{
logger.Debug("GetContent fail ! error:", err)
}
return s
}
33 changes: 28 additions & 5 deletions start.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
package agollo

import "github.com/zouyx/agollo/agcache"
import (
"github.com/zouyx/agollo/agcache"
)

func init() {
//init config
initFileConfig()

initCommon()
}

func initCommon() {
initDefaultConfig()

initAllNotifications()
}

//InitCustomConfig init config by custom
func InitCustomConfig(loadAppConfig func() (*AppConfig, error)) {

initConfig(loadAppConfig)

initCommon()
}

//start apollo
func Start() error {
return startAgollo()
}

//SetLogger 设置自定义logger组件
func SetLogger(loggerInterface LoggerInterface) {
func SetLogger(loggerInterface LoggerInterface) {
if loggerInterface != nil {
initLogger(loggerInterface)
}
}

//SetCache 设置自定义cache组件
func SetCache(cacheFactory *agcache.DefaultCacheFactory) {
func SetCache(cacheFactory *agcache.DefaultCacheFactory) {
if cacheFactory != nil {
initConfigCache(cacheFactory)
}
Expand All @@ -36,14 +59,14 @@ func StartWithCache(cacheFactory *agcache.DefaultCacheFactory) error {
func startAgollo() error {
//init server ip list
go initServerIpList()

//first sync
err := notifySyncConfigServices()
logger.Debug("init notifySyncConfigServices finished")

//first sync fail then load config file
if err != nil {
splitNamespaces(appConfig.NamespaceName, func(namespace string) {
config, _ := loadConfigFile(appConfig.BackupConfigPath,namespace)
config, _ := loadConfigFile(appConfig.BackupConfigPath, namespace)
if config != nil {
updateApolloConfig(config, false)
}
Expand Down

0 comments on commit 2d89707

Please sign in to comment.