转载

聊聊nacos-sdk-go的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方法发送监听请求
原文  https://juejin.im/post/5efb577e6fb9a07eb20c1105
正文到此结束
Loading...