序
本文主要研究一下nacos-sdk-go的ConfigProxy
ConfigProxy
nacos-sdk-go-v0.3.2/clients/config_client/config_proxy.go
type ConfigProxy struct {
nacosServer nacos_server.NacosServer
clientConfig constant.ClientConfig
}
- ConfigProxy定义了nacosServer、clientConfig属性
NewConfigProxy
nacos-sdk-go-v0.3.2/clients/config_client/config_proxy.go
func NewConfigProxy(serverConfig []constant.ServerConfig, clientConfig constant.ClientConfig, httpAgent http_agent.IHttpAgent) (ConfigProxy, error) {
proxy := ConfigProxy{}
var err error
proxy.nacosServer, err = nacos_server.NewNacosServer(serverConfig, clientConfig, httpAgent, clientConfig.TimeoutMs, clientConfig.Endpoint)
proxy.clientConfig = clientConfig
return proxy, err
}
- NewConfigProxy方法创建ConfigProxy,并设置其nacosServer、clientConfig属性
GetServerList
nacos-sdk-go-v0.3.2/clients/config_client/config_proxy.go
func (cp *ConfigProxy) GetServerList() []constant.ServerConfig {
return cp.nacosServer.GetServerList()
}
- GetServerList方法委托nacosServer.GetServerList()来获取serverList
GetConfigProxy
nacos-sdk-go-v0.3.2/clients/config_client/config_proxy.go
func (cp *ConfigProxy) GetConfigProxy(param vo.ConfigParam, tenant, accessKey, secretKey string) (string, error) {
params := util.TransformObject2Param(param)
if len(tenant) > 0 {
params["tenant"] = tenant
}
var headers = map[string]string{}
headers["accessKey"] = accessKey
headers["secretKey"] = secretKey
result, err := cp.nacosServer.ReqConfigApi(constant.CONFIG_PATH, params, headers, http.MethodGet, cp.clientConfig.TimeoutMs)
return result, err
}
- GetConfigProxy方法通过nacosServer.ReqConfigApi获取configProxy
SearchConfigProxy
nacos-sdk-go-v0.3.2/clients/config_client/config_proxy.go
func (cp *ConfigProxy) SearchConfigProxy(param vo.SearchConfigParm, tenant, accessKey, secretKey string) (*model.ConfigPage, error) {
params := util.TransformObject2Param(param)
if len(tenant) > 0 {
params["tenant"] = tenant
}
if _, ok := params["group"]; !ok {
params["group"] = ""
}
if _, ok := params["dataId"]; !ok {
params["dataId"] = ""
}
var headers = map[string]string{}
headers["accessKey"] = accessKey
headers["secretKey"] = secretKey
result, err := cp.nacosServer.ReqConfigApi(constant.CONFIG_PATH, params, headers, http.MethodGet, cp.clientConfig.TimeoutMs)
if err != nil {
return nil, err
}
var configPage model.ConfigPage
err = json.Unmarshal([]byte(result), &configPage)
if err != nil {
return nil, err
}
return &configPage, nil
}
- SearchConfigProxy方法通过cp.nacosServer.ReqConfigApi发送搜索请求,然后将结果json化为ConfigPage
PublishConfigProxy
nacos-sdk-go-v0.3.2/clients/config_client/config_proxy.go
func (cp *ConfigProxy) PublishConfigProxy(param vo.ConfigParam, tenant, accessKey, secretKey string) (bool, error) {
params := util.TransformObject2Param(param)
if len(tenant) > 0 {
params["tenant"] = tenant
}
var headers = map[string]string{}
headers["accessKey"] = accessKey
headers["secretKey"] = secretKey
result, err := cp.nacosServer.ReqConfigApi(constant.CONFIG_PATH, params, headers, http.MethodPost, cp.clientConfig.TimeoutMs)
if err != nil {
return false, errors.New("[client.PublishConfig] publish config failed:" + err.Error())
}
if strings.ToLower(strings.Trim(result, " ")) == "true" {
return true, nil
} else {
return false, errors.New("[client.PublishConfig] publish config failed:" + string(result))
}
}
- PublishConfigProxy方法通过cp.nacosServer.ReqConfigApi执行POST请求发布configProxy
DeleteConfigProxy
nacos-sdk-go-v0.3.2/clients/config_client/config_proxy.go
func (cp *ConfigProxy) DeleteConfigProxy(param vo.ConfigParam, tenant, accessKey, secretKey string) (bool, error) {
params := util.TransformObject2Param(param)
if len(tenant) > 0 {
params["tenant"] = tenant
}
var headers = map[string]string{}
headers["accessKey"] = accessKey
headers["secretKey"] = secretKey
result, err := cp.nacosServer.ReqConfigApi(constant.CONFIG_PATH, params, headers, http.MethodDelete, cp.clientConfig.TimeoutMs)
if err != nil {
return false, errors.New("[client.DeleteConfig] deleted config failed:" + err.Error())
}
if strings.ToLower(strings.Trim(result, " ")) == "true" {
return true, nil
} else {
return false, errors.New("[client.DeleteConfig] deleted config failed: " + string(result))
}
}
- DeleteConfigProxy方法通过cp.nacosServer.ReqConfigApi发送DELETE请求
ListenConfig
nacos-sdk-go-v0.3.2/clients/config_client/config_proxy.go
func (cp *ConfigProxy) ListenConfig(params map[string]string, tenant, accessKey, secretKey string) (string, error) {
headers := map[string]string{
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
"Long-Pulling-Timeout": strconv.FormatUint(cp.clientConfig.ListenInterval, 10),
}
headers["accessKey"] = accessKey
headers["secretKey"] = secretKey
log.Printf("[client.ListenConfig] request params:%+v header:%+v \n", params, headers)
result, err := cp.nacosServer.ReqConfigApi(constant.CONFIG_LISTEN_PATH, params, headers, http.MethodPost, cp.clientConfig.ListenInterval)
return result, err
}
- ListenConfig方法通过cp.nacosServer.ReqConfigApi执行POST方法发送监听请求
小结
ConfigProxy定义了nacosServer、clientConfig属性;它提供了NewConfigProxy、GetServerList、GetConfigProxy、SearchConfigProxy、PublishConfigProxy、DeleteConfigProxy、ListenConfig方法
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。