Skip to content

Commit

Permalink
independent configcenter source
Browse files Browse the repository at this point in the history
  • Loading branch information
zhulijian1 committed Mar 6, 2020
1 parent d95aacb commit 54e014b
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 296 deletions.
1 change: 0 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type RemoteInfo struct {
EnableSSL bool
TLSConfig *tls.Config
AutoDiscovery bool
ClientType string
APIVersion string
RefreshPort string
}
Expand Down
4 changes: 2 additions & 2 deletions remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package archaius_test

import (
"github.com/go-chassis/go-archaius"
"github.com/go-chassis/go-archaius/source/remote"
"github.com/go-chassis/go-archaius/source/remote/configcenter"
"testing"
)

func TestInstallRemoteSource(t *testing.T) {
archaius.InstallRemoteSource("config_center", remote.NewConfigCenterSource)
archaius.InstallRemoteSource("config_center", configcenter.NewConfigCenterSource)
}
73 changes: 0 additions & 73 deletions source/remote/client.go

This file was deleted.

16 changes: 0 additions & 16 deletions source/remote/client_test.go

This file was deleted.

48 changes: 48 additions & 0 deletions source/remote/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package remote

import (
"crypto/tls"
"errors"
"time"
)

//errors
var (
ErrInvalidEP = errors.New("invalid endpoint")
ErrLabelsNil = errors.New("labels can not be nil")
ErrAppEmpty = errors.New("app can not be empty")
ErrServiceTooLong = errors.New("exceeded max value for service name")
)

//const
const (
LabelService = "service"
LabelVersion = "version"
LabelEnvironment = "environment"
LabelApp = "app"

DefaultInterval = time.Second * 30
)

//Mode
const (
ModeWatch = iota
ModeInterval
)

//Options is client option
type Options struct {
ServerURI string
Endpoint string
TLSConfig *tls.Config
TenantName string
EnableSSL bool
APIVersion string
AutoDiscovery bool
RefreshPort string
WatchTimeOut int
VerifyPeer bool
ProjectID string

Labels map[string]string
}
100 changes: 0 additions & 100 deletions source/remote/config_center_source_test.go

This file was deleted.

79 changes: 3 additions & 76 deletions source/remote/configcenter/config_center.go → ...mote/configcenter/config_center_client.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
* limitations under the License.
*/

// Package configcenter created on 2017/6/20.
package configcenter

import (
"errors"
"github.com/go-chassis/go-archaius/pkg/configcenter"
"github.com/go-chassis/go-archaius/source/remote"
"strings"

"github.com/go-chassis/go-archaius/pkg/configcenter"
"github.com/go-chassis/go-archaius/source/remote"
"github.com/go-mesh/openlogging"
"github.com/gorilla/websocket"
)
Expand All @@ -32,8 +30,6 @@ const (
HeaderContentType = "Content-Type"
//HeaderUserAgent is a variable of type string
HeaderUserAgent = "User-Agent"
// Name of the Plugin
Name = "config_center"
)

//ConfigCenter is Implementation
Expand All @@ -44,7 +40,7 @@ type ConfigCenter struct {
}

//NewConfigCenter is a function
func NewConfigCenter(options remote.Options) (remote.Client, error) {
func NewConfigCenter(options remote.Options) (*ConfigCenter, error) {
if options.ServerURI == "" {
return nil, remote.ErrInvalidEP
}
Expand Down Expand Up @@ -100,79 +96,10 @@ func (c *ConfigCenter) PullConfigs(labels ...map[string]string) (map[string]inte
return configurations, nil
}

// PullConfig is the implementation of ConfigCenter to pull specific configurations from Config-Server
func (c *ConfigCenter) PullConfig(key, contentType string, labels map[string]string) (interface{}, error) {
if len(labels) == 0 {
labels = c.opts.Labels
}
d, err := GenerateDimension(c.opts.Labels[remote.LabelService], c.opts.Labels[remote.LabelVersion], c.opts.Labels[remote.LabelApp])
if err != nil {
return nil, err
}
// TODO use the contentType to return the configurations
configurations, error := c.c.Flatten(d)
if error != nil {
return nil, error
}
configurationsValue, ok := configurations[key]
if !ok {
openlogging.GetLogger().Error("Error in fetching the configurations for particular value,No Key found : " + key)
}

return configurationsValue, nil
}

// PushConfigs push configs to ConfigSource cc , success will return { "Result": "Success" }
func (c *ConfigCenter) PushConfigs(items map[string]interface{}, labels map[string]string) (map[string]interface{}, error) {
if len(items) == 0 {
em := "data is empty , which data need to send cc"
openlogging.GetLogger().Error(em)
return nil, errors.New(em)
}
if len(labels) == 0 {
labels = c.opts.Labels
}
d, err := GenerateDimension(c.opts.Labels[remote.LabelService], c.opts.Labels[remote.LabelVersion], c.opts.Labels[remote.LabelApp])
if err != nil {
return nil, err
}
configAPI := &configcenter.CreateConfigAPI{
DimensionInfo: d,
Items: items,
}

return c.c.AddConfig(configAPI)
}

// DeleteConfigsByKeys delete keys
func (c *ConfigCenter) DeleteConfigsByKeys(keys []string, labels map[string]string) (map[string]interface{}, error) {
if len(keys) == 0 {
em := "not key need to delete for cc, please check keys"
openlogging.GetLogger().Error(em)
return nil, errors.New(em)
}
if len(labels) == 0 {
labels = c.opts.Labels
}
d, err := GenerateDimension(c.opts.Labels[remote.LabelService], c.opts.Labels[remote.LabelVersion], c.opts.Labels[remote.LabelApp])
if err != nil {
return nil, err
}
configAPI := &configcenter.DeleteConfigAPI{
DimensionInfo: d,
Keys: keys,
}

return c.c.DeleteConfig(configAPI)
}

//Watch use ws
func (c *ConfigCenter) Watch(f func(map[string]interface{}), errHandler func(err error), labels map[string]string) error {
return c.c.Watch(f, errHandler)
}
func init() {
remote.InstallConfigClientPlugin(Name, NewConfigCenter)
}

//Options return options
func (c *ConfigCenter) Options() remote.Options {
Expand Down
File renamed without changes.
Loading

0 comments on commit 54e014b

Please sign in to comment.