Skip to content

Commit

Permalink
optimize extension local metadataService
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick0308 committed Apr 17, 2021
1 parent e3b111c commit 9a3d220
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 13 deletions.
20 changes: 12 additions & 8 deletions common/extension/metadata_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
)

import (
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/metadata/service"
)

Expand All @@ -34,16 +35,19 @@ var (
metadataServiceInsMap = make(map[string]func() (service.MetadataService, error), 2)
)

// SetMetadataService will store the msType => creator pair
func SetMetadataService(msType string, creator func() (service.MetadataService, error)) {
metadataServiceInsMap[msType] = creator
// SetLocalMetadataService will store the msType => creator pair
func SetLocalMetadataService(key string, creator func() (service.MetadataService, error)) {
metadataServiceInsMap[key] = creator
}

// GetMetadataService will create a MetadataService instance
func GetMetadataService(msType string) (service.MetadataService, error) {
if creator, ok := metadataServiceInsMap[msType]; ok {
// GetMetadataService will create a inmemory MetadataService instance
func GetLocalMetadataService(key string) (service.MetadataService, error) {
if key == "" {
key = constant.DEFAULT_KEY
}
if creator, ok := metadataServiceInsMap[key]; ok {
return creator()
}
return nil, perrors.New(fmt.Sprintf("could not find the metadata service creator for metadataType: %s, please check whether you have imported relative packages, \n" +
"local - github.com/apache/dubbo-go/metadata/service/inmemory, " + msType))
return nil, perrors.New(fmt.Sprintf("could not find the metadata service creator for metadataType: local, please check whether you have imported relative packages, \n" +
"local - github.com/apache/dubbo-go/metadata/service/inmemory, "))
}
2 changes: 1 addition & 1 deletion config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func createInstance(url *common.URL) (registry.ServiceInstance, error) {
// selectMetadataServiceExportedURL get already be exported url
func selectMetadataServiceExportedURL() *common.URL {
var selectedUrl *common.URL
metaDataService, err := extension.GetMetadataService(constant.DEFAULT_METADATA_STORAGE_TYPE)
metaDataService, err := extension.GetLocalMetadataService("")
if err != nil {
logger.Warn(err)
return nil
Expand Down
7 changes: 7 additions & 0 deletions config/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ func (c *ServiceConfig) Export() error {
c.exporters = append(c.exporters, exporter)
}
} else {
if ivkURL.GetParam(constant.INTERFACE_KEY, "") == constant.METADATA_SERVICE_NAME {
ms, err := extension.GetLocalMetadataService("")
if err != nil {
return err
}
ms.SetMetadataServiceURL(ivkURL)
}
invoker := proxyFactory.GetInvoker(ivkURL)
exporter := extension.GetProtocol(protocolwrapper.FILTER).Export(invoker)
if exporter == nil {
Expand Down
5 changes: 2 additions & 3 deletions metadata/remote/impl/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ func TestMetadataService(t *testing.T) {
u, err := common.NewURL("mock://127.0.0.1:20000/?sync.report=true")
assert.NoError(t, err)
instance.GetMetadataReportInstance(u)
mts, err := GetMetadataService()
mts, err := GetRemoteMetadataService()
assert.NoError(t, err)
assert.NotNil(t, mts)
}

func TestMockInmemoryProc(t *testing.T) *inmemory.MetadataService {
func TestMockInmemoryProc(t *testing.T) {
mts, _ := inmemory.GetInMemoryMetadataService()
serviceName := "com.ikurento.user.UserProvider"
group := "group1"
Expand Down Expand Up @@ -144,5 +144,4 @@ func TestMockInmemoryProc(t *testing.T) *inmemory.MetadataService {
serviceKey := definition.ServiceDescriperBuild(serviceName, group, version)
def2, _ := mts.GetServiceDefinitionByServiceKey(serviceKey)
assert.Equal(t, expected, def2)
return mts.(*inmemory.MetadataService)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

func init() {
extension.SetMetadataService(constant.DEFAULT_METADATA_STORAGE_TYPE, GetInMemoryMetadataService)
extension.SetLocalMetadataService(constant.DEFAULT_KEY, GetInMemoryMetadataService)
once = &sync.Once{}
}

Expand Down
4 changes: 4 additions & 0 deletions metadata/service/inmemory/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,7 @@ func (mts *MetadataService) Version() (string, error) {
func (mts *MetadataService) GetMetadataServiceURL() *common.URL {
return mts.metadataServiceURL
}

func (mts *MetadataService) SetMetadataServiceURL(url *common.URL) {
mts.metadataServiceURL = url
}
5 changes: 5 additions & 0 deletions metadata/service/inmemory/service_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ func (m *MetadataServiceProxy) GetExportedServiceURLs() []*common.URL {
}

func (m *MetadataServiceProxy) GetMetadataServiceURL() *common.URL {
logger.Error("you should never invoke this implementation")
return nil
}

func (m *MetadataServiceProxy) SetMetadataServiceURL(*common.URL) {
logger.Error("you should never invoke this implementation")
}

func (m *MetadataServiceProxy) MethodMapper() map[string]string {
return map[string]string{}
}
Expand Down
2 changes: 2 additions & 0 deletions metadata/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type MetadataService interface {
GetExportedServiceURLs() []*common.URL
// GetMetadataServiceURL will return the url of metadata service
GetMetadataServiceURL() *common.URL
// SetMetadataServiceURL will save the url of metadata service
SetMetadataServiceURL(*common.URL)
}

// BaseMetadataService is used for the event logic for struct who will implement interface MetadataService
Expand Down

0 comments on commit 9a3d220

Please sign in to comment.