Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
664e03f
refact metadata
FoghostCn Nov 22, 2023
bf150ce
fix cycle import
FoghostCn Nov 24, 2023
54116b3
Initially completed
FoghostCn Nov 25, 2023
dda0ba8
register success
FoghostCn Nov 28, 2023
f2134b4
refect serviceDiscoveryRegistry Server size api
FoghostCn Nov 29, 2023
20d02c0
move customizer to service discovery dir
FoghostCn Nov 29, 2023
d13afea
client call success
FoghostCn Dec 2, 2023
77983e5
mv metadata client to metadata dir
FoghostCn Dec 2, 2023
41f7474
refactor
FoghostCn Dec 5, 2023
5eadaae
refactor metadata exporter
FoghostCn Dec 11, 2023
2ac1b8e
mv metadata client to metadata dir
FoghostCn Dec 11, 2023
6b2fd9d
mv metadata info to metadata module
FoghostCn Dec 11, 2023
157e9c2
associate metadataReport with registry
FoghostCn Jan 2, 2024
8ab3557
unit test not done
FoghostCn Jan 4, 2024
a179a3d
update warn log
FoghostCn Jan 5, 2024
1a11af1
unit test tmp fix
FoghostCn Jan 5, 2024
c29963b
fix integrate_test
FoghostCn Jan 5, 2024
25d8711
fix some logic
FoghostCn Jan 8, 2024
e3ef66e
change RegistryType default value
FoghostCn Jan 9, 2024
88a537d
code review fix
FoghostCn Jan 15, 2024
328eeeb
change metadata to options style
FoghostCn Jan 16, 2024
d7097b1
fix unit test
FoghostCn Jan 16, 2024
5c6e8d7
fix integration test
FoghostCn Jan 16, 2024
e461a39
add doc for metadata report option WithAddress
FoghostCn Jan 22, 2024
ee77794
Merge branch 'main' into refect-metadata
FoghostCn Jan 23, 2024
b0c75a5
modify README.md (#2585)
FinalT Jan 26, 2024
a134e1b
feat:add internal service extension (#2577)
FinalT Jan 26, 2024
23bdc04
Merge remote-tracking branch 'origin/main' into refect-metadata
FoghostCn Jan 29, 2024
38a1cf0
Merge branch 'main' into refect-metadata
FoghostCn Jan 29, 2024
e283246
Merge branch 'dev-3.3' into refect-metadata
FoghostCn Feb 18, 2024
da7de7f
Merge branch 'dev-3.3' into refect-metadata
chickenlj Feb 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix cycle import
  • Loading branch information
FoghostCn committed Jan 16, 2024
commit bf150ce47a6ac4f80d2997d96a3e6b4521b74c03
89 changes: 0 additions & 89 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,17 @@ package config

import (
"errors"
"reflect"
"strconv"
)

import (
"github.com/dubbogo/gost/log/logger"

"github.com/knadh/koanf"

perrors "github.com/pkg/errors"
)

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/logger/zap"
"dubbo.apache.org/dubbo-go/v3/metadata/service"
"dubbo.apache.org/dubbo-go/v3/registry"
)

var (
Expand Down Expand Up @@ -76,87 +68,6 @@ func check() error {
return nil
}

// registerServiceInstance register service instance
func registerServiceInstance() {
url := selectMetadataServiceExportedURL()
if url == nil {
return
}
instance, err := createInstance(url)
if err != nil {
panic(err)
}
p := extension.GetProtocol(constant.RegistryProtocol)
var rp registry.RegistryFactory
var ok bool
if rp, ok = p.(registry.RegistryFactory); !ok {
panic("dubbo registry protocol{" + reflect.TypeOf(p).String() + "} is invalid")
}
rs := rp.GetRegistries()
for _, r := range rs {
var sdr registry.ServiceDiscoveryHolder
if sdr, ok = r.(registry.ServiceDiscoveryHolder); !ok {
continue
}
// publish app level data to registry
logger.Infof("Starting register instance address %v", instance)
err := sdr.GetServiceDiscovery().Register(instance)
if err != nil {
panic(err)
}
}
}
func selectMetadataServiceExportedURL() *common.URL {
var selectedUrl *common.URL
urlList := service.GlobalMetadataService.GetExportedServiceURLs()
if len(urlList) == 0 {
return nil
}
for _, url := range urlList {
selectedUrl = url
// rest first
if url.Protocol == "rest" {
break
}
}
return selectedUrl
}

// // nolint
func createInstance(url *common.URL) (registry.ServiceInstance, error) {
appConfig := GetApplicationConfig()
port, err := strconv.ParseInt(url.Port, 10, 32)
if err != nil {
return nil, perrors.WithMessage(err, "invalid port: "+url.Port)
}

host := url.Ip
if len(host) == 0 {
host = common.GetLocalIp()
}

// usually we will add more metadata
metadata := make(map[string]string, 8)
metadata[constant.MetadataStorageTypePropertyName] = appConfig.MetadataType

instance := &registry.DefaultServiceInstance{
ServiceName: appConfig.Name,
Host: host,
Port: int(port),
ID: host + constant.KeySeparator + url.Port,
Enable: true,
Healthy: true,
Metadata: metadata,
Tag: appConfig.Tag,
}

for _, cus := range extension.GetCustomizers() {
cus.Customize(instance)
}

return instance, nil
}

// GetRPCService get rpc service for consumer
func GetRPCService(name string) common.RPCService {
return rootConfig.Consumer.References[name].GetRPCService()
Expand Down
1 change: 0 additions & 1 deletion config/root_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ func (rc *RootConfig) Start() {
gracefulShutdownInit()
rc.Consumer.Load()
rc.Provider.Load()
registerServiceInstance()
})
}

Expand Down
1 change: 1 addition & 0 deletions imports/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
_ "dubbo.apache.org/dubbo-go/v3/filter/tps/limiter"
_ "dubbo.apache.org/dubbo-go/v3/filter/tps/strategy"
_ "dubbo.apache.org/dubbo-go/v3/filter/tracing"
_ "dubbo.apache.org/dubbo-go/v3/metadata/exporter"
_ "dubbo.apache.org/dubbo-go/v3/metadata/mapping/metadata"
_ "dubbo.apache.org/dubbo-go/v3/metadata/report/etcd"
_ "dubbo.apache.org/dubbo-go/v3/metadata/report/nacos"
Expand Down
50 changes: 50 additions & 0 deletions metadata/exporter/service_exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package exporter

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/metadata"
"github.com/dubbogo/gost/log/logger"
)

