Skip to content

Commit

Permalink
1、change logger implementation
Browse files Browse the repository at this point in the history
2、give some example
3、fix some bug
  • Loading branch information
lzp0412 committed Jul 30, 2020
1 parent ff9933a commit 226e7ed
Show file tree
Hide file tree
Showing 42 changed files with 1,005 additions and 529 deletions.
24 changes: 12 additions & 12 deletions clients/cache/disk_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,49 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"

"github.com/go-errors/errors"
"github.com/nacos-group/nacos-sdk-go/common/util"
"github.com/nacos-group/nacos-sdk-go/common/file"
"github.com/nacos-group/nacos-sdk-go/common/logger"
"github.com/nacos-group/nacos-sdk-go/model"
"github.com/nacos-group/nacos-sdk-go/utils"
"github.com/nacos-group/nacos-sdk-go/util"
)

func GetFileName(cacheKey string, cacheDir string) string {
return cacheDir + string(os.PathSeparator) + cacheKey
}

func WriteServicesToFile(service model.Service, cacheDir string) {
util.MkdirIfNecessary(cacheDir)
file.MkdirIfNecessary(cacheDir)
sb, _ := json.Marshal(service)
domFileName := GetFileName(utils.GetServiceCacheKey(service.Name, service.Clusters), cacheDir)
domFileName := GetFileName(util.GetServiceCacheKey(service.Name, service.Clusters), cacheDir)

err := ioutil.WriteFile(domFileName, sb, 0666)
if err != nil {
log.Printf("[ERROR]:faild to write name cache:%s ,value:%s ,err:%s \n", domFileName, string(sb), err.Error())
logger.Errorf("failed to write name cache:%s ,value:%s ,err:%s", domFileName, string(sb), err.Error())
}

}

