Skip to content

Commit

Permalink
make metadata report work without serviceDiscovery
Browse files Browse the repository at this point in the history
  • Loading branch information
cvictory committed Dec 21, 2020
1 parent b2b70d2 commit 785e323
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
17 changes: 17 additions & 0 deletions common/extension/metadata_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ import (
)

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

var (
// there will be two types: local or remote
metadataServiceInsMap = make(map[string]func() (service.MetadataService, error), 2)
// remoteMetadataService
remoteMetadataService service.MetadataService
)

// SetMetadataService will store the msType => creator pair
Expand All @@ -48,3 +51,17 @@ func GetMetadataService(msType string) (service.MetadataService, error) {
"local - github.com/apache/dubbo-go/metadata/service/inmemory, \n"+
"remote - github.com/apache/dubbo-go/metadata/service/remote", msType))
}

// GetMetadataService will create a MetadataService instance
func GetRemoteMetadataService() (service.MetadataService, error) {
if remoteMetadataService != nil {
return remoteMetadataService, nil
}
if creator, ok := metadataServiceInsMap["remote"]; ok {
var err error
remoteMetadataService, err = creator()
return remoteMetadataService, err
}
logger.Info("could not find the metadata service creator for metadataType: remote")
return nil, perrors.New(fmt.Sprintf("could not find the metadata service creator for metadataType: remote"))
}
9 changes: 8 additions & 1 deletion config/reference_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ func (c *ReferenceConfig) Refer(_ interface{}) {
// FailoverClusterInvoker(RegistryDirectory, routing happens here) -> Invoker
c.invoker = cluster.Join(directory.NewStaticDirectory(invokers))
}

// publish consumer metadata
publishConsumerDefinition(cfgURL)
// create proxy
if c.Async {
callback := GetCallback(c.id)
Expand Down Expand Up @@ -248,3 +249,9 @@ func (c *ReferenceConfig) GenericLoad(id string) {
c.Refer(genericService)
c.Implement(genericService)
}

func publishConsumerDefinition(url *common.URL) {
if remoteMetadataService, err := extension.GetRemoteMetadataService(); err == nil && remoteMetadataService != nil {
remoteMetadataService.PublishServiceDefinition(url)
}
}
15 changes: 2 additions & 13 deletions config/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,10 @@ import (
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/logger"
"github.com/apache/dubbo-go/metadata/service"
"github.com/apache/dubbo-go/protocol"
"github.com/apache/dubbo-go/protocol/protocolwrapper"
)

var remoteMetadataService service.MetadataService

// ServiceConfig is the configuration of the service provider
type ServiceConfig struct {
context context.Context
Expand Down Expand Up @@ -338,15 +335,7 @@ func (c *ServiceConfig) GetExportedUrls() []*common.URL {
}

func publishServiceDefinition(url *common.URL) {
if remoteMetadataService == nil {
var err error
if remoteMetadataService, err = extension.GetMetadataService("remote"); err != nil {
logger.Warnf("get remote metadataService fail. %v", err)
return
} else if remoteMetadataService == nil {
logger.Info("get remote metadataService will nil.")
return
}
if remoteMetadataService, err := extension.GetRemoteMetadataService(); err == nil && remoteMetadataService != nil {
remoteMetadataService.PublishServiceDefinition(url)
}
remoteMetadataService.PublishServiceDefinition(url)
}
4 changes: 4 additions & 0 deletions metadata/report/delegate/delegate_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ type MetadataReport struct {
// NewMetadataReport will create a MetadataReport with initiation
func NewMetadataReport() (*MetadataReport, error) {
url := instance.GetMetadataReportUrl()
if url == nil {
logger.Warn("the metadataReport URL is not configured, you should configure it.")
return nil, perrors.New("the metadataReport URL is not configured, you should configure it.")
}
bmr := &MetadataReport{
reportUrl: url,
syncReport: url.GetParamBool(constant.SYNC_REPORT_KEY, false),
Expand Down
32 changes: 26 additions & 6 deletions metadata/service/remote/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,42 @@ func (mts *MetadataService) UnsubscribeURL(url *common.URL) error {
func (mts *MetadataService) PublishServiceDefinition(url *common.URL) error {
interfaceName := url.GetParam(constant.INTERFACE_KEY, "")
isGeneric := url.GetParamBool(constant.GENERIC_KEY, false)
if len(interfaceName) > 0 && !isGeneric {
sv := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey())
sd := definition.BuildServiceDefinition(*sv, url)
if common.RoleType(common.PROVIDER).Role() == url.GetParam(constant.SIDE_KEY, "") {
if len(interfaceName) > 0 && !isGeneric {
sv := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey())
sd := definition.BuildServiceDefinition(*sv, url)
id := &identifier.MetadataIdentifier{
BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{
ServiceInterface: interfaceName,
Version: url.GetParam(constant.VERSION_KEY, ""),
// Group: url.GetParam(constant.GROUP_KEY, constant.SERVICE_DISCOVERY_DEFAULT_GROUP),
Group: url.GetParam(constant.GROUP_KEY, constant.DUBBO),
Side: url.GetParam(constant.SIDE_KEY, "provider"),
},
}
mts.delegateReport.StoreProviderMetadata(id, sd)
return nil
}
logger.Errorf("publishProvider interfaceName is empty . providerUrl:%v ", url)
} else {
params := make(map[string]string, len(url.GetParams()))
url.RangeParams(func(key, value string) bool {
params[key] = value
return true
})
id := &identifier.MetadataIdentifier{
BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{
ServiceInterface: interfaceName,
Version: url.GetParam(constant.VERSION_KEY, ""),
// Group: url.GetParam(constant.GROUP_KEY, constant.SERVICE_DISCOVERY_DEFAULT_GROUP),
Group: url.GetParam(constant.GROUP_KEY, constant.DUBBO),
Side: url.GetParam(constant.SIDE_KEY, "provider"),
Side: url.GetParam(constant.SIDE_KEY, "consumer"),
},
}
mts.delegateReport.StoreProviderMetadata(id, sd)
mts.delegateReport.StoreConsumerMetadata(id, params)
return nil
}
logger.Errorf("publishProvider interfaceName is empty . providerUrl:%v ", url)

return nil
}

Expand Down

0 comments on commit 785e323

Please sign in to comment.