func init() {
metadata.SetExporterFactory(NewMetadataServiceExporter)
}

// MetadataServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface
type MetadataServiceExporter struct {
app, metadataType string
ServiceConfig *config.ServiceConfig
}

func NewMetadataServiceExporter(app, metadataType string) metadata.MetadataServiceExporter {
return &MetadataServiceExporter{app: app, metadataType: metadataType}
}

// Export will export the metadataService
func (exporter *MetadataServiceExporter) Export() error {
exporter.ServiceConfig = config.NewServiceConfigBuilder().
SetServiceID(constant.SimpleMetadataServiceName).
SetProtocolIDs(constant.DefaultProtocol).
AddRCProtocol(constant.DefaultProtocol, config.NewProtocolConfigBuilder().
SetName(constant.DefaultProtocol).
Build()).
SetRegistryIDs("N/A").
SetInterface(constant.MetadataServiceName).
SetGroup(exporter.app).
SetVersion(metadata.GlobalMetadataService.Version()).
SetProxyFactoryKey(constant.DefaultKey).
SetMetadataType(exporter.metadataType).
Build()
exporter.ServiceConfig.Implement(metadata.GlobalMetadataService)
err := exporter.ServiceConfig.Export()
url := exporter.ServiceConfig.GetExportedUrls()[0]
metadata.GlobalMetadataService.SetMetadataServiceURL(url)
logger.Infof("[Metadata Service] The MetadataService exports urls : %v ", exporter.ServiceConfig.GetExportedUrls())
return err
}

// UnExport will unExport the metadataService
func (exporter *MetadataServiceExporter) UnExport() {
exporter.ServiceConfig.Unexport()
}
31 changes: 20 additions & 11 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,30 @@ import (
"github.com/dubbogo/gost/log/logger"
)

import (
"dubbo.apache.org/dubbo-go/v3/metadata/service"
)

var (
exportOnce sync.Once
GlobalMetadataService MetadataService = &metadataService{}
exportOnce sync.Once
Factory ExporterFactory
)

// ExporterFactory to create MetadataServiceExporter, avoid cycle import
type ExporterFactory func(app, metadataType string) MetadataServiceExporter

func SetExporterFactory(factory ExporterFactory) {
Factory = factory
}

