From d411e1160840817307173ac810042bf514c0919e Mon Sep 17 00:00:00 2001 From: "binbin.zhang" Date: Sat, 11 Dec 2021 02:35:31 +0800 Subject: [PATCH] support set nacos ContextPath (#1656) --- remoting/nacos/builder.go | 13 ++++++-- remoting/nacos/builder_test.go | 58 ++++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/remoting/nacos/builder.go b/remoting/nacos/builder.go index 6b846c043f..204b986ee7 100644 --- a/remoting/nacos/builder.go +++ b/remoting/nacos/builder.go @@ -69,8 +69,17 @@ func GetNacosConfig(url *common.URL) ([]nacosConstant.ServerConfig, nacosConstan return []nacosConstant.ServerConfig{}, nacosConstant.ClientConfig{}, perrors.WithMessagef(err, "split [%s] ", addr) } - port, _ := strconv.Atoi(portStr) - serverConfigs = append(serverConfigs, nacosConstant.ServerConfig{IpAddr: ip, Port: uint64(port)}) + portContextPath := strings.Split(portStr, constant.PathSeparator) + port, err := strconv.Atoi(portContextPath[0]) + if err != nil { + return []nacosConstant.ServerConfig{}, nacosConstant.ClientConfig{}, + perrors.WithMessagef(err, "port [%s] ", portContextPath[0]) + } + var contextPath string + if len(portContextPath) > 1 { + contextPath = constant.PathSeparator + strings.Join(portContextPath[1:], constant.PathSeparator) + } + serverConfigs = append(serverConfigs, nacosConstant.ServerConfig{IpAddr: ip, Port: uint64(port), ContextPath: contextPath}) } timeout := url.GetParamDuration(constant.TimeoutKey, constant.DefaultRegTimeout) diff --git a/remoting/nacos/builder_test.go b/remoting/nacos/builder_test.go index a057e180ff..83a1006ed7 100644 --- a/remoting/nacos/builder_test.go +++ b/remoting/nacos/builder_test.go @@ -34,26 +34,44 @@ import ( ) func TestNewNacosClient(t *testing.T) { - rc := &config.RemoteConfig{} - rc.Protocol = "nacos" - rc.Username = "nacos" - client, err := NewNacosClient(rc) - - // address is nil - assert.Nil(t, client) - assert.NotNil(t, err) - - rc.Address = "console.nacos.io:80:123" - client, err = NewNacosClient(rc) - // invalid address - assert.Nil(t, client) - assert.NotNil(t, err) - - rc.Address = "console.nacos.io:80" - rc.Timeout = "10s" - client, err = NewNacosClient(rc) - assert.NotNil(t, client) - assert.Nil(t, err) + t.Run("AddressIsNil", func(t *testing.T) { + rc := &config.RemoteConfig{} + rc.Protocol = "nacos" + rc.Username = "nacos" + client, err := NewNacosClient(rc) + + // address is nil + assert.Nil(t, client) + assert.NotNil(t, err) + }) + + t.Run("InvalidAddress", func(t *testing.T) { + rc := &config.RemoteConfig{} + rc.Address = "console.nacos.io:80:123" + client, err := NewNacosClient(rc) + // invalid address + assert.Nil(t, client) + assert.NotNil(t, err) + }) + + t.Run("Normal", func(t *testing.T) { + rc := &config.RemoteConfig{} + rc.Address = "console.nacos.io:80" + rc.Protocol = "nacos" + rc.Timeout = "10s" + client, err := NewNacosClient(rc) + assert.NotNil(t, client) + assert.Nil(t, err) + }) + + t.Run("NormalHasContextPath", func(t *testing.T) { + rc := &config.RemoteConfig{} + rc.Address = "console.nacos.io:80/nacos" + rc.Protocol = "nacos" + client, err := NewNacosClient(rc) + assert.NotNil(t, client) + assert.Nil(t, err) + }) } func TestGetNacosConfig(t *testing.T) {