From f94c3e7213374347332a465f41e3b9d137c616fe Mon Sep 17 00:00:00 2001
From: Joe Zou <yixian.zou@gmail.com>
Date: Sun, 1 Nov 2020 00:27:02 +0800
Subject: [PATCH] Merge pull request #784 from sanxun0325/metadata_default_port

Fix:  issue 774 [fix metadata default port]
---
 .../service/exporter/configurable/exporter.go | 19 +++++++--------
 .../exporter/configurable/exporter_test.go    | 24 ++++++++++++++-----
 metadata/service/exporter/exporter.go         |  2 +-
 .../service_discovery_registry.go             |  6 ++---
 4 files changed, 31 insertions(+), 20 deletions(-)

diff --git a/metadata/service/exporter/configurable/exporter.go b/metadata/service/exporter/configurable/exporter.go
index 5a930c5e9..75e52d8d1 100644
--- a/metadata/service/exporter/configurable/exporter.go
+++ b/metadata/service/exporter/configurable/exporter.go
@@ -19,6 +19,7 @@ package configurable
 
 import (
 	"context"
+	"errors"
 	"sync"
 )
 
@@ -46,12 +47,18 @@ func NewMetadataServiceExporter(metadataService service.MetadataService) exporte
 }
 
 // Export will export the metadataService
-func (exporter *MetadataServiceExporter) Export() error {
+func (exporter *MetadataServiceExporter) Export(url *common.URL) error {
 	if !exporter.IsExported() {
 		serviceConfig := config.NewServiceConfig(constant.SIMPLE_METADATA_SERVICE_NAME, context.Background())
 		serviceConfig.Protocol = constant.DEFAULT_PROTOCOL
+		if url == nil || url.SubURL == nil {
+			return errors.New("metadata server url is nil, pls check your configuration")
+		}
 		serviceConfig.Protocols = map[string]*config.ProtocolConfig{
-			constant.DEFAULT_PROTOCOL: generateMetadataProtocol(),
+			constant.DEFAULT_PROTOCOL: {
+				Name: url.SubURL.Protocol,
+				Port: url.SubURL.Port,
+			},
 		}
 		serviceConfig.InterfaceName = constant.METADATA_SERVICE_NAME
 		// identify this is a golang server
@@ -95,11 +102,3 @@ func (exporter *MetadataServiceExporter) IsExported() bool {
 	defer exporter.lock.RUnlock()
 	return exporter.ServiceConfig != nil && exporter.ServiceConfig.IsExport()
 }
-
-// generateMetadataProtocol will return a default ProtocolConfig
-func generateMetadataProtocol() *config.ProtocolConfig {
-	return &config.ProtocolConfig{
-		Name: constant.DEFAULT_PROTOCOL,
-		Port: "20000",
-	}
-}
diff --git a/metadata/service/exporter/configurable/exporter_test.go b/metadata/service/exporter/configurable/exporter_test.go
index b304b9153..ceda2550e 100644
--- a/metadata/service/exporter/configurable/exporter_test.go
+++ b/metadata/service/exporter/configurable/exporter_test.go
@@ -26,6 +26,7 @@ import (
 )
 
 import (
+	"github.com/apache/dubbo-go/common"
 	_ "github.com/apache/dubbo-go/common/proxy/proxy_factory"
 	"github.com/apache/dubbo-go/config"
 	_ "github.com/apache/dubbo-go/filter/filter_impl"
@@ -55,12 +56,23 @@ func TestConfigurableExporter(t *testing.T) {
 	mockInitProviderWithSingleRegistry()
 	metadataService, _ := inmemory.NewMetadataService()
 	exported := NewMetadataServiceExporter(metadataService)
-	assert.Equal(t, false, exported.IsExported())
-	assert.NoError(t, exported.Export())
-	assert.Equal(t, true, exported.IsExported())
-	assert.Regexp(t, "dubbo://:20000/MetadataService*", exported.GetExportedURLs()[0].String())
-	exported.Unexport()
-	assert.Equal(t, false, exported.IsExported())
+
+	t.Run("configurableExporterUrlNil", func(t *testing.T) {
+		assert.Equal(t, false, exported.IsExported())
+		assert.Error(t, exported.Export(nil), "metadata server url is nil, pls check your configuration")
+	})
+
+	t.Run("configurableExporter", func(t *testing.T) {
+		registryURL, _ := common.NewURL("service-discovery://localhost:12345")
+		subURL, _ := common.NewURL("dubbo://localhost:20003")
+		registryURL.SubURL = &subURL
+		assert.Equal(t, false, exported.IsExported())
+		assert.NoError(t, exported.Export(&registryURL))
+		assert.Equal(t, true, exported.IsExported())
+		assert.Regexp(t, "dubbo://:20003/MetadataService*", exported.GetExportedURLs()[0].String())
+		exported.Unexport()
+		assert.Equal(t, false, exported.IsExported())
+	})
 }
 
 // mockInitProviderWithSingleRegistry will init a mocked providerConfig
diff --git a/metadata/service/exporter/exporter.go b/metadata/service/exporter/exporter.go
index cfdef3a0e..33ceaca46 100644
--- a/metadata/service/exporter/exporter.go
+++ b/metadata/service/exporter/exporter.go
@@ -23,7 +23,7 @@ import (
 
 // MetadataServiceExporter will export & unexport the metadata service,  get exported url, and return is exported or not
 type MetadataServiceExporter interface {
-	Export() error
+	Export(url *common.URL) error
 	Unexport()
 	GetExportedURLs() []*common.URL
 	IsExported() bool
diff --git a/registry/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go
index 7576804eb..4db2c5aad 100644
--- a/registry/servicediscovery/service_discovery_registry.go
+++ b/registry/servicediscovery/service_discovery_registry.go
@@ -75,7 +75,7 @@ type serviceDiscoveryRegistry struct {
 
 func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) {
 
-	tryInitMetadataService()
+	tryInitMetadataService(url)
 
 	serviceDiscovery, err := creatServiceDiscovery(url)
 	if err != nil {
@@ -642,7 +642,7 @@ var (
 
 // tryInitMetadataService will try to initialize metadata service
 // TODO (move to somewhere)
-func tryInitMetadataService() {
+func tryInitMetadataService(url *common.URL) {
 
 	ms, err := extension.GetMetadataService(config.GetApplicationConfig().MetadataType)
 	if err != nil {
@@ -662,7 +662,7 @@ func tryInitMetadataService() {
 
 	expt := configurable.NewMetadataServiceExporter(ms)
 
-	err = expt.Export()
+	err = expt.Export(url)
 	if err != nil {
 		logger.Errorf("could not export the metadata service", err)
 	}
-- 
GitLab