func ExportMetadataService(app, metadataType string) {
exportOnce.Do(func() {
err := service.Exporter.Export(app, metadataType)
if err != nil {
logger.Errorf("export metadata service failed, got error %#v", err)
}
})
if Factory != nil {
exportOnce.Do(func() {
err := Factory(app, metadataType).Export()
if err != nil {
logger.Errorf("export metadata service failed, got error %#v", err)
}
})
} else {
logger.Warn("no metadata service exporter found, MetadataService will not be Exported")
}

// err = publishMapping(expt)
// if err != nil {
Expand Down
99 changes: 37 additions & 62 deletions metadata/service/service.go → metadata/metadata_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
* limitations under the License.
*/

package service

import (
"sync"
)
package metadata

import (
"github.com/dubbogo/gost/log/logger"
Expand All @@ -29,9 +25,7 @@ import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/metadata/info"
_ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo"
"dubbo.apache.org/dubbo-go/v3/registry"
)

Expand All @@ -41,15 +35,36 @@ const (
allServiceInterfaces = "*"
)

var (
metadataServiceUrl *common.URL
GlobalMetadataService MetadataService = &metadataService{}
Exporter = &MetadataServiceExporter{}
)
// MetadataService is used to define meta data related behaviors
// usually the implementation should be singleton
type MetadataService interface {
// GetExportedURLs will get the target exported url in metadata, the url should be unique
GetExportedURLs(serviceInterface string, group string, version string, protocol string) []*common.URL
// GetExportedServiceURLs will return exported service urls
GetExportedServiceURLs() []*common.URL
// GetSubscribedURLs will get the exported urls in metadata
GetSubscribedURLs() []*common.URL
Version() string
// GetMetadataInfo will return metadata info
GetMetadataInfo(revision string) *info.MetadataInfo
// GetMetadataServiceURL will return the url of metadata service
GetMetadataServiceURL() *common.URL
// SetMetadataServiceURL exporter to set url of metadata service
SetMetadataServiceURL(*common.URL)
}

type MetadataServiceExporter interface {
Export() error
UnExport()
}

// MetadataService is store and query the metadata info in memory when each service registry
type metadataService struct {
BaseMetadataService
url *common.URL
}

func (mts *metadataService) SetMetadataServiceURL(url *common.URL) {
mts.url = url
}

// GetExportedURLs get all exported urls
Expand Down Expand Up @@ -85,13 +100,13 @@ func (mts *metadataService) GetMetadataInfo(revision string) *info.MetadataInfo
return nil
}

func (mts *metadataService) getServiceDiscoveries() []registry.ServiceDiscovery {
sds := make([]registry.ServiceDiscovery, 0)
func (mts *metadataService) getServiceDiscoveries() []registry.ServiceDiscoveryMetadata {
sds := make([]registry.ServiceDiscoveryMetadata, 0)
p := extension.GetProtocol(constant.RegistryProtocol)
if factory, ok := p.(registry.RegistryFactory); ok {
for _, v := range factory.GetRegistries() {
if sd, ok := v.(registry.ServiceDiscoveryHolder); ok {
sds = append(sds, sd.GetServiceDiscovery())
sds = append(sds, sd.GetServiceDiscoverMetadata())
}
}
}
Expand All @@ -114,7 +129,7 @@ func (mts *metadataService) Version() string {

// GetMetadataServiceURL get url of MetadataService
func (mts *metadataService) GetMetadataServiceURL() *common.URL {
return metadataServiceUrl
return mts.url
}

func (mts *metadataService) GetSubscribedURLs() []*common.URL {
Expand All @@ -125,50 +140,10 @@ func (mts *metadataService) GetSubscribedURLs() []*common.URL {
return urls
}

// MetadataServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface
type MetadataServiceExporter struct {
ServiceConfig *config.ServiceConfig
lock sync.RWMutex
}

// Export will export the metadataService
func (exporter *MetadataServiceExporter) Export(app, metadataType string) error {
if !exporter.IsExported() {
exporter.lock.Lock()
defer exporter.lock.Unlock()
exporter.ServiceConfig = config.NewServiceConfigBuilder().
SetServiceID(constant.SimpleMetadataServiceName).
SetProtocolIDs(constant.DefaultProtocol).
AddRCProtocol(constant.DefaultProtocol, config.NewProtocolConfigBuilder().
SetName(constant.DefaultProtocol).
Build()).
SetRegistryIDs("N/A").
SetInterface(constant.MetadataServiceName).
SetGroup(app).
SetVersion(version).
SetProxyFactoryKey(constant.DefaultKey).
SetMetadataType(metadataType).
Build()
exporter.ServiceConfig.Implement(GlobalMetadataService)
err := exporter.ServiceConfig.Export()
metadataServiceUrl = exporter.ServiceConfig.GetExportedUrls()[0]
logger.Infof("[Metadata Service] The MetadataService exports urls : %v ", exporter.ServiceConfig.GetExportedUrls())
return err
}
logger.Warnf("[Metadata Service] The MetadataService has been exported : %v ", exporter.ServiceConfig.GetExportedUrls())
return nil
}

// UnExport will unExport the metadataService
func (exporter *MetadataServiceExporter) UnExport() {
if exporter.IsExported() {
exporter.ServiceConfig.Unexport()
// MethodMapper only for rename exported function, for example: rename the function GetMetadataInfo to getMetadataInfo
func (mts *metadataService) MethodMapper() map[string]string {
return map[string]string{
"GetExportedURLs": "getExportedURLs",
"GetMetadataInfo": "getMetadataInfo",
}
}

// IsExported will return is metadataServiceExporter exported or not
func (exporter *MetadataServiceExporter) IsExported() bool {
exporter.lock.RLock()
defer exporter.lock.RUnlock()
return exporter.ServiceConfig != nil && exporter.ServiceConfig.IsExport()
}
Loading