You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2022/02/12 15:29:44 UTC

[dubbo-go] branch 3.0 updated: refine client name id (#1751)

This is an automated email from the ASF dual-hosted git repository.

alexstocks pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new b087b84  refine client name id (#1751)
b087b84 is described below

commit b087b847faed58de41a0fe93d11ed7d7d9c66e72
Author: binbin.zhang <bb...@163.com>
AuthorDate: Sat Feb 12 23:29:38 2022 +0800

    refine client name id (#1751)
---
 config/config_center_config.go   |  7 +------
 config/config_utils.go           |  6 ++++++
 config/config_utils_test.go      | 20 ++++++++++++++++++++
 config/metadata_report_config.go | 11 +----------
 config/registry_config.go        |  9 ++-------
 config/remote_config.go          |  8 +-------
 6 files changed, 31 insertions(+), 30 deletions(-)

diff --git a/config/config_center_config.go b/config/config_center_config.go
index 57ec6e6..48baa80 100644
--- a/config/config_center_config.go
+++ b/config/config_center_config.go
@@ -68,11 +68,6 @@ func (CenterConfig) Prefix() string {
 	return constant.ConfigCenterPrefix
 }
 
-// NameId unique identifier id for client
-func (c *CenterConfig) NameId() string {
-	return strings.Join([]string{c.Prefix(), c.Protocol, c.Address}, "-")
-}
-
 func (c *CenterConfig) check() error {
 	if err := defaults.Set(c); err != nil {
 		return err
@@ -99,7 +94,7 @@ func (c *CenterConfig) GetUrlMap() url.Values {
 	urlMap.Set(constant.ConfigClusterKey, c.Cluster)
 	urlMap.Set(constant.ConfigAppIDKey, c.AppID)
 	urlMap.Set(constant.ConfigTimeoutKey, c.Timeout)
-	urlMap.Set(constant.ClientNameKey, c.NameId())
+	urlMap.Set(constant.ClientNameKey, clientNameID(c, c.Protocol, c.Address))
 
 	for key, val := range c.Params {
 		urlMap.Set(key, val)
diff --git a/config/config_utils.go b/config/config_utils.go
index 2ba76ab..aa6bedf 100644
--- a/config/config_utils.go
+++ b/config/config_utils.go
@@ -31,6 +31,7 @@ import (
 
 import (
 	"dubbo.apache.org/dubbo-go/v3/common/constant"
+	"dubbo.apache.org/dubbo-go/v3/config/interfaces"
 )
 
 var validate *validator.Validate
@@ -110,3 +111,8 @@ func verify(s interface{}) error {
 	}
 	return nil
 }
+
+// clientNameID unique identifier id for client
+func clientNameID(config interfaces.Config, protocol, address string) string {
+	return strings.Join([]string{config.Prefix(), protocol, address}, "-")
+}
diff --git a/config/config_utils_test.go b/config/config_utils_test.go
index 81fc3a3..65ad9d3 100644
--- a/config/config_utils_test.go
+++ b/config/config_utils_test.go
@@ -61,3 +61,23 @@ func TestRemoveMinus(t *testing.T) {
 	strList = removeMinus([]string{"c", "b", "a", "d", "c", "-c", "-a", "e", "f"})
 	assert.Equal(t, strList, "b,d,c,e,f")
 }
+
+type mockConfig struct {
+	protocol string
+	address  string
+}
+
+func (m *mockConfig) Prefix() string {
+	return "mock"
+}
+
+func TestClientNameID(t *testing.T) {
+	t.Run("normal", func(t *testing.T) {
+		m := &mockConfig{"nacos", "127.0.0.1:8848"}
+		assert.Equal(t, "mock-nacos-127.0.0.1:8848", clientNameID(m, m.protocol, m.address))
+	})
+	t.Run("protocolIsNil", func(t *testing.T) {
+		m := &mockConfig{}
+		assert.Equal(t, "mock--", clientNameID(m, m.protocol, m.address))
+	})
+}
diff --git a/config/metadata_report_config.go b/config/metadata_report_config.go
index dfa8812..9cc902d 100644
--- a/config/metadata_report_config.go
+++ b/config/metadata_report_config.go
@@ -18,10 +18,6 @@
 package config
 
 import (
-	"strings"
-)
-
-import (
 	perrors "github.com/pkg/errors"
 )
 
@@ -59,11 +55,6 @@ func (mc *MetadataReportConfig) Init(rc *RootConfig) error {
 	return mc.StartMetadataReport()
 }
 
-// NameId unique identifier id for client
-func (mc *MetadataReportConfig) NameId() string {
-	return strings.Join([]string{mc.Prefix(), mc.Protocol, mc.Address}, "-")
-}
-
 func (mc *MetadataReportConfig) ToUrl() (*common.URL, error) {
 	res, err := common.NewURL(mc.Address,
 		common.WithUsername(mc.Username),
@@ -74,7 +65,7 @@ func (mc *MetadataReportConfig) ToUrl() (*common.URL, error) {
 		common.WithParamsValue(constant.MetadataReportGroupKey, mc.Group),
 		common.WithParamsValue(constant.MetadataReportNamespaceKey, mc.Namespace),
 		common.WithParamsValue(constant.MetadataTypeKey, mc.metadataType),
-		common.WithParamsValue(constant.ClientNameKey, mc.NameId()),
+		common.WithParamsValue(constant.ClientNameKey, clientNameID(mc, mc.Protocol, mc.Address)),
 	)
 	if err != nil || len(res.Protocol) == 0 {
 		return nil, perrors.New("Invalid MetadataReport Config.")
diff --git a/config/registry_config.go b/config/registry_config.go
index 3aa7f06..aa4d5a0 100644
--- a/config/registry_config.go
+++ b/config/registry_config.go
@@ -66,11 +66,6 @@ func (RegistryConfig) Prefix() string {
 	return constant.RegistryConfigPrefix
 }
 
-// NameId unique identifier id for client
-func (c *RegistryConfig) NameId() string {
-	return strings.Join([]string{c.Prefix(), c.Protocol, c.Address}, "-")
-}
-
 func (c *RegistryConfig) Init() error {
 	if err := defaults.Set(c); err != nil {
 		return err
@@ -90,7 +85,7 @@ func (c *RegistryConfig) getUrlMap(roleType common.RoleType) url.Values {
 	urlMap.Set(constant.RegistryKey+"."+constant.RegistryZoneKey, c.Zone)
 	urlMap.Set(constant.RegistryKey+"."+constant.WeightKey, strconv.FormatInt(c.Weight, 10))
 	urlMap.Set(constant.RegistryTTLKey, c.TTL)
-	urlMap.Set(constant.ClientNameKey, c.NameId())
+	urlMap.Set(constant.ClientNameKey, clientNameID(c, c.Protocol, c.Address))
 
 	for k, v := range c.Params {
 		urlMap.Set(k, v)
@@ -118,7 +113,7 @@ func (c *RegistryConfig) toMetadataReportUrl() (*common.URL, error) {
 		common.WithUsername(c.Username),
 		common.WithPassword(c.Password),
 		common.WithParamsValue(constant.TimeoutKey, c.Timeout),
-		common.WithParamsValue(constant.ClientNameKey, c.NameId()),
+		common.WithParamsValue(constant.ClientNameKey, clientNameID(c, c.Protocol, c.Address)),
 		common.WithParamsValue(constant.MetadataReportGroupKey, c.Group),
 		common.WithParamsValue(constant.MetadataReportNamespaceKey, c.Namespace),
 	)
diff --git a/config/remote_config.go b/config/remote_config.go
index 9be342b..feea7b8 100644
--- a/config/remote_config.go
+++ b/config/remote_config.go
@@ -19,7 +19,6 @@ package config
 
 import (
 	"net/url"
-	"strings"
 	"time"
 )
 
@@ -55,11 +54,6 @@ func (rc *RemoteConfig) Init() error {
 	return nil
 }
 
-// NameId unique identifier id for client
-func (rc *RemoteConfig) NameId() string {
-	return strings.Join([]string{rc.Prefix(), rc.Protocol, rc.Address}, "-")
-}
-
 // GetTimeout return timeout duration.
 // if the configure is invalid, or missing, the default value 5s will be returned
 func (rc *RemoteConfig) GetTimeout() time.Duration {
@@ -99,7 +93,7 @@ func (rc *RemoteConfig) getUrlMap() url.Values {
 	urlMap.Set(constant.ConfigUsernameKey, rc.Username)
 	urlMap.Set(constant.ConfigPasswordKey, rc.Password)
 	urlMap.Set(constant.ConfigTimeoutKey, rc.Timeout)
-	urlMap.Set(constant.ClientNameKey, rc.NameId())
+	urlMap.Set(constant.ClientNameKey, clientNameID(rc, rc.Protocol, rc.Protocol))
 
 	for key, val := range rc.Params {
 		urlMap.Set(key, val)