You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by xi...@apache.org on 2020/10/22 02:01:00 UTC

[dubbo-go] branch develop updated: commit metadata service exporter port

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

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


The following commit(s) were added to refs/heads/develop by this push:
     new 65f5a10  commit metadata service exporter port
     new 382f183  Merge pull request #784 from sanxun0325/metadata_default_port
65f5a10 is described below

commit 65f5a107c4a215c25eb83a79edd746f93e74ae0b
Author: sanxun0325 <bb...@163.com>
AuthorDate: Fri Oct 9 14:22:12 2020 +0800

    commit metadata service exporter port
---
 metadata/service/exporter/configurable/exporter.go | 19 ++++++++---------
 .../service/exporter/configurable/exporter_test.go | 24 ++++++++++++++++------
 metadata/service/exporter/exporter.go              |  2 +-
 .../servicediscovery/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 5a930c5..75e52d8 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 b304b91..ceda255 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 cfdef3a..33ceaca 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 7576804..4db2c5a 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)
 	}