1 Star 0 Fork 0

csingo / cConfig

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
NacosClient.go 1.95 KB
Copy Edit Raw Blame History
joe authored 2024-02-20 13:25 . update
package cConfig
import (
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/clients/config_client"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
"gitee.com/csingo/cLog"
)
type NacosClient struct {
Scheme string `json:"scheme"`
Host string `json:"host"`
Port uint64 `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
Namespace string `json:"namespace"`
Path string `json:"path"`
client config_client.IConfigClient
}
func (i *NacosClient) Driver() ConfigCenterDriver {
return NacosDriver
}
func (i *NacosClient) InitClient() error {
var err error
i.client, err = clients.NewConfigClient(vo.NacosClientParam{
ClientConfig: &constant.ClientConfig{
NamespaceId: i.Namespace,
UpdateThreadNum: 0,
NotLoadCacheAtStart: true,
UpdateCacheWhenEmpty: false,
Username: i.Username,
Password: i.Password,
},
ServerConfigs: []constant.ServerConfig{
{
IpAddr: i.Host,
ContextPath: i.Path,
Port: i.Port,
Scheme: i.Scheme,
},
},
})
return err
}
func (i *NacosClient) ReadConf(id, group string, explain bool) (result string, err error) {
var names map[string][2]string
var ok bool
var counter int64
var includes = make(map[string]string)
result, err = i.client.GetConfig(vo.ConfigParam{
DataId: id,
Group: group,
})
if !explain {
return
}
for {
if counter >= config_center_config.Layers {
break
}
counter++
result, names, ok = Explain(result, includes)
if ok {
break
}
// 获取引用配置
for k, v := range names {
includes[k], err = i.ReadConf(v[0], v[1], false)
if err != nil {
cLog.WithContext(nil, map[string]any{
"source": "cConfig.NacosClient.ReadConf",
"config": v,
"err": err.Error(),
}).Errorf("配置读取异常: %s", i.Driver())
continue
}
}
}
return
}
Go
1
https://gitee.com/csingo/cConfig.git
git@gitee.com:csingo/cConfig.git
csingo
cConfig
cConfig
v0.4.4

Search