Skip to content

Commit

Permalink
Merge pull request #44 from tianxiaoliang/master
Browse files Browse the repository at this point in the history
refactor API
  • Loading branch information
tianxiaoliang authored Nov 2, 2018
2 parents 71340a4 + 5253cf9 commit 3dfd20d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 22 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
.idea
.idea
vendor
go.sum
26 changes: 16 additions & 10 deletions archaius.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"sync"

"errors"
"github.com/go-chassis/go-archaius/core"
"github.com/go-chassis/go-archaius/sources/configcenter-source"
"github.com/go-chassis/go-archaius/sources/file-source"
Expand Down Expand Up @@ -94,6 +95,12 @@ func Init(opts ...Option) error {
errG = err
return
}
if o.ConfigCenterInfo != (ConfigCenterInfo{}) {
if err := InitConfigCenter(o.ConfigCenterInfo); err != nil {
errG = err
return
}
}
err = factory.AddSource(fs)
if err != nil {
errG = err
Expand All @@ -113,20 +120,19 @@ func Init(opts ...Option) error {
}

// InitConfigCenter create a Config Center config singleton
func InitConfigCenter(opts ...Option) error {
func InitConfigCenter(ci ConfigCenterInfo) error {
var errG error
if ci == (ConfigCenterInfo{}) {
return errors.New("ConfigCenterInfo can not be empty")
}
onceConfigCenter.Do(func() {
var err error
o := &Options{}
for _, opt := range opts {
opt(o)
}

configCenterSource, err := configcentersource.InitConfigCenter(o.ConfigInfo.URL,
o.ConfigInfo.DimensionInfo, o.ConfigInfo.TenantName, o.ConfigInfo.EnableSSL,
o.ConfigInfo.TLSConfig, o.ConfigInfo.RefreshMode, o.ConfigInfo.RefreshInterval,
o.ConfigInfo.Autodiscovery, o.ConfigInfo.ClientType, o.ConfigInfo.Version, o.ConfigInfo.RefreshPort,
o.ConfigInfo.Environment)
configCenterSource, err := configcentersource.InitConfigCenter(ci.URL,
ci.DimensionInfo, ci.TenantName, ci.EnableSSL,
ci.TLSConfig, ci.RefreshMode, ci.RefreshInterval,
ci.Autodiscovery, ci.ClientType, ci.Version, ci.RefreshPort,
ci.Environment)

if err != nil {
errG = err
Expand Down
8 changes: 8 additions & 0 deletions archaius_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,11 @@ func TestConfig_RegisterListener(t *testing.T) {
defer archaius.UnRegisterListener(eventHandler, "a*")

}
func TestInitConfigCenter(t *testing.T) {
err := archaius.InitConfigCenter(archaius.ConfigCenterInfo{})
assert.Error(t, err)
err = archaius.InitConfigCenter(archaius.ConfigCenterInfo{
ClientType: "fake",
})
assert.Error(t, err)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/go-chassis/go-archaius

require (
github.com/fsnotify/fsnotify v1.4.7
github.com/go-chassis/go-cc-client v0.0.0-20180831085349-c2bb6cef1640
github.com/go-chassis/go-cc-client v0.0.0-20181102101915-dea430061a34
github.com/go-chassis/go-chassis v0.8.3
github.com/go-chassis/paas-lager v0.0.0-20180905100939-eff93e5e67db
github.com/go-mesh/openlogging v0.0.0-20180912071658-0fd4707a75ab
Expand Down
5 changes: 2 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ type Options struct {
RequiredFiles []string
OptionalFiles []string
FileHandler filesource.FileHandler
ConfigCenterInfo core.ConfigSource
ConfigInfo ConfigCenterInfo
ConfigCenterInfo ConfigCenterInfo
UseCLISource bool
UseENVSource bool
ExternalSource core.ConfigSource
Expand Down Expand Up @@ -44,7 +43,7 @@ func WithFileHandler(handler filesource.FileHandler) Option {
//WithConfigCenter accept the information for initiating a config center client and archaius config source
func WithConfigCenter(cci ConfigCenterInfo) Option {
return func(options *Options) {
options.ConfigInfo = cci
options.ConfigCenterInfo = cci
}
}

Expand Down
14 changes: 7 additions & 7 deletions sources/configcenter-source/configcentersource.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ func InitConfigCenter(ccEndpoint, dimensionInfo, tenantName string, enableSSL bo

memDiscovery.ConfigurationInit(cCenters)

if enbledAutoDiscovery(autoDiscovery) {
if enabledAutoDiscovery(autoDiscovery) {
refreshError := memDiscovery.RefreshMembers()
if refreshError != nil {
openlogging.GetLogger().Error(ConfigServerMemRefreshError + refreshError.Error())
Expand All @@ -860,17 +860,17 @@ func InitConfigCenter(ccEndpoint, dimensionInfo, tenantName string, enableSSL bo
refreshInterval, enableSSL, apiVersion, refreshPort, environment)

configcenterclient.MemberDiscoveryService = memDiscovery
installPlugin(clientType)
if err := installPlugin(clientType); err != nil {
return nil, err
}
return configCenterSource, nil
}

func installPlugin(clientType string) {

client.Enable(clientType)

func installPlugin(clientType string) error {
return client.Enable(clientType)
}

func enbledAutoDiscovery(autoDiscovery bool) bool {
func enabledAutoDiscovery(autoDiscovery bool) bool {
if autoDiscovery {
return true
}
Expand Down

0 comments on commit 3dfd20d

Please sign in to comment.