Skip to content
Snippets Groups Projects
Commit 804b73e1 authored by mark4z's avatar mark4z
Browse files

Enable Nacos registry local cache

parent b287ff2d
No related branches found
No related tags found
No related merge requests found
......@@ -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 (
......
......@@ -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
......
......@@ -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)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment