Skip to content
This repository has been archived by the owner on Dec 3, 2019. It is now read-only.

Commit

Permalink
refactor (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiaoliang authored Mar 27, 2019
1 parent 1963434 commit 5073987
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 42 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
### go-cc-client
[![Build Status](https://travis-ci.org/go-chassis/go-cc-client.svg?branch=master)](https://travis-ci.org/go-chassis/go-cc-client)
config center client can pull and push configs in distributed configuration
management service
### go-chassis-config
[![Build Status](https://travis-ci.org/go-chassis/go-chassis-config.svg?branch=master)](https://travis-ci.org/go-chassis/go-chassis-config)
go-chassis-config is able to pull configs from heterogeneous distributed configuration
management service.
it is decoupled with go chassis. you can use it directly without go chassis.

Supported distributed configuration management service:

| name | import |description |
|----------|----------|:-------------:|
|config_center |github.com/go-chassis/go-cc-client/configcenter |huawei cloud CSE config center https://www.huaweicloud.com/product/cse.html |
|apollo |github.com/go-chassis/go-cc-client/apollo |ctrip apollo https://github.com/ctripcorp/apollo |
|config_center |github.com/go-chassis/go-chassis-config/configcenter |huawei cloud CSE config center https://www.huaweicloud.com/product/cse.html |
|apollo |github.com/go-chassis/go-chassis-config/apollo |ctrip apollo https://github.com/ctripcorp/apollo |

# Example
Get a client of config center

1. import the config client you want to use
```go
import _ "github.com/go-chassis/go-cc-client/configcenter"
import _ "github.com/go-chassis/go-chassis-config/configcenter"
```

2. Create a client
Expand All @@ -30,7 +31,7 @@ c, err := ccclient.NewClient("config_center", ccclient.Options{
import (
"github.com/huaweicse/auth"
"github.com/go-chassis/foundation/httpclient"
_ "github.com/go-chassis/go-cc-client/configcenter"
_ "github.com/go-chassis/go-chassis-config/configcenter"
)
func main() {
Expand All @@ -44,4 +45,4 @@ func main() {
})
}
```
```
14 changes: 7 additions & 7 deletions apollo/apollo_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"strings"

"github.com/go-chassis/foundation/httpclient"
"github.com/go-chassis/go-cc-client"
"github.com/go-chassis/go-cc-client/serializers"
"github.com/go-chassis/go-chassis-config"
"github.com/go-chassis/go-chassis-config/serializers"
"github.com/go-mesh/openlogging"
)

// Client contains the implementation of ConfigClient
// Client contains the implementation of Client
type Client struct {
name string
client *httpclient.URLClient
Expand Down Expand Up @@ -50,7 +50,7 @@ func (apolloClient *Client) HTTPDo(method string, rawURL string, headers http.He
return apolloClient.client.HTTPDo(method, rawURL, headers, body)
}

// PullConfigs is the implementation of ConfigClient and pulls all the configuration for a given serviceName
// PullConfigs is the implementation of Client and pulls all the configuration for a given serviceName
func (apolloClient *Client) PullConfigs(serviceName, version, app, env string) (map[string]interface{}, error) {
/*
1. Compose the URL
Expand Down Expand Up @@ -103,7 +103,7 @@ func (apolloClient *Client) PullConfigs(serviceName, version, app, env string) (
return configValues, nil
}

// PullConfig is the implementation of the ConfigClient
// PullConfig is the implementation of the Client
func (apolloClient *Client) PullConfig(serviceName, version, app, env, key, contentType string) (interface{}, error) {
/*
1. Compose the URL
Expand Down Expand Up @@ -184,7 +184,7 @@ func (apolloClient *Client) DeleteConfigsByKeys(keys []string, dimensionInfo str
}

//InitConfigApollo initialize the Apollo Client
func InitConfigApollo(options ccclient.Options) ccclient.ConfigClient {
func InitConfigApollo(options config.Options) config.Client {
apolloClient := &Client{
serviceName: options.ApolloServiceName,
cluster: options.Cluster,
Expand All @@ -198,5 +198,5 @@ func (apolloClient *Client) Watch(f func(map[string]interface{}), errHandler fun
return nil
}
func init() {
ccclient.InstallConfigClientPlugin(Name, InitConfigApollo)
config.InstallConfigClientPlugin(Name, InitConfigApollo)
}
14 changes: 7 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ccclient
package config

import (
"errors"
Expand All @@ -7,19 +7,19 @@ import (
"github.com/go-mesh/openlogging"
)

var configClientPlugins = make(map[string]func(options Options) ConfigClient)
var configClientPlugins = make(map[string]func(options Options) Client)

//DefaultClient is config server's client
var DefaultClient ConfigClient
var DefaultClient Client

//InstallConfigClientPlugin install a config client plugin
func InstallConfigClientPlugin(name string, f func(options Options) ConfigClient) {
func InstallConfigClientPlugin(name string, f func(options Options) Client) {
configClientPlugins[name] = f
openlogging.GetLogger().Infof("Installed %s Plugin", name)
}

//ConfigClient is the interface of config server client, it has basic func to interact with config server
type ConfigClient interface {
//Client is the interface of config server client, it has basic func to interact with config server
type Client interface {
//PullConfigs pull all configs from remote
PullConfigs(serviceName, version, app, env string) (map[string]interface{}, error)
//PullConfig pull one config from remote
Expand All @@ -35,7 +35,7 @@ type ConfigClient interface {
}

//NewClient create config client implementation
func NewClient(name string, options Options) (ConfigClient, error) {
func NewClient(name string, options Options) (Client, error) {
plugins := configClientPlugins[name]
if plugins == nil {
return nil, errors.New(fmt.Sprintf("plugin [%s] not found", name))
Expand Down
9 changes: 5 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package ccclient_test
package config_test

import (
"github.com/go-chassis/go-cc-client"
_ "github.com/go-chassis/go-cc-client/configcenter"
"github.com/go-chassis/go-chassis-config"
"github.com/stretchr/testify/assert"
"testing"

_ "github.com/go-chassis/go-chassis-config/configcenter"
)

func TestEnable(t *testing.T) {
c, err := ccclient.NewClient("config_center", ccclient.Options{
c, err := config.NewClient("config_center", config.Options{
ServerURI: "http://127.0.0.1:30100",
})
assert.NoError(t, err)
Expand Down
18 changes: 9 additions & 9 deletions configcenter/configcenter_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ package configcenter

import (
"github.com/go-chassis/foundation/httpclient"
"github.com/go-chassis/go-cc-client"
"github.com/go-chassis/go-cc-client/serializers"
"github.com/go-mesh/openlogging"

"errors"
"fmt"
"github.com/go-chassis/go-chassis-config"
"github.com/go-chassis/go-chassis-config/serializers"
"github.com/gorilla/websocket"
"net/http"
"net/url"
Expand Down Expand Up @@ -71,7 +71,7 @@ const (
Name = "config_center"
)

//Client is Client Implementation of ConfigClient
//Client is Client Implementation of Client
type Client struct {
memDiscovery *MemDiscovery
refreshPort string
Expand All @@ -81,7 +81,7 @@ type Client struct {
}

//NewConfigCenter is a function
func NewConfigCenter(options ccclient.Options) ccclient.ConfigClient {
func NewConfigCenter(options config.Options) config.Client {
memDiscovery := new(MemDiscovery)
//memDiscovery.Logger = logger
memDiscovery.TLSConfig = options.TLSConfig
Expand Down Expand Up @@ -136,19 +136,19 @@ func NewConfigCenter(options ccclient.Options) ccclient.ConfigClient {
return ccclient
}

// PullConfigs is the implementation of ConfigClient to pull all the configurations from Config-Server
// PullConfigs is the implementation of Client to pull all the configurations from Config-Server
func (cclient *Client) PullConfigs(serviceName, version, app, env string) (map[string]interface{}, error) {
// serviceName is the defaultDimensionInfo passed from ConfigClient (small hack)
// serviceName is the defaultDimensionInfo passed from Client (small hack)
configurations, error := cclient.memDiscovery.pullConfigurationsFromServer(serviceName)
if error != nil {
return nil, error
}
return configurations, nil
}

// PullConfig is the implementation of ConfigClient to pull specific configurations from Config-Server
// PullConfig is the implementation of Client to pull specific configurations from Config-Server
func (cclient *Client) PullConfig(serviceName, version, app, env, key, contentType string) (interface{}, error) {
// serviceName is the defaultDimensionInfo passed from ConfigClient (small hack)
// serviceName is the defaultDimensionInfo passed from Client (small hack)
// TODO use the contentType to return the configurations
configurations, error := cclient.memDiscovery.pullConfigurationsFromServer(serviceName)
if error != nil {
Expand Down Expand Up @@ -323,5 +323,5 @@ func (cclient *Client) getWebSocketURL() (*url.URL, error) {
return hostURL, nil
}
func init() {
ccclient.InstallConfigClientPlugin(Name, NewConfigCenter)
config.InstallConfigClientPlugin(Name, NewConfigCenter)
}
3 changes: 2 additions & 1 deletion configcenter/member_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"crypto/tls"
"errors"
"fmt"

"github.com/go-chassis/foundation/httpclient"
"github.com/go-chassis/go-cc-client/serializers"
"github.com/go-chassis/go-chassis-config/serializers"
"github.com/go-mesh/openlogging"
"io/ioutil"
"math/rand"
Expand Down
2 changes: 1 addition & 1 deletion configcenter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package configcenter
import (
"fmt"

"github.com/go-chassis/go-cc-client/serializers"
"github.com/go-chassis/go-chassis-config/serializers"
"github.com/go-mesh/openlogging"
"github.com/gorilla/websocket"
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion configcenter/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package configcenter_test

import (
"encoding/json"
"github.com/go-chassis/go-cc-client/configcenter"
"github.com/go-chassis/go-chassis-config/configcenter"
"github.com/stretchr/testify/assert"
"testing"
)
Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ccclient
package config

import "crypto/tls"

Expand Down
2 changes: 1 addition & 1 deletion serializers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package serializers

import (
"errors"
"github.com/go-chassis/go-cc-client/serializers/json"
"github.com/go-chassis/go-chassis-config/serializers/json"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

// Package client created on 2017/6/22.
package ccclient
package config

// error response constants
const (
Expand Down

0 comments on commit 5073987

Please sign in to comment.