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

Commit

Permalink
refactor (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiaoliang authored Sep 17, 2019
1 parent 008a307 commit f7ebc9f
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 218 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Supported distributed configuration management service:

| name | import |description |
|----------|----------|:-------------:|
|config_center |github.com/go-chassis/go-chassis-config/configcenter |huawei cloud CSE config center https://www.huaweicloud.com/product/cse.html |
|config_center |github.com/go-chassis/go-chassis-config/configcenter |huawei cloud CSE config center https://www.huaweicloud.com/product/cse.html |
|apollo(not longer under maintenance) |github.com/go-chassis/go-chassis-config/apollo |ctrip apollo https://github.com/ctripcorp/apollo |
|servicecomb-kie |github.com/apache/servicecomb-kie |apache servicecomb-kie https://github.com/apache/servicecomb-kie |
|servicecomb-kie |github.com/o-chassis-config/servicecomb |apache servicecomb-kie https://github.com/apache/servicecomb-kie |

# Example
Get a client of config center
Expand Down
12 changes: 5 additions & 7 deletions configcenter/config_center.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ var (

//ConfigCenter is ConfigCenter Implementation of ConfigCenter
type ConfigCenter struct {
c *configcenter.Client
opts config.Options
refreshPort string
wsDialer *websocket.Dialer
c *configcenter.Client
opts config.Options
wsDialer *websocket.Dialer
}

//NewConfigCenter is a function
Expand Down Expand Up @@ -77,9 +76,8 @@ func NewConfigCenter(options config.Options) (config.Client, error) {
}

cc := &ConfigCenter{
c: c,
opts: options,
refreshPort: options.RefreshPort,
c: c,
opts: options,
}
openlogging.Info("new config center client", openlogging.WithTags(
openlogging.Tags{
Expand Down
2 changes: 1 addition & 1 deletion configcenter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package configcenter

import (
"errors"
"fmt"
"github.com/pkg/errors"
"regexp"
)

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

import (
"github.com/go-chassis/go-chassis-config/configcenter"
"gopkg.in/go-playground/assert.v1"
"github.com/stretchr/testify/assert"
"testing"
)

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/go-chassis/go-chassis-config
require (
github.com/apache/servicecomb-kie v0.1.0
github.com/go-chassis/foundation v0.0.0-20190621030543-c3b63f787f4c
github.com/go-chassis/paas-lager v1.0.1
github.com/go-mesh/openlogging v1.0.0
github.com/go-chassis/paas-lager v1.0.2-0.20190328010332-cf506050ddb2
github.com/go-mesh/openlogging v1.0.1-0.20181205082104-3d418c478b2d
github.com/gorilla/websocket v1.4.0
github.com/stretchr/testify v1.3.0
)
160 changes: 160 additions & 0 deletions servicecomb/kie_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package servicecomb

import (
"context"
"errors"

"github.com/apache/servicecomb-kie/client"
"github.com/apache/servicecomb-kie/pkg/model"
"github.com/go-chassis/go-chassis-config"
"github.com/go-mesh/openlogging"
)

// Client contains the implementation of Client
type Client struct {
KieClient *client.Client
DefaultLabels map[string]string
opts config.Options
}

const (
//Name of the Plugin
Name = "servicecomb-kie"
LabelService = "serviceName"
LabelVersion = "version"
LabelEnvironment = "environment"
LabelApp = "app"
)

// NewClient init the necessary objects needed for seamless communication to Kie Server
func NewClient(options config.Options) (config.Client, error) {
kieClient := &Client{
opts: options,
}
DefaultLabels := map[string]string{
LabelApp: options.App,
LabelEnvironment: options.Env,
LabelService: options.ServiceName,
LabelVersion: options.Version,
}
configInfo := client.Config{Endpoint: kieClient.opts.ServerURI, DefaultLabels: DefaultLabels, VerifyPeer: kieClient.opts.EnableSSL}
var err error
kieClient.KieClient, err = client.New(configInfo)
if err != nil {
openlogging.Error("KieClient Initialization Failed: " + err.Error())
}
openlogging.Debug("KieClient Initialized successfully")
return kieClient, err
}

// PullConfigs is used for pull config from servicecomb-kie
func (c *Client) PullConfigs(serviceName, version, app, env string) (map[string]interface{}, error) {
openlogging.Debug("KieClient begin PullConfigs")
labels := map[string]string{LabelService: serviceName, LabelVersion: version, LabelApp: app, LabelEnvironment: env}
labelsAppLevel := map[string]string{LabelApp: app, LabelEnvironment: env}
configsInfo := make(map[string]interface{})
configurationsValue, err := c.KieClient.SearchByLabels(context.TODO(), client.WithGetProject(serviceName), client.WithLabels(labels, labelsAppLevel))
if err != nil {
openlogging.GetLogger().Errorf("Error in Querying the Response from Kie %s %#v", err.Error(), labels)
return nil, err
}
openlogging.GetLogger().Debugf("KieClient SearchByLabels. %#v", labels)
//Parse config result.
for _, docRes := range configurationsValue {
for _, docInfo := range docRes.Data {
configsInfo[docInfo.Key] = docInfo.Value
}
}
return configsInfo, nil
}

// PullConfig get config by key and labels.
func (c *Client) PullConfig(serviceName, version, app, env, key, contentType string) (interface{}, error) {
labels := map[string]string{LabelService: serviceName, LabelVersion: version, LabelApp: app, LabelEnvironment: env}
configurationsValue, err := c.KieClient.Get(context.TODO(), key, client.WithGetProject(serviceName), client.WithLabels(labels))
if err != nil {
openlogging.GetLogger().Error("Error in Querying the Response from Kie: " + err.Error())
return nil, err
}
for _, doc := range configurationsValue {
for _, kvDoc := range doc.Data {
if key == kvDoc.Key {
openlogging.GetLogger().Debugf("The Key Value of : ", kvDoc.Value)
return doc, nil
}
}
}
return nil, errors.New("can not find value")
}

//PullConfigsByDI not implemented
func (c *Client) PullConfigsByDI(dimensionInfo string) (map[string]map[string]interface{}, error) {
// TODO Return the configurations for customized Projects in Kie Configs
return nil, errors.New("not implemented")
}

//PushConfigs put config in kie by key and labels.
func (c *Client) PushConfigs(data map[string]interface{}, serviceName, version, app, env string) (map[string]interface{}, error) {
var configReq model.KVDoc
labels := map[string]string{LabelService: serviceName, LabelVersion: version, LabelApp: app, LabelEnvironment: env}
configResult := make(map[string]interface{})
for key, configValue := range data {
configReq.Key = key
configReq.Value = configValue.(string)
configReq.Labels = labels
configurationsValue, err := c.KieClient.Put(context.TODO(), configReq, client.WithProject(serviceName))
if err != nil {
openlogging.Error("Error in PushConfigs to Kie: " + err.Error())
return nil, err
}
openlogging.Debug("The Key Value of : " + configurationsValue.Value)
configResult[configurationsValue.Key] = configurationsValue.Value
}
return configResult, nil
}

//DeleteConfigsByKeys use keyId for delete
func (c *Client) DeleteConfigsByKeys(keys []string, serviceName, version, app, env string) (map[string]interface{}, error) {
result := make(map[string]interface{})
for _, keyId := range keys {
err := c.KieClient.Delete(context.TODO(), keyId, "", client.WithProject(serviceName))
if err != nil {
openlogging.Error("Error in Delete from Kie. " + err.Error())
return nil, err
}
openlogging.GetLogger().Debugf("Delete The KeyId:%s", keyId)
}
return result, nil
}

//Watch not implemented because kie not support.
func (c *Client) Watch(f func(map[string]interface{}), errHandler func(err error)) error {
// TODO watch change events
return errors.New("not implemented")
}

//Options.
func (c *Client) Options() config.Options {
return c.opts
}

func init() {
config.InstallConfigClientPlugin(Name, NewClient)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package servicecombkie
package servicecomb

import (
"encoding/json"
Expand Down
Loading

0 comments on commit f7ebc9f

Please sign in to comment.