diff --git a/clients/config_client/config_client.go b/clients/config_client/config_client.go index 529b590a..4b4272ae 100644 --- a/clients/config_client/config_client.go +++ b/clients/config_client/config_client.go @@ -9,6 +9,7 @@ import ( "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/vo" @@ -381,3 +382,35 @@ func (client *ConfigClient) buildBasePath(serverConfig constant.ServerConfig) (b strconv.FormatUint(serverConfig.Port, 10) + serverConfig.ContextPath + constant.CONFIG_PATH return } + +func (client *ConfigClient) SearchConfig(param vo.SearchConfigParm) (*model.ConfigPage, error) { + return client.searchConfigInnter(param) +} + +func (client *ConfigClient) searchConfigInnter(param vo.SearchConfigParm) (*model.ConfigPage, error) { + if param.Search != "accurate" && param.Search != "blur" { + return nil, errors.New("[client.searchConfigInnter] param.search must be accurate or blur") + } + if param.PageNo <= 0 { + param.PageNo = 1 + } + if param.PageSize <= 0 { + param.PageSize = 10 + } + 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()) + if _, ok := err.(*nacos_error.NacosError); ok { + nacosErr := err.(*nacos_error.NacosError) + if nacosErr.ErrorCode() == "404" { + return nil, errors.New("config not found") + } + if nacosErr.ErrorCode() == "403" { + return nil, errors.New("get config forbidden") + } + } + return nil, err + } + return configItems, nil +} diff --git a/clients/config_client/config_client_interface.go b/clients/config_client/config_client_interface.go index a95ba666..72874f0b 100644 --- a/clients/config_client/config_client_interface.go +++ b/clients/config_client/config_client_interface.go @@ -1,6 +1,7 @@ package config_client import ( + "github.com/nacos-group/nacos-sdk-go/model" "github.com/nacos-group/nacos-sdk-go/vo" ) @@ -40,4 +41,13 @@ type IConfigClient interface { // group require // tenant ==>nacos.namespace optional ListenConfig(params vo.ConfigParam) (err error) + + // 搜索配置 + // search require search=accurate--精确搜索 search=blur--模糊搜索 + // group option + // dataId option + // tenant ==>nacos.namespace optional + // pageNo option + // pageSize option + SearchConfig(param vo.SearchConfigParm) (*model.ConfigPage, error) } diff --git a/clients/config_client/config_client_test.go b/clients/config_client/config_client_test.go index d46ee00c..5545e212 100644 --- a/clients/config_client/config_client_test.go +++ b/clients/config_client/config_client_test.go @@ -134,6 +134,20 @@ func Test_GetConfig(t *testing.T) { assert.Equal(t, "hello world!222222", content) } +func Test_SearchConfig(t *testing.T) { + client := cretateConfigClientTest() + configPage, err := client.SearchConfig(vo.SearchConfigParm{ + Search: "accurate", + DataId: "", + Group: "DEFAULT_GROUP", + PageNo: 1, + PageSize: 10, + }) + assert.Nil(t, err) + assert.Nil(t, configPage) + assert.NotEmpty(t, configPage.PageItems) +} + func Test_GetConfigWithErrorResponse_401(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() diff --git a/clients/config_client/config_proxy.go b/clients/config_client/config_proxy.go index acde591d..cd87ffe6 100644 --- a/clients/config_client/config_proxy.go +++ b/clients/config_client/config_proxy.go @@ -1,11 +1,13 @@ package config_client import ( + "encoding/json" "errors" "github.com/nacos-group/nacos-sdk-go/common/constant" "github.com/nacos-group/nacos-sdk-go/common/http_agent" "github.com/nacos-group/nacos-sdk-go/common/nacos_server" "github.com/nacos-group/nacos-sdk-go/common/util" + "github.com/nacos-group/nacos-sdk-go/model" "github.com/nacos-group/nacos-sdk-go/vo" "net/http" "strings" @@ -41,6 +43,31 @@ func (cp *ConfigProxy) GetConfigProxy(param vo.ConfigParam, tenant, accessKey, s return result, err } +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) + 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 +} func (cp *ConfigProxy) PublishConfigProxy(param vo.ConfigParam, tenant, accessKey, secretKey string) (bool, error) { params := util.TransformObject2Param(param) if len(tenant) > 0 { diff --git a/clients/naming_client/naming_client_test.go b/clients/naming_client/naming_client_test.go index 9fa21bea..2150024a 100644 --- a/clients/naming_client/naming_client_test.go +++ b/clients/naming_client/naming_client_test.go @@ -113,7 +113,7 @@ func Test_RegisterServiceInstance_withGroupeName(t *testing.T) { Ip: "10.0.0.10", Port: 80, GroupName: "test_group", - Ephemeral: true, + Ephemeral: true, }) assert.Equal(t, nil, err) assert.Equal(t, true, success) @@ -156,7 +156,7 @@ func Test_RegisterServiceInstance_withCluster(t *testing.T) { Port: 80, GroupName: "test_group", ClusterName: "test", - Ephemeral: true, + Ephemeral: true, }) assert.Equal(t, nil, err) assert.Equal(t, true, success) @@ -198,7 +198,7 @@ func Test_RegisterServiceInstance_401(t *testing.T) { Ip: "10.0.0.10", Port: 80, GroupName: "test_group", - Ephemeral: true, + Ephemeral: true, }) assert.Equal(t, false, result) assert.NotNil(t, err) @@ -233,7 +233,7 @@ func TestNamingProxy_DeristerService_WithoutGroupName(t *testing.T) { ServiceName: "DEMO", Ip: "10.0.0.10", Port: 80, - Ephemeral: true, + Ephemeral: true, }) } @@ -267,7 +267,7 @@ func TestNamingProxy_DeristerService_WithGroupName(t *testing.T) { Ip: "10.0.0.10", Port: 80, GroupName: "test_group", - Ephemeral: true, + Ephemeral: true, }) } @@ -301,7 +301,7 @@ func TestNamingProxy_DeristerService_401(t *testing.T) { Ip: "10.0.0.10", Port: 80, GroupName: "test_group", - Ephemeral: true, + Ephemeral: true, }) } diff --git a/clients/naming_client/naming_proxy.go b/clients/naming_client/naming_proxy.go index 0e262614..31800ad8 100644 --- a/clients/naming_client/naming_proxy.go +++ b/clients/naming_client/naming_proxy.go @@ -15,8 +15,8 @@ import ( ) type NamingProxy struct { - clientConfig constant.ClientConfig - nacosServer nacos_server.NacosServer + clientConfig constant.ClientConfig + nacosServer nacos_server.NacosServer } func NewNamingProxy(clientCfg constant.ClientConfig, serverCfgs []constant.ServerConfig, httpAgent http_agent.IHttpAgent) (NamingProxy, error) { @@ -166,4 +166,3 @@ func (proxy *NamingProxy) GetAllServiceInfoList(namespace string, groupName stri api := constant.SERVICE_INFO_PATH + "/getAll" return proxy.nacosServer.ReqApi(api, param, http.MethodGet) } - diff --git a/clients/naming_client/push_receiver.go b/clients/naming_client/push_receiver.go index 9ff4ada0..4e753787 100644 --- a/clients/naming_client/push_receiver.go +++ b/clients/naming_client/push_receiver.go @@ -6,7 +6,6 @@ import ( "log" "math/rand" "net" - "os" "strconv" "time" ) diff --git a/common/constant/config.go b/common/constant/config.go index a4215310..2715c79c 100644 --- a/common/constant/config.go +++ b/common/constant/config.go @@ -30,6 +30,6 @@ type ClientConfig struct { UpdateCacheWhenEmpty bool OpenKMS bool RegionId string - Username string - Password string + Username string + Password string } diff --git a/example/main/main.go b/example/main/main.go index df6ff5f7..1493a703 100644 --- a/example/main/main.go +++ b/example/main/main.go @@ -24,8 +24,8 @@ func main() { ListenInterval: 10000, NotLoadCacheAtStart: true, LogDir: "data/nacos/log", - Username: "nacos", - Password: "nacos", + //Username: "nacos", + //Password: "nacos", }, }) diff --git a/model/config.go b/model/config.go new file mode 100644 index 00000000..90cb4b94 --- /dev/null +++ b/model/config.go @@ -0,0 +1,16 @@ +package model + +type ConfigItem struct { + Id int64 `param:"id"` + Group string `param:"group"` + Content string `param:"content"` + Md5 string `param:"md5"` + Tenant string `param:"tenant"` + Appname string `param:"appname"` +} +type ConfigPage struct { + TotalCount int `param:"totalCount"` + PageNumber int `param:"pageNumber"` + PagesAvailable int `param:"pagesAvailable"` + PageItems []ConfigItem `param:"pageItems"` +} diff --git a/vo/config_param.go b/vo/config_param.go index 45abd726..96a9c56b 100644 --- a/vo/config_param.go +++ b/vo/config_param.go @@ -17,3 +17,13 @@ type ConfigParam struct { AppName string `param:"appName"` OnChange func(namespace, group, dataId, data string) } + +type SearchConfigParm struct { + Search string `param:"search"` + DataId string `param:"dataId"` + Group string `param:"group"` + Tag string `param:"tag"` + AppName string `param:"appName"` + PageNo int `param:"pageNo"` + PageSize int `param:"pageSize"` +}