Skip to content

Commit

Permalink
Merge 8bd4950 into 295a72a
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Apr 19, 2020
2 parents 295a72a + 8bd4950 commit e4dd20b
Show file tree
Hide file tree
Showing 28 changed files with 309 additions and 121 deletions.
File renamed without changes.
18 changes: 0 additions & 18 deletions agcache/cache.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package agcache

var (
gobalCacheFactory CacheFactory
)

func init() {
gobalCacheFactory = &DefaultCacheFactory{}
}

//CacheInterface 自定义缓存组件接口
type CacheInterface interface {
Set(key string, value []byte, expireSeconds int) (err error)
Expand All @@ -28,13 +20,3 @@ type CacheFactory interface {
//Create 创建缓存组件
Create() CacheInterface
}

//GetCacheFactory 获取CacheFactory
func GetCacheFactory() CacheFactory {
return gobalCacheFactory
}

//UseCacheFactory 替换CacheFactory
func UseCacheFactory(cacheFactory CacheFactory) {
gobalCacheFactory = cacheFactory
}
22 changes: 0 additions & 22 deletions agcache/cache_test.go

This file was deleted.

11 changes: 9 additions & 2 deletions agcache/default.go → agcache/memory/memory.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package agcache
package memory

import (
"errors"
"sync"

"github.com/zouyx/agollo/v3/agcache"
"github.com/zouyx/agollo/v3/extension"
)

func init() {
extension.SetCacheFactory(&DefaultCacheFactory{})
}

//DefaultCache 默认缓存
type DefaultCache struct {
defaultCache sync.Map
Expand Down Expand Up @@ -56,6 +63,6 @@ type DefaultCacheFactory struct {
}

//Create 创建默认缓存组件
func (d *DefaultCacheFactory) Create() CacheInterface {
func (d *DefaultCacheFactory) Create() agcache.CacheInterface {
return &DefaultCache{}
}
5 changes: 3 additions & 2 deletions agcache/default_test.go → agcache/memory/memory_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package agcache
package memory

import (
"testing"

. "github.com/tevid/gohamcrest"
"github.com/zouyx/agollo/v3/agcache"
)

var testDefaultCache CacheInterface
var testDefaultCache agcache.CacheInterface

func init() {
factory := &DefaultCacheFactory{}
Expand Down
13 changes: 13 additions & 0 deletions cluster/load_balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cluster

import (
"sync"

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

//LoadBalance 负载均衡器
type LoadBalance interface {
//Load 负载均衡,获取对应服务信息
Load(servers *sync.Map) *config.ServerInfo
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ import (
"sync"

"github.com/zouyx/agollo/v3/env/config"
"github.com/zouyx/agollo/v3/loadbalance"
"github.com/zouyx/agollo/v3/extension"
)

func init() {
loadbalance.SetLoadBalance(&RoundRobin{})
}

//InitLoadBalance 初始化负载均衡器
func InitLoadBalance() {

extension.SetLoadBalance(&RoundRobin{})
}

//RoundRobin 轮询调度
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

. "github.com/tevid/gohamcrest"
"github.com/zouyx/agollo/v3/env"
"github.com/zouyx/agollo/v3/loadbalance"
)

const servicesConfigResponseStr = `[{
Expand Down Expand Up @@ -61,7 +60,7 @@ const servicesConfigResponseStr = `[{
]`

func TestSelectHost(t *testing.T) {
balanace := loadbalance.GetLoadBalance()
balanace := &RoundRobin{}
//mock ip data
trySyncServerIPList()

Expand Down
6 changes: 3 additions & 3 deletions component/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"testing"

. "github.com/tevid/gohamcrest"
_ "github.com/zouyx/agollo/v3/cluster/roundrobin"
"github.com/zouyx/agollo/v3/env"
"github.com/zouyx/agollo/v3/env/config"
"github.com/zouyx/agollo/v3/env/config/json"
"github.com/zouyx/agollo/v3/loadbalance"
_ "github.com/zouyx/agollo/v3/loadbalance/roundrobin"
"github.com/zouyx/agollo/v3/extension"
)

const servicesConfigResponseStr = `[{
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestSelectOnlyOneHost(t *testing.T) {
appConfig := env.GetPlainAppConfig()
host := "http://localhost:8888/"
Assert(t, host, Equal(appConfig.GetHost()))
load := loadbalance.GetLoadBalance().Load(env.GetServers())
load := extension.GetLoadBalance().Load(env.GetServers())
Assert(t, load, NotNilVal())
Assert(t, host, NotEqual(load.HomepageURL))
}
Expand Down
6 changes: 2 additions & 4 deletions component/notify/change_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"time"

. "github.com/tevid/gohamcrest"
_ "github.com/zouyx/agollo/v3/agcache/memory"
"github.com/zouyx/agollo/v3/env"
jsonFile "github.com/zouyx/agollo/v3/env/file/json"
"github.com/zouyx/agollo/v3/extension"
_ "github.com/zouyx/agollo/v3/env/file/json"
"github.com/zouyx/agollo/v3/storage"
)

Expand Down Expand Up @@ -58,7 +58,6 @@ func TestListenChangeEvent(t *testing.T) {
}

func buildNotifyResult(t *testing.T) {
extension.SetFileHandler(&jsonFile.JSONFileHandler{})
initNotifications()
server := runChangeConfigResponse()
defer server.Close()
Expand All @@ -82,7 +81,6 @@ func buildNotifyResult(t *testing.T) {
}

func TestRemoveChangeListener(t *testing.T) {
extension.SetFileHandler(&jsonFile.JSONFileHandler{})
go buildNotifyResult(t)

listener := &CustomChangeListener{}
Expand Down
5 changes: 2 additions & 3 deletions component/notify/componet_notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"time"

. "github.com/tevid/gohamcrest"
_ "github.com/zouyx/agollo/v3/cluster/roundrobin"
"github.com/zouyx/agollo/v3/env"
"github.com/zouyx/agollo/v3/env/config"
jsonConfig "github.com/zouyx/agollo/v3/env/config/json"
jsonFile "github.com/zouyx/agollo/v3/env/file/json"
_ "github.com/zouyx/agollo/v3/env/file/json"
"github.com/zouyx/agollo/v3/extension"
_ "github.com/zouyx/agollo/v3/loadbalance/roundrobin"
)

var (
Expand Down Expand Up @@ -56,7 +56,6 @@ func initMockNotifyAndConfigServer() {
}

func TestSyncConfigServices(t *testing.T) {
extension.SetFileHandler(&jsonFile.JSONFileHandler{})
initMockNotifyAndConfigServer()

err := AsyncConfigs()
Expand Down
12 changes: 6 additions & 6 deletions env/file/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
const Suffix = ".json"

func init() {
extension.SetFileHandler(&JSONFileHandler{})
extension.SetFileHandler(&jsonFileHandler{})
}

var (
Expand All @@ -25,17 +25,17 @@ var (
configFileMap = make(map[string]string, 1)
)

//JSONFileHandler 默认备份文件读写
type JSONFileHandler struct {
//jsonFileHandler 默认备份文件读写
type jsonFileHandler struct {
}

//WriteConfigFile write config to file
func (fileHandler *JSONFileHandler) WriteConfigFile(config *env.ApolloConfig, configPath string) error {
func (fileHandler *jsonFileHandler) WriteConfigFile(config *env.ApolloConfig, configPath string) error {
return jsonFileConfig.Write(config, fileHandler.GetConfigFile(configPath, config.NamespaceName))
}

//GetConfigFile get real config file
func (fileHandler *JSONFileHandler) GetConfigFile(configDir string, namespace string) string {
func (fileHandler *jsonFileHandler) GetConfigFile(configDir string, namespace string) string {
fullPath := configFileMap[namespace]
if fullPath == "" {
filePath := fmt.Sprintf("%s%s", namespace, Suffix)
Expand All @@ -49,7 +49,7 @@ func (fileHandler *JSONFileHandler) GetConfigFile(configDir string, namespace st
}

//LoadConfigFile load config from file
func (fileHandler *JSONFileHandler) LoadConfigFile(configDir string, namespace string) (*env.ApolloConfig, error) {
func (fileHandler *jsonFileHandler) LoadConfigFile(configDir string, namespace string) (*env.ApolloConfig, error) {
configFilePath := fileHandler.GetConfigFile(configDir, namespace)
log.Info("load config file from :", configFilePath)
c, e := jsonFileConfig.Load(configFilePath, func(b []byte) (interface{}, error) {
Expand Down
4 changes: 2 additions & 2 deletions env/file/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestJSONFileHandler_WriteConfigFile(t *testing.T) {
extension.SetFileHandler(&JSONFileHandler{})
extension.SetFileHandler(&jsonFileHandler{})
configPath := ""
jsonStr := `{
"appId": "100004458",
Expand All @@ -32,7 +32,7 @@ func TestJSONFileHandler_WriteConfigFile(t *testing.T) {
}

func TestJSONFileHandler_LoadConfigFile(t *testing.T) {
extension.SetFileHandler(&JSONFileHandler{})
extension.SetFileHandler(&jsonFileHandler{})
jsonStr := `{
"appId": "100004458",
"cluster": "default",
Expand Down
23 changes: 19 additions & 4 deletions env/file/json/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ package json
import (
"fmt"
"os"
"sync"

"github.com/zouyx/agollo/v3/env"
"github.com/zouyx/agollo/v3/env/file"
)

//RawHandler 写入备份文件时,同时写入原始内容和namespace类型
type RawHandler struct {
*JSONFileHandler
var (
raw file.FileHandler
rawOnce sync.Once
)

//rawFileHandler 写入备份文件时,同时写入原始内容和namespace类型
type rawFileHandler struct {
*jsonFileHandler
}

func writeWithRaw(config *env.ApolloConfig, configDir string) error {
Expand All @@ -33,7 +40,15 @@ func writeWithRaw(config *env.ApolloConfig, configDir string) error {
}

//WriteConfigFile write config to file
func (fileHandler *RawHandler) WriteConfigFile(config *env.ApolloConfig, configPath string) error {
func (fileHandler *rawFileHandler) WriteConfigFile(config *env.ApolloConfig, configPath string) error {
writeWithRaw(config, configPath)
return jsonFileConfig.Write(config, fileHandler.GetConfigFile(configPath, config.NamespaceName))
}

// GetRawFileHandler 获取 rawFileHandler 实例
func GetRawFileHandler() file.FileHandler {
rawOnce.Do(func() {
raw = &rawFileHandler{}
})
return raw
}
10 changes: 9 additions & 1 deletion env/file/json/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestRawHandler_WriteConfigFile(t *testing.T) {
extension.SetFileHandler(&RawHandler{})
extension.SetFileHandler(&rawFileHandler{})
configPath := ""
jsonStr := `{
"appId": "100004458",
Expand All @@ -30,3 +30,11 @@ func TestRawHandler_WriteConfigFile(t *testing.T) {
e := extension.GetFileHandler().WriteConfigFile(config, configPath)
Assert(t, e, NilVal())
}

func TestGetRawFileHandler(t *testing.T) {
handler := GetRawFileHandler()
Assert(t, handler, NotNilVal())

fileHandler := GetRawFileHandler()
Assert(t, handler, Equal(fileHandler))
}
17 changes: 17 additions & 0 deletions extension/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package extension

import "github.com/zouyx/agollo/v3/agcache"

var (
gobalCacheFactory agcache.CacheFactory
)

//GetCacheFactory 获取CacheFactory
func GetCacheFactory() agcache.CacheFactory {
return gobalCacheFactory
}

//SetCacheFactory 替换CacheFactory
func SetCacheFactory(cacheFactory agcache.CacheFactory) {
gobalCacheFactory = cacheFactory
}

0 comments on commit e4dd20b

Please sign in to comment.