聊聊nacossdkgo的ConfigProxy

32次阅读

共计 4694 个字符,预计需要花费 12 分钟才能阅读完成。

本文主要研究一下 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 方法

doc

  • config_proxy

正文完
 0