diff --git a/common/constant/key.go b/common/constant/key.go index 50aea81371bfc8b189e80f19a5c17c5d9de7ae51..0515094f285a4bf598b04e2ea1ef376325de7ac1 100644 --- a/common/constant/key.go +++ b/common/constant/key.go @@ -173,6 +173,7 @@ const ( NACOS_NAMESPACE_ID = "namespaceId" NACOS_PASSWORD = "password" NACOS_USERNAME = "username" + NACOS_NOT_LOAD_LOCAL_CACHE = "nacos.not.load.cache" ) const ( diff --git a/registry/nacos/registry.go b/registry/nacos/registry.go index 444e98c380c4c412cd16e6dee163e2f490df1378..99fb7c8da00a75d1bb2ef4267f205606ecd3181b 100644 --- a/registry/nacos/registry.go +++ b/registry/nacos/registry.go @@ -23,22 +23,19 @@ import ( "strconv" "strings" "time" -) -import ( "github.com/nacos-group/nacos-sdk-go/clients" "github.com/nacos-group/nacos-sdk-go/clients/naming_client" + nacosConstant "github.com/nacos-group/nacos-sdk-go/common/constant" "github.com/nacos-group/nacos-sdk-go/vo" - perrors "github.com/pkg/errors" -) -import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/common/logger" "github.com/apache/dubbo-go/registry" + perrors "github.com/pkg/errors" ) var ( @@ -289,7 +286,13 @@ func getNacosConfig(url *common.URL) (map[string]interface{}, error) { clientConfig.NamespaceId = url.GetParam(constant.NACOS_NAMESPACE_ID, "") //enable local cache when nacos can not connect. - clientConfig.NotLoadCacheAtStart = false + notLoadCache, err := strconv.ParseBool(url.GetParam(constant.NACOS_NOT_LOAD_LOCAL_CACHE, "false")) + if err != nil { + logger.Errorf("ParseBool - error: %v", err) + notLoadCache = false + } + clientConfig.NotLoadCacheAtStart = notLoadCache + configMap["clientConfig"] = clientConfig return configMap, nil diff --git a/registry/nacos/registry_test.go b/registry/nacos/registry_test.go index f49b5e7a2572c5c6f9ba804474c445abefb0f16c..dd26c3ea72306494337152f5891234a2b49d8ff1 100644 --- a/registry/nacos/registry_test.go +++ b/registry/nacos/registry_test.go @@ -24,16 +24,11 @@ import ( "strconv" "testing" "time" -) - -import ( - "github.com/nacos-group/nacos-sdk-go/vo" - "github.com/stretchr/testify/assert" -) -import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" + "github.com/nacos-group/nacos-sdk-go/vo" + "github.com/stretchr/testify/assert" ) func TestNacosRegistry_Register(t *testing.T) { @@ -47,6 +42,7 @@ func TestNacosRegistry_Register(t *testing.T) { urlMap.Set(constant.INTERFACE_KEY, "com.ikurento.user.UserProvider") urlMap.Set(constant.VERSION_KEY, "1.0.0") urlMap.Set(constant.CLUSTER_KEY, "mock") + urlMap.Set(constant.NACOS_NOT_LOAD_LOCAL_CACHE, "true") testUrl, _ := common.NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider", common.WithParams(urlMap), common.WithMethods([]string{"GetUser", "AddUser"})) reg, err := newNacosRegistry(regurl) @@ -55,7 +51,6 @@ func TestNacosRegistry_Register(t *testing.T) { t.Errorf("new nacos registry error:%s \n", err.Error()) return } - time.Sleep(60 * time.Second) err = reg.Register(testUrl) assert.Nil(t, err) if err != nil { @@ -81,10 +76,10 @@ func TestNacosRegistry_Subscribe(t *testing.T) { urlMap.Set(constant.VERSION_KEY, "1.0.0") urlMap.Set(constant.CLUSTER_KEY, "mock") urlMap.Set(constant.NACOS_PATH_KEY, "") + urlMap.Set(constant.NACOS_NOT_LOAD_LOCAL_CACHE, "true") testUrl, _ := common.NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider", common.WithParams(urlMap), common.WithMethods([]string{"GetUser", "AddUser"})) reg, _ := newNacosRegistry(regurl) - time.Sleep(60 * time.Second) err := reg.Register(testUrl) assert.Nil(t, err) if err != nil { @@ -123,6 +118,7 @@ func TestNacosRegistry_Subscribe_del(t *testing.T) { urlMap.Set(constant.VERSION_KEY, "2.0.0") urlMap.Set(constant.CLUSTER_KEY, "mock") urlMap.Set(constant.NACOS_PATH_KEY, "") + urlMap.Set(constant.NACOS_NOT_LOAD_LOCAL_CACHE, "true") url1, _ := common.NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider", common.WithParams(urlMap), common.WithMethods([]string{"GetUser", "AddUser"})) url2, _ := common.NewURL("dubbo://127.0.0.2:20000/com.ikurento.user.UserProvider", common.WithParams(urlMap), common.WithMethods([]string{"GetUser", "AddUser"})) @@ -191,6 +187,8 @@ func TestNacosListener_Close(t *testing.T) { urlMap.Set(constant.VERSION_KEY, "1.0.0") urlMap.Set(constant.CLUSTER_KEY, "mock") urlMap.Set(constant.NACOS_PATH_KEY, "") + urlMap.Set(constant.NACOS_NOT_LOAD_LOCAL_CACHE, "true") + url1, _ := common.NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider2", common.WithParams(urlMap), common.WithMethods([]string{"GetUser", "AddUser"})) reg, _ := newNacosRegistry(regurl) listener, err := reg.(*nacosRegistry).subscribe(url1)