Skip to content

Commit

Permalink
Merge pull request #47 from lzp0412/feature-search
Browse files Browse the repository at this point in the history
Feature search
  • Loading branch information
lzp0412 authored Apr 12, 2020
2 parents 3dc1d05 + 3224565 commit 5940a2b
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 18 deletions.
35 changes: 34 additions & 1 deletion clients/config_client/config_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -104,7 +105,7 @@ func (client *ConfigClient) decrypt(dataId, content string) (string, error) {
request.CiphertextBlob = content
response, err := client.kmsClient.Decrypt(request)
if err != nil {
return "", errors.New("ksm decrypt failed")
return "", errors.New("kms decrypt failed")
}
content = response.Plaintext
}
Expand Down Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions clients/config_client/config_client_interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config_client

import (
"github.com/nacos-group/nacos-sdk-go/model"
"github.com/nacos-group/nacos-sdk-go/vo"
)

Expand Down Expand Up @@ -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)
}
14 changes: 14 additions & 0 deletions clients/config_client/config_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
27 changes: 27 additions & 0 deletions clients/config_client/config_proxy.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions clients/naming_client/host_reator.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func (hr *HostReactor) ProcessServiceJson(result string) {
return
}
}
hr.updateTimeMap.Set(cacheKey, uint64(utils.CurrentMillis()))
hr.serviceInfoMap.Set(cacheKey, *service)
if !ok || ok && !reflect.DeepEqual(service.Hosts, oldDomain.(model.Service).Hosts) {
if !ok {
log.Println("[INFO] service not found in cache " + cacheKey)
Expand All @@ -79,8 +81,6 @@ func (hr *HostReactor) ProcessServiceJson(result string) {
cache.WriteServicesToFile(*service, hr.cacheDir)
hr.subCallback.ServiceChanged(service)
}
hr.updateTimeMap.Set(cacheKey, uint64(utils.CurrentMillis()))
hr.serviceInfoMap.Set(cacheKey, *service)
}

func (hr *HostReactor) GetServiceInfo(serviceName string, clusters string) model.Service {
Expand Down
12 changes: 6 additions & 6 deletions clients/naming_client/naming_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestNamingProxy_DeristerService_WithoutGroupName(t *testing.T) {
ServiceName: "DEMO",
Ip: "10.0.0.10",
Port: 80,
Ephemeral: true,
Ephemeral: true,
})
}

Expand Down Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -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,
})
}

Expand Down
5 changes: 2 additions & 3 deletions clients/naming_client/naming_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

3 changes: 1 addition & 2 deletions clients/naming_client/push_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"math/rand"
"net"
"os"
"strconv"
"time"
)
Expand Down Expand Up @@ -64,7 +63,7 @@ func (us *PushReceiver) startServer() {

if !ok && i == 2 {
log.Panicf("failed to start udp server after trying 3 times.")
os.Exit(1)
//os.Exit(1) //It is weird dangerous to invoke the os.Exit() as a Middleware.
}
}

Expand Down
4 changes: 2 additions & 2 deletions common/constant/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ type ClientConfig struct {
UpdateCacheWhenEmpty bool
OpenKMS bool
RegionId string
Username string
Password string
Username string
Password string
}
4 changes: 2 additions & 2 deletions example/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func main() {
ListenInterval: 10000,
NotLoadCacheAtStart: true,
LogDir: "data/nacos/log",
Username: "nacos",
Password: "nacos",
//Username: "nacos",
//Password: "nacos",
},
})

Expand Down
16 changes: 16 additions & 0 deletions model/config.go
Original file line number Diff line number Diff line change
@@ -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"`
}
10 changes: 10 additions & 0 deletions vo/config_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 5940a2b

Please sign in to comment.