func ReadServicesFromFile(cacheDir string) map[string]model.Service {
files, err := ioutil.ReadDir(cacheDir)
if err != nil {
log.Printf("[ERROR]:read cacheDir:%s failed!err:%s \n", cacheDir, err.Error())
logger.Errorf("read cacheDir:%s failed!err:%s", cacheDir, err.Error())
return nil
}
serviceMap := map[string]model.Service{}
for _, f := range files {
fileName := GetFileName(f.Name(), cacheDir)
b, err := ioutil.ReadFile(fileName)
if err != nil {
log.Printf("[ERROR]:failed to read name cache file:%s,err:%s! ", fileName, err.Error())
logger.Errorf("failed to read name cache file:%s,err:%s! ", fileName, err.Error())
continue
}

s := string(b)
service := utils.JsonToService(s)
service := util.JsonToService(s)

if service == nil {
continue
Expand All @@ -55,16 +55,16 @@ func ReadServicesFromFile(cacheDir string) map[string]model.Service {
serviceMap[f.Name()] = *service
}

log.Printf("finish loading name cache, total: " + strconv.Itoa(len(files)))
logger.Info("finish loading name cache, total: " + strconv.Itoa(len(files)))
return serviceMap
}

func WriteConfigToFile(cacheKey string, cacheDir string, content string) {
util.MkdirIfNecessary(cacheDir)
file.MkdirIfNecessary(cacheDir)
fileName := GetFileName(cacheKey, cacheDir)
err := ioutil.WriteFile(fileName, []byte(content), 0666)
if err != nil {
log.Printf("[ERROR]:faild to write config cache:%s ,value:%s ,err:%s \n", fileName, string(content), err.Error())
logger.Errorf("failed to write config cache:%s ,value:%s ,err:%s", fileName, string(content), err.Error())
}
}

Expand Down
28 changes: 11 additions & 17 deletions clients/client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,30 @@ import (
"github.com/nacos-group/nacos-sdk-go/common/http_agent"
)

// 创建配置相关的客户端
func CreateConfigClient(properties map[string]interface{}) (iClient config_client.IConfigClient,
err error) {
nacosClient, errSetConfig := setConfig(properties)
if errSetConfig != nil {
err = errSetConfig
// CreateConfigClient use to create config client
func CreateConfigClient(properties map[string]interface{}) (iClient config_client.IConfigClient, err error) {
nacosClient, err := setConfig(properties)
if err != nil {
return
}
nacosClient.SetHttpAgent(&http_agent.HttpAgent{})
config, errNew := config_client.NewConfigClient(nacosClient)
if errNew != nil {
err = errNew
config, err := config_client.NewConfigClient(nacosClient)
if err != nil {
return
}
iClient = &config
return
}

// 创建服务发现相关的客户端
//CreateNamingClient use to create a nacos naming client
func CreateNamingClient(properties map[string]interface{}) (iClient naming_client.INamingClient, err error) {
nacosClient, errSetConfig := setConfig(properties)
if errSetConfig != nil {
err = errSetConfig
nacosClient, err := setConfig(properties)
if err != nil {
return
}
nacosClient.SetHttpAgent(&http_agent.HttpAgent{})
naming, errNew := naming_client.NewNamingClient(nacosClient)
if errNew != nil {
err = errNew
naming, err := naming_client.NewNamingClient(nacosClient)
if err != nil {
return
}
iClient = &naming
Expand All @@ -61,7 +56,6 @@ func setConfig(properties map[string]interface{}) (iClient nacos_client.INacosCl
BeatInterval: 5 * 1000,
})
}
// 设置 serverConfig
if serverConfigTmp, exist := properties[constant.KEY_SERVER_CONFIGS]; exist {
if serverConfigs, ok := serverConfigTmp.([]constant.ServerConfig); ok {
err = client.SetServerConfig(serverConfigs)
Expand Down
94 changes: 47 additions & 47 deletions clients/config_client/config_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config_client

import (
"errors"
"log"
"math"
"os"
"strconv"
Expand All @@ -17,9 +16,8 @@ import (
"github.com/nacos-group/nacos-sdk-go/common/http_agent"
"github.com/nacos-group/nacos-sdk-go/common/logger"
"github.com/nacos-group/nacos-sdk-go/common/nacos_error"
"github.com/nacos-group/nacos-sdk-go/common/util"
"github.com/nacos-group/nacos-sdk-go/model"
"github.com/nacos-group/nacos-sdk-go/utils"
"github.com/nacos-group/nacos-sdk-go/util"
"github.com/nacos-group/nacos-sdk-go/vo"
)

Expand Down Expand Up @@ -73,7 +71,12 @@ func NewConfigClient(nc nacos_client.INacosClient) (ConfigClient, error) {
if err != nil {
return config, err
}
err = logger.InitLog(clientConfig.LogDir)
err = logger.InitLogger(logger.Config{
Level: clientConfig.LogLevel,
OutputPath: clientConfig.LogDir,
RotationTime: clientConfig.RotateTime,
MaxAge: clientConfig.MaxAge,
})
if err != nil {
return config, err
}
Expand All @@ -97,19 +100,18 @@ func (client *ConfigClient) sync() (clientConfig constant.ClientConfig,
serverConfigs []constant.ServerConfig, agent http_agent.IHttpAgent, err error) {
clientConfig, err = client.GetClientConfig()
if err != nil {
log.Println(err, ";do you call client.SetClientConfig()?")
logger.Errorf("getClientConfig catch error:%+v", err)
return
}
if err == nil {
serverConfigs, err = client.GetServerConfig()
if err != nil {
log.Println(err, ";do you call client.SetServerConfig()?")
}
serverConfigs, err = client.GetServerConfig()
if err != nil {
logger.Errorf("getServerConfig catch error:%+v", err)
return
}
if err == nil {
agent, err = client.GetHttpAgent()
if err != nil {
log.Println(err, ";do you call client.SetHttpAgent()?")
}

agent, err = client.GetHttpAgent()
if err != nil {
logger.Errorf("getHttpAgent catch error:%+v", err)
}
return
}
Expand Down Expand Up @@ -151,11 +153,11 @@ func (client *ConfigClient) getConfigInner(param vo.ConfigParam) (content string
return "", err
}
clientConfig, _ := client.GetClientConfig()
cacheKey := utils.GetConfigCacheKey(param.DataId, param.Group, clientConfig.NamespaceId)
cacheKey := util.GetConfigCacheKey(param.DataId, param.Group, clientConfig.NamespaceId)
content, err = client.configProxy.GetConfigProxy(param, clientConfig.NamespaceId, clientConfig.AccessKey, clientConfig.SecretKey)

if err != nil {
log.Printf("[ERROR] get config from server error:%s ", err.Error())
logger.Infof("get config from server error:%s ", err.Error())
if _, ok := err.(*nacos_error.NacosError); ok {
nacosErr := err.(*nacos_error.NacosError)
if nacosErr.ErrorCode() == "404" {
Expand All @@ -168,7 +170,7 @@ func (client *ConfigClient) getConfigInner(param vo.ConfigParam) (content string
}
content, err = cache.ReadConfigFromFile(cacheKey, client.configCacheDir)
if err != nil {
log.Printf("[ERROR] get config from cache error:%s ", err.Error())
logger.Errorf("get config from cache error:%s ", err.Error())
return "", errors.New("read config from both server and cache fail")
}

Expand All @@ -193,8 +195,7 @@ func (client *ConfigClient) PublishConfig(param vo.ConfigParam) (published bool,
return client.configProxy.PublishConfigProxy(param, clientConfig.NamespaceId, clientConfig.AccessKey, clientConfig.SecretKey)
}

func (client *ConfigClient) DeleteConfig(param vo.ConfigParam) (deleted bool,
err error) {
func (client *ConfigClient) DeleteConfig(param vo.ConfigParam) (deleted bool, err error) {
if len(param.DataId) <= 0 {
err = errors.New("[client.DeleteConfig] param.dataId can not be empty")
}
Expand All @@ -210,16 +211,15 @@ func (client *ConfigClient) DeleteConfig(param vo.ConfigParam) (deleted bool,
func (client *ConfigClient) CancelListenConfig(param vo.ConfigParam) (err error) {
clientConfig, err := client.GetClientConfig()
if err != nil {
log.Fatalf("[checkConfigInfo.GetClientConfig] failed.")
logger.Errorf("[checkConfigInfo.GetClientConfig] failed,err:%+v", err.Error())
return
}
cacheMap.Remove(utils.GetConfigCacheKey(param.DataId, param.Group, clientConfig.NamespaceId))
log.Printf("Cancel listen config DataId:%s Group:%s", param.DataId, param.Group)
cacheMap.Remove(util.GetConfigCacheKey(param.DataId, param.Group, clientConfig.NamespaceId))
logger.Infof("Cancel listen config DataId:%s Group:%s", param.DataId, param.Group)
remakeId := int(math.Ceil(float64(len(cacheMap.Keys())) / float64(perTaskConfigSize)))
if remakeId < currentTaskCount {
remakeCacheDataTaskId(remakeId)
}

return err
}

Expand Down Expand Up @@ -252,19 +252,19 @@ func (client *ConfigClient) ListenConfig(param vo.ConfigParam) (err error) {
}
clientConfig, err := client.GetClientConfig()
if err != nil {
err = errors.New("[checkConfigInfo.GetClientConfig] failed.")
err = errors.New("[checkConfigInfo.GetClientConfig] failed")
return err
}

key := utils.GetConfigCacheKey(param.DataId, param.Group, clientConfig.NamespaceId)
key := util.GetConfigCacheKey(param.DataId, param.Group, clientConfig.NamespaceId)
var cData cacheData
if v, ok := cacheMap.Get(key); ok {
cData = v.(cacheData)
cData.isInitializing = true
} else {
content, err := cache.ReadConfigFromFile(key, client.configCacheDir)
if err != nil {
log.Printf("[cache.ReadConfigFromFile] error: %+v", err)
logger.Errorf("[cache.ReadConfigFromFile] error: %+v", err)
content = ""
}
md5Str := util.Md5(content)
Expand All @@ -275,7 +275,6 @@ func (client *ConfigClient) ListenConfig(param vo.ConfigParam) (err error) {

cData = cacheData{
isInitializing: true,
appName: param.AppName,
dataId: param.DataId,
group: param.Group,
tenant: clientConfig.NamespaceId,
Expand Down Expand Up @@ -356,7 +355,7 @@ func longPulling(taskId int) func() {
if len(listeningConfigs) > 0 {
clientConfig, err := client.GetClientConfig()
if err != nil {
log.Printf("[checkConfigInfo.GetClientConfig] err: %+v", err)
logger.Errorf("[checkConfigInfo.GetClientConfig] err: %+v", err)
return
}
// http get
Expand All @@ -371,17 +370,17 @@ func longPulling(taskId int) func() {
if _, ok := err.(*nacos_error.NacosError); ok {
changed = changedTmp
} else {
log.Printf("[client.ListenConfig] listen config error: %+v", err)
logger.Errorf("[client.ListenConfig] listen config error: %+v", err)
}
}
for _, v := range initializationList {
v.isInitializing = false
cacheMap.Set(utils.GetConfigCacheKey(v.dataId, v.group, clientConfig.NamespaceId), v)
cacheMap.Set(util.GetConfigCacheKey(v.dataId, v.group, clientConfig.NamespaceId), v)
}
if strings.ToLower(strings.Trim(changed, " ")) == "" {
log.Println("[client.ListenConfig] no change")
if len(strings.ToLower(strings.Trim(changed, " "))) == 0 {
logger.Info("[client.ListenConfig] no change")
} else {
log.Print("[client.ListenConfig] config changed:" + changed)
logger.Info("[client.ListenConfig] config changed:" + changed)
client.callListener(changed, clientConfig.NamespaceId)
}
}
Expand All @@ -396,21 +395,22 @@ func (client *ConfigClient) callListener(changed, tenant string) {
for _, config := range changedConfigs {
attrs := strings.Split(config, "%02")
if len(attrs) >= 2 {
if value, ok := cacheMap.Get(utils.GetConfigCacheKey(attrs[0], attrs[1], tenant)); ok {
if value, ok := cacheMap.Get(util.GetConfigCacheKey(attrs[0], attrs[1], tenant)); ok {
cData := value.(cacheData)
if content, err := client.getConfigInner(vo.ConfigParam{
content, err := client.getConfigInner(vo.ConfigParam{
DataId: cData.dataId,
Group: cData.group,
}); err == nil {
cData.content = content
cData.md5 = util.Md5(content)
if cData.md5 != cData.cacheDataListener.lastMd5 {
go cData.cacheDataListener.listener("", attrs[1], attrs[0], cData.content)
cData.cacheDataListener.lastMd5 = cData.md5
cacheMap.Set(utils.GetConfigCacheKey(cData.dataId, cData.group, tenant), cData)
}
} else {
log.Printf("[client.getConfigInner] DataId:[%s] Group:[%s] Error:[%+v]", cData.dataId, cData.group, err)
})
if err != nil {
logger.Errorf("[client.getConfigInner] DataId:[%s] Group:[%s] Error:[%+v]", cData.dataId, cData.group, err)
continue
}
cData.content = content
cData.md5 = util.Md5(content)
if cData.md5 != cData.cacheDataListener.lastMd5 {
go cData.cacheDataListener.listener("", attrs[1], attrs[0], cData.content)
cData.cacheDataListener.lastMd5 = cData.md5
cacheMap.Set(util.GetConfigCacheKey(cData.dataId, cData.group, tenant), cData)
}
}
}
Expand Down Expand Up @@ -440,7 +440,7 @@ func (client *ConfigClient) searchConfigInnter(param vo.SearchConfigParm) (*mode
clientConfig, _ := client.GetClientConfig()
configItems, err := client.configProxy.SearchConfigProxy(param, clientConfig.NamespaceId, clientConfig.AccessKey, clientConfig.SecretKey)
if err != nil {
log.Printf("[ERROR] search config from server error:%s ", err.Error())
logger.Errorf("search config from server error:%s ", err.Error())
if _, ok := err.(*nacos_error.NacosError); ok {
nacosErr := err.(*nacos_error.NacosError)
if nacosErr.ErrorCode() == "404" {
Expand Down
Loading

0 comments on commit 226e7ed

Please sign in to comment.