Skip to content
Snippets Groups Projects
Commit f94c3e72 authored by 邹毅贤's avatar 邹毅贤
Browse files

Merge pull request #784 from sanxun0325/metadata_default_port

Fix:  issue 774 [fix metadata default port]
parent 6f12982f
No related branches found
No related tags found
No related merge requests found
......@@ -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",
}
}
......@@ -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
......
......@@ -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
......
......@@ -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)
}
......
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