Skip to content

Commit

Permalink
decouple with huawei cloud config center (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiaoliang authored Jul 2, 2019
1 parent 4d2017a commit d61dbc9
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ give config client to init config center source
//input your config center source config
}
//create config client
cc:=ccclient.NewClient("config_center",ccclient.Options{
cc,_:=ccclient.NewClient("config_center",ccclient.Options{
ServerURI:"the address of config server endpoint",
})
//manage local and remote key value at same time
Expand Down
10 changes: 6 additions & 4 deletions archaius.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,24 @@ func EnableConfigCenterSource(ci ConfigCenterInfo, cc config.Client) error {
var err error
if cc == nil {
opts := config.Options{
DimensionInfo: ci.DefaultDimensionInfo,
ServerURI: ci.URL,
TenantName: ci.TenantName,
EnableSSL: ci.EnableSSL,
TLSConfig: ci.TLSConfig,
RefreshPort: ci.RefreshPort,
AutoDiscovery: ci.AutoDiscovery,
Env: ci.Environment,

Version: ci.Version,
ServiceName: ci.Service,
App: ci.App,
Env: ci.Environment,
}
cc, err = config.NewClient(ci.ClientType, opts)
if err != nil {
return err
}
}
configCenterSource := configcenter.NewConfigCenterSource(cc,
ci.DefaultDimensionInfo, ci.RefreshMode,
configCenterSource := configcenter.NewConfigCenterSource(cc, ci.RefreshMode,
ci.RefreshInterval)
err = factory.AddSource(configCenterSource)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module github.com/go-chassis/go-archaius
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.7
github.com/go-chassis/foundation v0.0.0-20190203091418-304855ea28bf // indirect
github.com/go-chassis/go-chassis-config v0.6.0
github.com/go-mesh/openlogging v0.0.0-20181205082104-3d418c478b2d
github.com/go-chassis/go-chassis-config v0.7.1-0.20190701112316-2f79c0f7e065
github.com/go-mesh/openlogging v1.0.0
github.com/gorilla/websocket v1.4.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/spf13/cast v1.2.0
github.com/stretchr/testify v1.2.2
github.com/stretchr/testify v1.3.0
golang.org/x/sys v0.0.0-20180824143301-4910a1d54f87 // indirect
gopkg.in/go-playground/assert.v1 v1.2.1
gopkg.in/yaml.v2 v2.2.1
)

Expand Down
11 changes: 6 additions & 5 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ type ConfigCenterInfo struct {
//required.
//Key value can be in different namespace, we call it dimension.
//although key is same but in different dimension, the value is different.
//you must specify the default dimension, so that the config center source will just pull this dimension's key value
DefaultDimensionInfo string

//you must specify the service,app and version, so that the config center source will just pull this unique key value
Service string
App string
Version string
//archaius config center source support 2 types of refresh mechanism:
//0: Web-Socket Based - client makes an web socket connection with
//the config server and keeps getting an events whenever any data changes.
Expand All @@ -26,14 +27,14 @@ type ConfigCenterInfo struct {
RefreshInterval int

//Configurations for config client implementation
//if you alread create a client, don't need to set those config
//if you already create a client, don't need to set those config
URL string
TenantName string
EnableSSL bool
TLSConfig *tls.Config
AutoDiscovery bool
ClientType string
Version string
APIVersion string
RefreshPort string
Environment string
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,9 @@ import (
)

const (
defaultTimeout = 10 * time.Second
//ConfigCenterSourceConst variable of type string
ConfigCenterSourceConst = "ConfigCenterSource"
configCenterSourcePriority = 0
dimensionsInfo = "dimensionsInfo"
dynamicConfigAPI = `/configuration/refresh/items`
getConfigAPI = `/configuration/items`
defaultContentType = "application/json"
//ConfigServerMemRefreshError is error message
ConfigServerMemRefreshError = "error in poulating config server member"
)

var (
Expand All @@ -50,9 +43,12 @@ var (

//Handler handles configs from config center
type Handler struct {
Service string
Version string
App string
Env string
cc config.Client
dynamicConfigHandler *DynamicConfigHandler
dimensionsInfo string
dimensionInfoMap map[string]string
Configurations map[string]interface{}
dimensionsInfoConfiguration map[string]map[string]interface{}
Expand All @@ -69,14 +65,13 @@ type Handler struct {
var ConfigCenterConfig *Handler

//NewConfigCenterSource initializes all components of configuration center
func NewConfigCenterSource(cc config.Client, dimInfo string,
func NewConfigCenterSource(cc config.Client,
refreshMode, refreshInterval int) core.ConfigSource {

if ConfigCenterConfig == nil {
ConfigCenterConfig = new(Handler)
ConfigCenterConfig.priority = configCenterSourcePriority
ConfigCenterConfig.cc = cc
ConfigCenterConfig.dimensionsInfo = dimInfo
ConfigCenterConfig.initSuccess = true
ConfigCenterConfig.RefreshMode = refreshMode
ConfigCenterConfig.RefreshInterval = time.Second * time.Duration(refreshInterval)
Expand Down Expand Up @@ -157,21 +152,16 @@ func (cfgSrcHandler *Handler) refreshConfigurations(dimensionInfo string) error
)

if dimensionInfo == "" {
config, err = cfgSrcHandler.cc.PullConfigs(cfgSrcHandler.dimensionsInfo, "", "", "")
config, err = cfgSrcHandler.cc.PullConfigs(cfgSrcHandler.Service,
cfgSrcHandler.Version, cfgSrcHandler.App, cfgSrcHandler.Env)
if err != nil {
openlogging.GetLogger().Warnf("Failed to pull configurations from config center server", err) //Warn
return err
}
//Populate the events based on the changed value between current config and newly received Config
events, err = cfgSrcHandler.populateEvents(config)
} else {
var diInfo string
for _, value := range cfgSrcHandler.dimensionInfoMap {
if value == dimensionInfo {
diInfo = dimensionInfo
}
}
configByDI, err = cfgSrcHandler.cc.PullConfigsByDI(dimensionInfo, diInfo)
configByDI, err = cfgSrcHandler.cc.PullConfigsByDI(dimensionInfo)
if err != nil {
openlogging.GetLogger().Warnf("Failed to pull configurations from config center server", err) //Warn
return err
Expand All @@ -194,13 +184,13 @@ func (cfgSrcHandler *Handler) refreshConfigurations(dimensionInfo string) error
}

cfgSrcHandler.Lock()
cfgSrcHandler.updatedimensionsInfoConfigurations(dimensionInfo, configByDI, config)
cfgSrcHandler.updateDimensionsInfoConfigurations(dimensionInfo, configByDI, config)
cfgSrcHandler.Unlock()

return nil
}

func (cfgSrcHandler *Handler) updatedimensionsInfoConfigurations(dimensionInfo string,
func (cfgSrcHandler *Handler) updateDimensionsInfoConfigurations(dimensionInfo string,
configByDI map[string]map[string]interface{}, config map[string]interface{}) {

if dimensionInfo == "" {
Expand Down Expand Up @@ -323,7 +313,7 @@ func (cfgSrcHandler *Handler) DynamicConfigHandler(callback core.DynamicConfigCa
if cfgSrcHandler.RefreshMode == 0 {
// Pull All the configuration for the first time.
cfgSrcHandler.refreshConfigurations("")
//Start a web socket connection to recieve change events.
//Start a web socket connection to receive change events.
dynCfgHandler.startDynamicConfigHandler()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"encoding/json"
"errors"
"github.com/go-chassis/go-chassis-config"
"math/rand"
"testing"
"time"
)
Expand All @@ -34,19 +33,6 @@ func (ccenter *TestDynamicConfigHandler) OnEvent(event *core.Event) {
ccenter.EventValue = event.Value
}

func (*Testingsource) GetDimensionInfo() string {
rand.Seed(time.Now().UTC().UnixNano())
const chars = "abcdefghijklmnopqrstuvwxyz"
result := make([]byte, 5)

for i := 0; i < 5; i++ {
result[i] = chars[rand.Intn(len(chars))]
}

dimensioninfo := string(result)
return dimensioninfo
}

func (*Testingsource) GetConfigServer() []string {
configserver := []string{`http://10.18.206.218:30103`}

Expand All @@ -58,16 +44,16 @@ func (*Testingsource) GetInvalidConfigServer() []string {
}

func TestGetConfigurationsForInvalidCCIP(t *testing.T) {
testSource := &Testingsource{}

t.Log("Test configcenter.go")
opts := config.Options{
DimensionInfo: testSource.GetDimensionInfo(),
TenantName: "default",
ServiceName: "cart",
App: "default",
TenantName: "default",
}
cc, err := config.NewClient("config_center", opts)
assert.NoError(t, err)
ccs := configcenter.NewConfigCenterSource(cc, testSource.GetDimensionInfo(), 1,
ccs := configcenter.NewConfigCenterSource(cc, 1,
1)

_, er := ccs.GetConfigurations()
Expand All @@ -80,14 +66,14 @@ func TestGetConfigurationsForInvalidCCIP(t *testing.T) {
}

func TestGetConfigurationsWithCCIP(t *testing.T) {
testSource := &Testingsource{}
opts := config.Options{
DimensionInfo: testSource.GetDimensionInfo(),
TenantName: "default",
ServiceName: "cart",
App: "default",
TenantName: "default",
}
cc, err := config.NewClient("config_center", opts)
assert.NoError(t, err)
ccs := configcenter.NewConfigCenterSource(cc, testSource.GetDimensionInfo(), 1, 1)
ccs := configcenter.NewConfigCenterSource(cc, 1, 1)

t.Log("Accessing concenter source configurations")
time.Sleep(2 * time.Second)
Expand Down Expand Up @@ -136,14 +122,14 @@ func TestGetConfigurationsWithCCIP(t *testing.T) {
}

func Test_DynamicConfigHandler(t *testing.T) {
testsource := &Testingsource{}
opts := config.Options{
DimensionInfo: testsource.GetDimensionInfo(),
TenantName: "default",
ServiceName: "cart",
App: "default",
TenantName: "default",
}
cc, err := config.NewClient("config_center", opts)
assert.NoError(t, err)
ccs := configcenter.NewConfigCenterSource(cc, testsource.GetDimensionInfo(), 1, 1)
ccs := configcenter.NewConfigCenterSource(cc, 1, 1)

dynamicconfig := new(TestDynamicConfigHandler)

Expand All @@ -160,14 +146,14 @@ func Test_DynamicConfigHandler(t *testing.T) {
}

func Test_OnReceive(t *testing.T) {
testSource := &Testingsource{}
opts := config.Options{
DimensionInfo: testSource.GetDimensionInfo(),
TenantName: "default",
ServiceName: "cart",
App: "default",
TenantName: "default",
}
cc, err := config.NewClient("config_center", opts)
assert.NoError(t, err)
ccs := configcenter.NewConfigCenterSource(cc, testSource.GetDimensionInfo(), 1, 1)
ccs := configcenter.NewConfigCenterSource(cc, 1, 1)

_, er := ccs.GetConfigurations()
if er != nil {
Expand Down
1 change: 0 additions & 1 deletion sources/configcenter/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func newDynConfigHandlerSource(cfgSrc *Handler, callback core.DynamicConfigCallb
eventHandler := newConfigCenterEventHandler(cfgSrc, callback)
dynCfgHandler := new(DynamicConfigHandler)
dynCfgHandler.EventHandler = eventHandler
dynCfgHandler.dimensionsInfo = cfgSrc.dimensionsInfo
dynCfgHandler.cc = cfgSrc.cc
return dynCfgHandler, nil
}
Expand Down

0 comments on commit d61dbc9

Please sign in to comment.