diff --git a/.gitignore b/.gitignore index 01a7dbfc96..a38e3cf5cc 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,4 @@ metadata/report/zookeeper/zookeeper-4unittest/ # macOS .DS_Store +.history diff --git a/client/action.go b/client/action.go index b5a0fafd6d..c5ef02d74f 100644 --- a/client/action.go +++ b/client/action.go @@ -154,8 +154,6 @@ func (refOpts *ReferenceOptions) refer(srv common.RPCService, info *ClientInfo) refOpts.urls = urls refOpts.invoker = invoker - // publish consumer's metadata - publishServiceDefinition(cfgURL) // create proxy if info == nil && srv != nil { if ref.Async { @@ -279,21 +277,6 @@ func buildInvoker(urls []*common.URL, ref *global.ReferenceConfig) (protocol.Inv return resInvoker, nil } -func publishServiceDefinition(url *common.URL) { - localService, err := extension.GetLocalMetadataService(constant.DefaultKey) - if err != nil { - logger.Warnf("get local metadata service failed, please check if you have imported _ \"dubbo.apache.org/dubbo-go/v3/metadata/service/local\"") - return - } - localService.PublishServiceDefinition(url) - if url.GetParam(constant.MetadataTypeKey, "") != constant.RemoteMetadataStorageType { - return - } - if remoteMetadataService, err := extension.GetRemoteMetadataService(); err == nil && remoteMetadataService != nil { - remoteMetadataService.PublishServiceDefinition(url) - } -} - func (refOpts *ReferenceOptions) CheckAvailable() bool { ref := refOpts.Reference if refOpts.invoker == nil { diff --git a/client/client.go b/client/client.go index 98ab66733e..3d0967acfb 100644 --- a/client/client.go +++ b/client/client.go @@ -20,10 +20,13 @@ package client import ( "context" + "time" ) import ( "dubbo.apache.org/dubbo-go/v3/common/constant" + "dubbo.apache.org/dubbo-go/v3/global" + "dubbo.apache.org/dubbo-go/v3/metadata" "dubbo.apache.org/dubbo-go/v3/protocol" invocation_impl "dubbo.apache.org/dubbo-go/v3/protocol/invocation" ) @@ -103,6 +106,9 @@ func (cli *Client) DialWithInfo(interfaceName string, info *ClientInfo, opts ... } func (cli *Client) dial(interfaceName string, info *ClientInfo, opts ...ReferenceOption) (*Connection, error) { + if err := cli.initRegistryMetadataReport(); err != nil { + return nil, err + } newRefOpts := defaultReferenceOptions() finalOpts := []ReferenceOption{ setReference(cli.cliOpts.overallReference), @@ -122,6 +128,45 @@ func (cli *Client) dial(interfaceName string, info *ClientInfo, opts ...Referenc return &Connection{refOpts: newRefOpts}, nil } + +func (cli *Client) initRegistryMetadataReport() error { + if len(cli.cliOpts.Registries) > 0 { + for id, reg := range cli.cliOpts.Registries { + if reg.UseAsMetaReport { + opts, err := registryToReportOptions(id, reg) + if err != nil { + return err + } + if err := opts.Init(); err != nil { + return err + } + } + } + } + return nil +} + +func registryToReportOptions(id string, rc *global.RegistryConfig) (*metadata.ReportOptions, error) { + opts := metadata.NewReportOptions( + metadata.WithRegistryId(id), + metadata.WithProtocol(rc.Protocol), + metadata.WithAddress(rc.Address), + metadata.WithUsername(rc.Username), + metadata.WithPassword(rc.Password), + metadata.WithGroup(rc.Group), + metadata.WithNamespace(rc.Namespace), + metadata.WithParams(rc.Params), + ) + if rc.Timeout != "" { + timeout, err := time.ParseDuration(rc.Timeout) + if err != nil { + return nil, err + } + metadata.WithTimeout(timeout)(opts) + } + return opts, nil +} + func generateInvocation(methodName string, reqs []interface{}, resp interface{}, callType string, opts *CallOptions) (protocol.Invocation, error) { var paramsRawVals []interface{} for _, req := range reqs { diff --git a/common/constant/key.go b/common/constant/key.go index 5dd9dcb0cd..9e29e36c69 100644 --- a/common/constant/key.go +++ b/common/constant/key.go @@ -166,6 +166,7 @@ const ( // registry keys const ( RegistryKey = "registry" + RegistryIdKey = "registry.id" RegistryProtocol = "registry" ServiceRegistryProtocol = "service-discovery-registry" RegistryRoleKey = "registry.role" @@ -188,6 +189,7 @@ const ( const ( ApplicationKey = "application" + ApplicationTagKey = "application.tag" OrganizationKey = "organization" NameKey = "name" ModuleKey = "module" diff --git a/common/extension/metadata_remote.go b/common/extension/metadata_remote.go deleted file mode 100644 index 2ed5652ea9..0000000000 --- a/common/extension/metadata_remote.go +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package extension - -import ( - "fmt" -) - -import ( - perrors "github.com/pkg/errors" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/metadata/service" -) - -type remoteMetadataServiceCreator func() (service.RemoteMetadataService, error) - -var ( - creator remoteMetadataServiceCreator -) - -// SetRemoteMetadataService will store the remote metadata service -func SetRemoteMetadataService(creatorFunc remoteMetadataServiceCreator) { - creator = creatorFunc -} - -// GetRemoteMetadataService will create a MetadataService instance -func GetRemoteMetadataService() (service.RemoteMetadataService, error) { - if creator != nil { - return creator() - } - return nil, perrors.New(fmt.Sprintf("could not find the metadata service creator for metadataType: remote, " + - "please check whether you have imported relative packages, " + - "remote - dubbo.apache.org/dubbo-go/v3/metadata/remote/impl")) -} diff --git a/common/extension/metadata_report_factory.go b/common/extension/metadata_report_factory.go index f80b2502d3..cd65598732 100644 --- a/common/extension/metadata_report_factory.go +++ b/common/extension/metadata_report_factory.go @@ -18,18 +18,18 @@ package extension import ( - "dubbo.apache.org/dubbo-go/v3/metadata/report/factory" + "dubbo.apache.org/dubbo-go/v3/metadata/report" ) -var metaDataReportFactories = make(map[string]func() factory.MetadataReportFactory, 8) +var metaDataReportFactories = make(map[string]func() report.MetadataReportFactory, 8) // SetMetadataReportFactory sets the MetadataReportFactory with @name -func SetMetadataReportFactory(name string, v func() factory.MetadataReportFactory) { +func SetMetadataReportFactory(name string, v func() report.MetadataReportFactory) { metaDataReportFactories[name] = v } // GetMetadataReportFactory finds the MetadataReportFactory with @name -func GetMetadataReportFactory(name string) factory.MetadataReportFactory { +func GetMetadataReportFactory(name string) report.MetadataReportFactory { if metaDataReportFactories[name] == nil { return nil } diff --git a/common/extension/metadata_service.go b/common/extension/metadata_service.go deleted file mode 100644 index 39770ada8f..0000000000 --- a/common/extension/metadata_service.go +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package extension - -import ( - "fmt" -) - -import ( - perrors "github.com/pkg/errors" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common/constant" - "dubbo.apache.org/dubbo-go/v3/metadata/service" -) - -type localMetadataServiceCreator func() (service.MetadataService, error) - -var ( - localMetadataServiceInsMap = make(map[string]localMetadataServiceCreator, 2) -) - -// SetLocalMetadataService will store the msType => creator pair -func SetLocalMetadataService(key string, creator localMetadataServiceCreator) { - localMetadataServiceInsMap[key] = creator -} - -// GetLocalMetadataService will create a local MetadataService instance -func GetLocalMetadataService(key string) (service.MetadataService, error) { - if key == "" { - key = constant.DefaultKey - } - if creator, ok := localMetadataServiceInsMap[key]; ok { - return creator() - } - return nil, perrors.New(fmt.Sprintf("could not find the metadata service creator for metadataType: local, " + - "please check whether you have imported relative packages, " + - "local - dubbo.apache.org/dubbo-go/v3/metadata/service/local")) -} diff --git a/common/extension/metadata_service_exporter.go b/common/extension/metadata_service_exporter.go deleted file mode 100644 index ab8cff8f4d..0000000000 --- a/common/extension/metadata_service_exporter.go +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package extension - -import ( - "dubbo.apache.org/dubbo-go/v3/common/constant" - "dubbo.apache.org/dubbo-go/v3/metadata/service" - "dubbo.apache.org/dubbo-go/v3/metadata/service/exporter" -) - -type MetadataServiceExporterCreator func(service.MetadataService) exporter.MetadataServiceExporter - -var ( - metadataServiceExporterInsMap = make(map[string]MetadataServiceExporterCreator, 2) -) - -// SetMetadataServiceExporter will store the type => creator pair -func SetMetadataServiceExporter(key string, creator MetadataServiceExporterCreator) { - metadataServiceExporterInsMap[key] = creator -} - -// GetMetadataServiceExporter will create a MetadataServiceExporter instance -func GetMetadataServiceExporter(key string, s service.MetadataService) exporter.MetadataServiceExporter { - if key == "" { - key = constant.DefaultKey - } - if creator, ok := metadataServiceExporterInsMap[key]; ok { - return creator(s) - } - return nil -} diff --git a/common/extension/metadata_service_proxy_factory.go b/common/extension/metadata_service_proxy_factory.go deleted file mode 100644 index ecb8fb45b4..0000000000 --- a/common/extension/metadata_service_proxy_factory.go +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package extension - -import ( - "fmt" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common/constant" - "dubbo.apache.org/dubbo-go/v3/metadata/service" -) - -var metadataServiceProxyFactoryMap = make(map[string]func() service.MetadataServiceProxyFactory, 2) - -type MetadataServiceProxyFactoryFunc func() service.MetadataServiceProxyFactory - -// SetMetadataServiceProxyFactory store the name-creator pair -func SetMetadataServiceProxyFactory(name string, creator MetadataServiceProxyFactoryFunc) { - metadataServiceProxyFactoryMap[name] = creator -} - -// GetMetadataServiceProxyFactory will create an instance. -// it will panic if the factory with name not found -func GetMetadataServiceProxyFactory(name string) service.MetadataServiceProxyFactory { - if name == "" { - name = constant.DefaultKey - } - if f, ok := metadataServiceProxyFactoryMap[name]; ok { - return f() - } - panic(fmt.Sprintf("could not find the metadata service factory creator for name: %s, "+ - "please check whether you have imported relative packages, "+ - "local - dubbo.apache.org/dubbo-go/v3/metadata/service/local", name)) -} diff --git a/common/metadata_info.go b/common/metadata_info.go deleted file mode 100644 index aad5926b70..0000000000 --- a/common/metadata_info.go +++ /dev/null @@ -1,249 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package common - -import ( - "fmt" - "hash/crc32" - "net/url" - "sort" - "strings" -) - -import ( - gxset "github.com/dubbogo/gost/container/set" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common/constant" -) - -var IncludeKeys = gxset.NewSet( - constant.ApplicationKey, - constant.GroupKey, - constant.TimestampKey, - constant.SerializationKey, - constant.ClusterKey, - constant.LoadbalanceKey, - constant.PathKey, - constant.TimeoutKey, - constant.TokenKey, - constant.VersionKey, - constant.WarmupKey, - constant.WeightKey, - constant.ReleaseKey) - -// MetadataInfo the metadata information of instance -type MetadataInfo struct { - Reported bool `json:"-"` - App string `json:"app,omitempty"` - Revision string `json:"revision,omitempty"` - Services map[string]*ServiceInfo `json:"services,omitempty"` -} - -// nolint -func NewMetadataInfWithApp(app string) *MetadataInfo { - return NewMetadataInfo(app, "", make(map[string]*ServiceInfo)) -} - -// nolint -func NewMetadataInfo(app string, revision string, services map[string]*ServiceInfo) *MetadataInfo { - return &MetadataInfo{ - App: app, - Revision: revision, - Services: services, - Reported: false, - } -} - -// nolint -func (mi *MetadataInfo) JavaClassName() string { - return "org.apache.dubbo.metadata.MetadataInfo" -} - -// CalAndGetRevision is different from Dubbo because golang doesn't support overload -// so that we should use interface + method name as identifier and ignore the method params -// in my opinion, it's enough because Dubbo actually ignore the URL params. -// please refer org.apache.dubbo.common.URL#toParameterString(java.lang.String...) -func (mi *MetadataInfo) CalAndGetRevision() string { - if mi.Revision != "" && mi.Reported { - return mi.Revision - } - if len(mi.Services) == 0 { - return "0" - } - candidates := make([]string, 0, 8) - - for _, s := range mi.Services { - iface := s.URL.GetParam(constant.InterfaceKey, "") - ms := s.URL.Methods - if len(ms) == 0 { - candidates = append(candidates, iface) - } else { - for _, m := range ms { - // methods are part of candidates - candidates = append(candidates, iface+constant.KeySeparator+m) - } - } - - // append URL params if we need it - } - sort.Strings(candidates) - - // it's nearly impossible to be overflow - res := uint64(0) - for _, c := range candidates { - res += uint64(crc32.ChecksumIEEE([]byte(c))) - } - mi.Revision = fmt.Sprint(res) - return mi.Revision - -} - -// nolint -func (mi *MetadataInfo) HasReported() bool { - return mi.Reported -} - -// nolint -func (mi *MetadataInfo) MarkReported() { - mi.Reported = true -} - -// nolint -func (mi *MetadataInfo) AddService(service *ServiceInfo) { - if service == nil { - return - } - mi.Services[service.GetMatchKey()] = service -} - -// nolint -func (mi *MetadataInfo) RemoveService(service *ServiceInfo) { - if service == nil { - return - } - delete(mi.Services, service.MatchKey) -} - -// ServiceInfo the information of service -type ServiceInfo struct { - Name string `json:"name,omitempty"` - Group string `json:"group,omitempty"` - Version string `json:"version,omitempty"` - Protocol string `json:"protocol,omitempty"` - Path string `json:"path,omitempty"` - Params map[string]string `json:"params,omitempty"` - - ServiceKey string `json:"-"` - MatchKey string `json:"-"` - URL *URL `json:"-"` -} - -// nolint -func NewServiceInfoWithURL(url *URL) *ServiceInfo { - service := NewServiceInfo(url.Service(), url.Group(), url.Version(), url.Protocol, url.Path, nil) - service.URL = url - // TODO includeKeys load dynamic - p := make(map[string]string, 8) - for _, keyInter := range IncludeKeys.Values() { - key := keyInter.(string) - value := url.GetParam(key, "") - if len(value) != 0 { - p[key] = value - } - for _, method := range url.Methods { - value = url.GetMethodParam(method, key, "") - if len(value) != 0 { - p[method+"."+key] = value - } - } - } - service.Params = p - return service -} - -// nolint -func NewServiceInfo(name, group, version, protocol, path string, params map[string]string) *ServiceInfo { - serviceKey := ServiceKey(name, group, version) - matchKey := MatchKey(serviceKey, protocol) - return &ServiceInfo{ - Name: name, - Group: group, - Version: version, - Protocol: protocol, - Path: strings.TrimPrefix(path, "/"), - Params: params, - ServiceKey: serviceKey, - MatchKey: matchKey, - } -} - -// nolint -func (si *ServiceInfo) JavaClassName() string { - return "org.apache.dubbo.metadata.MetadataInfo$ServiceInfo" -} - -// nolint -func (si *ServiceInfo) GetMethods() []string { - if si.Params[constant.MethodsKey] != "" { - s := si.Params[constant.MethodsKey] - return strings.Split(s, ",") - } - methods := make([]string, 0, 8) - for k, _ := range si.Params { - ms := strings.Index(k, ".") - if ms > 0 { - methods = append(methods, k[0:ms]) - } - } - return methods -} - -// nolint -func (si *ServiceInfo) GetParams() url.Values { - v := url.Values{} - for k, p := range si.Params { - ms := strings.Index(k, ".") - if ms > 0 { - v.Set("methods."+k, p) - } else { - v.Set(k, p) - } - } - return v -} - -// nolint -func (si *ServiceInfo) GetMatchKey() string { - if si.MatchKey != "" { - return si.MatchKey - } - serviceKey := si.GetServiceKey() - si.MatchKey = MatchKey(serviceKey, si.Protocol) - return si.MatchKey -} - -// nolint -func (si *ServiceInfo) GetServiceKey() string { - if si.ServiceKey != "" { - return si.ServiceKey - } - si.ServiceKey = ServiceKey(si.Name, si.Group, si.Version) - return si.ServiceKey -} diff --git a/config/application_config_test.go b/config/application_config_test.go index 86eea09cb3..f2ae9ab683 100644 --- a/config/application_config_test.go +++ b/config/application_config_test.go @@ -27,6 +27,7 @@ import ( import ( "dubbo.apache.org/dubbo-go/v3/common/constant" + _ "dubbo.apache.org/dubbo-go/v3/metadata/report/nacos" ) func TestApplicationConfig(t *testing.T) { diff --git a/config/config_loader.go b/config/config_loader.go index eda382d8fd..128720949e 100644 --- a/config/config_loader.go +++ b/config/config_loader.go @@ -19,24 +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/registry" ) var ( @@ -75,78 +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) - } - } - // publish metadata to remote - if GetApplicationConfig().MetadataType == constant.RemoteMetadataStorageType { - if remoteMetadataService, err := extension.GetRemoteMetadataService(); err == nil && remoteMetadataService != nil { - remoteMetadataService.PublishMetadata(GetApplicationConfig().Name) - } - } -} - -// // 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 := ®istry.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() diff --git a/config/custom_config_test.go b/config/custom_config_test.go index ccf3c58eb0..8b36c3155a 100644 --- a/config/custom_config_test.go +++ b/config/custom_config_test.go @@ -28,6 +28,7 @@ import ( import ( "dubbo.apache.org/dubbo-go/v3/common/constant" + _ "dubbo.apache.org/dubbo-go/v3/metadata/report/zookeeper" ) func TestCustomInit(t *testing.T) { diff --git a/config/instance/metadata_report.go b/config/instance/metadata_report.go deleted file mode 100644 index 16ba071219..0000000000 --- a/config/instance/metadata_report.go +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package instance - -import ( - "sync" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/extension" - "dubbo.apache.org/dubbo-go/v3/metadata/report" -) - -var ( - instance report.MetadataReport - reportUrl *common.URL - once sync.Once -) - -func GetMetadataReportInstance() report.MetadataReport { - if instance == nil { - instance = report.NewPubMetricEventReport(GetMetadataReportByRegistryProtocol("")) - } - return instance -} - -// SetMetadataReportInstance, init metadat report instance -func SetMetadataReportInstance(selectiveUrl ...*common.URL) { - once.Do(func() { - var url *common.URL - if len(selectiveUrl) > 0 { - url = selectiveUrl[0] - fac := extension.GetMetadataReportFactory(url.Protocol) - if fac != nil { - instance = report.NewPubMetricEventReport(fac.CreateMetadataReport(url)) - } - reportUrl = url - } - }) -} - -// GetMetadataReportUrl will return the report instance url -func GetMetadataReportUrl() *common.URL { - return reportUrl -} - -// SetMetadataReportUrl will only can be used by unit test to mock url -func SetMetadataReportUrl(url *common.URL) { - reportUrl = url -} diff --git a/config/instance/metadata_report_test.go b/config/instance/metadata_report_test.go deleted file mode 100644 index d1156b2c68..0000000000 --- a/config/instance/metadata_report_test.go +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package instance - -import ( - "testing" -) - -import ( - gxset "github.com/dubbogo/gost/container/set" - - "github.com/stretchr/testify/assert" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/extension" - "dubbo.apache.org/dubbo-go/v3/metadata/identifier" - "dubbo.apache.org/dubbo-go/v3/metadata/report" - "dubbo.apache.org/dubbo-go/v3/metadata/report/factory" - "dubbo.apache.org/dubbo-go/v3/registry" -) - -func TestGetMetadataReportInstance(t *testing.T) { - extension.SetMetadataReportFactory("mock", func() factory.MetadataReportFactory { - return &mockMetadataReportFactory{} - }) - u, _ := common.NewURL("mock://127.0.0.1") - SetMetadataReportInstance(u) - rpt := GetMetadataReportInstance() - assert.NotNil(t, rpt) -} - -type mockMetadataReportFactory struct{} - -func (m *mockMetadataReportFactory) CreateMetadataReport(*common.URL) report.MetadataReport { - return &mockMetadataReport{} -} - -type mockMetadataReport struct{} - -func (m mockMetadataReport) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { - //TODO implement me - panic("implement me") -} - -func (m mockMetadataReport) RegisterServiceAppMapping(string, string, string) error { - panic("implement me") -} - -func (m mockMetadataReport) GetServiceAppMapping(string, string, registry.MappingListener) (*gxset.HashSet, error) { - panic("implement me") -} - -func (m mockMetadataReport) RemoveServiceAppMappingListener(string, string) error { - panic("implement me") -} - -func (m mockMetadataReport) GetAppMetadata(*identifier.SubscriberMetadataIdentifier) (*common.MetadataInfo, error) { - panic("implement me") -} - -func (m mockMetadataReport) PublishAppMetadata(*identifier.SubscriberMetadataIdentifier, *common.MetadataInfo) error { - panic("implement me") -} - -func (m mockMetadataReport) StoreProviderMetadata(*identifier.MetadataIdentifier, string) error { - panic("implement me") -} - -func (m mockMetadataReport) StoreConsumerMetadata(*identifier.MetadataIdentifier, string) error { - panic("implement me") -} - -func (m mockMetadataReport) SaveServiceMetadata(*identifier.ServiceMetadataIdentifier, *common.URL) error { - panic("implement me") -} - -func (m mockMetadataReport) RemoveServiceMetadata(*identifier.ServiceMetadataIdentifier) error { - panic("implement me") -} - -func (m mockMetadataReport) GetExportedURLs(*identifier.ServiceMetadataIdentifier) ([]string, error) { - panic("implement me") -} - -func (m mockMetadataReport) SaveSubscribedData(*identifier.SubscriberMetadataIdentifier, string) error { - panic("implement me") -} - -func (m mockMetadataReport) GetSubscribedURLs(*identifier.SubscriberMetadataIdentifier) ([]string, error) { - panic("implement me") -} - -func (m mockMetadataReport) GetServiceDefinition(*identifier.MetadataIdentifier) (string, error) { - panic("implement me") -} diff --git a/config/instance/registry_metadata_report.go b/config/instance/registry_metadata_report.go deleted file mode 100644 index cd89a69ed3..0000000000 --- a/config/instance/registry_metadata_report.go +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package instance - -import ( - "sync" -) - -import ( - "github.com/dubbogo/gost/log/logger" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/extension" - "dubbo.apache.org/dubbo-go/v3/metadata/report" -) - -var ( - regInstances = make(map[string]report.MetadataReport, 8) - mux sync.RWMutex -) - -// GetMetadataReportByRegistryProtocol obtain metadata report instance through registry config -func GetMetadataReportByRegistryProtocol(protocol string) report.MetadataReport { - mux.RLock() - defer mux.RUnlock() - if protocol == "" { - // return the first instance - for _, regInstance := range regInstances { - return regInstance - } - } - // find the accurate instance - regInstance, ok := regInstances[protocol] - if !ok { - return nil - } - return regInstance -} - -// SetMetadataReportInstanceByReg set metadata reporting instances by registering urls -func SetMetadataReportInstanceByReg(url *common.URL) { - mux.Lock() - defer mux.Unlock() - if _, ok := regInstances[url.Protocol]; ok { - return - } - - fac := extension.GetMetadataReportFactory(url.Protocol) - if fac != nil { - regInstances[url.Protocol] = fac.CreateMetadataReport(url) - } else { - logger.Infof("Metadata of type %v not registered.", url.Protocol) - } -} diff --git a/config/instance/registry_metadata_report_test.go b/config/instance/registry_metadata_report_test.go deleted file mode 100644 index a72844c85d..0000000000 --- a/config/instance/registry_metadata_report_test.go +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package instance - -import ( - "testing" -) - -import ( - "github.com/stretchr/testify/assert" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/extension" - "dubbo.apache.org/dubbo-go/v3/metadata/report" - "dubbo.apache.org/dubbo-go/v3/metadata/report/factory" -) - -func TestGetMetadataReportInstanceByReg(t *testing.T) { - t.Run("nil", func(t *testing.T) { - reportInstance := GetMetadataReportByRegistryProtocol("mock") - assert.Nil(t, reportInstance) - }) - - t.Run("normal", func(t *testing.T) { - u, _ := common.NewURL("mock://127.0.0.1") - extension.SetMetadataReportFactory("mock", func() factory.MetadataReportFactory { - return &mockMetadataReportFactory{} - }) - SetMetadataReportInstanceByReg(u) - reportInstance := GetMetadataReportByRegistryProtocol("mock") - assert.NotNil(t, reportInstance) - }) -} - -func TestSetMetadataReportInstanceByReg(t *testing.T) { - - t.Run("exist", func(t *testing.T) { - regInstances = make(map[string]report.MetadataReport, 8) - u, _ := common.NewURL("mock://127.0.0.1") - extension.SetMetadataReportFactory("mock", func() factory.MetadataReportFactory { - return &mockMetadataReportFactory{} - }) - SetMetadataReportInstanceByReg(u) - SetMetadataReportInstanceByReg(u) - assert.True(t, len(regInstances) == 1) - }) -} diff --git a/config/metadata_report_config.go b/config/metadata_config.go similarity index 61% rename from config/metadata_report_config.go rename to config/metadata_config.go index bde3732e7f..f25b597758 100644 --- a/config/metadata_report_config.go +++ b/config/metadata_config.go @@ -17,6 +17,11 @@ package config +import ( + "strconv" + "time" +) + import ( "github.com/dubbogo/gost/log/logger" @@ -26,8 +31,7 @@ import ( 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/instance" + "dubbo.apache.org/dubbo-go/v3/metadata" ) // MetadataReportConfig is app level configuration @@ -44,17 +48,116 @@ type MetadataReportConfig struct { metadataType string } +func initMetadata(rc *RootConfig) error { + opts := metadata.NewOptions( + metadata.WithAppName(rc.Application.Name), + metadata.WithMetadataType(rc.Application.MetadataType), + metadata.WithPort(getMetadataPort(rc)), + ) + if err := opts.Init(); err != nil { + return err + } + return nil +} + +func getMetadataPort(rc *RootConfig) int { + port := rc.Application.MetadataServicePort + if port == "" { + protocolConfig, ok := rootConfig.Protocols[constant.DefaultProtocol] + if ok { + port = protocolConfig.Port + } else { + logger.Warnf("[Metadata Service] Dubbo-go %s version's MetadataService only support dubbo protocol,"+ + "MetadataService will use random port", + constant.Version) + } + } + if port == "" { + return 0 + } + p, err := strconv.Atoi(port) + if err != nil { + logger.Error("MetadataService port parse error %v, MetadataService will use random port", err) + return 0 + } + return p +} + // Prefix dubbo.consumer -func (MetadataReportConfig) Prefix() string { +func (*MetadataReportConfig) Prefix() string { return constant.MetadataReportPrefix } +func (mc *MetadataReportConfig) toReportOptions() (*metadata.ReportOptions, error) { + opts := metadata.NewReportOptions( + metadata.WithRegistryId(constant.DefaultKey), + metadata.WithProtocol(mc.Protocol), + metadata.WithAddress(mc.Address), + metadata.WithUsername(mc.Username), + metadata.WithPassword(mc.Password), + metadata.WithGroup(mc.Group), + metadata.WithNamespace(mc.Namespace), + metadata.WithParams(mc.Params), + ) + if mc.Timeout != "" { + timeout, err := time.ParseDuration(mc.Timeout) + if err != nil { + return nil, err + } + metadata.WithTimeout(timeout)(opts) + } + return opts, nil +} + +func registryToReportOptions(id string, rc *RegistryConfig) (*metadata.ReportOptions, error) { + opts := metadata.NewReportOptions( + metadata.WithRegistryId(id), + metadata.WithProtocol(rc.Protocol), + metadata.WithAddress(rc.Address), + metadata.WithUsername(rc.Username), + metadata.WithPassword(rc.Password), + metadata.WithGroup(rc.Group), + metadata.WithNamespace(rc.Namespace), + metadata.WithParams(rc.Params), + ) + if rc.Timeout != "" { + timeout, err := time.ParseDuration(rc.Timeout) + if err != nil { + return nil, err + } + metadata.WithTimeout(timeout)(opts) + } + return opts, nil +} + func (mc *MetadataReportConfig) Init(rc *RootConfig) error { if mc == nil { return nil } mc.metadataType = rc.Application.MetadataType - return mc.StartMetadataReport() + if mc.Address != "" { + // if metadata report config is avaible, then init metadata report instance + opts, err := mc.toReportOptions() + if err != nil { + return err + } + return opts.Init() + } + if rc.Registries != nil && len(rc.Registries) > 0 { + // if metadata report config is not avaible, then init metadata report instance with registries + for id, reg := range rc.Registries { + if reg.UseAsMetaReport && isValid(reg.Address) { + opts, err := registryToReportOptions(id, reg) + if err != nil { + return err + } + if err = opts.Init(); err != nil { + return err + } + } + } + } + return nil } func (mc *MetadataReportConfig) ToUrl() (*common.URL, error) { @@ -79,63 +182,6 @@ func (mc *MetadataReportConfig) ToUrl() (*common.URL, error) { return res, nil } -func (mc *MetadataReportConfig) IsValid() bool { - return len(mc.Protocol) != 0 -} - -// StartMetadataReport The entry of metadata report start -func (mc *MetadataReportConfig) StartMetadataReport() error { - if mc == nil || !mc.IsValid() { - return nil - } - if tmpUrl, err := mc.ToUrl(); err == nil { - instance.SetMetadataReportInstance(tmpUrl) - return nil - } else { - return perrors.Wrap(err, "Start MetadataReport failed.") - } -} - -func publishServiceDefinition(url *common.URL) { - localService, err := extension.GetLocalMetadataService(constant.DefaultKey) - if err != nil { - logger.Warnf("get local metadata service failed, please check if you have imported _ \"dubbo.apache.org/dubbo-go/v3/metadata/service/local\"") - return - } - localService.PublishServiceDefinition(url) - if url.GetParam(constant.MetadataTypeKey, "") != constant.RemoteMetadataStorageType { - return - } - if remoteMetadataService, err := extension.GetRemoteMetadataService(); err == nil && remoteMetadataService != nil { - remoteMetadataService.PublishServiceDefinition(url) - } -} - -// selectMetadataServiceExportedURL get already be exported url -func selectMetadataServiceExportedURL() *common.URL { - var selectedUrl *common.URL - metaDataService, err := extension.GetLocalMetadataService(constant.DefaultKey) - if err != nil { - logger.Warnf("get metadata service exporter failed, pls check if you import _ \"dubbo.apache.org/dubbo-go/v3/metadata/service/local\"") - return nil - } - urlList, err := metaDataService.GetExportedURLs(constant.AnyValue, constant.AnyValue, constant.AnyValue, constant.AnyValue) - if err != nil { - panic(err) - } - if len(urlList) == 0 { - return nil - } - for _, url := range urlList { - selectedUrl = url - // rest first - if url.Protocol == "rest" { - break - } - } - return selectedUrl -} - type MetadataReportConfigBuilder struct { metadataReportConfig *MetadataReportConfig } diff --git a/config/metadata_report_config_test.go b/config/metadata_config_test.go similarity index 97% rename from config/metadata_report_config_test.go rename to config/metadata_config_test.go index 1b1a9ac809..1e7caf5ace 100644 --- a/config/metadata_report_config_test.go +++ b/config/metadata_config_test.go @@ -39,7 +39,6 @@ func TestNewMetadataReportConfigBuilder(t *testing.T) { SetGroup("dubbo"). Build() - assert.Equal(t, config.IsValid(), true) assert.Equal(t, config.Prefix(), constant.MetadataReportPrefix) url, err := config.ToUrl() diff --git a/config/provider_config_test.go b/config/provider_config_test.go index 8f954941b5..8fb7c0ec05 100644 --- a/config/provider_config_test.go +++ b/config/provider_config_test.go @@ -27,6 +27,7 @@ import ( import ( "dubbo.apache.org/dubbo-go/v3/common/constant" + _ "dubbo.apache.org/dubbo-go/v3/metadata/report/nacos" ) func TestProviderConfigEmptyRegistry(t *testing.T) { diff --git a/config/reference_config.go b/config/reference_config.go index 5cd98cc876..c801afef80 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -291,8 +291,6 @@ func (rc *ReferenceConfig) Refer(srv interface{}) { } } - // publish consumer's metadata - publishServiceDefinition(cfgURL) // create proxy if rc.Async { callback := GetCallback(rc.id) diff --git a/config/registry_config.go b/config/registry_config.go index 9eca5ea386..887bf706bb 100644 --- a/config/registry_config.go +++ b/config/registry_config.go @@ -35,7 +35,6 @@ 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/instance" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -93,13 +92,6 @@ func (c *RegistryConfig) getUrlMap(roleType common.RoleType) url.Values { func (c *RegistryConfig) startRegistryConfig() error { c.translateRegistryAddress() - if c.UseAsMetaReport && isValid(c.Address) { - if tmpUrl, err := c.toMetadataReportUrl(); err == nil { - instance.SetMetadataReportInstanceByReg(tmpUrl) - } else { - return perrors.Wrap(err, "Start RegistryConfig failed.") - } - } return verify(c) } @@ -237,6 +229,9 @@ func LoadRegistries(registryIds []string, registries map[string]*RegistryConfig, logger.Errorf("The registry id: %s url is invalid, error: %#v", k, err) panic(err) } else { + for _, u := range urls { + u.AddParam(constant.RegistryIdKey, k) + } registryURLs = append(registryURLs, urls...) } } diff --git a/config/root_config.go b/config/root_config.go index 8239b1e109..ca4a6bb4ca 100644 --- a/config/root_config.go +++ b/config/root_config.go @@ -27,23 +27,17 @@ import ( "github.com/dubbogo/gost/log/logger" "github.com/knadh/koanf" - - perrors "github.com/pkg/errors" - - "go.uber.org/atomic" ) 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_center" - "dubbo.apache.org/dubbo-go/v3/metadata/service/exporter" + "dubbo.apache.org/dubbo-go/v3/registry/exposed_tmp" ) var ( startOnce sync.Once - exporting = &atomic.Bool{} ) // RootConfig is the root config @@ -122,8 +116,6 @@ func (rc *RootConfig) getRegistryIds() []string { return removeDuplicateElement(ids) } func registerPOJO() { - hessian.RegisterPOJO(&common.MetadataInfo{}) - hessian.RegisterPOJO(&common.ServiceInfo{}) hessian.RegisterPOJO(&common.URL{}) } @@ -212,9 +204,12 @@ func (rc *RootConfig) Start() { gracefulShutdownInit() rc.Consumer.Load() rc.Provider.Load() - // todo if register consumer instance or has exported services - exportMetadataService() - registerServiceInstance() + if err := initMetadata(rc); err != nil { + panic(err) + } + if err := exposed_tmp.RegisterServiceInstance(); err != nil { + panic(err) + } }) } @@ -346,56 +341,6 @@ func (rb *RootConfigBuilder) Build() *RootConfig { return rb.rootConfig } -func exportMetadataService() { - ms, err := extension.GetLocalMetadataService(constant.DefaultKey) - if err != nil { - logger.Warnf("could not init metadata service", err) - return - } - - if !IsProvider() || exporting.Load() { - return - } - - // In theory, we can use sync.Once - // But sync.Once is not reentrant. - // Now the invocation chain is createRegistry -> tryInitMetadataService -> metadataServiceExporter.export - // -> createRegistry -> initMetadataService... - // So using sync.Once will result in dead lock - exporting.Store(true) - - expt := extension.GetMetadataServiceExporter(constant.DefaultKey, ms) - if expt == nil { - logger.Warnf("get metadata service exporter failed, pls check if you import _ \"dubbo.apache.org/dubbo-go/v3/metadata/service/exporter/configurable\"") - return - } - - err = expt.Export(nil) - if err != nil { - logger.Errorf("could not export the metadata service, err = %s", err.Error()) - return - } - - // report interface-app mapping - err = publishMapping(expt) - if err != nil { - logger.Errorf("Publish interface-application mapping failed, got error %#v", err) - } -} - -// OnEvent only handle ServiceConfigExportedEvent -func publishMapping(sc exporter.MetadataServiceExporter) error { - urls := sc.GetExportedURLs() - - for _, u := range urls { - err := extension.GetGlobalServiceNameMapping().Map(u) - if err != nil { - return perrors.WithMessage(err, "could not map the service: "+u.String()) - } - } - return nil -} - // Process receive changing listener's event, dynamic update config func (rc *RootConfig) Process(event *config_center.ConfigChangeEvent) { logger.Infof("CenterConfig process event:\n%+v", event) diff --git a/config/service_config.go b/config/service_config.go index 0454b6aca2..071a508cff 100644 --- a/config/service_config.go +++ b/config/service_config.go @@ -93,6 +93,7 @@ type ServiceConfig struct { exporters []protocol.Exporter metadataType string + rc *RootConfig } // Prefix returns dubbo.service.${InterfaceName}. @@ -101,6 +102,7 @@ func (s *ServiceConfig) Prefix() string { } func (s *ServiceConfig) Init(rc *RootConfig) error { + s.rc = rc if err := initProviderMethodConfig(s); err != nil { return err } @@ -283,6 +285,7 @@ func (s *ServiceConfig) Export() error { common.WithPort(port), common.WithParams(urlMap), common.WithParamsValue(constant.BeanNameKey, s.id), + common.WithParamsValue(constant.ApplicationTagKey, s.rc.Application.Tag), //common.WithParamsValue(constant.SslEnabledKey, strconv.FormatBool(config.GetSslEnabled())), common.WithMethods(strings.Split(methods, ",")), common.WithToken(s.Token), @@ -327,16 +330,6 @@ func (s *ServiceConfig) Export() error { s.exporters = append(s.exporters, exporter) } } else { - if ivkURL.GetParam(constant.InterfaceKey, "") == constant.MetadataServiceName { - ms, err := extension.GetLocalMetadataService("") - if err != nil { - logger.Warnf("export org.apache.dubbo.metadata.MetadataService failed beacause of %s ! pls check if you import _ \"dubbo.apache.org/dubbo-go/v3/metadata/service/local\"", err) - return nil - } - if err := ms.SetMetadataServiceURL(ivkURL); err != nil { - logger.Warnf("SetMetadataServiceURL error = %s", err) - } - } invoker = s.generatorInvoker(ivkURL, info) exporter := extension.GetProtocol(protocolwrapper.FILTER).Export(invoker) if exporter == nil { @@ -344,7 +337,6 @@ func (s *ServiceConfig) Export() error { } s.exporters = append(s.exporters, exporter) } - publishServiceDefinition(ivkURL) } s.exported.Store(true) return nil diff --git a/config/testdata/config/provider/registry_application.yaml b/config/testdata/config/provider/registry_application.yaml index d31583f4b8..f150c23182 100644 --- a/config/testdata/config/provider/registry_application.yaml +++ b/config/testdata/config/provider/registry_application.yaml @@ -2,7 +2,7 @@ dubbo: registries: nacos: timeout: 3s - address: naocs://127.0.0.1:8848 + address: nacos://127.0.0.1:8848 provider: registry-ids: nacos services: diff --git a/global/application_config.go b/global/application_config.go index 426378cafc..a49b3b2535 100644 --- a/global/application_config.go +++ b/global/application_config.go @@ -27,8 +27,9 @@ type ApplicationConfig struct { Owner string `default:"dubbo-go" yaml:"owner" json:"owner,omitempty" property:"owner"` Environment string `yaml:"environment" json:"environment,omitempty" property:"environment"` // the metadata type. remote or local - MetadataType string `default:"local" yaml:"metadata-type" json:"metadataType,omitempty" property:"metadataType"` - Tag string `yaml:"tag" json:"tag,omitempty" property:"tag"` + MetadataType string `default:"local" yaml:"metadata-type" json:"metadataType,omitempty" property:"metadataType"` + Tag string `yaml:"tag" json:"tag,omitempty" property:"tag"` + MetadataServicePort string `yaml:"metadata-service-port" json:"metadata-service-port,omitempty" property:"metadata-service-port"` } func DefaultApplicationConfig() *ApplicationConfig { diff --git a/global/metadata_report_config.go b/global/metadata_report_config.go index bac446e849..cdd1b320af 100644 --- a/global/metadata_report_config.go +++ b/global/metadata_report_config.go @@ -27,8 +27,6 @@ type MetadataReportConfig struct { Group string `yaml:"group" json:"group,omitempty"` Namespace string `yaml:"namespace" json:"namespace,omitempty"` Params map[string]string `yaml:"params" json:"parameters,omitempty"` - // metadataType of this application is defined by application config, local or remote - metadataType string } func DefaultMetadataReportConfig() *MetadataReportConfig { diff --git a/global/registry_config.go b/global/registry_config.go index 783dccd2b0..9adc5cc037 100644 --- a/global/registry_config.go +++ b/global/registry_config.go @@ -42,5 +42,5 @@ type RegistryConfig struct { } func DefaultRegistryConfig() *RegistryConfig { - return &RegistryConfig{} + return &RegistryConfig{UseAsMetaReport: true, UseAsConfigCenter: true, Timeout: "5s", TTL: "15m"} } diff --git a/go.mod b/go.mod index 630870e12c..4181b9c305 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,6 @@ require ( github.com/envoyproxy/go-control-plane v0.11.0 github.com/envoyproxy/protoc-gen-validate v0.10.0 // indirect github.com/fsnotify/fsnotify v1.6.0 - github.com/go-co-op/gocron v1.9.0 github.com/go-logr/logr v1.2.4 // indirect github.com/go-playground/validator/v10 v10.12.0 github.com/go-resty/resty/v2 v2.7.0 diff --git a/go.sum b/go.sum index 6e9903f3c0..0cf39f3912 100644 --- a/go.sum +++ b/go.sum @@ -603,8 +603,6 @@ github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JY github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-asn1-ber/asn1-ber v1.3.1/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0= -github.com/go-co-op/gocron v1.9.0 h1:+V+DDenw3ryB7B+tK1bAIC5p0ruw4oX9IqAsdRnGIf0= -github.com/go-co-op/gocron v1.9.0/go.mod h1:DbJm9kdgr1sEvWpHCA7dFFs/PGHPMil9/97EXCRPr4k= github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= @@ -1131,8 +1129,6 @@ github.com/rabbitmq/amqp091-go v1.1.0/go.mod h1:ogQDLSOACsLPsIq0NpbtiifNZi2YOz0V github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rhnvrm/simples3 v0.6.1/go.mod h1:Y+3vYm2V7Y4VijFoJHHTrja6OgPrJ2cBti8dPGkC3sA= -github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= -github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= diff --git a/graceful_shutdown/common.go b/graceful_shutdown/common.go index 82cc4ccf41..19bfc8d15b 100644 --- a/graceful_shutdown/common.go +++ b/graceful_shutdown/common.go @@ -32,8 +32,6 @@ import ( ) func init() { - hessian.RegisterPOJO(&common.MetadataInfo{}) - hessian.RegisterPOJO(&common.ServiceInfo{}) hessian.RegisterPOJO(&common.URL{}) } diff --git a/imports/imports.go b/imports/imports.go index bb8845e3cb..1a9fa18254 100644 --- a/imports/imports.go +++ b/imports/imports.go @@ -63,9 +63,6 @@ import ( _ "dubbo.apache.org/dubbo-go/v3/metadata/report/etcd" _ "dubbo.apache.org/dubbo-go/v3/metadata/report/nacos" _ "dubbo.apache.org/dubbo-go/v3/metadata/report/zookeeper" - _ "dubbo.apache.org/dubbo-go/v3/metadata/service/exporter/configurable" - _ "dubbo.apache.org/dubbo-go/v3/metadata/service/local" - _ "dubbo.apache.org/dubbo-go/v3/metadata/service/remote" _ "dubbo.apache.org/dubbo-go/v3/metrics/app_info" _ "dubbo.apache.org/dubbo-go/v3/metrics/prometheus" _ "dubbo.apache.org/dubbo-go/v3/otel/trace/jaeger" diff --git a/metadata/client.go b/metadata/client.go new file mode 100644 index 0000000000..6b9120e1ea --- /dev/null +++ b/metadata/client.go @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package metadata + +import ( + "context" + "encoding/json" + "time" +) + +import ( + "github.com/dubbogo/gost/log/logger" + + 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/metadata/info" + "dubbo.apache.org/dubbo-go/v3/registry" +) + +const metadataProxyDefaultTimeout = 5000 + +// GetMetadataFromMetadataReport test depends on dubbo protocol, if dubbo not dependent on config package, can move to metadata dir +func GetMetadataFromMetadataReport(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { + report := GetMetadataReport() + return report.GetAppMetadata(instance.GetServiceName(), revision) +} + +func GetMetadataFromRpc(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { + service, destroy, err := createRpcClient(instance) + if err != nil { + return nil, err + } + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(metadataProxyDefaultTimeout)) + defer cancel() + defer destroy() + return service.GetMetadataInfo(ctx, revision) +} + +type remoteMetadataService struct { + GetMetadataInfo func(context context.Context, revision string) (*info.MetadataInfo, error) `dubbo:"getMetadataInfo"` +} + +func createRpcClient(instance registry.ServiceInstance) (*remoteMetadataService, func(), error) { + params := getMetadataServiceUrlParams(instance.GetMetadata()[constant.MetadataServiceURLParamsPropertyName]) + url := buildMetadataServiceURL(instance.GetServiceName(), instance.GetHost(), params) + return createRpcClientByUrl(url) +} + +func createRpcClientByUrl(url *common.URL) (*remoteMetadataService, func(), error) { + rpcService := &remoteMetadataService{} + invoker := extension.GetProtocol(constant.Dubbo).Refer(url) + if invoker == nil { + return nil, nil, perrors.New("create invoker error, can not connect to the metadata report server: " + url.Ip + ":" + url.Port) + } + proxy := extension.GetProxyFactory(constant.DefaultKey).GetProxy(invoker, url) + proxy.Implement(rpcService) + destroy := func() { + invoker.Destroy() + } + return rpcService, destroy, nil +} + +// buildMetadataServiceURL will use standard format to build the metadata service url. +func buildMetadataServiceURL(serviceName string, host string, params map[string]string) *common.URL { + if params[constant.ProtocolKey] == "" { + return nil + } + convertedParams := make(map[string][]string, len(params)) + for k, v := range params { + convertedParams[k] = []string{v} + } + u := common.NewURLWithOptions(common.WithIp(host), + common.WithPath(constant.MetadataServiceName), + common.WithProtocol(params[constant.ProtocolKey]), + common.WithPort(params[constant.PortKey]), + common.WithParams(convertedParams), + common.WithParamsValue(constant.GroupKey, serviceName), + common.WithParamsValue(constant.InterfaceKey, constant.MetadataServiceName)) + + return u +} + +// getMetadataServiceUrlParams this will convert the metadata service url parameters to map structure +// it looks like: +// {"dubbo":{"timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"20880"}} +func getMetadataServiceUrlParams(jsonStr string) map[string]string { + res := make(map[string]string, 2) + if len(jsonStr) > 0 { + err := json.Unmarshal([]byte(jsonStr), &res) + if err != nil { + logger.Errorf("could not parse the metadata service url parameters to map", err) + } + } + return res +} diff --git a/metadata/definition/definition.go b/metadata/definition/definition.go deleted file mode 100644 index d4d5780033..0000000000 --- a/metadata/definition/definition.go +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package definition - -import ( - "bytes" - "encoding/json" - "fmt" - "sort" - "strings" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/constant" -) - -// ServiceDefiner is a interface of service's definition -type ServiceDefiner interface { - ToBytes() ([]byte, error) -} - -// ServiceDefinition is the describer of service definition -type ServiceDefinition struct { - CanonicalName string `json:"canonicalName"` - CodeSource string `json:"codeSource"` - Methods []MethodDefinition `json:"methods"` - Types []TypeDefinition `json:"types"` -} - -// ToBytes convert ServiceDefinition to json string -func (def *ServiceDefinition) ToBytes() ([]byte, error) { - return json.Marshal(def) -} - -// String will iterate all methods and parameters and convert them to json string -func (def *ServiceDefinition) String() string { - var methodStr strings.Builder - for _, m := range def.Methods { - var paramType strings.Builder - for _, p := range m.ParameterTypes { - paramType.WriteString(fmt.Sprintf("{type:%v}", p)) - } - var param strings.Builder - for _, d := range m.Parameters { - param.WriteString(fmt.Sprintf("{id:%v,type:%v,builderName:%v}", d.ID, d.Type, d.TypeBuilderName)) - } - methodStr.WriteString(fmt.Sprintf("{name:%v,parameterTypes:[%v],returnType:%v,params:[%v] }", m.Name, paramType.String(), m.ReturnType, param.String())) - } - var types strings.Builder - for _, d := range def.Types { - types.WriteString(fmt.Sprintf("{id:%v,type:%v,builderName:%v}", d.ID, d.Type, d.TypeBuilderName)) - } - return fmt.Sprintf("{canonicalName:%v, codeSource:%v, methods:[%v], types:[%v]}", def.CanonicalName, def.CodeSource, methodStr.String(), types.String()) -} - -// FullServiceDefinition is the describer of service definition with parameters -type FullServiceDefinition struct { - Parameters map[string]string `json:"parameters"` - ServiceDefinition -} - -// MethodDefinition is the describer of method definition -type MethodDefinition struct { - Name string `json:"name"` - ParameterTypes []string `json:"parameterTypes"` - ReturnType string `json:"returnType"` - Parameters []TypeDefinition `json:"parameters"` -} - -// TypeDefinition is the describer of type definition -type TypeDefinition struct { - ID string `json:"id"` - Type string `json:"type"` - Items []TypeDefinition `json:"items"` - Enums []string `json:"enums"` - Properties map[string]TypeDefinition `json:"properties"` - TypeBuilderName string `json:"typeBuilderName"` -} - -// BuildServiceDefinition can build service definition which will be used to describe a service -func BuildServiceDefinition(service common.Service, url *common.URL) *ServiceDefinition { - sd := &ServiceDefinition{} - sd.CanonicalName = url.Service() - - for k, m := range service.Method() { - var paramTypes []string - if len(m.ArgsType()) > 0 { - for _, t := range m.ArgsType() { - paramTypes = append(paramTypes, t.Kind().String()) - } - } - - var returnType string - if m.ReplyType() != nil { - returnType = m.ReplyType().Kind().String() - } - - methodD := MethodDefinition{ - Name: k, - ParameterTypes: paramTypes, - ReturnType: returnType, - } - sd.Methods = append(sd.Methods, methodD) - } - - return sd -} - -// BuildFullDefinition can build service definition with full url parameters -func BuildFullDefinition(service common.Service, url *common.URL) *FullServiceDefinition { - fsd := &FullServiceDefinition{} - sd := BuildServiceDefinition(service, url) - fsd.ServiceDefinition = *sd - fsd.Parameters = make(map[string]string) - for k, v := range url.GetParams() { - fsd.Parameters[k] = strings.Join(v, ",") - } - return fsd -} - -// ToBytes convert ServiceDefinition to json string -func (def *FullServiceDefinition) ToBytes() ([]byte, error) { - return json.Marshal(def) -} - -// String will iterate all methods and parameters and convert them to json string -func (def *FullServiceDefinition) String() string { - var methodStr strings.Builder - for _, m := range def.Methods { - var paramType strings.Builder - for _, p := range m.ParameterTypes { - paramType.WriteString(fmt.Sprintf("{type:%v}", p)) - } - var param strings.Builder - for _, d := range m.Parameters { - param.WriteString(fmt.Sprintf("{id:%v,type:%v,builderName:%v}", d.ID, d.Type, d.TypeBuilderName)) - } - methodStr.WriteString(fmt.Sprintf("{name:%v,parameterTypes:[%v],returnType:%v,params:[%v] }", m.Name, paramType.String(), m.ReturnType, param.String())) - } - var types strings.Builder - for _, d := range def.Types { - types.WriteString(fmt.Sprintf("{id:%v,type:%v,builderName:%v}", d.ID, d.Type, d.TypeBuilderName)) - } - - sortSlice := make([]string, 0) - var parameters strings.Builder - for k := range def.Parameters { - sortSlice = append(sortSlice, k) - } - sort.Slice(sortSlice, func(i, j int) bool { return sortSlice[i] < sortSlice[j] }) - for _, k := range sortSlice { - parameters.WriteString(fmt.Sprintf("%v:%v,", k, def.Parameters[k])) - } - - return fmt.Sprintf("{parameters:{%v}, canonicalName:%v, codeSource:%v, methods:[%v], types:[%v]}", - strings.TrimRight(parameters.String(), ","), def.CanonicalName, def.CodeSource, methodStr.String(), types.String()) -} - -// ServiceDescriperBuild builds the service key, format is `group/serviceName:version` which be same as URL's service key -func ServiceDescriperBuild(serviceName string, group string, version string) string { - buf := &bytes.Buffer{} - if group != "" { - buf.WriteString(group) - buf.WriteString(constant.PathSeparator) - } - buf.WriteString(serviceName) - if version != "" && version != "0.0.0" { - buf.WriteString(constant.KeySeparator) - buf.WriteString(version) - } - return buf.String() -} diff --git a/metadata/definition/definition_test.go b/metadata/definition/definition_test.go deleted file mode 100644 index 7515b09cf8..0000000000 --- a/metadata/definition/definition_test.go +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package definition - -import ( - "fmt" - "testing" -) - -import ( - "github.com/stretchr/testify/assert" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" -) - -func TestBuildServiceDefinition(t *testing.T) { - serviceName := "com.ikurento.user.UserProvider" - group := "group1" - version := "0.0.1" - protocol := "dubbo" - beanName := "UserProvider" - url, err := common.NewURL(fmt.Sprintf( - "%v://127.0.0.1:20000/com.ikurento.user.UserProvider1?anyhost=true&"+ - "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ - "environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+ - "owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000×tamp=1556509797245&group=%v&version=%v&bean.name=%v", - protocol, serviceName, group, version, beanName)) - assert.NoError(t, err) - _, err = common.ServiceMap.Register(serviceName, protocol, group, version, &UserProvider{}) - assert.NoError(t, err) - service := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey()) - sd := BuildServiceDefinition(*service, url) - assert.Equal(t, "{canonicalName:com.ikurento.user.UserProvider, codeSource:, methods:[{name:GetUser,parameterTypes:[{type:slice}],returnType:ptr,params:[] }], types:[]}", sd.String()) - fsd := BuildFullDefinition(*service, url) - assert.Equal(t, "{parameters:{anyhost:true,application:BDTService,bean.name:UserProvider,category:providers,default.timeout:10000,dubbo:dubbo-provider-golang-1.0.0,environment:dev,group:group1,interface:com.ikurento.user.UserProvider,ip:192.168.56.1,methods:GetUser,module:dubbogo user-info server,org:ikurento.com,owner:ZX,pid:1447,revision:0.0.1,side:provider,timeout:3000,timestamp:1556509797245,version:0.0.1}, canonicalName:com.ikurento.user.UserProvider, codeSource:, methods:[{name:GetUser,parameterTypes:[{type:slice}],returnType:ptr,params:[] }], types:[]}", fsd.String()) -} diff --git a/metadata/definition/mock.go b/metadata/definition/mock.go deleted file mode 100644 index 65cef4ef51..0000000000 --- a/metadata/definition/mock.go +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package definition - -import ( - "context" - "time" -) - -type User struct { - ID string - Name string - Age int32 - Time time.Time -} - -type UserProvider struct{} - -func (u *UserProvider) GetUser(ctx context.Context, req []interface{}) (*User, error) { - rsp := User{"A001", "Alex Stocks", 18, time.Now()} - return &rsp, nil -} - -func (u *UserProvider) Reference() string { - return "UserProvider" -} - -func (u User) JavaClassName() string { - return "com.ikurento.user.User" -} diff --git a/metadata/identifier/base_metadata_identifier.go b/metadata/identifier/base_metadata_identifier.go deleted file mode 100644 index 7df3e6c3af..0000000000 --- a/metadata/identifier/base_metadata_identifier.go +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package identifier - -import ( - "net/url" -) - -import ( - "github.com/dubbogo/gost/log/logger" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common/constant" -) - -// IMetadataIdentifier is the interface which describes the Metadata base identify -type IMetadataIdentifier interface { - GetFilePathKey() string - GetIdentifierKey() string -} - -// BaseMetadataIdentifier is the base implement of BaseMetadataIdentifier interface -type BaseMetadataIdentifier struct { - ServiceInterface string - Version string - Group string - Side string -} - -// joinParams will join the specified char in slice, and build as string -func joinParams(joinChar string, params []string) string { - var joinedStr string - for _, param := range params { - if param == "" { - logger.Info("[Metadata report] Break loop in `joinParams` to avoid invalid path when meeting empty param") - break - } - joinedStr += joinChar - joinedStr += param - } - return joinedStr -} - -// getIdentifierKey returns string that format is service:Version:Group:Side:param1:param2... -func (mdi *BaseMetadataIdentifier) getIdentifierKey(params ...string) string { - return mdi.ServiceInterface + - constant.KeySeparator + mdi.Version + - constant.KeySeparator + mdi.Group + - constant.KeySeparator + mdi.Side + - joinParams(constant.KeySeparator, params) -} - -// getFilePathKey returns string that format is metadata/path/Version/Group/Side/param1/param2... -func (mdi *BaseMetadataIdentifier) getFilePathKey(params ...string) string { - path := serviceToPath(mdi.ServiceInterface) - - return constant.DefaultPathTag + - withPathSeparator(path) + - withPathSeparator(mdi.Version) + - withPathSeparator(mdi.Group) + - withPathSeparator(mdi.Side) + - joinParams(constant.PathSeparator, params) -} - -// serviceToPath uss URL encode to decode the @serviceInterface -func serviceToPath(serviceInterface string) string { - if serviceInterface == constant.AnyValue { - return "" - } - return url.PathEscape(serviceInterface) -} - -// withPathSeparator return "/" + @path -func withPathSeparator(path string) string { - if len(path) != 0 { - path = constant.PathSeparator + path - } - return path -} - -// BaseApplicationMetadataIdentifier is the base implement of BaseApplicationMetadataIdentifier interface -type BaseApplicationMetadataIdentifier struct { - Application string - Group string -} - -// getIdentifierKey returns string that format is application/param -func (madi *BaseApplicationMetadataIdentifier) getIdentifierKey(params ...string) string { - return madi.Application + joinParams(constant.KeySeparator, params) -} - -// getFilePathKey returns string that format is metadata/application/revision -func (madi *BaseApplicationMetadataIdentifier) getFilePathKey(params ...string) string { - return constant.DefaultPathTag + - withPathSeparator(madi.Application) + - joinParams(constant.PathSeparator, params) -} diff --git a/metadata/identifier/base_metadata_identifier_test.go b/metadata/identifier/base_metadata_identifier_test.go deleted file mode 100644 index 8baeed4dfd..0000000000 --- a/metadata/identifier/base_metadata_identifier_test.go +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package identifier - -import ( - "testing" -) - -import ( - "github.com/stretchr/testify/assert" -) - -var baseId = &BaseMetadataIdentifier{ - ServiceInterface: "org.apache.pkg.mockService", - Version: "1.0.0", - Group: "Group", - Side: "provider", -} - -func TestBaseGetFilePathKey(t *testing.T) { - assert.Equal(t, "metadata/org.apache.pkg.mockService/1.0.0/Group/provider/a/b/c", baseId.getFilePathKey("a", "b", "c")) -} - -func TestBaseGetIdentifierKey(t *testing.T) { - assert.Equal(t, "org.apache.pkg.mockService:1.0.0:Group:provider:a:b:c", baseId.getIdentifierKey("a", "b", "c")) -} diff --git a/metadata/identifier/metadata_identifier.go b/metadata/identifier/metadata_identifier.go deleted file mode 100644 index 7e50c4c6b9..0000000000 --- a/metadata/identifier/metadata_identifier.go +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package identifier - -// MetadataIdentifier is inherit baseMetaIdentifier with Application name -type MetadataIdentifier struct { - Application string - BaseMetadataIdentifier -} - -// GetIdentifierKey returns string that format is service:Version:Group:Side:Application -func (mdi *MetadataIdentifier) GetIdentifierKey() string { - return mdi.BaseMetadataIdentifier.getIdentifierKey(mdi.Application) -} - -// GetFilePathKey returns string that format is metadata/path/Version/Group/Side/Application -func (mdi *MetadataIdentifier) GetFilePathKey() string { - return mdi.BaseMetadataIdentifier.getFilePathKey(mdi.Application) -} diff --git a/metadata/identifier/metadata_identifier_test.go b/metadata/identifier/metadata_identifier_test.go deleted file mode 100644 index 2b4d89fe2a..0000000000 --- a/metadata/identifier/metadata_identifier_test.go +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package identifier - -import ( - "testing" -) - -import ( - "github.com/stretchr/testify/assert" -) - -var metadataId = &MetadataIdentifier{ - Application: "app", - BaseMetadataIdentifier: BaseMetadataIdentifier{ - ServiceInterface: "org.apache.pkg.mockService", - Version: "1.0.0", - Group: "Group", - Side: "provider", - }, -} - -func TestGetFilePathKey(t *testing.T) { - assert.Equal(t, "metadata/org.apache.pkg.mockService/1.0.0/Group/provider/app", metadataId.GetFilePathKey()) -} - -func TestGetIdentifierKey(t *testing.T) { - assert.Equal(t, "org.apache.pkg.mockService:1.0.0:Group:provider:app", metadataId.GetIdentifierKey()) -} diff --git a/metadata/identifier/service_metadata_identifier.go b/metadata/identifier/service_metadata_identifier.go deleted file mode 100644 index 7ef6ff2265..0000000000 --- a/metadata/identifier/service_metadata_identifier.go +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package identifier - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/constant" -) - -// ServiceMetadataIdentifier is inherit baseMetaIdentifier with service params: Revision and Protocol -type ServiceMetadataIdentifier struct { - Revision string - Protocol string - BaseMetadataIdentifier -} - -// NewServiceMetadataIdentifier create instance. The ServiceInterface is the @url.Service() -// other parameters are read from @url -func NewServiceMetadataIdentifier(url *common.URL) *ServiceMetadataIdentifier { - return &ServiceMetadataIdentifier{ - BaseMetadataIdentifier: BaseMetadataIdentifier{ - ServiceInterface: url.Service(), - Version: url.GetParam(constant.VersionKey, ""), - Group: url.GetParam(constant.GroupKey, ""), - Side: url.GetParam(constant.SideKey, ""), - }, - Protocol: url.Protocol, - } -} - -// GetIdentifierKey returns string that format is service:Version:Group:Side:Protocol:"revision"+Revision -func (mdi *ServiceMetadataIdentifier) GetIdentifierKey() string { - return mdi.BaseMetadataIdentifier.getIdentifierKey(mdi.Protocol, constant.KeyRevisionPrefix+mdi.Revision) -} - -// GetFilePathKey returns string that format is metadata/path/Version/Group/Side/Protocol/"revision"+Revision -func (mdi *ServiceMetadataIdentifier) GetFilePathKey() string { - return mdi.BaseMetadataIdentifier.getFilePathKey(mdi.Protocol, constant.KeyRevisionPrefix+mdi.Revision) -} diff --git a/metadata/identifier/service_metadata_identifier_test.go b/metadata/identifier/service_metadata_identifier_test.go deleted file mode 100644 index ccf4ea1f26..0000000000 --- a/metadata/identifier/service_metadata_identifier_test.go +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package identifier - -import ( - "testing" -) - -import ( - "github.com/stretchr/testify/assert" -) - -var serviceMetadataId = &ServiceMetadataIdentifier{ - Revision: "1.0", - Protocol: "dubbo", - BaseMetadataIdentifier: BaseMetadataIdentifier{ - ServiceInterface: "org.apache.pkg.mockService", - Version: "1.0.0", - Group: "Group", - Side: "provider", - }, -} - -func TestServiceGetFilePathKey(t *testing.T) { - assert.Equal(t, "metadata/org.apache.pkg.mockService/1.0.0/Group/provider/dubbo/revision1.0", serviceMetadataId.GetFilePathKey()) -} - -func TestServiceGetIdentifierKey(t *testing.T) { - assert.Equal(t, "org.apache.pkg.mockService:1.0.0:Group:provider:dubbo:revision1.0", serviceMetadataId.GetIdentifierKey()) -} diff --git a/metadata/identifier/subscribe_metadata_identifier.go b/metadata/identifier/subscribe_metadata_identifier.go deleted file mode 100644 index 1e0c5e3b5a..0000000000 --- a/metadata/identifier/subscribe_metadata_identifier.go +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package identifier - -import ( - "dubbo.apache.org/dubbo-go/v3/common/constant" -) - -// SubscriberMetadataIdentifier is inherit baseMetaIdentifier with service params: Revision -type SubscriberMetadataIdentifier struct { - Revision string - BaseApplicationMetadataIdentifier -} - -func NewSubscriberMetadataIdentifier(application string, revision string) *SubscriberMetadataIdentifier { - return &SubscriberMetadataIdentifier{ - Revision: revision, - BaseApplicationMetadataIdentifier: BaseApplicationMetadataIdentifier{ - Application: application, - Group: constant.Dubbo, - }, - } -} - -// GetIdentifierKey returns string that format is service:Version:Group:Side:Revision -func (mdi *SubscriberMetadataIdentifier) GetIdentifierKey() string { - return mdi.BaseApplicationMetadataIdentifier.getIdentifierKey(mdi.Revision) -} - -// GetFilePathKey returns string that format is metadata/path/Version/Group/Side/Revision -func (mdi *SubscriberMetadataIdentifier) GetFilePathKey() string { - return mdi.BaseApplicationMetadataIdentifier.getFilePathKey(mdi.Revision) -} diff --git a/metadata/identifier/subscribe_metadata_identifier_test.go b/metadata/identifier/subscribe_metadata_identifier_test.go deleted file mode 100644 index 58bbef2642..0000000000 --- a/metadata/identifier/subscribe_metadata_identifier_test.go +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package identifier - -import ( - "testing" -) - -import ( - "github.com/stretchr/testify/assert" -) - -var subscribeMetadataId = &SubscriberMetadataIdentifier{ - Revision: "1.0", - BaseApplicationMetadataIdentifier: BaseApplicationMetadataIdentifier{ - Application: "foo", - }, -} - -func TestSubscribeGetFilePathKey(t *testing.T) { - t.Logf(subscribeMetadataId.GetFilePathKey()) - assert.Equal(t, "metadata/foo/1.0", subscribeMetadataId.GetFilePathKey()) -} - -func TestSubscribeGetIdentifierKey(t *testing.T) { - assert.Equal(t, "foo:1.0", subscribeMetadataId.GetIdentifierKey()) -} diff --git a/metadata/info/metadata_info.go b/metadata/info/metadata_info.go new file mode 100644 index 0000000000..471a97d81e --- /dev/null +++ b/metadata/info/metadata_info.go @@ -0,0 +1,298 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package info + +import ( + "fmt" + "hash/crc32" + "net/url" + "sort" + "strings" +) + +import ( + hessian "github.com/apache/dubbo-go-hessian2" + + gxset "github.com/dubbogo/gost/container/set" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common" + "dubbo.apache.org/dubbo-go/v3/common/constant" +) + +func init() { + hessian.RegisterPOJO(&MetadataInfo{}) + hessian.RegisterPOJO(&ServiceInfo{}) +} + +var IncludeKeys = gxset.NewSet( + constant.ApplicationKey, + constant.GroupKey, + constant.TimestampKey, + constant.SerializationKey, + constant.ClusterKey, + constant.LoadbalanceKey, + constant.PathKey, + constant.TimeoutKey, + constant.TokenKey, + constant.VersionKey, + constant.WarmupKey, + constant.WeightKey, + constant.ReleaseKey) + +// MetadataInfo the metadata information of instance +type MetadataInfo struct { + App string `json:"app,omitempty" hessian:"app"` + Revision string `json:"revision,omitempty" hessian:"revision"` + Tag string + Services map[string]*ServiceInfo `json:"services,omitempty" hessian:"services"` + exportedServiceURLs map[string][]*common.URL `hessian:"-"` // server exported service urls + subscribedServiceURLs map[string][]*common.URL `hessian:"-"` // client subscribed service urls +} + +func NewMetadataInfo(app, tag string) *MetadataInfo { + return NewMetadataInfoWithParams(app, "", make(map[string]*ServiceInfo)) +} + +func NewMetadataInfoWithParams(app string, revision string, services map[string]*ServiceInfo) *MetadataInfo { + return &MetadataInfo{ + App: app, + Revision: revision, + Services: services, + exportedServiceURLs: make(map[string][]*common.URL), + subscribedServiceURLs: make(map[string][]*common.URL), + } +} + +func (info *MetadataInfo) JavaClassName() string { + return "org.apache.dubbo.metadata.MetadataInfo" +} + +// CalAndGetRevision is different from Dubbo because golang doesn't support overload +// so that we should use interface + method name as identifier and ignore the method params +// in my opinion, it's enough because Dubbo actually ignore the URL params. +// please refer org.apache.dubbo.common.URL#toParameterString(java.lang.String...) +func (info *MetadataInfo) CalAndGetRevision() string { + if info.Revision != "" { + return info.Revision + } + if len(info.Services) == 0 { + return "0" + } + candidates := make([]string, 0, 8) + + for _, s := range info.Services { + iface := s.URL.GetParam(constant.InterfaceKey, "") + ms := s.URL.Methods + if len(ms) == 0 { + candidates = append(candidates, iface) + } else { + for _, m := range ms { + // methods are part of candidates + candidates = append(candidates, iface+constant.KeySeparator+m) + } + } + + // append URL params if we need it + } + sort.Strings(candidates) + + // it's nearly impossible to be overflow + res := uint64(0) + for _, c := range candidates { + res += uint64(crc32.ChecksumIEEE([]byte(c))) + } + info.Revision = fmt.Sprint(res) + return info.Revision + +} + +// AddService add provider service info to MetadataInfo +func (info *MetadataInfo) AddService(url *common.URL) { + service := NewServiceInfoWithURL(url) + info.Services[service.GetMatchKey()] = service + addUrl(info.exportedServiceURLs, url) + if info.App == "" { + info.App = url.GetParam(constant.ApplicationKey, "") + } +} + +func addUrl(m map[string][]*common.URL, url *common.URL) { + if _, ok := m[url.ServiceKey()]; !ok { + m[url.ServiceKey()] = make([]*common.URL, 0) + } + m[url.ServiceKey()] = append(m[url.ServiceKey()], url) +} + +func removeUrl(m map[string][]*common.URL, url *common.URL) { + if urls, ok := m[url.ServiceKey()]; ok { + for i, u := range urls { + if u == url { + m[url.ServiceKey()] = deleteItem(urls, i) + break + } + } + if len(m[url.ServiceKey()]) == 0 { + delete(m, url.ServiceKey()) + } + } +} + +func deleteItem(slice []*common.URL, index int) []*common.URL { + copy(slice[index:], slice[index+1:]) + slice = slice[:len(slice)-1] + return slice +} + +func (info *MetadataInfo) RemoveService(url *common.URL) { + service := NewServiceInfoWithURL(url) + delete(info.Services, service.GetMatchKey()) + removeUrl(info.exportedServiceURLs, url) +} + +// AddSubscribeURL client subscribe a service url +func (info *MetadataInfo) AddSubscribeURL(url *common.URL) { + addUrl(info.subscribedServiceURLs, url) +} + +// RemoveSubscribeURL client unsubscribe a service url +func (info *MetadataInfo) RemoveSubscribeURL(url *common.URL) { + removeUrl(info.subscribedServiceURLs, url) +} + +func (info *MetadataInfo) GetExportedServiceURLs() []*common.URL { + res := make([]*common.URL, 0) + for _, urls := range info.exportedServiceURLs { + res = append(res, urls...) + } + return res +} + +func (info *MetadataInfo) GetSubscribedURLs() []*common.URL { + res := make([]*common.URL, 0) + for _, urls := range info.subscribedServiceURLs { + res = append(res, urls...) + } + return res +} + +// ServiceInfo the information of service +type ServiceInfo struct { + Name string `json:"name,omitempty" hessian:"name"` + Group string `json:"group,omitempty" hessian:"group"` + Version string `json:"version,omitempty" hessian:"version"` + Protocol string `json:"protocol,omitempty" hessian:"protocol"` + Port int `json:"port,omitempty" hessian:"port"` + Path string `json:"path,omitempty" hessian:"path"` + Params map[string]string `json:"params,omitempty" hessian:"params"` + + ServiceKey string `json:"-" hessian:"-"` + MatchKey string `json:"-" hessian:"-"` + URL *common.URL `json:"-" hessian:"-"` +} + +// nolint +func NewServiceInfoWithURL(url *common.URL) *ServiceInfo { + service := NewServiceInfo(url.Service(), url.Group(), url.Version(), url.Protocol, url.Path, nil) + service.URL = url + // TODO includeKeys load dynamic + p := make(map[string]string, 8) + for _, keyInter := range IncludeKeys.Values() { + key := keyInter.(string) + value := url.GetParam(key, "") + if len(value) != 0 { + p[key] = value + } + for _, method := range url.Methods { + value = url.GetMethodParam(method, key, "") + if len(value) != 0 { + p[method+"."+key] = value + } + } + } + p[constant.MethodsKey] = strings.Join(url.Methods, ",") + service.Params = p + return service +} + +// nolint +func NewServiceInfo(name, group, version, protocol, path string, params map[string]string) *ServiceInfo { + serviceKey := common.ServiceKey(name, group, version) + matchKey := common.MatchKey(serviceKey, protocol) + return &ServiceInfo{ + Name: name, + Group: group, + Version: version, + Protocol: protocol, + Path: strings.TrimPrefix(path, "/"), + Params: params, + ServiceKey: serviceKey, + MatchKey: matchKey, + } +} + +func (si *ServiceInfo) JavaClassName() string { + return "org.apache.dubbo.metadata.MetadataInfo$ServiceInfo" +} + +func (si *ServiceInfo) GetMethods() []string { + s := si.Params[constant.MethodsKey] + return strings.Split(s, ",") +} + +// nolint +func (si *ServiceInfo) GetParams() url.Values { + v := url.Values{} + methodNames := si.Params[constant.MethodsKey] + if len(methodNames) == 0 { + return v + } + methods := gxset.NewSet() + for _, method := range strings.Split(si.Params[constant.MethodsKey], ",") { + methods.Add(method) + } + for k, p := range si.Params { + ms := strings.Index(k, ".") + if ms > 0 && methods.Contains(k[0:ms]) { + v.Set("methods."+k, p) + } else { + v.Set(k, p) + } + } + return v +} + +// nolint +func (si *ServiceInfo) GetMatchKey() string { + if si.MatchKey != "" { + return si.MatchKey + } + serviceKey := si.GetServiceKey() + si.MatchKey = common.MatchKey(serviceKey, si.Protocol) + return si.MatchKey +} + +// nolint +func (si *ServiceInfo) GetServiceKey() string { + if si.ServiceKey != "" { + return si.ServiceKey + } + si.ServiceKey = common.ServiceKey(si.Name, si.Group, si.Version) + return si.ServiceKey +} diff --git a/metadata/info/metadata_info_test.go b/metadata/info/metadata_info_test.go new file mode 100644 index 0000000000..41889bfdc6 --- /dev/null +++ b/metadata/info/metadata_info_test.go @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package info + +import ( + "encoding/json" + "testing" +) + +import ( + hessian "github.com/apache/dubbo-go-hessian2" + + "github.com/stretchr/testify/assert" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common" +) + +func TestMetadataInfoAddService(t *testing.T) { + metadataInfo := &MetadataInfo{ + Services: make(map[string]*ServiceInfo), + exportedServiceURLs: make(map[string][]*common.URL), + subscribedServiceURLs: make(map[string][]*common.URL), + } + + url, _ := common.NewURL("dubbo://127.0.0.1:20000?application=foo&category=providers&check=false&dubbo=dubbo-go+v1.5.0&interface=com.foo.Bar&methods=GetPetByID%2CGetPetTypes&organization=Apache&owner=foo&revision=1.0.0&side=provider&version=1.0.0") + metadataInfo.AddService(url) + assert.True(t, len(metadataInfo.Services) > 0) + assert.True(t, len(metadataInfo.exportedServiceURLs) > 0) + + metadataInfo.RemoveService(url) + assert.True(t, len(metadataInfo.Services) == 0) + assert.True(t, len(metadataInfo.exportedServiceURLs) == 0) +} + +func TestHessian(t *testing.T) { + metadataInfo := &MetadataInfo{ + App: "test", + Revision: "1", + Services: make(map[string]*ServiceInfo), + exportedServiceURLs: make(map[string][]*common.URL), + subscribedServiceURLs: make(map[string][]*common.URL), + } + metadataInfo.Services["1"] = NewServiceInfo("dubbo.io", "default", "1.0.0", "dubbo", "", make(map[string]string)) + e := hessian.NewEncoder() + err := e.Encode(metadataInfo) + assert.Nil(t, err) + obj, err := hessian.NewDecoder(e.Buffer()).Decode() + assert.Nil(t, err) + objJson, _ := json.Marshal(obj) + metaJson, _ := json.Marshal(metadataInfo) + assert.Equal(t, objJson, metaJson) +} diff --git a/metadata/mapping/memory/service_name_mapping.go b/metadata/mapping/memory/service_name_mapping.go deleted file mode 100644 index 9d7a72010d..0000000000 --- a/metadata/mapping/memory/service_name_mapping.go +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package memory - -import ( - "sync" -) - -import ( - gxset "github.com/dubbogo/gost/container/set" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/extension" - "dubbo.apache.org/dubbo-go/v3/config" - "dubbo.apache.org/dubbo-go/v3/metadata/mapping" - "dubbo.apache.org/dubbo-go/v3/registry" -) - -func init() { - extension.SetGlobalServiceNameMapping(GetNameMappingInstance) -} - -type InMemoryServiceNameMapping struct{} - -func (i *InMemoryServiceNameMapping) Map(url *common.URL) error { - return nil -} - -func (i *InMemoryServiceNameMapping) Get(url *common.URL, listener registry.MappingListener) (*gxset.HashSet, error) { - return gxset.NewSet(config.GetApplicationConfig().Name), nil -} - -func (i *InMemoryServiceNameMapping) Remove(url *common.URL) error { - return nil -} - -var ( - serviceNameMappingInstance *InMemoryServiceNameMapping - serviceNameMappingOnce sync.Once -) - -func GetNameMappingInstance() mapping.ServiceNameMapping { - serviceNameMappingOnce.Do(func() { - serviceNameMappingInstance = &InMemoryServiceNameMapping{} - }) - return serviceNameMappingInstance -} diff --git a/metadata/mapping/metadata/service_name_mapping.go b/metadata/mapping/metadata/service_name_mapping.go index 2580733499..95e588ef91 100644 --- a/metadata/mapping/metadata/service_name_mapping.go +++ b/metadata/mapping/metadata/service_name_mapping.go @@ -32,90 +32,77 @@ 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/config/instance" + "dubbo.apache.org/dubbo-go/v3/metadata" "dubbo.apache.org/dubbo-go/v3/metadata/mapping" - "dubbo.apache.org/dubbo-go/v3/metadata/report" - "dubbo.apache.org/dubbo-go/v3/registry" ) const ( DefaultGroup = "mapping" - slash = "/" + retryTimes = 10 ) func init() { extension.SetGlobalServiceNameMapping(GetNameMappingInstance) } -// MetadataServiceNameMapping is the implementation based on metadata report +var ( + serviceNameMappingInstance *ServiceNameMapping + serviceNameMappingOnce sync.Once +) + +// GetNameMappingInstance return an instance, if not found, it creates one +func GetNameMappingInstance() mapping.ServiceNameMapping { + serviceNameMappingOnce.Do(func() { + serviceNameMappingInstance = &ServiceNameMapping{} + }) + return serviceNameMappingInstance +} + +// ServiceNameMapping is the implementation based on metadata report // it's a singleton -type MetadataServiceNameMapping struct { +type ServiceNameMapping struct { } // Map will map the service to this application-level service -func (d *MetadataServiceNameMapping) Map(url *common.URL) error { +func (d *ServiceNameMapping) Map(url *common.URL) error { serviceInterface := url.GetParam(constant.InterfaceKey, "") - // metadata service is admin service, should not be mapped - if constant.MetadataServiceName == serviceInterface { - logger.Debug("try to map the metadata service, will be ignored") - return nil + appName := url.GetParam(constant.ApplicationKey, "") + // url is the service url,not the registry url,this url has no registry id info,can not get where to write mapping,so write all + // if the mapping can hold a report instance, it can write once + metadataReports := metadata.GetMetadataReports() + if len(metadataReports) == 0 { + return perrors.New("can not registering mapping to remote cause no metadata report instance found") } - - appName := config.GetApplicationConfig().Name - - metadataReport := getMetaDataReport(url.GetParam(constant.RegistryKey, "")) - if metadataReport == nil { - logger.Info("get metadata report instance is nil, metadata service will be enabled!") - } else { - err := metadataReport.RegisterServiceAppMapping(serviceInterface, DefaultGroup, appName) + for _, metadataReport := range metadataReports { + var err error + for i := 0; i < retryTimes; i++ { + if err = metadataReport.RegisterServiceAppMapping(serviceInterface, DefaultGroup, appName); err == nil { + break + } + } if err != nil { - return perrors.WithStack(err) + logger.Errorf("Failed registering mapping to remote, &v", err) + return err } } return nil } // Get will return the application-level services. If not found, the empty set will be returned. -func (d *MetadataServiceNameMapping) Get(url *common.URL, listener registry.MappingListener) (*gxset.HashSet, error) { +func (d *ServiceNameMapping) Get(url *common.URL, listener mapping.MappingListener) (*gxset.HashSet, error) { serviceInterface := url.GetParam(constant.InterfaceKey, "") - metadataReport := instance.GetMetadataReportInstance() + metadataReport := metadata.GetMetadataReport() + if metadataReport == nil { + return nil, perrors.New("can not get mapping in remote cause no metadata report instance found") + } return metadataReport.GetServiceAppMapping(serviceInterface, DefaultGroup, listener) } -func (d *MetadataServiceNameMapping) Remove(url *common.URL) error { +func (d *ServiceNameMapping) Remove(url *common.URL) error { serviceInterface := url.GetParam(constant.InterfaceKey, "") - metadataReport := instance.GetMetadataReportInstance() - return metadataReport.RemoveServiceAppMappingListener(serviceInterface, DefaultGroup) -} - -// buildMappingKey will return mapping key, it looks like DefaultGroup/serviceInterface -func (d *MetadataServiceNameMapping) buildMappingKey(serviceInterface string) string { - // the issue : https://github.com/apache/dubbo/issues/4671 - // so other params are ignored and remove, including group string, version string, protocol string - return DefaultGroup + slash + serviceInterface -} - -var ( - serviceNameMappingInstance *MetadataServiceNameMapping - serviceNameMappingOnce sync.Once -) - -// GetNameMappingInstance return an instance, if not found, it creates one -func GetNameMappingInstance() mapping.ServiceNameMapping { - serviceNameMappingOnce.Do(func() { - serviceNameMappingInstance = &MetadataServiceNameMapping{} - }) - return serviceNameMappingInstance -} - -// getMetaDataReport obtain metadata reporting instances through registration protocol -// if the metadata type is remote, obtain the instance from the metadata report configuration -func getMetaDataReport(protocol string) report.MetadataReport { - var metadataReport report.MetadataReport - if config.GetApplicationConfig().MetadataType == constant.RemoteMetadataStorageType { - metadataReport = instance.GetMetadataReportInstance() - return metadataReport + metadataReport := metadata.GetMetadataReport() + if metadataReport == nil { + return perrors.New("can not remove mapping in remote cause no metadata report instance found") } - return instance.GetMetadataReportByRegistryProtocol(protocol) + return metadataReport.RemoveServiceAppMappingListener(serviceInterface, DefaultGroup) } diff --git a/metadata/mapping/mock_service_name_mapping.go b/metadata/mapping/mock_service_name_mapping.go deleted file mode 100644 index 0a5a437d5e..0000000000 --- a/metadata/mapping/mock_service_name_mapping.go +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package mapping - -import ( - gxset "github.com/dubbogo/gost/container/set" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/registry" -) - -type MockServiceNameMapping struct{} - -func NewMockServiceNameMapping() *MockServiceNameMapping { - return &MockServiceNameMapping{} -} - -func (m *MockServiceNameMapping) Map(*common.URL) error { - return nil -} - -func (m *MockServiceNameMapping) Get(*common.URL, registry.MappingListener) (*gxset.HashSet, error) { - panic("implement me") -} - -func (m *MockServiceNameMapping) Remove(*common.URL) error { - panic("implement me") -} diff --git a/metadata/mapping/service_name_mapping.go b/metadata/mapping/service_name_mapping.go index c36b5ed49d..51fa4ecd7a 100644 --- a/metadata/mapping/service_name_mapping.go +++ b/metadata/mapping/service_name_mapping.go @@ -19,20 +19,24 @@ package mapping import ( gxset "github.com/dubbogo/gost/container/set" + "github.com/dubbogo/gost/gof/observer" ) import ( "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/registry" ) // ServiceNameMapping is the interface which trys to build the mapping between application-level service and interface-level service. -// -// # Map method will map the service to this application-level service -// -// Get method will return the application-level services type ServiceNameMapping interface { + // Map the service to this application-level service, store the mapping Map(url *common.URL) error - Get(url *common.URL, listener registry.MappingListener) (*gxset.HashSet, error) + // Get the application-level services which are mapped to this service + Get(url *common.URL, listener MappingListener) (*gxset.HashSet, error) + // Remove the mapping between application-level service and interface-level service Remove(url *common.URL) error } + +type MappingListener interface { + OnEvent(e observer.Event) error + Stop() +} diff --git a/metadata/metadata.go b/metadata/metadata.go index d4fa2c494f..6fbbfcd8ca 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -19,69 +19,40 @@ package metadata import ( - "github.com/dubbogo/gost/log/logger" - - perrors "github.com/pkg/errors" - - "go.uber.org/atomic" -) - -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/metadata/service/exporter" + "dubbo.apache.org/dubbo-go/v3/metadata/info" ) var ( - exporting = &atomic.Bool{} + metadataService MetadataService = &DefaultMetadataService{} + appMetadataInfoMap = make(map[string]*info.MetadataInfo) ) -func ExportMetadataService() { - ms, err := extension.GetLocalMetadataService(constant.DefaultKey) - if err != nil { - logger.Warnf("could not init metadata service", err) - return - } - - if exporting.Load() { - return - } - - // In theory, we can use sync.Once - // But sync.Once is not reentrant. - // Now the invocation chain is createRegistry -> tryInitMetadataService -> metadataServiceExporter.export - // -> createRegistry -> initMetadataService... - // So using sync.Once will result in dead lock - exporting.Store(true) - - expt := extension.GetMetadataServiceExporter(constant.DefaultKey, ms) - if expt == nil { - logger.Warnf("get metadata service exporter failed, pls check if you import _ \"dubbo.apache.org/dubbo-go/v3/metadata/service/exporter/configurable\"") - return - } +func GetMetadataService() MetadataService { + return metadataService +} - err = expt.Export(nil) - if err != nil { - logger.Errorf("could not export the metadata service, err = %s", err.Error()) - return - } +func GetMetadataInfo(registryId string) *info.MetadataInfo { + return appMetadataInfoMap[registryId] +} - // report interface-app mapping - err = publishMapping(expt) - if err != nil { - logger.Errorf("Publish interface-application mapping failed, got error %#v", err) +func AddService(registryId string, url *common.URL) { + if _, exist := appMetadataInfoMap[registryId]; !exist { + appMetadataInfoMap[registryId] = info.NewMetadataInfo( + url.GetParam(constant.ApplicationKey, ""), + url.GetParam(constant.ApplicationTagKey, ""), + ) } + appMetadataInfoMap[registryId].AddService(url) } -// OnEvent only handle ServiceConfigExportedEvent -func publishMapping(sc exporter.MetadataServiceExporter) error { - urls := sc.GetExportedURLs() - - for _, u := range urls { - err := extension.GetGlobalServiceNameMapping().Map(u) - if err != nil { - return perrors.WithMessage(err, "could not map the service: "+u.String()) - } +func AddSubscribeURL(registryId string, url *common.URL) { + if _, exist := appMetadataInfoMap[registryId]; !exist { + appMetadataInfoMap[registryId] = info.NewMetadataInfo( + url.GetParam(constant.ApplicationKey, ""), + url.GetParam(constant.ApplicationTagKey, ""), + ) } - return nil + appMetadataInfoMap[registryId].AddSubscribeURL(url) } diff --git a/metadata/metadata_service.go b/metadata/metadata_service.go new file mode 100644 index 0000000000..5a3591c54c --- /dev/null +++ b/metadata/metadata_service.go @@ -0,0 +1,204 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package metadata + +import ( + "fmt" + "strconv" + "strings" +) + +import ( + "github.com/dubbogo/gost/log/logger" + gxnet "github.com/dubbogo/gost/net" + + 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/metadata/info" + "dubbo.apache.org/dubbo-go/v3/protocol" +) + +// version will be used by Version func +const ( + version = "1.0.0" + allServiceInterfaces = "*" +) + +// 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, error) + // GetExportedServiceURLs will return exported service urls + GetExportedServiceURLs() ([]*common.URL, error) + // GetSubscribedURLs will get the exported urls in metadata + GetSubscribedURLs() ([]*common.URL, error) + Version() (string, error) + // GetMetadataInfo will return metadata info + GetMetadataInfo(revision string) (*info.MetadataInfo, error) + // GetMetadataServiceURL will return the url of metadata service + GetMetadataServiceURL() (*common.URL, error) + // SetMetadataServiceURL exporter to set url of metadata service, will not be exported by exporter,cause no error return + SetMetadataServiceURL(*common.URL) +} + +// DefaultMetadataService is store and query the metadata info in memory when each service registry +type DefaultMetadataService struct { + metadataUrl *common.URL +} + +func (mts *DefaultMetadataService) SetMetadataServiceURL(url *common.URL) { + mts.metadataUrl = url +} + +// GetExportedURLs get all exported urls +func (mts *DefaultMetadataService) GetExportedURLs(serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) { + if allServiceInterfaces == serviceInterface { + return mts.GetExportedServiceURLs() + } + all, err := mts.GetExportedServiceURLs() + if err != nil { + return nil, err + } + urls := make([]*common.URL, 0) + for _, url := range all { + if url.GetParam(constant.InterfaceKey, "") == serviceInterface && + url.GetParam(constant.GroupKey, "") == group && + url.GetParam(constant.ProtocolKey, "") == protocol && + url.GetParam(constant.VersionKey, "") == version { + urls = append(urls, url) + } + } + return urls, nil +} + +// GetMetadataInfo can get metadata in memory +func (mts *DefaultMetadataService) GetMetadataInfo(revision string) (*info.MetadataInfo, error) { + if revision == "" { + return nil, nil + } + for _, metadataInfo := range appMetadataInfoMap { + if metadataInfo.Revision == revision { + return metadataInfo, nil + } + } + logger.Warnf("metadata not found for revision: %s", revision) + return nil, nil +} + +// GetExportedServiceURLs get exported service urls +func (mts *DefaultMetadataService) GetExportedServiceURLs() ([]*common.URL, error) { + urls := make([]*common.URL, 0) + for _, metadataInfo := range appMetadataInfoMap { + urls = append(urls, metadataInfo.GetExportedServiceURLs()...) + } + return urls, nil +} + +// Version will return the version of metadata service +func (mts *DefaultMetadataService) Version() (string, error) { + return version, nil +} + +// GetMetadataServiceURL get url of MetadataService +func (mts *DefaultMetadataService) GetMetadataServiceURL() (*common.URL, error) { + return mts.metadataUrl, nil +} + +func (mts *DefaultMetadataService) GetSubscribedURLs() ([]*common.URL, error) { + urls := make([]*common.URL, 0) + for _, metadataInfo := range appMetadataInfoMap { + urls = append(urls, metadataInfo.GetSubscribedURLs()...) + } + return urls, nil +} + +// MethodMapper only for rename exported function, for example: rename the function GetMetadataInfo to getMetadataInfo +func (mts *DefaultMetadataService) MethodMapper() map[string]string { + return map[string]string{ + "GetExportedURLs": "getExportedURLs", + "GetMetadataInfo": "getMetadataInfo", + } +} + +// ServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface +type ServiceExporter struct { + opts *Options + service MetadataService + protocolExporter protocol.Exporter +} + +// Export will export the metadataService +func (e *ServiceExporter) Export() error { + version, _ := e.service.Version() + var port string + if e.opts.port == 0 { + port = randomPort() + } else { + port = strconv.Itoa(e.opts.port) + } + ivkURL := common.NewURLWithOptions( + common.WithPath(constant.MetadataServiceName), + common.WithProtocol(constant.DefaultProtocol), + common.WithPort(port), + common.WithParamsValue(constant.GroupKey, e.opts.appName), + common.WithParamsValue(constant.SerializationKey, constant.Hessian2Serialization), + common.WithParamsValue(constant.ReleaseKey, constant.Version), + common.WithParamsValue(constant.VersionKey, version), + common.WithParamsValue(constant.InterfaceKey, constant.MetadataServiceName), + common.WithParamsValue(constant.BeanNameKey, constant.SimpleMetadataServiceName), + common.WithParamsValue(constant.MetadataTypeKey, e.opts.metadataType), + common.WithParamsValue(constant.SideKey, constant.SideProvider), + ) + methods, err := common.ServiceMap.Register(ivkURL.Interface(), ivkURL.Protocol, ivkURL.Group(), ivkURL.Version(), e.service) + if err != nil { + formatErr := perrors.Errorf("The service %v needExport the protocol %v error! Error message is %v.", + ivkURL.Interface(), ivkURL.Protocol, err.Error()) + logger.Errorf(formatErr.Error()) + return formatErr + } + ivkURL.Methods = strings.Split(methods, ",") + proxyFactory := extension.GetProxyFactory("") + invoker := proxyFactory.GetInvoker(ivkURL) + e.protocolExporter = extension.GetProtocol(ivkURL.Protocol).Export(invoker) + e.service.SetMetadataServiceURL(ivkURL) + logger.Infof("[Metadata Service] The MetadataService exports urls : %v ", ivkURL) + return nil +} + +func randomPort() string { + tcp, err := gxnet.ListenOnTCPRandomPort("") + if err != nil { + panic(perrors.New(fmt.Sprintf("Get tcp port error, err is {%v}", err))) + } + err = tcp.Close() + if err != nil { + panic(perrors.New(fmt.Sprintf("Close tcp port error, err is {%v}", err))) + } + return strings.Split(tcp.Addr().String(), ":")[1] +} + +// UnExport will unExport the metadataService +func (e *ServiceExporter) UnExport() { + e.protocolExporter.UnExport() +} diff --git a/metadata/options.go b/metadata/options.go index 34ea25d991..30e866abee 100644 --- a/metadata/options.go +++ b/metadata/options.go @@ -20,20 +20,33 @@ package metadata import ( "strconv" "strings" + "sync" "time" ) +import ( + "github.com/dubbogo/gost/log/logger" +) + import ( "dubbo.apache.org/dubbo-go/v3/common/constant" + "dubbo.apache.org/dubbo-go/v3/common/extension" "dubbo.apache.org/dubbo-go/v3/global" ) +var ( + metadataOptions *Options + exportOnce sync.Once +) + type Options struct { - Metadata *global.MetadataReportConfig + appName string + metadataType string + port int } func defaultOptions() *Options { - return &Options{Metadata: global.DefaultMetadataReportConfig()} + return &Options{metadataType: constant.DefaultMetadataStorageType} } func NewOptions(opts ...Option) *Options { @@ -44,73 +57,153 @@ func NewOptions(opts ...Option) *Options { return metaOptions } +func (opts *Options) Init() error { + metadataOptions = opts + var err error + exportOnce.Do(func() { + if opts.metadataType != constant.RemoteMetadataStorageType { + exporter := &ServiceExporter{service: metadataService, opts: opts} + defer func() { + // TODO remove this recover func,this just to avoid some unit test failed,this will not happen in user side mostly + // config test -> metadata exporter -> dubbo protocol/remoting -> config,cycle import will occur + // some day we fix the cycle import then can remove this recover + if err := recover(); err != nil { + logger.Errorf("metadata export failed,please check if dubbo protocol is imported, error: %v", err) + } + }() + err = exporter.Export() + } + }) + return err +} + type Option func(*Options) -func WithZookeeper() Option { - return func(opts *Options) { - opts.Metadata.Protocol = constant.ZookeeperKey +func WithAppName(app string) Option { + return func(options *Options) { + options.appName = app } } -func WithNacos() Option { - return func(opts *Options) { - opts.Metadata.Protocol = constant.NacosKey +func WithMetadataType(typ string) Option { + return func(options *Options) { + options.metadataType = typ } } -func WithEtcdV3() Option { - return func(opts *Options) { - opts.Metadata.Protocol = constant.EtcdV3Key +func WithPort(port int) Option { + return func(options *Options) { + options.port = port } } -func WithMetadata(meta string) Option { - return func(opts *Options) { - opts.Metadata.Protocol = meta +type ReportOptions struct { + registryId string + *global.MetadataReportConfig +} + +func (opts *ReportOptions) Init() error { + fac := extension.GetMetadataReportFactory(opts.Protocol) + if fac == nil { + logger.Errorf("no metadata report factory of protocol %s found!", opts.Protocol) + return nil + } + url, err := toUrl(opts) + if err != nil { + logger.Errorf("metadata report create error %v", err) + return err + } + instances[opts.registryId] = &DelegateMetadataReport{instance: fac.CreateMetadataReport(url)} + return nil +} + +func defaultReportOptions() *ReportOptions { + return &ReportOptions{MetadataReportConfig: global.DefaultMetadataReportConfig()} +} + +func NewReportOptions(opts ...ReportOption) *ReportOptions { + reportOptions := defaultReportOptions() + for _, opt := range opts { + opt(reportOptions) } + return reportOptions } -func WithAddress(address string) Option { - return func(opts *Options) { +type ReportOption func(*ReportOptions) + +func WithZookeeper() ReportOption { + return func(opts *ReportOptions) { + opts.Protocol = constant.ZookeeperKey + } +} + +func WithNacos() ReportOption { + return func(opts *ReportOptions) { + opts.Protocol = constant.NacosKey + } +} + +func WithEtcdV3() ReportOption { + return func(opts *ReportOptions) { + opts.Protocol = constant.EtcdV3Key + } +} + +func WithProtocol(meta string) ReportOption { + return func(opts *ReportOptions) { + opts.Protocol = meta + } +} + +// WithAddress address metadata report will to use, if a URL schema is set,this will also set the protocol, +// such as WithAddress("zookeeper://127.0.0.1") will set address to "127.0.0.1" and protocol to "zookeeper" +func WithAddress(address string) ReportOption { + return func(opts *ReportOptions) { if i := strings.Index(address, "://"); i > 0 { - opts.Metadata.Protocol = address[0:i] + opts.Protocol = address[0:i] } - opts.Metadata.Address = address + opts.Address = address + } +} + +func WithUsername(username string) ReportOption { + return func(opts *ReportOptions) { + opts.Username = username } } -func WithUsername(username string) Option { - return func(opts *Options) { - opts.Metadata.Username = username +func WithPassword(password string) ReportOption { + return func(opts *ReportOptions) { + opts.Password = password } } -func WithPassword(password string) Option { - return func(opts *Options) { - opts.Metadata.Password = password +func WithTimeout(timeout time.Duration) ReportOption { + return func(opts *ReportOptions) { + opts.Timeout = strconv.Itoa(int(timeout.Milliseconds())) } } -func WithTimeout(timeout time.Duration) Option { - return func(opts *Options) { - opts.Metadata.Timeout = strconv.Itoa(int(timeout.Milliseconds())) +func WithGroup(group string) ReportOption { + return func(opts *ReportOptions) { + opts.Group = group } } -func WithGroup(group string) Option { - return func(opts *Options) { - opts.Metadata.Group = group +func WithNamespace(namespace string) ReportOption { + return func(opts *ReportOptions) { + opts.Namespace = namespace } } -func WithNamespace(namespace string) Option { - return func(opts *Options) { - opts.Metadata.Namespace = namespace +func WithParams(params map[string]string) ReportOption { + return func(opts *ReportOptions) { + opts.Params = params } } -func WithParams(params map[string]string) Option { - return func(opts *Options) { - opts.Metadata.Params = params +func WithRegistryId(id string) ReportOption { + return func(opts *ReportOptions) { + opts.registryId = id } } diff --git a/metadata/report/delegate/delegate_report.go b/metadata/report/delegate/delegate_report.go deleted file mode 100644 index 3ca22b126f..0000000000 --- a/metadata/report/delegate/delegate_report.go +++ /dev/null @@ -1,319 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package delegate - -import ( - "encoding/json" - "runtime/debug" - "sync" - "time" -) - -import ( - gxset "github.com/dubbogo/gost/container/set" - "github.com/dubbogo/gost/log/logger" - - "github.com/go-co-op/gocron" - - perrors "github.com/pkg/errors" - - "go.uber.org/atomic" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/constant" - "dubbo.apache.org/dubbo-go/v3/config/instance" - "dubbo.apache.org/dubbo-go/v3/metadata/definition" - "dubbo.apache.org/dubbo-go/v3/metadata/identifier" -) - -const ( - defaultMetadataReportRetryTimes int64 = 100 // max times to retry - defaultMetadataReportRetryPeriod int64 = 3 // cycle interval to retry, the unit is second - defaultMetadataReportCycleReport bool = true // cycle report or not -) - -// metadataReportRetry is a scheduler for retrying task -type metadataReportRetry struct { - retryPeriod int64 - retryLimit int64 - scheduler *gocron.Scheduler - job *gocron.Job - retryCounter *atomic.Int64 - // if no failed report, wait how many times to run retry task. - retryTimesIfNonFail int64 -} - -// newMetadataReportRetry will create a scheduler for retry task -func newMetadataReportRetry(retryPeriod int64, retryLimit int64, retryFunc func() bool) (*metadataReportRetry, error) { - s1 := gocron.NewScheduler(time.UTC) - - mrr := &metadataReportRetry{ - retryPeriod: retryPeriod, - retryLimit: retryLimit, - scheduler: s1, - retryCounter: atomic.NewInt64(0), - retryTimesIfNonFail: 600, - } - - newJob, err := mrr.scheduler.Every(int(mrr.retryPeriod)).Seconds().Do( - func() { - mrr.retryCounter.Inc() - logger.Infof("start to retry task for metadata report. retry times: %v", mrr.retryCounter.Load()) - if mrr.retryCounter.Load() > mrr.retryLimit { - mrr.scheduler.Clear() - } else if retryFunc() && mrr.retryCounter.Load() > mrr.retryTimesIfNonFail { - mrr.scheduler.Clear() // may not interrupt the running job - } - }) - - mrr.job = newJob - return mrr, err -} - -// startRetryTask will make scheduler with retry task run -func (mrr *metadataReportRetry) startRetryTask() { - mrr.scheduler.StartAt(time.Now().Add(500 * time.Millisecond)) - mrr.scheduler.StartAsync() -} - -// MetadataReport is a absolute delegate for MetadataReport -type MetadataReport struct { - reportUrl *common.URL - syncReport bool - metadataReportRetry *metadataReportRetry - - failedReports map[*identifier.MetadataIdentifier]interface{} - failedReportsLock sync.RWMutex - - // allMetadataReports store all the metdadata reports records in memory - allMetadataReports map[*identifier.MetadataIdentifier]interface{} - allMetadataReportsLock sync.RWMutex -} - -// 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.SyncReportKey, false), - failedReports: make(map[*identifier.MetadataIdentifier]interface{}, 4), - allMetadataReports: make(map[*identifier.MetadataIdentifier]interface{}, 4), - } - - mrr, err := newMetadataReportRetry( - url.GetParamInt(constant.RetryPeriodKey, defaultMetadataReportRetryPeriod), - url.GetParamInt(constant.RetryTimesKey, defaultMetadataReportRetryTimes), - bmr.retry, - ) - if err != nil { - return nil, err - } - - bmr.metadataReportRetry = mrr - if url.GetParamBool(constant.CycleReportKey, defaultMetadataReportCycleReport) { - scheduler := gocron.NewScheduler(time.UTC) - _, err := scheduler.Every(1).Day().Do( - func() { - logger.Infof("start to publish all metadata in metadata report %s.", url.String()) - bmr.allMetadataReportsLock.RLock() - bmr.doHandlerMetadataCollection(bmr.allMetadataReports) - bmr.allMetadataReportsLock.RUnlock() - }) - if err != nil { - return nil, err - } - scheduler.StartAt(time.Now().Add(500 * time.Millisecond)) - scheduler.StartAsync() - } - return bmr, nil -} - -// PublishAppMetadata delegate publish metadata info -func (mr *MetadataReport) PublishAppMetadata(identifier *identifier.SubscriberMetadataIdentifier, info *common.MetadataInfo) error { - report := instance.GetMetadataReportInstance() - return report.PublishAppMetadata(identifier, info) -} - -// GetAppMetadata delegate get metadata info -func (mr *MetadataReport) GetAppMetadata(identifier *identifier.SubscriberMetadataIdentifier) (*common.MetadataInfo, error) { - report := instance.GetMetadataReportInstance() - return report.GetAppMetadata(identifier) -} - -// retry will do metadata failed reports collection by call metadata report sdk -func (mr *MetadataReport) retry() bool { - mr.failedReportsLock.RLock() - defer mr.failedReportsLock.RUnlock() - return mr.doHandlerMetadataCollection(mr.failedReports) -} - -// StoreProviderMetadata will delegate to call remote metadata's sdk to store provider service definition -func (mr *MetadataReport) StoreProviderMetadata(identifier *identifier.MetadataIdentifier, definer definition.ServiceDefiner) { - if mr.syncReport { - mr.storeMetadataTask(common.PROVIDER, identifier, definer) - } - go mr.storeMetadataTask(common.PROVIDER, identifier, definer) -} - -// storeMetadataTask will delegate to call remote metadata's sdk to store -func (mr *MetadataReport) storeMetadataTask(role int, identifier *identifier.MetadataIdentifier, definer interface{}) { - logger.Infof("publish provider identifier and definition: Identifier :%v ; definition: %v .", identifier, definer) - mr.allMetadataReportsLock.Lock() - mr.allMetadataReports[identifier] = definer - mr.allMetadataReportsLock.Unlock() - - mr.failedReportsLock.Lock() - delete(mr.failedReports, identifier) - mr.failedReportsLock.Unlock() - // data is store the json marshaled definition - var ( - data []byte - err error - ) - - defer func() { - if r := recover(); r != nil { - mr.failedReportsLock.Lock() - mr.failedReports[identifier] = definer - mr.failedReportsLock.Unlock() - mr.metadataReportRetry.startRetryTask() - logger.Errorf("Failed to put provider metadata %v in %v, cause: %v\n%s\n", - identifier, string(data), r, string(debug.Stack())) - } - }() - - data, err = json.Marshal(definer) - if err != nil { - logger.Errorf("storeProviderMetadataTask error in stage json.Marshal, msg is %+v", err) - panic(err) - } - report := instance.GetMetadataReportInstance() - if role == common.PROVIDER { - err = report.StoreProviderMetadata(identifier, string(data)) - } else if role == common.CONSUMER { - err = report.StoreConsumerMetadata(identifier, string(data)) - } - - if err != nil { - logger.Errorf("storeProviderMetadataTask error in stage call metadata report to StoreProviderMetadata, msg is %+v", err) - } -} - -// StoreConsumerMetadata will delegate to call remote metadata's sdk to store consumer side service definition -func (mr *MetadataReport) StoreConsumerMetadata(identifier *identifier.MetadataIdentifier, definer map[string]string) { - if mr.syncReport { - mr.storeMetadataTask(common.CONSUMER, identifier, definer) - } - go mr.storeMetadataTask(common.CONSUMER, identifier, definer) -} - -// SaveServiceMetadata will delegate to call remote metadata's sdk to save service metadata -func (mr *MetadataReport) SaveServiceMetadata(identifier *identifier.ServiceMetadataIdentifier, url *common.URL) error { - report := instance.GetMetadataReportInstance() - if mr.syncReport { - return report.SaveServiceMetadata(identifier, url) - } - go func() { - if err := report.SaveServiceMetadata(identifier, url); err != nil { - logger.Warnf("report.SaveServiceMetadata(identifier:%v, url:%v) = error:%v", identifier, url, err) - } - }() - return nil -} - -// RemoveServiceMetadata will delegate to call remote metadata's sdk to remove service metadata -func (mr *MetadataReport) RemoveServiceMetadata(identifier *identifier.ServiceMetadataIdentifier) error { - report := instance.GetMetadataReportInstance() - if mr.syncReport { - return report.RemoveServiceMetadata(identifier) - } - go func() { - if err := report.RemoveServiceMetadata(identifier); err != nil { - logger.Warnf("report.RemoveServiceMetadata(identifier:%v) = error:%v", identifier, err) - } - }() - return nil -} - -// GetExportedURLs will delegate to call remote metadata's sdk to get exported urls -func (mr *MetadataReport) GetExportedURLs(identifier *identifier.ServiceMetadataIdentifier) ([]string, error) { - report := instance.GetMetadataReportInstance() - return report.GetExportedURLs(identifier) -} - -// SaveSubscribedData will delegate to call remote metadata's sdk to save subscribed data -func (mr *MetadataReport) SaveSubscribedData(identifier *identifier.SubscriberMetadataIdentifier, urls []*common.URL) error { - urlStrList := make([]string, 0, len(urls)) - for _, url := range urls { - urlStrList = append(urlStrList, url.String()) - } - bytes, err := json.Marshal(urlStrList) - if err != nil { - return perrors.WithMessage(err, "Could not convert the array to json") - } - - report := instance.GetMetadataReportInstance() - if mr.syncReport { - return report.SaveSubscribedData(identifier, string(bytes)) - } - go func() { - if err := report.SaveSubscribedData(identifier, string(bytes)); err != nil { - logger.Warnf("report.SaveSubscribedData(identifier:%v, string(bytes):%v) = error: %v", - identifier, string(bytes), err) - } - }() - return nil -} - -// GetSubscribedURLs will delegate to call remote metadata's sdk to get subscribed urls -func (mr *MetadataReport) GetSubscribedURLs(identifier *identifier.SubscriberMetadataIdentifier) ([]string, error) { - report := instance.GetMetadataReportInstance() - return report.GetSubscribedURLs(identifier) -} - -// GetServiceDefinition will delegate to call remote metadata's sdk to get service definitions -func (mr *MetadataReport) GetServiceDefinition(identifier *identifier.MetadataIdentifier) (string, error) { - report := instance.GetMetadataReportInstance() - return report.GetServiceDefinition(identifier) -} - -// doHandlerMetadataCollection will store metadata to metadata support with given metadataMap -func (mr *MetadataReport) doHandlerMetadataCollection(metadataMap map[*identifier.MetadataIdentifier]interface{}) bool { - if len(metadataMap) == 0 { - return true - } - for e := range metadataMap { - if common.RoleType(common.PROVIDER).Role() == e.Side { - mr.StoreProviderMetadata(e, metadataMap[e].(*definition.FullServiceDefinition)) - } else if common.RoleType(common.CONSUMER).Role() == e.Side { - mr.StoreConsumerMetadata(e, metadataMap[e].(map[string]string)) - } - } - return false -} - -func (mr *MetadataReport) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { - //TODO implement me - panic("implement me") -} diff --git a/metadata/report/delegate/delegate_report_test.go b/metadata/report/delegate/delegate_report_test.go deleted file mode 100644 index 060cf161b9..0000000000 --- a/metadata/report/delegate/delegate_report_test.go +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package delegate - -import ( - "fmt" - "testing" - "time" -) - -import ( - "github.com/dubbogo/gost/log/logger" - - "github.com/stretchr/testify/assert" - - "go.uber.org/atomic" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/constant" - "dubbo.apache.org/dubbo-go/v3/config/instance" - "dubbo.apache.org/dubbo-go/v3/metadata/definition" - "dubbo.apache.org/dubbo-go/v3/metadata/identifier" -) - -func TestMetadataReport_MetadataReportRetry(t *testing.T) { - counter := atomic.NewInt64(1) - - retry, err := newMetadataReportRetry(1, 10, func() bool { - counter.Add(1) - return true - }) - assert.NoError(t, err) - retry.startRetryTask() - <-time.After(2000 * time.Millisecond) - retry.scheduler.Clear() - assert.Equal(t, int64(3), counter.Load()) - logger.Info("over") -} - -func TestMetadataReport_MetadataReportRetryWithLimit(t *testing.T) { - counter := atomic.NewInt64(1) - - retry, err := newMetadataReportRetry(1, 1, func() bool { - counter.Add(1) - return true - }) - assert.NoError(t, err) - retry.startRetryTask() - <-time.After(2000 * time.Millisecond) - retry.scheduler.Clear() - assert.Equal(t, int64(2), counter.Load()) - logger.Info("over") -} - -func mockNewMetadataReport(t *testing.T) *MetadataReport { - syncReportKey := "false" - retryPeriodKey := "3" - retryTimesKey := "100" - cycleReportKey := "true" - - url, err := common.NewURL(fmt.Sprintf( - "test://127.0.0.1:20000/?"+constant.SyncReportKey+"=%v&"+constant.RetryPeriodKey+"=%v&"+ - constant.RetryTimesKey+"=%v&"+constant.CycleReportKey+"=%v", - syncReportKey, retryPeriodKey, retryTimesKey, cycleReportKey)) - assert.NoError(t, err) - instance.SetMetadataReportUrl(url) - mtr, err := NewMetadataReport() - assert.NoError(t, err) - assert.NotNil(t, mtr) - return mtr -} - -func TestMetadataReport_StoreProviderMetadata(t *testing.T) { - mtr := mockNewMetadataReport(t) - metadataId := &identifier.MetadataIdentifier{ - Application: "app", - BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{ - ServiceInterface: "com.ikurento.user.UserProvider", - Version: "0.0.1", - Group: "group1", - Side: "provider", - }, - } - - mtr.StoreProviderMetadata(metadataId, getMockDefinition(metadataId, t)) -} - -func getMockDefinition(id *identifier.MetadataIdentifier, t *testing.T) *definition.ServiceDefinition { - protocol := "dubbo" - beanName := "UserProvider" - url, err := common.NewURL(fmt.Sprintf( - "%v://127.0.0.1:20000/com.ikurento.user.UserProvider1?anyhost=true&"+ - "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ - "environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+ - "owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000×tamp=1556509797245&group=%v&version=%v&bean.name=%v", - protocol, id.ServiceInterface, id.Group, id.Version, beanName)) - assert.NoError(t, err) - _, err = common.ServiceMap.Register(id.ServiceInterface, protocol, id.Group, id.Version, &definition.UserProvider{}) - assert.NoError(t, err) - service := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey()) - return definition.BuildServiceDefinition(*service, url) -} diff --git a/metadata/report/etcd/report.go b/metadata/report/etcd/report.go index 487b039692..0b325247bf 100644 --- a/metadata/report/etcd/report.go +++ b/metadata/report/etcd/report.go @@ -34,46 +34,40 @@ 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/metadata/identifier" + "dubbo.apache.org/dubbo-go/v3/metadata/info" + "dubbo.apache.org/dubbo-go/v3/metadata/mapping" "dubbo.apache.org/dubbo-go/v3/metadata/report" - "dubbo.apache.org/dubbo-go/v3/metadata/report/factory" - "dubbo.apache.org/dubbo-go/v3/registry" ) const DEFAULT_ROOT = "dubbo" func init() { - extension.SetMetadataReportFactory(constant.EtcdV3Key, func() factory.MetadataReportFactory { + extension.SetMetadataReportFactory(constant.EtcdV3Key, func() report.MetadataReportFactory { return &etcdMetadataReportFactory{} }) } // etcdMetadataReport is the implementation of MetadataReport based etcd type etcdMetadataReport struct { - client *gxetcd.Client - root string -} - -func (e *etcdMetadataReport) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { - //TODO implement me - panic("implement me") + client *gxetcd.Client + rootDir string } // GetAppMetadata get metadata info from etcd -func (e *etcdMetadataReport) GetAppMetadata(metadataIdentifier *identifier.SubscriberMetadataIdentifier) (*common.MetadataInfo, error) { - key := e.getNodeKey(metadataIdentifier) +func (e *etcdMetadataReport) GetAppMetadata(application, revision string) (*info.MetadataInfo, error) { + key := e.rootDir + application + constant.PathSeparator + revision data, err := e.client.Get(key) if err != nil { return nil, err } - info := &common.MetadataInfo{} - return info, json.Unmarshal([]byte(data), info) + meta := &info.MetadataInfo{} + return meta, json.Unmarshal([]byte(data), meta) } // PublishAppMetadata publish metadata info to etcd -func (e *etcdMetadataReport) PublishAppMetadata(metadataIdentifier *identifier.SubscriberMetadataIdentifier, info *common.MetadataInfo) error { - key := e.getNodeKey(metadataIdentifier) +func (e *etcdMetadataReport) PublishAppMetadata(application, revision string, info *info.MetadataInfo) error { + key := e.rootDir + application + constant.PathSeparator + revision value, err := json.Marshal(info) if err == nil { err = e.client.Put(key, string(value)) @@ -82,77 +76,9 @@ func (e *etcdMetadataReport) PublishAppMetadata(metadataIdentifier *identifier.S return err } -// StoreProviderMetadata will store the metadata -// metadata including the basic info of the server, provider info, and other user custom info -func (e *etcdMetadataReport) StoreProviderMetadata(providerIdentifier *identifier.MetadataIdentifier, serviceDefinitions string) error { - key := e.getNodeKey(providerIdentifier) - return e.client.Put(key, serviceDefinitions) -} - -// StoreConsumerMetadata will store the metadata -// metadata including the basic info of the server, consumer info, and other user custom info -func (e *etcdMetadataReport) StoreConsumerMetadata(consumerMetadataIdentifier *identifier.MetadataIdentifier, serviceParameterString string) error { - key := e.getNodeKey(consumerMetadataIdentifier) - return e.client.Put(key, serviceParameterString) -} - -// SaveServiceMetadata will store the metadata -// metadata including the basic info of the server, service info, and other user custom info -func (e *etcdMetadataReport) SaveServiceMetadata(metadataIdentifier *identifier.ServiceMetadataIdentifier, url *common.URL) error { - key := e.getNodeKey(metadataIdentifier) - return e.client.Put(key, url.String()) -} - -// RemoveServiceMetadata will remove the service metadata -func (e *etcdMetadataReport) RemoveServiceMetadata(metadataIdentifier *identifier.ServiceMetadataIdentifier) error { - return e.client.Delete(e.getNodeKey(metadataIdentifier)) -} - -// GetExportedURLs will look up the exported urls. -// if not found, an empty list will be returned. -func (e *etcdMetadataReport) GetExportedURLs(metadataIdentifier *identifier.ServiceMetadataIdentifier) ([]string, error) { - content, err := e.client.Get(e.getNodeKey(metadataIdentifier)) - if err != nil { - logger.Errorf("etcdMetadataReport GetExportedURLs err:{%v}", err.Error()) - return []string{}, err - } - if content == "" { - return []string{}, nil - } - return []string{content}, nil -} - -// SaveSubscribedData will convert the urlList to json array and then store it -func (e *etcdMetadataReport) SaveSubscribedData(subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier, urls string) error { - key := e.getNodeKey(subscriberMetadataIdentifier) - return e.client.Put(key, urls) -} - -// GetSubscribedURLs will lookup the url -// if not found, an empty list will be returned -func (e *etcdMetadataReport) GetSubscribedURLs(subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier) ([]string, error) { - content, err := e.client.Get(e.getNodeKey(subscriberMetadataIdentifier)) - if err != nil { - logger.Errorf("etcdMetadataReport GetSubscribedURLs err:{%v}", err.Error()) - return nil, err - } - return []string{content}, nil -} - -// GetServiceDefinition will lookup the service definition -func (e *etcdMetadataReport) GetServiceDefinition(metadataIdentifier *identifier.MetadataIdentifier) (string, error) { - key := e.getNodeKey(metadataIdentifier) - content, err := e.client.Get(key) - if err != nil { - logger.Errorf("etcdMetadataReport GetServiceDefinition err:{%v}", err.Error()) - return "", err - } - return content, nil -} - // RegisterServiceAppMapping map the specified Dubbo service interface to current Dubbo app name func (e *etcdMetadataReport) RegisterServiceAppMapping(key string, group string, value string) error { - path := e.root + constant.PathSeparator + group + constant.PathSeparator + key + path := e.rootDir + constant.PathSeparator + group + constant.PathSeparator + key oldVal, err := e.client.Get(path) if perrors.Cause(err) == gxetcd.ErrKVPairNotFound { return e.client.Put(path, value) @@ -167,8 +93,8 @@ func (e *etcdMetadataReport) RegisterServiceAppMapping(key string, group string, } // GetServiceAppMapping get the app names from the specified Dubbo service interface -func (e *etcdMetadataReport) GetServiceAppMapping(key string, group string, listener registry.MappingListener) (*gxset.HashSet, error) { - path := e.root + constant.PathSeparator + group + constant.PathSeparator + key +func (e *etcdMetadataReport) GetServiceAppMapping(key string, group string, listener mapping.MappingListener) (*gxset.HashSet, error) { + path := e.rootDir + constant.PathSeparator + group + constant.PathSeparator + key v, err := e.client.Get(path) if err != nil { return nil, err @@ -196,17 +122,7 @@ func (e *etcdMetadataReportFactory) CreateMetadataReport(url *common.URL) report logger.Errorf("Could not create etcd metadata report. URL: %s,error:{%v}", url.String(), err) return nil } - group := url.GetParam(constant.GroupKey, DEFAULT_ROOT) + group := url.GetParam(constant.MetadataReportGroupKey, DEFAULT_ROOT) group = constant.PathSeparator + strings.TrimPrefix(group, constant.PathSeparator) - return &etcdMetadataReport{client: client, root: group} -} - -func (e *etcdMetadataReport) getNodeKey(MetadataIdentifier identifier.IMetadataIdentifier) string { - var rootDir string - if e.root == constant.PathSeparator { - rootDir = e.root - } else { - rootDir = e.root + constant.PathSeparator - } - return rootDir + MetadataIdentifier.GetFilePathKey() + return &etcdMetadataReport{client: client, rootDir: group} } diff --git a/metadata/report/nacos/report.go b/metadata/report/nacos/report.go index fd8182c270..e7f64c3e8e 100644 --- a/metadata/report/nacos/report.go +++ b/metadata/report/nacos/report.go @@ -19,7 +19,6 @@ package nacos import ( "encoding/json" - "net/url" "strings" ) @@ -37,25 +36,16 @@ 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/metadata/identifier" - "dubbo.apache.org/dubbo-go/v3/metadata/mapping/metadata" + "dubbo.apache.org/dubbo-go/v3/metadata/info" + "dubbo.apache.org/dubbo-go/v3/metadata/mapping" "dubbo.apache.org/dubbo-go/v3/metadata/report" - "dubbo.apache.org/dubbo-go/v3/metadata/report/factory" "dubbo.apache.org/dubbo-go/v3/registry" "dubbo.apache.org/dubbo-go/v3/remoting/nacos" ) -const ( - // the number is a little big tricky - // it will be used in query which looks up all keys with the target group - // now, one key represents one application - // so only a group has more than 9999 applications will failed - maxKeysNum = 9999 -) - func init() { mf := &nacosMetadataReportFactory{} - extension.SetMetadataReportFactory("nacos", func() factory.MetadataReportFactory { + extension.SetMetadataReportFactory("nacos", func() report.MetadataReportFactory { return mf }) } @@ -64,20 +54,23 @@ func init() { // of MetadataReport based on nacos. type nacosMetadataReport struct { client *nacosClient.NacosConfigClient - group string } // GetAppMetadata get metadata info from nacos -func (n *nacosMetadataReport) GetAppMetadata(metadataIdentifier *identifier.SubscriberMetadataIdentifier) (*common.MetadataInfo, error) { +func (n *nacosMetadataReport) GetAppMetadata(application, revision string) (*info.MetadataInfo, error) { data, err := n.getConfig(vo.ConfigParam{ - DataId: metadataIdentifier.GetIdentifierKey(), - Group: n.group, + DataId: application + constant.PathSeparator + revision, }) + // TODO java impl as below + //data, err = n.getConfig(vo.ConfigParam{ + // DataId: application, + // Group: revision, + //}) if err != nil { return nil, err } - var metadataInfo common.MetadataInfo + var metadataInfo info.MetadataInfo err = json.Unmarshal([]byte(data), &metadataInfo) if err != nil { return nil, err @@ -86,85 +79,18 @@ func (n *nacosMetadataReport) GetAppMetadata(metadataIdentifier *identifier.Subs } // PublishAppMetadata publish metadata info to nacos -func (n *nacosMetadataReport) PublishAppMetadata(metadataIdentifier *identifier.SubscriberMetadataIdentifier, info *common.MetadataInfo) error { - data, err := json.Marshal(info) +func (n *nacosMetadataReport) PublishAppMetadata(application, revision string, meta *info.MetadataInfo) error { + data, err := json.Marshal(meta) if err != nil { return err } return n.storeMetadata(vo.ConfigParam{ - DataId: metadataIdentifier.GetIdentifierKey(), - Group: n.group, + DataId: application + constant.PathSeparator + revision, Content: string(data), }) } -// StoreProviderMetadata stores the metadata. -func (n *nacosMetadataReport) StoreProviderMetadata(providerIdentifier *identifier.MetadataIdentifier, serviceDefinitions string) error { - return n.storeMetadata(vo.ConfigParam{ - DataId: providerIdentifier.GetIdentifierKey(), - Group: n.group, - Content: serviceDefinitions, - }) -} - -// StoreConsumerMetadata stores the metadata. -func (n *nacosMetadataReport) StoreConsumerMetadata(consumerMetadataIdentifier *identifier.MetadataIdentifier, serviceParameterString string) error { - return n.storeMetadata(vo.ConfigParam{ - DataId: consumerMetadataIdentifier.GetIdentifierKey(), - Group: n.group, - Content: serviceParameterString, - }) -} - -// SaveServiceMetadata saves the metadata. -func (n *nacosMetadataReport) SaveServiceMetadata(metadataIdentifier *identifier.ServiceMetadataIdentifier, url *common.URL) error { - return n.storeMetadata(vo.ConfigParam{ - DataId: metadataIdentifier.GetIdentifierKey(), - Group: n.group, - Content: url.String(), - }) -} - -// RemoveServiceMetadata removes the metadata. -func (n *nacosMetadataReport) RemoveServiceMetadata(metadataIdentifier *identifier.ServiceMetadataIdentifier) error { - return n.deleteMetadata(vo.ConfigParam{ - DataId: metadataIdentifier.GetIdentifierKey(), - Group: n.group, - }) -} - -// GetExportedURLs gets the urls. -func (n *nacosMetadataReport) GetExportedURLs(metadataIdentifier *identifier.ServiceMetadataIdentifier) ([]string, error) { - return n.getConfigAsArray(vo.ConfigParam{ - DataId: metadataIdentifier.GetIdentifierKey(), - Group: n.group, - }) -} - -// SaveSubscribedData saves the urls. -func (n *nacosMetadataReport) SaveSubscribedData(subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier, urls string) error { - return n.storeMetadata(vo.ConfigParam{ - DataId: subscriberMetadataIdentifier.GetIdentifierKey(), - Content: urls, - }) -} - -// GetSubscribedURLs gets the urls. -func (n *nacosMetadataReport) GetSubscribedURLs(subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier) ([]string, error) { - return n.getConfigAsArray(vo.ConfigParam{ - DataId: subscriberMetadataIdentifier.GetIdentifierKey(), - }) -} - -// GetServiceDefinition gets the service definition. -func (n *nacosMetadataReport) GetServiceDefinition(metadataIdentifier *identifier.MetadataIdentifier) (string, error) { - return n.getConfig(vo.ConfigParam{ - DataId: metadataIdentifier.GetIdentifierKey(), - Group: n.group, - }) -} - // storeMetadata will publish the metadata to Nacos // if failed or error is not nil, error will be returned func (n *nacosMetadataReport) storeMetadata(param vo.ConfigParam) error { @@ -178,38 +104,6 @@ func (n *nacosMetadataReport) storeMetadata(param vo.ConfigParam) error { return nil } -// deleteMetadata will delete the metadata -func (n *nacosMetadataReport) deleteMetadata(param vo.ConfigParam) error { - res, err := n.client.Client().DeleteConfig(param) - if err != nil { - return perrors.WithMessage(err, "Could not delete the metadata") - } - if !res { - return perrors.New("Deleting the metadata failed.") - } - return nil -} - -// getConfigAsArray will read the config and then convert it as an one-element array -// error or config not found, an empty list will be returned. -func (n *nacosMetadataReport) getConfigAsArray(param vo.ConfigParam) ([]string, error) { - res := make([]string, 0, 1) - - cfg, err := n.getConfig(param) - if err != nil || len(cfg) == 0 { - return res, err - } - - decodeCfg, err := url.QueryUnescape(cfg) - if err != nil { - logger.Errorf("The config is invalid: %s", cfg) - return res, err - } - - res = append(res, decodeCfg) - return res, nil -} - // getConfig will read the config func (n *nacosMetadataReport) getConfig(param vo.ConfigParam) (string, error) { cfg, err := n.client.Client().GetConfig(param) @@ -220,7 +114,7 @@ func (n *nacosMetadataReport) getConfig(param vo.ConfigParam) (string, error) { return cfg, nil } -func (n *nacosMetadataReport) addListener(key string, group string, notify registry.MappingListener) error { +func (n *nacosMetadataReport) addListener(key string, group string, notify mapping.MappingListener) error { return n.client.Client().ListenConfig(vo.ConfigParam{ DataId: key, Group: group, @@ -230,7 +124,7 @@ func (n *nacosMetadataReport) addListener(key string, group string, notify regis }) } -func callback(notify registry.MappingListener, dataId, data string) { +func callback(notify mapping.MappingListener, dataId, data string) { appNames := strings.Split(data, constant.CommaSeparator) set := gxset.NewSet() for _, app := range appNames { @@ -273,7 +167,7 @@ func (n *nacosMetadataReport) RegisterServiceAppMapping(key string, group string } // GetServiceAppMapping get the app names from the specified Dubbo service interface -func (n *nacosMetadataReport) GetServiceAppMapping(key string, group string, listener registry.MappingListener) (*gxset.HashSet, error) { +func (n *nacosMetadataReport) GetServiceAppMapping(key string, group string, listener mapping.MappingListener) (*gxset.HashSet, error) { // add service mapping listener if listener != nil { if err := n.addListener(key, group, listener); err != nil { @@ -298,37 +192,6 @@ func (n *nacosMetadataReport) GetServiceAppMapping(key string, group string, lis return set, nil } -// GetConfigKeysByGroup will return all keys with the group -func (n *nacosMetadataReport) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { - group = n.resolvedGroup(group) - page, err := n.client.Client().SearchConfig(vo.SearchConfigParam{ - Search: "accurate", - Group: group, - PageNo: 1, - // actually it's impossible for user to create 9999 application under one group - PageSize: maxKeysNum, - }) - - result := gxset.NewSet() - if err != nil { - return result, perrors.WithMessage(err, "can not find the configClient config") - } - for _, itm := range page.PageItems { - result.Add(itm.DataId) - } - return result, nil -} - -// resolvedGroup will regular the group. Now, it will replace the '/' with '-'. -// '/' is a special character for nacos -func (n *nacosMetadataReport) resolvedGroup(group string) string { - if len(group) <= 0 { - group = metadata.DefaultGroup - return group - } - return strings.ReplaceAll(group, "/", "-") -} - // RemoveServiceAppMappingListener remove the serviceMapping listener from metadata center func (n *nacosMetadataReport) RemoveServiceAppMappingListener(key string, group string) error { return n.removeServiceMappingListener(key, group) @@ -349,5 +212,5 @@ func (n *nacosMetadataReportFactory) CreateMetadataReport(url *common.URL) repor logger.Errorf("Could not create nacos metadata report. URL: %s", url.String()) return nil } - return &nacosMetadataReport{client: client, group: group} + return &nacosMetadataReport{client: client} } diff --git a/metadata/report/nacos/report_test.go b/metadata/report/nacos/report_test.go index 759a456614..ac68e59cc7 100644 --- a/metadata/report/nacos/report_test.go +++ b/metadata/report/nacos/report_test.go @@ -20,7 +20,6 @@ package nacos import ( "encoding/json" "reflect" - "strconv" "testing" ) @@ -34,36 +33,9 @@ import ( ) import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/constant" - "dubbo.apache.org/dubbo-go/v3/metadata/identifier" + "dubbo.apache.org/dubbo-go/v3/metadata/info" ) -func newServiceMetadataIdentifier() *identifier.ServiceMetadataIdentifier { - return &identifier.ServiceMetadataIdentifier{ - Protocol: "nacos", - Revision: "a", - BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{ - ServiceInterface: "com.test.MyTest", - Version: "1.0.0", - Group: "test_group", - Side: "service", - }, - } -} - -func newMetadataIdentifier(side string) *identifier.MetadataIdentifier { - return &identifier.MetadataIdentifier{ - Application: "test", - BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{ - ServiceInterface: "com.test.MyTest", - Version: "1.0.0", - Group: "test_group", - Side: side, - }, - } -} - // MockIConfigClient is a mock of IConfigClient interface type MockIConfigClient struct { ctrl *gomock.Controller @@ -184,18 +156,12 @@ type fields struct { client *nacosClient.NacosConfigClient } type args struct { - subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier - info *common.MetadataInfo - providerIdentifier *identifier.MetadataIdentifier - serviceDefinitions string - consumerMetadataIdentifier *identifier.MetadataIdentifier - serviceParameterString string - url *common.URL - serviceMetadataIdentifier *identifier.ServiceMetadataIdentifier - urls string - key string - group string - value string + application string + revision string + info *info.MetadataInfo + key string + group string + value string } func newNacosMetadataReport(f fields) *nacosMetadataReport { @@ -205,7 +171,7 @@ func newNacosMetadataReport(f fields) *nacosMetadataReport { } func Test_nacosMetadataReport_GetAppMetadata(t *testing.T) { - mi := common.MetadataInfo{ + mi := info.MetadataInfo{ App: "GetAppMetadata", } data, _ := json.Marshal(mi) @@ -220,7 +186,7 @@ func Test_nacosMetadataReport_GetAppMetadata(t *testing.T) { name string fields fields args args - want *common.MetadataInfo + want *info.MetadataInfo wantErr bool }{ { @@ -229,7 +195,8 @@ func Test_nacosMetadataReport_GetAppMetadata(t *testing.T) { client: nc, }, args: args{ - subscriberMetadataIdentifier: &identifier.SubscriberMetadataIdentifier{}, + application: "dubbo", + revision: "revision", }, want: &mi, wantErr: false, @@ -238,7 +205,7 @@ func Test_nacosMetadataReport_GetAppMetadata(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { n := newNacosMetadataReport(tt.fields) - got, err := n.GetAppMetadata(tt.args.subscriberMetadataIdentifier) + got, err := n.GetAppMetadata(tt.args.application, tt.args.revision) if (err != nil) != tt.wantErr { t.Errorf("GetAppMetadata() error = %v, wantErr %v", err, tt.wantErr) return @@ -269,8 +236,9 @@ func Test_nacosMetadataReport_PublishAppMetadata(t *testing.T) { client: nc, }, args: args{ - subscriberMetadataIdentifier: &identifier.SubscriberMetadataIdentifier{}, - info: &common.MetadataInfo{ + application: "PublishAppMetadata", + revision: "PublishAppMetadata", + info: &info.MetadataInfo{ App: "PublishAppMetadata", }, }, @@ -280,191 +248,13 @@ func Test_nacosMetadataReport_PublishAppMetadata(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { n := newNacosMetadataReport(tt.fields) - if err := n.PublishAppMetadata(tt.args.subscriberMetadataIdentifier, tt.args.info); (err != nil) != tt.wantErr { + if err := n.PublishAppMetadata(tt.args.application, tt.args.revision, tt.args.info); (err != nil) != tt.wantErr { t.Errorf("PublishAppMetadata() error = %v, wantErr %v", err, tt.wantErr) } }) } } -func Test_nacosMetadataReport_StoreProviderMetadata(t *testing.T) { - ctrl := gomock.NewController(t) - mnc := NewMockIConfigClient(ctrl) - mnc.EXPECT().PublishConfig(gomock.Any()).Return(true, nil) - nc := &nacosClient.NacosConfigClient{} - nc.SetClient(mnc) - - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - { - name: "test", - fields: fields{ - client: nc, - }, - args: args{ - providerIdentifier: newMetadataIdentifier("provider"), - serviceDefinitions: "provider", - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - n := newNacosMetadataReport(tt.fields) - if err := n.StoreProviderMetadata(tt.args.providerIdentifier, tt.args.serviceDefinitions); (err != nil) != tt.wantErr { - t.Errorf("StoreProviderMetadata() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_nacosMetadataReport_StoreConsumerMetadata(t *testing.T) { - ctrl := gomock.NewController(t) - mnc := NewMockIConfigClient(ctrl) - mnc.EXPECT().PublishConfig(gomock.Any()).Return(true, nil) - nc := &nacosClient.NacosConfigClient{} - nc.SetClient(mnc) - - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - { - name: "test", - fields: fields{ - client: nc, - }, - args: args{ - consumerMetadataIdentifier: newMetadataIdentifier("conusmer"), - serviceParameterString: "conusmer", - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - n := newNacosMetadataReport(tt.fields) - if err := n.StoreConsumerMetadata(tt.args.consumerMetadataIdentifier, tt.args.serviceParameterString); (err != nil) != tt.wantErr { - t.Errorf("StoreConsumerMetadata() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_nacosMetadataReport_SaveServiceMetadata(t *testing.T) { - ctrl := gomock.NewController(t) - mnc := NewMockIConfigClient(ctrl) - mnc.EXPECT().PublishConfig(gomock.Any()).Return(true, nil) - nc := &nacosClient.NacosConfigClient{} - nc.SetClient(mnc) - - serviceURL, _ := common.NewURL("registry://test.nacos.io:80", - common.WithParamsValue(constant.RegistryRoleKey, strconv.Itoa(common.PROVIDER)), - common.WithParamsValue(constant.ClientNameKey, "nacos-client")) - - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - { - name: "test", - fields: fields{ - client: nc, - }, - args: args{ - serviceMetadataIdentifier: newServiceMetadataIdentifier(), - url: serviceURL, - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - n := newNacosMetadataReport(tt.fields) - if err := n.SaveServiceMetadata(tt.args.serviceMetadataIdentifier, tt.args.url); (err != nil) != tt.wantErr { - t.Errorf("SaveServiceMetadata() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_nacosMetadataReport_RemoveServiceMetadata(t *testing.T) { - ctrl := gomock.NewController(t) - mnc := NewMockIConfigClient(ctrl) - mnc.EXPECT().DeleteConfig(gomock.Any()).Return(true, nil) - nc := &nacosClient.NacosConfigClient{} - nc.SetClient(mnc) - - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - { - name: "test", - fields: fields{ - client: nc, - }, - args: args{ - serviceMetadataIdentifier: newServiceMetadataIdentifier(), - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - n := newNacosMetadataReport(tt.fields) - if err := n.RemoveServiceMetadata(tt.args.serviceMetadataIdentifier); (err != nil) != tt.wantErr { - t.Errorf("RemoveServiceMetadata() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func Test_nacosMetadataReport_SaveSubscribedData(t *testing.T) { - ctrl := gomock.NewController(t) - mnc := NewMockIConfigClient(ctrl) - mnc.EXPECT().PublishConfig(gomock.Any()).Return(true, nil) - nc := &nacosClient.NacosConfigClient{} - nc.SetClient(mnc) - - tests := []struct { - name string - fields fields - args args - wantErr bool - }{ - { - name: "test", - fields: fields{ - client: nc, - }, - args: args{ - subscriberMetadataIdentifier: &identifier.SubscriberMetadataIdentifier{}, - urls: "urls", - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - n := newNacosMetadataReport(tt.fields) - if err := n.SaveSubscribedData(tt.args.subscriberMetadataIdentifier, tt.args.urls); (err != nil) != tt.wantErr { - t.Errorf("SaveSubscribedData() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - func Test_nacosMetadataReport_RegisterServiceAppMapping(t *testing.T) { ctrl := gomock.NewController(t) mnc := NewMockIConfigClient(ctrl) diff --git a/metadata/report/report.go b/metadata/report/report.go index 9688825667..edabce0a60 100644 --- a/metadata/report/report.go +++ b/metadata/report/report.go @@ -22,60 +22,24 @@ import ( ) import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/metadata/identifier" - "dubbo.apache.org/dubbo-go/v3/registry" + "dubbo.apache.org/dubbo-go/v3/metadata/info" + "dubbo.apache.org/dubbo-go/v3/metadata/mapping" ) // MetadataReport is an interface of remote metadata report. type MetadataReport interface { - // StoreProviderMetadata stores the metadata. - // Metadata includes the basic info of the server, - // provider info, and other user custom info. - StoreProviderMetadata(*identifier.MetadataIdentifier, string) error - - // StoreConsumerMetadata stores the metadata. - // Metadata includes the basic info of the server, - // consumer info, and other user custom info. - StoreConsumerMetadata(*identifier.MetadataIdentifier, string) error - - // SaveServiceMetadata saves the metadata. - // Metadata includes the basic info of the server, - // service info, and other user custom info. - SaveServiceMetadata(*identifier.ServiceMetadataIdentifier, *common.URL) error - - // RemoveServiceMetadata removes the metadata. - RemoveServiceMetadata(*identifier.ServiceMetadataIdentifier) error - - // GetExportedURLs gets the urls. - // If not found, an empty list will be returned. - GetExportedURLs(*identifier.ServiceMetadataIdentifier) ([]string, error) - - // SaveSubscribedData saves the urls. - // If not found, an empty str will be returned. - SaveSubscribedData(*identifier.SubscriberMetadataIdentifier, string) error - - // GetSubscribedURLs gets the urls. - // If not found, an empty list will be returned. - GetSubscribedURLs(*identifier.SubscriberMetadataIdentifier) ([]string, error) - - // GetServiceDefinition gets the service definition. - GetServiceDefinition(*identifier.MetadataIdentifier) (string, error) - // GetAppMetadata get metadata info from report - GetAppMetadata(*identifier.SubscriberMetadataIdentifier) (*common.MetadataInfo, error) + GetAppMetadata(application, revision string) (*info.MetadataInfo, error) - // PublishAppMetadata publish metadata info to reportss - PublishAppMetadata(*identifier.SubscriberMetadataIdentifier, *common.MetadataInfo) error + // PublishAppMetadata publish metadata info to report + PublishAppMetadata(application, revision string, info *info.MetadataInfo) error // RegisterServiceAppMapping map the specified Dubbo service interface to current Dubbo app name - RegisterServiceAppMapping(string, string, string) error + RegisterServiceAppMapping(interfaceName, group string, application string) error // GetServiceAppMapping get the app names from the specified Dubbo service interface - GetServiceAppMapping(string, string, registry.MappingListener) (*gxset.HashSet, error) + GetServiceAppMapping(interfaceName, group string, l mapping.MappingListener) (*gxset.HashSet, error) // RemoveServiceAppMappingListener remove the serviceMapping listener by key and group - RemoveServiceAppMappingListener(string, string) error - - GetConfigKeysByGroup(group string) (*gxset.HashSet, error) + RemoveServiceAppMappingListener(interfaceName, group string) error } diff --git a/metadata/report/factory/report_factory.go b/metadata/report/report_factory.go similarity index 89% rename from metadata/report/factory/report_factory.go rename to metadata/report/report_factory.go index b58c023677..2c2af7349f 100644 --- a/metadata/report/factory/report_factory.go +++ b/metadata/report/report_factory.go @@ -15,16 +15,15 @@ * limitations under the License. */ -package factory +package report import ( "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/metadata/report" ) // MetadataReportFactory interface will create metadata report type MetadataReportFactory interface { - CreateMetadataReport(*common.URL) report.MetadataReport + CreateMetadataReport(*common.URL) MetadataReport } type BaseMetadataReportFactory struct{} diff --git a/metadata/report/reporter_metric.go b/metadata/report/reporter_metric.go deleted file mode 100644 index 5ca0995d15..0000000000 --- a/metadata/report/reporter_metric.go +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package report - -import ( - "time" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/constant" - "dubbo.apache.org/dubbo-go/v3/metadata/identifier" - "dubbo.apache.org/dubbo-go/v3/metrics" - "dubbo.apache.org/dubbo-go/v3/metrics/metadata" -) - -type PubMetricEventReport struct { - MetadataReport -} - -func NewPubMetricEventReport(r MetadataReport) MetadataReport { - return &PubMetricEventReport{MetadataReport: r} -} - -func (r *PubMetricEventReport) StoreProviderMetadata(i *identifier.MetadataIdentifier, s string) error { - event := metadata.NewMetadataMetricTimeEvent(metadata.StoreProvider) - err := r.MetadataReport.StoreProviderMetadata(i, s) - event.Succ = err == nil - event.End = time.Now() - event.Attachment[constant.InterfaceKey] = i.ServiceInterface - metrics.Publish(event) - return err -} - -func (r *PubMetricEventReport) GetAppMetadata(i *identifier.SubscriberMetadataIdentifier) (*common.MetadataInfo, error) { - event := metadata.NewMetadataMetricTimeEvent(metadata.MetadataSub) - info, err := r.MetadataReport.GetAppMetadata(i) - event.Succ = err == nil - event.End = time.Now() - metrics.Publish(event) - return info, err -} - -func (r *PubMetricEventReport) PublishAppMetadata(i *identifier.SubscriberMetadataIdentifier, info *common.MetadataInfo) error { - event := metadata.NewMetadataMetricTimeEvent(metadata.MetadataPush) - err := r.MetadataReport.PublishAppMetadata(i, info) - event.Succ = err == nil - event.End = time.Now() - metrics.Publish(event) - return err -} diff --git a/metadata/report/zookeeper/listener.go b/metadata/report/zookeeper/listener.go index 1dca921d05..84e79f6a44 100644 --- a/metadata/report/zookeeper/listener.go +++ b/metadata/report/zookeeper/listener.go @@ -29,6 +29,7 @@ import ( import ( "dubbo.apache.org/dubbo-go/v3/common/constant" + "dubbo.apache.org/dubbo-go/v3/metadata/mapping" "dubbo.apache.org/dubbo-go/v3/registry" "dubbo.apache.org/dubbo-go/v3/remoting" "dubbo.apache.org/dubbo-go/v3/remoting/zookeeper" @@ -48,7 +49,7 @@ func NewCacheListener(rootPath string, listener *zookeeper.ZkEventListener) *Cac } // AddListener will add a listener if loaded -func (l *CacheListener) AddListener(key string, listener registry.MappingListener) { +func (l *CacheListener) AddListener(key string, listener mapping.MappingListener) { // FIXME do not use Client.ExistW, cause it has a bug(can not watch zk node that do not exist) _, _, _, err := l.zkEventListener.Client.Conn.ExistsW(key) // reference from https://stackoverflow.com/questions/34018908/golang-why-dont-we-have-a-set-datastructure @@ -56,25 +57,25 @@ func (l *CacheListener) AddListener(key string, listener registry.MappingListene if err != nil { return } - listeners, loaded := l.keyListeners.LoadOrStore(key, map[registry.MappingListener]struct{}{listener: {}}) + listeners, loaded := l.keyListeners.LoadOrStore(key, map[mapping.MappingListener]struct{}{listener: {}}) if loaded { - listeners.(map[registry.MappingListener]struct{})[listener] = struct{}{} + listeners.(map[mapping.MappingListener]struct{})[listener] = struct{}{} l.keyListeners.Store(key, listeners) } } // RemoveListener will delete a listener if loaded -func (l *CacheListener) RemoveListener(key string, listener registry.MappingListener) { +func (l *CacheListener) RemoveListener(key string, listener mapping.MappingListener) { listeners, loaded := l.keyListeners.Load(key) if loaded { - delete(listeners.(map[registry.MappingListener]struct{}), listener) + delete(listeners.(map[mapping.MappingListener]struct{}), listener) } } // DataChange changes all listeners' event func (l *CacheListener) DataChange(event remoting.Event) bool { if listeners, ok := l.keyListeners.Load(event.Path); ok { - for listener := range listeners.(map[registry.MappingListener]struct{}) { + for listener := range listeners.(map[mapping.MappingListener]struct{}) { appNames := strings.Split(event.Content, constant.CommaSeparator) set := gxset.NewSet() for _, e := range appNames { diff --git a/metadata/report/zookeeper/report.go b/metadata/report/zookeeper/report.go index 93dde1bac4..2d80f5541d 100644 --- a/metadata/report/zookeeper/report.go +++ b/metadata/report/zookeeper/report.go @@ -36,11 +36,10 @@ 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/metadata/identifier" + "dubbo.apache.org/dubbo-go/v3/metadata/info" + "dubbo.apache.org/dubbo-go/v3/metadata/mapping" "dubbo.apache.org/dubbo-go/v3/metadata/mapping/metadata" "dubbo.apache.org/dubbo-go/v3/metadata/report" - "dubbo.apache.org/dubbo-go/v3/metadata/report/factory" - "dubbo.apache.org/dubbo-go/v3/registry" "dubbo.apache.org/dubbo-go/v3/remoting/zookeeper" ) @@ -48,7 +47,7 @@ var emptyStrSlice = make([]string, 0) func init() { mf := &zookeeperMetadataReportFactory{} - extension.SetMetadataReportFactory("zookeeper", func() factory.MetadataReportFactory { + extension.SetMetadataReportFactory("zookeeper", func() report.MetadataReportFactory { return mf }) } @@ -63,13 +62,13 @@ type zookeeperMetadataReport struct { } // GetAppMetadata get metadata info from zookeeper -func (m *zookeeperMetadataReport) GetAppMetadata(metadataIdentifier *identifier.SubscriberMetadataIdentifier) (*common.MetadataInfo, error) { - k := m.rootDir + metadataIdentifier.GetFilePathKey() +func (m *zookeeperMetadataReport) GetAppMetadata(application, revision string) (*info.MetadataInfo, error) { + k := m.rootDir + application + constant.PathSeparator + revision data, _, err := m.client.GetContent(k) if err != nil { return nil, err } - var metadataInfo common.MetadataInfo + var metadataInfo info.MetadataInfo err = json.Unmarshal(data, &metadataInfo) if err != nil { return nil, err @@ -78,9 +77,9 @@ func (m *zookeeperMetadataReport) GetAppMetadata(metadataIdentifier *identifier. } // PublishAppMetadata publish metadata info to zookeeper -func (m *zookeeperMetadataReport) PublishAppMetadata(metadataIdentifier *identifier.SubscriberMetadataIdentifier, info *common.MetadataInfo) error { - k := m.rootDir + metadataIdentifier.GetFilePathKey() - data, err := json.Marshal(info) +func (m *zookeeperMetadataReport) PublishAppMetadata(application, revision string, meta *info.MetadataInfo) error { + k := m.rootDir + application + constant.PathSeparator + revision + data, err := json.Marshal(meta) if err != nil { return err } @@ -92,76 +91,9 @@ func (m *zookeeperMetadataReport) PublishAppMetadata(metadataIdentifier *identif return err } -// StoreProviderMetadata stores the metadata. -func (m *zookeeperMetadataReport) StoreProviderMetadata(providerIdentifier *identifier.MetadataIdentifier, serviceDefinitions string) error { - k := m.rootDir + providerIdentifier.GetFilePathKey() - err := m.client.CreateWithValue(k, []byte(serviceDefinitions)) - if perrors.Is(err, zk.ErrNodeExists) { - logger.Debugf("Try to store provider metadata failed. In most cases, it's not a problem. ") - return nil - } - return err -} - -// StoreConsumerMetadata stores the metadata. -func (m *zookeeperMetadataReport) StoreConsumerMetadata(consumerMetadataIdentifier *identifier.MetadataIdentifier, serviceParameterString string) error { - k := m.rootDir + consumerMetadataIdentifier.GetFilePathKey() - err := m.client.CreateWithValue(k, []byte(serviceParameterString)) - if perrors.Is(err, zk.ErrNodeExists) { - logger.Debugf("Try to store consumer metadata failed. In most cases, it's not a problem. ") - return nil - } - return err -} - -// SaveServiceMetadata saves the metadata. -func (m *zookeeperMetadataReport) SaveServiceMetadata(metadataIdentifier *identifier.ServiceMetadataIdentifier, url *common.URL) error { - k := m.rootDir + metadataIdentifier.GetFilePathKey() - return m.client.CreateWithValue(k, []byte(url.String())) -} - -// RemoveServiceMetadata removes the metadata. -func (m *zookeeperMetadataReport) RemoveServiceMetadata(metadataIdentifier *identifier.ServiceMetadataIdentifier) error { - k := m.rootDir + metadataIdentifier.GetFilePathKey() - return m.client.Delete(k) -} - -// GetExportedURLs gets the urls. -func (m *zookeeperMetadataReport) GetExportedURLs(metadataIdentifier *identifier.ServiceMetadataIdentifier) ([]string, error) { - k := m.rootDir + metadataIdentifier.GetFilePathKey() - v, _, err := m.client.GetContent(k) - if err != nil || len(v) == 0 { - return emptyStrSlice, err - } - return []string{string(v)}, nil -} - -// SaveSubscribedData saves the urls. -func (m *zookeeperMetadataReport) SaveSubscribedData(subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier, urls string) error { - k := m.rootDir + subscriberMetadataIdentifier.GetFilePathKey() - return m.client.CreateWithValue(k, []byte(urls)) -} - -// GetSubscribedURLs gets the urls. -func (m *zookeeperMetadataReport) GetSubscribedURLs(subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier) ([]string, error) { - k := m.rootDir + subscriberMetadataIdentifier.GetFilePathKey() - v, _, err := m.client.GetContent(k) - if err != nil || len(v) == 0 { - return emptyStrSlice, err - } - return []string{string(v)}, nil -} - -// GetServiceDefinition gets the service definition. -func (m *zookeeperMetadataReport) GetServiceDefinition(metadataIdentifier *identifier.MetadataIdentifier) (string, error) { - k := m.rootDir + metadataIdentifier.GetFilePathKey() - v, _, err := m.client.GetContent(k) - return string(v), err -} - // RegisterServiceAppMapping map the specified Dubbo service interface to current Dubbo app name func (m *zookeeperMetadataReport) RegisterServiceAppMapping(key string, group string, value string) error { - path := m.getPath(key, group) + path := m.rootDir + group + constant.PathSeparator + key v, state, err := m.client.GetContent(path) if err == zk.ErrNoNode { return m.client.CreateWithValue(path, []byte(value)) @@ -178,7 +110,7 @@ func (m *zookeeperMetadataReport) RegisterServiceAppMapping(key string, group st } // GetServiceAppMapping get the app names from the specified Dubbo service interface -func (m *zookeeperMetadataReport) GetServiceAppMapping(key string, group string, listener registry.MappingListener) (*gxset.HashSet, error) { +func (m *zookeeperMetadataReport) GetServiceAppMapping(key string, group string, listener mapping.MappingListener) (*gxset.HashSet, error) { path := m.rootDir + group + constant.PathSeparator + key // listen to mapping changes first @@ -198,42 +130,10 @@ func (m *zookeeperMetadataReport) GetServiceAppMapping(key string, group string, return set, nil } -// GetConfigKeysByGroup will return all keys with the group -func (m *zookeeperMetadataReport) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { - path := m.getPath("", group) - result, err := m.client.GetChildren(path) - if err != nil { - return nil, perrors.WithStack(err) - } - - if len(result) == 0 { - return nil, perrors.New("could not find keys with group: " + group) - } - set := gxset.NewSet() - for _, e := range result { - set.Add(e) - } - return set, nil -} - func (m *zookeeperMetadataReport) RemoveServiceAppMappingListener(key string, group string) error { return nil } -func (m *zookeeperMetadataReport) getPath(key string, group string) string { - if len(key) == 0 { - return m.buildPath(group) - } - return m.buildPath(group) + constant.PathSeparator + key -} - -func (m *zookeeperMetadataReport) buildPath(group string) string { - if len(group) == 0 { - group = metadata.DefaultGroup - } - return m.rootDir + group -} - type zookeeperMetadataReportFactory struct{} // nolint diff --git a/metadata/report_instance.go b/metadata/report_instance.go new file mode 100644 index 0000000000..2238144f09 --- /dev/null +++ b/metadata/report_instance.go @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package metadata + +import ( + "strings" + "time" +) + +import ( + "github.com/dubbogo/gost/container/set" + + 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/metadata/info" + "dubbo.apache.org/dubbo-go/v3/metadata/mapping" + "dubbo.apache.org/dubbo-go/v3/metadata/report" + "dubbo.apache.org/dubbo-go/v3/metrics" + metadataMetrics "dubbo.apache.org/dubbo-go/v3/metrics/metadata" +) + +var ( + instances = make(map[string]report.MetadataReport) +) + +func toUrl(opts *ReportOptions) (*common.URL, error) { + res, err := common.NewURL(opts.Address, + common.WithUsername(opts.Username), + common.WithPassword(opts.Password), + common.WithLocation(opts.Address), + common.WithProtocol(opts.Protocol), + common.WithParamsValue(constant.TimeoutKey, opts.Timeout), + common.WithParamsValue(constant.MetadataReportGroupKey, opts.Group), + common.WithParamsValue(constant.MetadataReportNamespaceKey, opts.Namespace), + common.WithParamsValue(constant.ClientNameKey, strings.Join([]string{constant.MetadataReportPrefix, opts.Protocol, opts.Address}, "-")), + ) + if err != nil || len(res.Protocol) == 0 { + return nil, perrors.New("Invalid MetadataReport Config.") + } + res.SetParam("metadata", res.Protocol) + for key, val := range opts.Params { + res.SetParam(key, val) + } + return res, nil +} + +func GetMetadataReport() report.MetadataReport { + for _, v := range instances { + return v + } + return nil +} + +func GetMetadataReportByRegistry(registry string) report.MetadataReport { + if len(registry) == 0 { + registry = constant.DefaultKey + } + if r, ok := instances[registry]; ok { + return r + } + return GetMetadataReport() +} + +func GetMetadataReports() []report.MetadataReport { + reports := make([]report.MetadataReport, len(instances)) + index := 0 + for _, r := range instances { + reports[index] = r + index++ + } + return reports +} + +func GetMetadataType() string { + return metadataOptions.metadataType +} + +// DelegateMetadataReport is a absolute delegate for DelegateMetadataReport +type DelegateMetadataReport struct { + instance report.MetadataReport +} + +// PublishAppMetadata delegate publish metadata info +func (d *DelegateMetadataReport) PublishAppMetadata(application, revision string, meta *info.MetadataInfo) error { + event := metadataMetrics.NewMetadataMetricTimeEvent(metadataMetrics.MetadataPush) + err := d.instance.PublishAppMetadata(application, revision, meta) + event.Succ = err == nil + event.End = time.Now() + metrics.Publish(event) + return err +} + +// GetAppMetadata delegate get metadata info +func (d *DelegateMetadataReport) GetAppMetadata(application, revision string) (*info.MetadataInfo, error) { + event := metadataMetrics.NewMetadataMetricTimeEvent(metadataMetrics.MetadataSub) + meta, err := d.instance.GetAppMetadata(application, revision) + event.Succ = err == nil + event.End = time.Now() + metrics.Publish(event) + return meta, err +} + +func (d *DelegateMetadataReport) GetServiceAppMapping(application string, group string, listener mapping.MappingListener) (*gxset.HashSet, error) { + return d.instance.GetServiceAppMapping(application, group, listener) +} + +func (d *DelegateMetadataReport) RegisterServiceAppMapping(interfaceName, group string, application string) error { + return d.instance.RegisterServiceAppMapping(interfaceName, group, application) +} + +func (d *DelegateMetadataReport) RemoveServiceAppMappingListener(interfaceName, group string) error { + return d.instance.RemoveServiceAppMappingListener(interfaceName, group) +} diff --git a/metadata/service/exporter/configurable/exporter.go b/metadata/service/exporter/configurable/exporter.go deleted file mode 100644 index 6fcdc444a3..0000000000 --- a/metadata/service/exporter/configurable/exporter.go +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package configurable - -import ( - "sync" -) - -import ( - "github.com/dubbogo/gost/log/logger" -) - -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/mapping/metadata" - "dubbo.apache.org/dubbo-go/v3/metadata/service" - "dubbo.apache.org/dubbo-go/v3/metadata/service/exporter" - _ "dubbo.apache.org/dubbo-go/v3/metadata/service/remote" - _ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo" -) - -// MetadataServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface -type MetadataServiceExporter struct { - ServiceConfig *config.ServiceConfig - lock sync.RWMutex - metadataService service.MetadataService -} - -func init() { - extension.SetMetadataServiceExporter(constant.DefaultKey, NewMetadataServiceExporter) -} - -// NewMetadataServiceExporter will return a service_exporter.MetadataServiceExporter with the specified metadata service -func NewMetadataServiceExporter(metadataService service.MetadataService) exporter.MetadataServiceExporter { - return &MetadataServiceExporter{ - metadataService: metadataService, - } -} - -// Export will export the metadataService -func (exporter *MetadataServiceExporter) Export(url *common.URL) error { - if !exporter.IsExported() { - version, _ := exporter.metadataService.Version() - 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).SetPort(getMetadataPort()). - Build()). - SetRegistryIDs("N/A"). - SetInterface(constant.MetadataServiceName). - SetGroup(config.GetApplicationConfig().Name). - SetVersion(version). - SetProxyFactoryKey(constant.DefaultKey). - SetMetadataType(config.GetApplicationConfig().MetadataType). - Build() - exporter.ServiceConfig.Implement(exporter.metadataService) - err := exporter.ServiceConfig.Export() - - 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 -} - -func getMetadataPort() string { - rootConfig := config.GetRootConfig() - port := rootConfig.Application.MetadataServicePort - if port == "" { - protocolConfig, ok := rootConfig.Protocols[constant.DefaultProtocol] - if ok { - port = protocolConfig.Port - } else { - logger.Warnf("[Metadata Service] Dubbo-go %s version's MetadataService only support dubbo protocol,"+ - "MetadataService will use random port", - constant.Version) - } - } - return port -} - -// Unexport will unexport the metadataService -func (exporter *MetadataServiceExporter) Unexport() { - if exporter.IsExported() { - exporter.ServiceConfig.Unexport() - } -} - -// GetExportedURLs will return the urls that export use. -// Notice!The exported url is not same as url in registry , for example it lack the ip. -func (exporter *MetadataServiceExporter) GetExportedURLs() []*common.URL { - return exporter.ServiceConfig.GetExportedUrls() -} - -// 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() -} diff --git a/metadata/service/exporter/configurable/exporter_test.go b/metadata/service/exporter/configurable/exporter_test.go deleted file mode 100644 index aeb260cfde..0000000000 --- a/metadata/service/exporter/configurable/exporter_test.go +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package configurable - -import ( - "testing" -) - -import ( - "github.com/stretchr/testify/assert" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/config" - _ "dubbo.apache.org/dubbo-go/v3/filter/filter_impl" - "dubbo.apache.org/dubbo-go/v3/metadata/service/local" - _ "dubbo.apache.org/dubbo-go/v3/metrics/prometheus" - _ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo" - _ "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory" - "dubbo.apache.org/dubbo-go/v3/remoting/getty" -) - -func TestConfigurableExporter(t *testing.T) { - getty.SetServerConfig(getty.ServerConfig{ - SessionNumber: 700, - SessionTimeout: "20s", - GettySessionParam: getty.GettySessionParam{ - CompressEncoding: false, - TcpNoDelay: true, - TcpKeepAlive: true, - KeepAlivePeriod: "120s", - TcpRBufSize: 262144, - TcpWBufSize: 65536, - TcpReadTimeout: "1s", - TcpWriteTimeout: "5s", - WaitTimeout: "1s", - MaxMsgLen: 10240000000, - SessionName: "server", - }, - }) - mockInitProviderWithSingleRegistry() - metadataService, _ := local.GetLocalMetadataService() - exported := NewMetadataServiceExporter(metadataService) - - 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://:[0-9]{1,}/org.apache.dubbo.metadata.MetadataService*", exported.GetExportedURLs()[0].String()) - exported.Unexport() - assert.Equal(t, false, exported.IsExported()) - }) -} - -// mockInitProviderWithSingleRegistry will init a mocked providerConfig -func mockInitProviderWithSingleRegistry() { - providerConfig := config.NewProviderConfigBuilder().AddService("MockService", config.NewServiceConfigBuilder().Build()).Build() - providerConfig.Services["MockService"].InitExported() - config.SetRootConfig(config.RootConfig{ - Application: &config.ApplicationConfig{ - Organization: "dubbo_org", - Name: "dubbo", - Module: "module", - Version: "1.0.0", - Owner: "dubbo", - Environment: "test", - }, - Registries: map[string]*config.RegistryConfig{ - "mock": { - Address: "mock://127.0.0.1:2181", - Username: "user1", - Password: "pwd1", - }, - }, - Protocols: map[string]*config.ProtocolConfig{ - "mock": { - Name: "mock", - Ip: "127.0.0.1", - Port: "20000", - }, - }, - - Provider: &config.ProviderConfig{ - Services: map[string]*config.ServiceConfig{ - "MockService": { - Interface: "com.MockService", - ProtocolIDs: []string{"mock"}, - Cluster: "failover", - Loadbalance: "random", - Retries: "3", - Group: "huadong_idc", - Version: "1.0.0", - Methods: []*config.MethodConfig{ - { - Name: "GetUser", - Retries: "2", - LoadBalance: "random", - Weight: 200, - }, - { - Name: "GetUser1", - Retries: "2", - LoadBalance: "random", - Weight: 200, - }, - }, - }, - }, - }, - }) -} diff --git a/metadata/service/exporter/exporter.go b/metadata/service/exporter/exporter.go deleted file mode 100644 index ab4e2feddf..0000000000 --- a/metadata/service/exporter/exporter.go +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package exporter - -import ( - "dubbo.apache.org/dubbo-go/v3/common" -) - -// MetadataServiceExporter will export & unexport the metadata service, get exported url, and return is exported or not -type MetadataServiceExporter interface { - Export(url *common.URL) error - Unexport() - GetExportedURLs() []*common.URL - IsExported() bool -} diff --git a/metadata/service/local/metadata_service_proxy_factory.go b/metadata/service/local/metadata_service_proxy_factory.go deleted file mode 100644 index b914778054..0000000000 --- a/metadata/service/local/metadata_service_proxy_factory.go +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package local - -import ( - "encoding/json" - "sync" -) - -import ( - "github.com/dubbogo/gost/log/logger" -) - -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/metadata/service" - "dubbo.apache.org/dubbo-go/v3/registry" -) - -func init() { - factory := service.NewBaseMetadataServiceProxyFactory(createProxy) - extension.SetMetadataServiceProxyFactory(constant.DefaultKey, func() service.MetadataServiceProxyFactory { - return factory - }) -} - -var ( - factory service.MetadataServiceProxyFactory - once *sync.Once -) - -func GetInMemoryMetadataServiceProxyFactory() service.MetadataServiceProxyFactory { - once.Do(func() { - factory = service.NewBaseMetadataServiceProxyFactory(createProxy) - }) - return factory -} - -// createProxy creates an instance of MetadataServiceProxy -// we read the metadata from ins.Metadata() -// and then create an Invoker instance -// also we will mark this proxy as golang's proxy -func createProxy(ins registry.ServiceInstance) service.MetadataService { - urls := buildStandardMetadataServiceURL(ins) - if len(urls) == 0 { - logger.Errorf("metadata service urls not found, %v", ins) - return nil - } - - u := urls[0] - p := extension.GetProtocol(u.Protocol) - invoker := p.Refer(u) - return &MetadataServiceProxy{ - Invoker: invoker, - } -} - -// buildStandardMetadataServiceURL will use standard format to build the metadata service url. -func buildStandardMetadataServiceURL(ins registry.ServiceInstance) []*common.URL { - ps := getMetadataServiceUrlParams(ins) - if ps[constant.ProtocolKey] == "" { - return nil - } - res := make([]*common.URL, 0, len(ps)) - sn := ins.GetServiceName() - host := ins.GetHost() - convertedParams := make(map[string][]string, len(ps)) - for k, v := range ps { - convertedParams[k] = []string{v} - } - u := common.NewURLWithOptions(common.WithIp(host), - common.WithPath(constant.MetadataServiceName), - common.WithProtocol(ps[constant.ProtocolKey]), - common.WithPort(ps[constant.PortKey]), - common.WithParams(convertedParams), - common.WithParamsValue(constant.GroupKey, sn), - common.WithParamsValue(constant.InterfaceKey, constant.MetadataServiceName)) - res = append(res, u) - - return res -} - -// getMetadataServiceUrlParams this will convert the metadata service url parameters to map structure -// it looks like: -// {"dubbo":{"timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"20880"}} -func getMetadataServiceUrlParams(ins registry.ServiceInstance) map[string]string { - ps := ins.GetMetadata() - res := make(map[string]string, 2) - if str, ok := ps[constant.MetadataServiceURLParamsPropertyName]; ok && len(str) > 0 { - - err := json.Unmarshal([]byte(str), &res) - if err != nil { - logger.Errorf("could not parse the metadata service url parameters to map", err) - } - } - return res -} diff --git a/metadata/service/local/metadata_service_proxy_factory_test.go b/metadata/service/local/metadata_service_proxy_factory_test.go deleted file mode 100644 index 98aafced69..0000000000 --- a/metadata/service/local/metadata_service_proxy_factory_test.go +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package local - -import ( - "context" - "encoding/json" - "testing" -) - -import ( - "github.com/stretchr/testify/assert" -) - -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/protocol" - "dubbo.apache.org/dubbo-go/v3/registry" -) - -func TestMetadataService_GetMetadataServiceUrlParams(t *testing.T) { - str := `{"dubbo":{"timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"20880"}}` - tmp := make(map[string]map[string]string) - err := json.Unmarshal([]byte(str), &tmp) - assert.Nil(t, err) -} - -func TestCreateProxy(t *testing.T) { - extension.SetProtocol("mock", func() protocol.Protocol { - return &mockProtocol{} - }) - ins := ®istry.DefaultServiceInstance{ - ID: "test-id", - ServiceName: "com.dubbo", - Host: "localhost", - Port: 8080, - Enable: true, - Healthy: true, - } - pxy := createProxy(ins) - assert.Nil(t, pxy) - - ins.Metadata = map[string]string{constant.MetadataServiceURLParamsPropertyName: `{"protocol":"mock","timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"20880"}`} - pxy = createProxy(ins) - assert.NotNil(t, pxy) -} - -type mockProtocol struct{} - -func (m mockProtocol) Export(protocol.Invoker) protocol.Exporter { - panic("implement me") -} - -func (m mockProtocol) Refer(*common.URL) protocol.Invoker { - return &mockInvoker{} -} - -func (m mockProtocol) Destroy() { - panic("implement me") -} - -type mockInvoker struct{} - -func (m *mockInvoker) GetURL() *common.URL { - panic("implement me") -} - -func (m *mockInvoker) IsAvailable() bool { - panic("implement me") -} - -func (m *mockInvoker) Destroy() { - panic("implement me") -} - -func (m *mockInvoker) Invoke(context.Context, protocol.Invocation) protocol.Result { - return &protocol.RPCResult{Rest: []string{"dubbo://localhost"}} -} diff --git a/metadata/service/local/service.go b/metadata/service/local/service.go deleted file mode 100644 index 440953e070..0000000000 --- a/metadata/service/local/service.go +++ /dev/null @@ -1,282 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package local - -import ( - "sort" - "sync" -) - -import ( - "github.com/Workiva/go-datastructures/slice/skip" - - "github.com/dubbogo/gost/log/logger" -) - -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/definition" - "dubbo.apache.org/dubbo-go/v3/metadata/service" -) - -func init() { - extension.SetLocalMetadataService(constant.DefaultKey, GetLocalMetadataService) -} - -// version will be used by Version func -const ( - version = "1.0.0" -) - -// MetadataService is store and query the metadata info in memory when each service registry -type MetadataService struct { - service.BaseMetadataService - exportedServiceURLs *sync.Map - subscribedServiceURLs *sync.Map - serviceDefinitions *sync.Map - lock *sync.RWMutex - mOnce *sync.Once - metadataInfo *common.MetadataInfo - metadataServiceURL *common.URL -} - -var ( - metadataServiceInstance *MetadataService - metadataServiceInitOnce sync.Once -) - -// GetLocalMetadataService which should be singleton initiates a metadata service -func GetLocalMetadataService() (service.MetadataService, error) { - metadataServiceInitOnce.Do(func() { - metadataServiceInstance = &MetadataService{ - // todo(DMwangnima): use external config - BaseMetadataService: service.NewBaseMetadataService(config.GetApplicationConfig().Name), - exportedServiceURLs: &sync.Map{}, - subscribedServiceURLs: &sync.Map{}, - serviceDefinitions: &sync.Map{}, - lock: &sync.RWMutex{}, - metadataInfo: nil, - mOnce: &sync.Once{}, - } - }) - return metadataServiceInstance, nil -} - -// addURL will add URL in memory -func (mts *MetadataService) addURL(targetMap *sync.Map, url *common.URL) bool { - var ( - urlSet interface{} - loaded bool - ) - logger.Debug(url.ServiceKey()) - if urlSet, loaded = targetMap.LoadOrStore(url.ServiceKey(), skip.New(uint64(0))); loaded { - mts.lock.RLock() - wantedUrl := urlSet.(*skip.SkipList).Get(url) - if len(wantedUrl) > 0 && wantedUrl[0] != nil { - mts.lock.RUnlock() - return false - } - mts.lock.RUnlock() - } - mts.lock.Lock() - // double chk - wantedUrl := urlSet.(*skip.SkipList).Get(url) - if len(wantedUrl) > 0 && wantedUrl[0] != nil { - mts.lock.Unlock() - return false - } - urlSet.(*skip.SkipList).Insert(url) - mts.lock.Unlock() - return true -} - -// removeURL is used to remove specified url -func (mts *MetadataService) removeURL(targetMap *sync.Map, url *common.URL) { - if value, loaded := targetMap.Load(url.ServiceKey()); loaded { - mts.lock.Lock() - value.(*skip.SkipList).Delete(url) - mts.lock.Unlock() - mts.lock.RLock() - defer mts.lock.RUnlock() - if value.(*skip.SkipList).Len() == 0 { - targetMap.Delete(url.ServiceKey()) - } - } -} - -// getAllService can return all the exportedUrlString except for metadataService -func (mts *MetadataService) getAllService(services *sync.Map) []*common.URL { - // using skip list to dedup and sorting - var res []*common.URL - services.Range(func(key, value interface{}) bool { - urls := value.(*skip.SkipList) - for i := uint64(0); i < urls.Len(); i++ { - url := urls.ByPosition(i).(*common.URL) - if url.Service() != constant.MetadataServiceName { - res = append(res, url) - } - } - return true - }) - sort.Sort(common.URLSlice(res)) - return res -} - -// getSpecifiedService can return specified service url by serviceKey -func (mts *MetadataService) getSpecifiedService(services *sync.Map, serviceKey string, protocol string) []*common.URL { - var res []*common.URL - serviceList, loaded := services.Load(serviceKey) - if loaded { - urls := serviceList.(*skip.SkipList) - for i := uint64(0); i < urls.Len(); i++ { - url := urls.ByPosition(i).(*common.URL) - if len(protocol) == 0 || protocol == constant.AnyValue || url.Protocol == protocol || url.GetParam(constant.ProtocolKey, "") == protocol { - res = append(res, url) - } - } - sort.Stable(common.URLSlice(res)) - } - return res -} - -// ExportURL can store the in memory -func (mts *MetadataService) ExportURL(url *common.URL) (bool, error) { - if constant.MetadataServiceName == url.GetParam(constant.InterfaceKey, "") { - mts.metadataServiceURL = url - return true, nil - } - mts.mOnce.Do(func() { - mts.metadataInfo = common.NewMetadataInfWithApp(config.GetApplicationConfig().Name) - }) - mts.metadataInfo.AddService(common.NewServiceInfoWithURL(url)) - return mts.addURL(mts.exportedServiceURLs, url), nil -} - -// UnexportURL can remove the url store in memory -func (mts *MetadataService) UnexportURL(url *common.URL) error { - if constant.MetadataServiceName == url.GetParam(constant.InterfaceKey, "") { - mts.metadataServiceURL = nil - return nil - } - if mts.metadataInfo != nil { - mts.metadataInfo.RemoveService(common.NewServiceInfoWithURL(url)) - } - mts.removeURL(mts.exportedServiceURLs, url) - return nil -} - -// SubscribeURL can store the in memory -func (mts *MetadataService) SubscribeURL(url *common.URL) (bool, error) { - return mts.addURL(mts.subscribedServiceURLs, url), nil -} - -// UnsubscribeURL can remove the url store in memory -func (mts *MetadataService) UnsubscribeURL(url *common.URL) error { - mts.removeURL(mts.subscribedServiceURLs, url) - return nil -} - -// PublishServiceDefinition publish url's service metadata info, and write into memory -func (mts *MetadataService) PublishServiceDefinition(url *common.URL) error { - if common.RoleType(common.CONSUMER).Role() == url.GetParam(constant.SideKey, "") { - return nil - } - interfaceName := url.GetParam(constant.InterfaceKey, "") - isGeneric := url.GetParamBool(constant.GenericKey, false) - if len(interfaceName) > 0 && !isGeneric { - tmpService := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey()) - sd := definition.BuildFullDefinition(*tmpService, url) - data, err := sd.ToBytes() - if err != nil { - logger.Errorf("publishProvider getServiceDescriptor error. providerUrl:%v , error:%v ", url, err) - return nil - } - mts.serviceDefinitions.Store(url.ServiceKey(), string(data)) - return nil - } - logger.Errorf("publishProvider interfaceName is empty . providerUrl:%v ", url) - return nil -} - -// GetExportedURLs get all exported urls -func (mts *MetadataService) GetExportedURLs(serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) { - if serviceInterface == constant.AnyValue { - return mts.getAllService(mts.exportedServiceURLs), nil - } else { - serviceKey := definition.ServiceDescriperBuild(serviceInterface, group, version) - return mts.getSpecifiedService(mts.exportedServiceURLs, serviceKey, protocol), nil - } -} - -// GetSubscribedURLs get all subscribedUrl -func (mts *MetadataService) GetSubscribedURLs() ([]*common.URL, error) { - return mts.getAllService(mts.subscribedServiceURLs), nil -} - -// GetServiceDefinition can get service definition by interfaceName, group and version -func (mts *MetadataService) GetServiceDefinition(interfaceName string, group string, version string) (string, error) { - serviceKey := definition.ServiceDescriperBuild(interfaceName, group, version) - v, _ := mts.serviceDefinitions.Load(serviceKey) - return v.(string), nil -} - -// GetServiceDefinitionByServiceKey can get service definition by serviceKey -func (mts *MetadataService) GetServiceDefinitionByServiceKey(serviceKey string) (string, error) { - v, _ := mts.serviceDefinitions.Load(serviceKey) - return v.(string), nil -} - -// GetMetadataInfo can get metadata in memory -func (mts *MetadataService) GetMetadataInfo(revision string) (*common.MetadataInfo, error) { - if revision == "" { - return mts.metadataInfo, nil - } - if mts.metadataInfo.CalAndGetRevision() != revision { - return nil, nil - } - return mts.metadataInfo, nil -} - -// GetExportedServiceURLs get exported service urls -func (mts *MetadataService) GetExportedServiceURLs() ([]*common.URL, error) { - return mts.getAllService(mts.exportedServiceURLs), nil -} - -// RefreshMetadata will always return true because it will be implement by remote service -func (mts *MetadataService) RefreshMetadata(string, string) (bool, error) { - return true, nil -} - -// Version will return the version of metadata service -func (mts *MetadataService) Version() (string, error) { - return version, nil -} - -// GetMetadataServiceURL get url of MetadataService -func (mts *MetadataService) GetMetadataServiceURL() (*common.URL, error) { - return mts.metadataServiceURL, nil -} - -// SetMetadataServiceURL save url of MetadataService -func (mts *MetadataService) SetMetadataServiceURL(url *common.URL) error { - mts.metadataServiceURL = url - return nil -} diff --git a/metadata/service/local/service_proxy.go b/metadata/service/local/service_proxy.go deleted file mode 100644 index 47421b110f..0000000000 --- a/metadata/service/local/service_proxy.go +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package local - -import ( - "context" - "reflect" -) - -import ( - "github.com/dubbogo/gost/log/logger" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/constant" - "dubbo.apache.org/dubbo-go/v3/protocol" - "dubbo.apache.org/dubbo-go/v3/protocol/invocation" -) - -// MetadataServiceProxy actually is a RPC stub which will only be used by client-side. -// If the metadata service is "local" metadata service in server side, which means that -// metadata service is RPC service too. So in client-side, if we want to get the metadata -// information, we must call metadata service .This is the stub, or proxy for now, only -// GetMetadataInfo need to be implemented. -// TODO use ProxyFactory to create proxy -type MetadataServiceProxy struct { - Invoker protocol.Invoker -} - -// nolint -func (m *MetadataServiceProxy) GetExportedURLs(serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) { - siV := reflect.ValueOf(serviceInterface) - gV := reflect.ValueOf(group) - vV := reflect.ValueOf(version) - pV := reflect.ValueOf(protocol) - - const methodName = "getExportedURLs" - - inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName(methodName), - invocation.WithArguments([]interface{}{siV.Interface(), gV.Interface(), vV.Interface(), pV.Interface()}), - invocation.WithReply(reflect.ValueOf(&[]interface{}{}).Interface()), - invocation.WithAttachments(map[string]interface{}{constant.AsyncKey: "false"}), - invocation.WithParameterValues([]reflect.Value{siV, gV, vV, pV})) - - res := m.Invoker.Invoke(context.Background(), inv) - if res.Error() != nil { - logger.Errorf("could not get the metadata service from remote provider: %v", res.Error()) - return []*common.URL{}, nil - } - - urlStrs := res.Result().([]string) - ret := make([]*common.URL, 0, len(urlStrs)) - for _, v := range urlStrs { - tempURL, err := common.NewURL(v) - if err != nil { - return []*common.URL{}, err - } - ret = append(ret, tempURL) - } - return ret, nil -} - -// nolint -func (m *MetadataServiceProxy) GetExportedServiceURLs() ([]*common.URL, error) { - logger.Error("you should never invoke this implementation") - return nil, nil -} - -// nolint -func (m *MetadataServiceProxy) GetMetadataServiceURL() (*common.URL, error) { - logger.Error("you should never invoke this implementation") - return nil, nil -} - -// nolint -func (m *MetadataServiceProxy) SetMetadataServiceURL(*common.URL) error { - logger.Error("you should never invoke this implementation") - return nil -} - -// nolint -func (m *MetadataServiceProxy) MethodMapper() map[string]string { - return map[string]string{} -} - -// nolint -func (m *MetadataServiceProxy) Reference() string { - logger.Error("you should never invoke this implementation") - return constant.MetadataServiceName -} - -// nolint -func (m *MetadataServiceProxy) ServiceName() (string, error) { - logger.Error("you should never invoke this implementation") - return "", nil -} - -// nolint -func (m *MetadataServiceProxy) ExportURL(url *common.URL) (bool, error) { - logger.Error("you should never invoke this implementation") - return false, nil -} - -// nolint -func (m *MetadataServiceProxy) UnexportURL(url *common.URL) error { - logger.Error("you should never invoke this implementation") - return nil -} - -// nolint -func (m *MetadataServiceProxy) SubscribeURL(url *common.URL) (bool, error) { - logger.Error("you should never invoke this implementation") - return false, nil -} - -// nolint -func (m *MetadataServiceProxy) UnsubscribeURL(url *common.URL) error { - logger.Error("you should never invoke this implementation") - return nil -} - -// nolint -func (m *MetadataServiceProxy) PublishServiceDefinition(url *common.URL) error { - logger.Error("you should never invoke this implementation") - return nil -} - -// nolint -func (m *MetadataServiceProxy) GetSubscribedURLs() ([]*common.URL, error) { - logger.Error("you should never invoke this implementation") - return nil, nil -} - -// nolint -func (m *MetadataServiceProxy) GetServiceDefinition(interfaceName string, group string, version string) (string, error) { - logger.Error("you should never invoke this implementation") - return "", nil -} - -// nolint -func (m *MetadataServiceProxy) GetServiceDefinitionByServiceKey(serviceKey string) (string, error) { - logger.Error("you should never invoke this implementation") - return "", nil -} - -// nolint -func (m *MetadataServiceProxy) RefreshMetadata(exportedRevision string, subscribedRevision string) (bool, error) { - logger.Error("you should never invoke this implementation") - return false, nil -} - -// nolint -func (m *MetadataServiceProxy) Version() (string, error) { - logger.Error("you should never invoke this implementation") - return "", nil -} - -// nolint -func (m *MetadataServiceProxy) GetMetadataInfo(revision string) (*common.MetadataInfo, error) { - rV := reflect.ValueOf(revision) - const methodName = "getMetadataInfo" - inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName(methodName), - invocation.WithArguments([]interface{}{rV.Interface()}), - invocation.WithReply(reflect.ValueOf(&common.MetadataInfo{}).Interface()), - invocation.WithAttachments(map[string]interface{}{constant.AsyncKey: "false"}), - invocation.WithParameterValues([]reflect.Value{rV})) - res := m.Invoker.Invoke(context.Background(), inv) - if res.Error() != nil { - logger.Errorf("could not get the metadata info from remote provider: %v", res.Error()) - return nil, res.Error() - } - metaDataInfo := res.Result().(*common.MetadataInfo) - return metaDataInfo, nil -} diff --git a/metadata/service/local/service_proxy_test.go b/metadata/service/local/service_proxy_test.go deleted file mode 100644 index 6c89076c20..0000000000 --- a/metadata/service/local/service_proxy_test.go +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package local - -import ( - "testing" -) - -import ( - "github.com/stretchr/testify/assert" -) - -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/metadata/service" - "dubbo.apache.org/dubbo-go/v3/protocol" - "dubbo.apache.org/dubbo-go/v3/registry" -) - -func TestMetadataServiceProxy_GetExportedURLs(t *testing.T) { - pxy := createPxy() - assert.NotNil(t, pxy) - res, err := pxy.GetExportedURLs(constant.AnyValue, constant.AnyValue, constant.AnyValue, constant.AnyValue) - assert.Nil(t, err) - assert.Len(t, res, 1) -} - -// TestNewMetadataService: those methods are not implemented -// when we implement them, adding UT -func TestNewMetadataService(t *testing.T) { - pxy := createPxy() - _, err := pxy.ServiceName() - assert.Nil(t, err) - err = pxy.PublishServiceDefinition(&common.URL{}) - assert.Nil(t, err) - _, err = pxy.GetServiceDefinition(constant.AnyValue, constant.AnyValue, constant.AnyValue) - assert.Nil(t, err) - _, err = pxy.Version() - assert.Nil(t, err) - _, err = pxy.GetSubscribedURLs() - assert.Nil(t, err) - err = pxy.UnsubscribeURL(&common.URL{}) - assert.Nil(t, err) - _, err = pxy.GetServiceDefinitionByServiceKey("any") - assert.Nil(t, err) - ok, err := pxy.ExportURL(&common.URL{}) - assert.False(t, ok) - assert.NoError(t, err) - ok, err = pxy.SubscribeURL(&common.URL{}) - assert.False(t, ok) - assert.NoError(t, err) - m := pxy.MethodMapper() - assert.True(t, len(m) == 0) - err = pxy.UnexportURL(&common.URL{}) - assert.NoError(t, err) - ok, err = pxy.RefreshMetadata(constant.AnyValue, constant.AnyValue) - assert.False(t, ok) - assert.NoError(t, err) -} - -func createPxy() service.MetadataService { - extension.SetProtocol("mock", func() protocol.Protocol { - return &mockProtocol{} - }) - - ins := ®istry.DefaultServiceInstance{ - ID: "test-id", - ServiceName: "com.dubbo", - Host: "localhost", - Port: 8080, - Enable: true, - Healthy: true, - Metadata: map[string]string{constant.MetadataServiceURLParamsPropertyName: `{"timeout":"10000", "protocol":"mock","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"20880"}`}, - } - - return extension.GetMetadataServiceProxyFactory("").GetProxy(ins) -} diff --git a/metadata/service/local/service_test.go b/metadata/service/local/service_test.go deleted file mode 100644 index c1e952c873..0000000000 --- a/metadata/service/local/service_test.go +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package local - -import ( - "fmt" - "testing" -) - -import ( - "github.com/stretchr/testify/assert" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/metadata/definition" -) - -func TestMetadataService(t *testing.T) { - mts, _ := GetLocalMetadataService() - serviceName := "com.ikurento.user.UserProvider" - group := "group1" - version := "0.0.1" - protocol := "dubbo" - beanName := "UserProvider" - - u2, err := common.NewURL(fmt.Sprintf( - "%v://127.0.0.1:20000/com.ikurento.user.UserProvider2?anyhost=true&"+ - "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ - "environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+ - "owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000×tamp=1556509797245&group=%v&version=%v&bean.name=%v", - protocol, serviceName, group, version, beanName)) - assert.NoError(t, err) - ok, err := mts.ExportURL(u2) - assert.True(t, ok) - assert.NoError(t, err) - - u3, err := common.NewURL(fmt.Sprintf( - "%v://127.0.0.1:20000/com.ikurento.user.UserProvider3?anyhost=true&"+ - "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ - "environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+ - "owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000×tamp=1556509797245&group=%v&version=%v&bean.name=%v", - protocol, serviceName, group, version, beanName)) - assert.NoError(t, err) - ok, err = mts.ExportURL(u3) - assert.True(t, ok) - assert.NoError(t, err) - - u, err := common.NewURL(fmt.Sprintf( - "%v://127.0.0.1:20000/com.ikurento.user.UserProvider1?anyhost=true&"+ - "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ - "environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+ - "owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000×tamp=1556509797245&group=%v&version=%v&bean.name=%v", - protocol, serviceName, group, version, beanName)) - assert.NoError(t, err) - ok, err = mts.ExportURL(u) - assert.True(t, ok) - assert.NoError(t, err) - list, _ := mts.GetExportedURLs(serviceName, group, version, protocol) - assert.Equal(t, 3, len(list)) - ok, err = mts.SubscribeURL(u) - assert.True(t, ok) - assert.NoError(t, err) - - ok, err = mts.SubscribeURL(u) - assert.False(t, ok) - assert.NoError(t, err) - list2, err := mts.GetSubscribedURLs() - assert.Equal(t, 1, len(list2)) - assert.NoError(t, err) - - err = mts.UnexportURL(u) - assert.NoError(t, err) - list3, _ := mts.GetExportedURLs(serviceName, group, version, protocol) - assert.Equal(t, 2, len(list3)) - - err = mts.UnsubscribeURL(u) - assert.NoError(t, err) - list4, _ := mts.GetSubscribedURLs() - assert.Equal(t, 0, len(list4)) - - userProvider := &definition.UserProvider{} - _, err = common.ServiceMap.Register(serviceName, protocol, group, version, userProvider) - assert.NoError(t, err) - err = mts.PublishServiceDefinition(u) - assert.NoError(t, err) - expected := "{\"parameters\":{\"anyhost\":\"true\",\"application\":\"BDTService\"," + - "\"bean.name\":\"UserProvider\",\"category\":\"providers\",\"default.timeout\":\"10000\"," + - "\"dubbo\":\"dubbo-provider-golang-1.0.0\",\"environment\":\"dev\",\"group\":\"group1\"," + - "\"interface\":\"com.ikurento.user.UserProvider\",\"ip\":\"192.168.56.1\"," + - "\"methods\":\"GetUser\",\"module\":\"dubbogo user-info server\",\"org\":\"ikurento.com\"," + - "\"owner\":\"ZX\",\"pid\":\"1447\",\"revision\":\"0.0.1\",\"side\":\"provider\"," + - "\"timeout\":\"3000\",\"timestamp\":\"1556509797245\",\"version\":\"0.0.1\"}," + - "\"canonicalName\":\"com.ikurento.user.UserProvider\",\"codeSource\":\"\"," + - "\"methods\":[{\"name\":\"GetUser\",\"parameterTypes\":[\"slice\"],\"returnType\":\"ptr\"," + - "\"parameters\":null}],\"types\":null}" - def1, err := mts.GetServiceDefinition(serviceName, group, version) - assert.Equal(t, expected, def1) - assert.NoError(t, err) - serviceKey := definition.ServiceDescriperBuild(serviceName, group, version) - def2, err := mts.GetServiceDefinitionByServiceKey(serviceKey) - assert.Equal(t, expected, def2) - assert.NoError(t, err) -} diff --git a/metadata/service/local_service.go b/metadata/service/local_service.go deleted file mode 100644 index 1251a9d4f5..0000000000 --- a/metadata/service/local_service.go +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package service - -import ( - "sync" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/constant" - "dubbo.apache.org/dubbo-go/v3/registry" -) - -// MetadataService is used to define meta data related behaviors -// usually the implementation should be singleton -type MetadataService interface { - common.ReferencedRPCService - // ServiceName will get the service's name in meta service , which is application name - ServiceName() (string, error) - // ExportURL will store the exported url in metadata - ExportURL(url *common.URL) (bool, error) - // UnexportURL will delete the exported url in metadata - UnexportURL(url *common.URL) error - // SubscribeURL will store the subscribed url in metadata - SubscribeURL(url *common.URL) (bool, error) - // UnsubscribeURL will delete the subscribed url in metadata - UnsubscribeURL(url *common.URL) error - // PublishServiceDefinition will generate the target url's code info - PublishServiceDefinition(url *common.URL) error - // GetExportedURLs will get the target exported url in metadata - // the url should be unique - // due to dubbo-go only support return array []interface{} in RPCService, so we should declare the return type as []interface{} - // actually, it's []String - GetExportedURLs(serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) - // MethodMapper for rename dubbo method name - MethodMapper() map[string]string - // GetSubscribedURLs will get the target subscribed url in metadata the url should be unique - GetSubscribedURLs() ([]*common.URL, error) - // GetServiceDefinition will get the target service info store in metadata - GetServiceDefinition(interfaceName string, group string, version string) (string, error) - // GetServiceDefinitionByServiceKey will get the target service info store in metadata by service key - GetServiceDefinitionByServiceKey(serviceKey string) (string, error) - // RefreshMetadata will refresh the metadata - RefreshMetadata(exportedRevision string, subscribedRevision string) (bool, error) - // Version will return the metadata service version - Version() (string, error) - // GetMetadataInfo will return metadata info - GetMetadataInfo(revision string) (*common.MetadataInfo, error) - // GetExportedServiceURLs will return exported service urls - GetExportedServiceURLs() ([]*common.URL, error) - // GetMetadataServiceURL will return the url of metadata service - GetMetadataServiceURL() (*common.URL, error) - // SetMetadataServiceURL will save the url of metadata service - SetMetadataServiceURL(*common.URL) error -} - -// BaseMetadataService is used for the event logic for struct who will implement interface MetadataService -type BaseMetadataService struct { - serviceName string -} - -func NewBaseMetadataService(serviceName string) BaseMetadataService { - return BaseMetadataService{ - serviceName: serviceName, - } -} - -func (mts *BaseMetadataService) MethodMapper() map[string]string { - return map[string]string{ - "GetExportedURLs": "getExportedURLs", - "GetMetadataInfo": "getMetadataInfo", - } -} - -// ServiceName can get the service's name in meta service , which is application name -func (mts *BaseMetadataService) ServiceName() (string, error) { - return mts.serviceName, nil -} - -// Reference will return the reference id of metadata service -func (mts *BaseMetadataService) Reference() string { - return constant.SimpleMetadataServiceName -} - -type MetadataServiceProxyFactory interface { - GetProxy(ins registry.ServiceInstance) MetadataService -} - -type MetadataServiceProxyCreator func(ins registry.ServiceInstance) MetadataService - -type BaseMetadataServiceProxyFactory struct { - proxies sync.Map - creator MetadataServiceProxyCreator -} - -func NewBaseMetadataServiceProxyFactory(creator MetadataServiceProxyCreator) *BaseMetadataServiceProxyFactory { - return &BaseMetadataServiceProxyFactory{ - creator: creator, - } -} - -func (b *BaseMetadataServiceProxyFactory) GetProxy(ins registry.ServiceInstance) MetadataService { - return b.creator(ins) -} - -func getExportedServicesRevision(serviceInstance registry.ServiceInstance) string { - metaData := serviceInstance.GetMetadata() - return metaData[constant.ExportedServicesRevisionPropertyName] -} - -func ConvertURLArrToIntfArr(urls []*common.URL) []interface{} { - if len(urls) == 0 { - return []interface{}{} - } - - res := make([]interface{}, 0, len(urls)) - for _, u := range urls { - res = append(res, u.String()) - } - return res -} diff --git a/metadata/service/remote/service.go b/metadata/service/remote/service.go deleted file mode 100644 index 9b2ba3909f..0000000000 --- a/metadata/service/remote/service.go +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package remote - -import ( - "sync" -) - -import ( - "github.com/dubbogo/gost/log/logger" - - "go.uber.org/atomic" -) - -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/metadata/definition" - "dubbo.apache.org/dubbo-go/v3/metadata/identifier" - "dubbo.apache.org/dubbo-go/v3/metadata/report/delegate" - "dubbo.apache.org/dubbo-go/v3/metadata/service" - "dubbo.apache.org/dubbo-go/v3/metadata/service/local" - "dubbo.apache.org/dubbo-go/v3/registry" -) - -type MetadataService struct { - *local.MetadataService - exportedRevision atomic.String - subscribedRevision atomic.String - delegateReport *delegate.MetadataReport -} - -var ( - metadataServiceOnce sync.Once - remoteMetadataServiceInstance service.RemoteMetadataService -) - -func init() { - extension.SetRemoteMetadataService(GetRemoteMetadataService) -} - -// GetRemoteMetadataService will create a new remote MetadataService instance -func GetRemoteMetadataService() (service.RemoteMetadataService, error) { - var err error - metadataServiceOnce.Do(func() { - var mr *delegate.MetadataReport - mr, err = delegate.NewMetadataReport() - if err != nil { - return - } - // it will never return error - inms, _ := local.GetLocalMetadataService() - remoteMetadataServiceInstance = &MetadataService{ - // todo serviceName - //BaseMetadataService: service.NewBaseMetadataService(""), - MetadataService: inms.(*local.MetadataService), - delegateReport: mr, - } - }) - return remoteMetadataServiceInstance, err -} - -// PublishMetadata publishes the metadata info of @service to remote metadata center -func (s *MetadataService) PublishMetadata(service string) { - info, err := s.MetadataService.GetMetadataInfo("") - if err != nil { - logger.Errorf("GetMetadataInfo error[%v]", err) - return - } - if info.HasReported() { - return - } - id := identifier.NewSubscriberMetadataIdentifier(service, info.CalAndGetRevision()) - err = s.delegateReport.PublishAppMetadata(id, info) - if err != nil { - logger.Errorf("Publishing metadata to error[%v]", err) - return - } - info.MarkReported() -} - -// GetMetadata get the medata info of service from report -func (s *MetadataService) GetMetadata(instance registry.ServiceInstance) (*common.MetadataInfo, error) { - revision := instance.GetMetadata()[constant.ExportedServicesRevisionPropertyName] - id := identifier.NewSubscriberMetadataIdentifier(instance.GetServiceName(), revision) - return s.delegateReport.GetAppMetadata(id) -} - -// PublishServiceDefinition will call remote metadata's StoreProviderMetadata to store url info and service definition -func (s *MetadataService) PublishServiceDefinition(url *common.URL) error { - interfaceName := url.GetParam(constant.InterfaceKey, "") - isGeneric := url.GetParamBool(constant.GenericKey, false) - if common.RoleType(common.PROVIDER).Role() == url.GetParam(constant.SideKey, "") { - if len(interfaceName) > 0 && !isGeneric { - sv := common.ServiceMap.GetServiceByServiceKey(url.Protocol, url.ServiceKey()) - sd := definition.BuildFullDefinition(*sv, url) - id := &identifier.MetadataIdentifier{ - BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{ - ServiceInterface: interfaceName, - Version: url.GetParam(constant.VersionKey, ""), - Group: url.GetParam(constant.GroupKey, constant.Dubbo), - Side: url.GetParam(constant.SideKey, constant.ProviderProtocol), - }, - } - s.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.VersionKey, ""), - Group: url.GetParam(constant.GroupKey, constant.Dubbo), - Side: url.GetParam(constant.SideKey, "consumer"), - }, - } - s.delegateReport.StoreConsumerMetadata(id, params) - return nil - } - - return nil -} diff --git a/metadata/service/remote/service_test.go b/metadata/service/remote/service_test.go deleted file mode 100644 index 10f2b1e53f..0000000000 --- a/metadata/service/remote/service_test.go +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package remote - -import ( - "fmt" - "testing" -) - -import ( - gxset "github.com/dubbogo/gost/container/set" - "github.com/dubbogo/gost/log/logger" - - "github.com/stretchr/testify/assert" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/extension" - "dubbo.apache.org/dubbo-go/v3/config/instance" - "dubbo.apache.org/dubbo-go/v3/metadata/definition" - "dubbo.apache.org/dubbo-go/v3/metadata/identifier" - "dubbo.apache.org/dubbo-go/v3/metadata/report" - "dubbo.apache.org/dubbo-go/v3/metadata/report/factory" - "dubbo.apache.org/dubbo-go/v3/metadata/service/local" - "dubbo.apache.org/dubbo-go/v3/registry" -) - -var ( - serviceMetadata = make(map[*identifier.ServiceMetadataIdentifier]*common.URL, 4) - subscribedMetadata = make(map[*identifier.SubscriberMetadataIdentifier]string, 4) -) - -func getMetadataReportFactory() factory.MetadataReportFactory { - return &metadataReportFactory{} -} - -type metadataReportFactory struct{} - -func (mrf *metadataReportFactory) CreateMetadataReport(*common.URL) report.MetadataReport { - return &metadataReport{} -} - -type metadataReport struct{} - -func (mr metadataReport) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { - //TODO implement me - panic("implement me") -} - -func (mr metadataReport) RegisterServiceAppMapping(string, string, string) error { - panic("implement me") -} - -func (mr metadataReport) GetServiceAppMapping(string, string, registry.MappingListener) (*gxset.HashSet, error) { - panic("implement me") -} - -func (mr metadataReport) RemoveServiceAppMappingListener(string, string) error { - panic("implement me") -} - -func (mr metadataReport) GetAppMetadata(*identifier.SubscriberMetadataIdentifier) (*common.MetadataInfo, error) { - panic("implement me") -} - -func (mr metadataReport) PublishAppMetadata(*identifier.SubscriberMetadataIdentifier, *common.MetadataInfo) error { - panic("implement me") -} - -func (metadataReport) StoreProviderMetadata(*identifier.MetadataIdentifier, string) error { - return nil -} - -func (metadataReport) StoreConsumerMetadata(*identifier.MetadataIdentifier, string) error { - return nil -} - -func (mr *metadataReport) SaveServiceMetadata(id *identifier.ServiceMetadataIdentifier, url *common.URL) error { - logger.Infof("SaveServiceMetadata , url is %v", url) - serviceMetadata[id] = url - return nil -} - -func (metadataReport) RemoveServiceMetadata(*identifier.ServiceMetadataIdentifier) error { - return nil -} - -func (metadataReport) GetExportedURLs(*identifier.ServiceMetadataIdentifier) ([]string, error) { - return nil, nil -} - -func (mr *metadataReport) SaveSubscribedData(id *identifier.SubscriberMetadataIdentifier, urls string) error { - logger.Infof("SaveSubscribedData, , url is %v", urls) - subscribedMetadata[id] = urls - return nil -} - -func (metadataReport) GetSubscribedURLs(*identifier.SubscriberMetadataIdentifier) ([]string, error) { - return nil, nil -} - -func (metadataReport) GetServiceDefinition(*identifier.MetadataIdentifier) (string, error) { - return "", nil -} - -func TestMetadataService(t *testing.T) { - extension.SetMetadataReportFactory("mock", getMetadataReportFactory) - u, err := common.NewURL("mock://127.0.0.1:20000/?sync.report=true") - assert.NoError(t, err) - instance.SetMetadataReportInstance(u) - mts, err := GetRemoteMetadataService() - assert.NoError(t, err) - assert.NotNil(t, mts) -} - -func TestMockInmemoryProc(t *testing.T) { - mts, _ := local.GetLocalMetadataService() - serviceName := "com.ikurento.user.UserProvider" - group := "group1" - version := "0.0.1" - protocol := "dubbo" - beanName := "UserProvider" - userProvider := &definition.UserProvider{} - - u, err := common.NewURL(fmt.Sprintf( - "%v://127.0.0.1:20000/com.ikurento.user.UserProvider1?anyhost=true&"+ - "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ - "environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+ - "owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000×tamp=1556509797245&group=%v&version=%v&bean.name=%v", - protocol, serviceName, group, version, beanName)) - assert.NoError(t, err) - - _, err = mts.ExportURL(u) - assert.NoError(t, err) - _, err = mts.SubscribeURL(u) - assert.NoError(t, err) - - _, err = common.ServiceMap.Register(serviceName, protocol, group, version, userProvider) - assert.NoError(t, err) - err = mts.PublishServiceDefinition(u) - assert.NoError(t, err) - - expected := "{\"parameters\":{\"anyhost\":\"true\",\"application\":\"BDTService\"," + - "\"bean.name\":\"UserProvider\",\"category\":\"providers\",\"default.timeout\":\"10000\"," + - "\"dubbo\":\"dubbo-provider-golang-1.0.0\",\"environment\":\"dev\",\"group\":\"group1\"," + - "\"interface\":\"com.ikurento.user.UserProvider\",\"ip\":\"192.168.56.1\"," + - "\"methods\":\"GetUser\",\"module\":\"dubbogo user-info server\",\"org\":\"ikurento.com\"," + - "\"owner\":\"ZX\",\"pid\":\"1447\",\"revision\":\"0.0.1\",\"side\":\"provider\"," + - "\"timeout\":\"3000\",\"timestamp\":\"1556509797245\",\"version\":\"0.0.1\"}," + - "\"canonicalName\":\"com.ikurento.user.UserProvider\",\"codeSource\":\"\"," + - "\"methods\":[{\"name\":\"GetUser\",\"parameterTypes\":[\"slice\"],\"returnType\":\"ptr\"," + - "\"parameters\":null}],\"types\":null}" - def1, _ := mts.GetServiceDefinition(serviceName, group, version) - assert.Equal(t, expected, def1) - serviceKey := definition.ServiceDescriperBuild(serviceName, group, version) - def2, _ := mts.GetServiceDefinitionByServiceKey(serviceKey) - assert.Equal(t, expected, def2) -} diff --git a/metadata/service/remote_service.go b/metadata/service/remote_service.go deleted file mode 100644 index 95bf0687a5..0000000000 --- a/metadata/service/remote_service.go +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package service - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/registry" -) - -type RemoteMetadataService interface { - // PublishMetadata publish the medata info of service from report - PublishMetadata(service string) - // GetMetadata get the medata info of service from report - GetMetadata(instance registry.ServiceInstance) (*common.MetadataInfo, error) - // PublishServiceDefinition will call remote metadata's StoreProviderMetadata to store url info and service definition - PublishServiceDefinition(url *common.URL) error -} diff --git a/options.go b/options.go index 0e6719473c..969f5e7816 100644 --- a/options.go +++ b/options.go @@ -17,6 +17,10 @@ package dubbo +import ( + "time" +) + import ( log "github.com/dubbogo/gost/log/logger" ) @@ -128,9 +132,6 @@ func (rc *InstanceOptions) init(opts ...InstanceOption) error { } } - if err := rcCompat.MetadataReport.Init(rcCompat); err != nil { - return err - } if err := rcCompat.Metrics.Init(rcCompat); err != nil { return err } @@ -160,9 +161,88 @@ func (rc *InstanceOptions) init(opts ...InstanceOption) error { } config.SetRootConfig(*rcCompat) + if err := rc.initMetadataReport(); err != nil { + return err + } + if err := rc.initRegistryMetadataReport(); err != nil { + return err + } + + return nil +} + +func (rc *InstanceOptions) initMetadataReport() error { + if rc.MetadataReport.Address != "" { + opts, err := reportConfigToReportOptions(rc.MetadataReport) + if err != nil { + return err + } + if err := opts.Init(); err != nil { + return err + } + } + return nil +} + +func reportConfigToReportOptions(mc *global.MetadataReportConfig) (*metadata.ReportOptions, error) { + opts := metadata.NewReportOptions( + metadata.WithRegistryId(constant.DefaultKey), + metadata.WithProtocol(mc.Protocol), + metadata.WithAddress(mc.Address), + metadata.WithUsername(mc.Username), + metadata.WithPassword(mc.Password), + metadata.WithGroup(mc.Group), + metadata.WithNamespace(mc.Namespace), + metadata.WithParams(mc.Params), + ) + if mc.Timeout != "" { + timeout, err := time.ParseDuration(mc.Timeout) + if err != nil { + return nil, err + } + metadata.WithTimeout(timeout)(opts) + } + return opts, nil +} + +func (rc *InstanceOptions) initRegistryMetadataReport() error { + if len(rc.Registries) > 0 { + for id, reg := range rc.Registries { + if reg.UseAsMetaReport { + opts, err := registryToReportOptions(id, reg) + if err != nil { + return err + } + if err := opts.Init(); err != nil { + return err + } + } + } + } return nil } +func registryToReportOptions(id string, rc *global.RegistryConfig) (*metadata.ReportOptions, error) { + opts := metadata.NewReportOptions( + metadata.WithRegistryId(id), + metadata.WithProtocol(rc.Protocol), + metadata.WithAddress(rc.Address), + metadata.WithUsername(rc.Username), + metadata.WithPassword(rc.Password), + metadata.WithGroup(rc.Group), + metadata.WithNamespace(rc.Namespace), + metadata.WithParams(rc.Params), + ) + if rc.Timeout != "" { + timeout, err := time.ParseDuration(rc.Timeout) + if err != nil { + return nil, err + } + metadata.WithTimeout(timeout)(opts) + } + return opts, nil +} + func (rc *InstanceOptions) Prefix() string { return constant.Dubbo } @@ -262,11 +342,11 @@ func WithConfigCenter(opts ...config_center.Option) InstanceOption { } } -func WithMetadataReport(opts ...metadata.Option) InstanceOption { - metadataOpts := metadata.NewOptions(opts...) +func WithMetadataReport(opts ...metadata.ReportOption) InstanceOption { + metadataOpts := metadata.NewReportOptions(opts...) return func(cfg *InstanceOptions) { - cfg.MetadataReport = metadataOpts.Metadata + cfg.MetadataReport = metadataOpts.MetadataReportConfig } } diff --git a/protocol/dubbo3/dubbo3_invoker_test.go b/protocol/dubbo3/dubbo3_invoker_test.go index f04e794f52..9b04f8cb11 100644 --- a/protocol/dubbo3/dubbo3_invoker_test.go +++ b/protocol/dubbo3/dubbo3_invoker_test.go @@ -17,10 +17,6 @@ package dubbo3 -import ( - _ "dubbo.apache.org/dubbo-go/v3/metadata/service/exporter/configurable" -) - const ( mockDubbo3CommonUrl2 = "tri://127.0.0.1:20003/DubboGreeterImpl?accesslog=&anyhost=true&app.version=0.0.1&application=BDTService&async=false&bean.name=greeterImpl" + "&category=providers&cluster=failover&dubbo=dubbo-provider-golang-2.6.0&environment=dev&execute.limit=&execute.limit.rejected.handler=&generic=false&group=&interface=org.apache.dubbo.DubboGreeterImpl" + diff --git a/registry/etcdv3/service_discovery_test.go b/registry/etcdv3/service_discovery_test.go index 7db112c1b9..d6973dbb44 100644 --- a/registry/etcdv3/service_discovery_test.go +++ b/registry/etcdv3/service_discovery_test.go @@ -132,13 +132,10 @@ func (m *mockInvoker) Destroy() { } func (m *mockInvoker) Invoke(context.Context, protocol.Invocation) protocol.Result { - // for getMetadataInfo and ServiceInstancesChangedListenerImpl onEvent - serviceInfo := &common.ServiceInfo{ServiceKey: "test", MatchKey: "test"} - services := make(map[string]*common.ServiceInfo) - services["test"] = serviceInfo return &protocol.RPCResult{ - Rest: &common.MetadataInfo{ - Services: services, - }, + Rest: &mockResult{}, } } + +type mockResult struct { +} diff --git a/registry/exposed_tmp/exposed.go b/registry/exposed_tmp/exposed.go index f6861e068d..d4a1b77c45 100644 --- a/registry/exposed_tmp/exposed.go +++ b/registry/exposed_tmp/exposed.go @@ -17,116 +17,35 @@ package exposed_tmp -import ( - "reflect" - "strconv" -) - import ( "github.com/dubbogo/gost/log/logger" - - 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/registry" ) // RegisterServiceInstance register service instance -func RegisterServiceInstance(applicationName string, tag string, metadataType string) { - url := selectMetadataServiceExportedURL() - if url == nil { - return - } - instance, err := createInstance(url, applicationName, tag, metadataType) - 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 +func RegisterServiceInstance() error { + defer func() { + // TODO remove this recover func,this just to avoid some unit test failed,this will not happen in user side mostly + // config test -> metadata exporter -> dubbo protocol/remoting -> config,cycle import will occur + // some day we fix the cycle import then can remove this recover + if err := recover(); err != nil { + logger.Errorf("register service instance failed,please check if registry protocol is imported, error: %v", err) } - // publish app level data to registry - logger.Infof("Starting register instance address %v", instance) - err := sdr.GetServiceDiscovery().Register(instance) - if err != nil { - panic(err) - } - } - // publish metadata to remote - if metadataType == constant.RemoteMetadataStorageType { - if remoteMetadataService, err := extension.GetRemoteMetadataService(); err == nil && remoteMetadataService != nil { - remoteMetadataService.PublishMetadata(applicationName) - } - } -} - -// // nolint -func createInstance(url *common.URL, applicationName string, tag string, metadataType string) (registry.ServiceInstance, error) { - 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] = metadataType - - instance := ®istry.DefaultServiceInstance{ - ServiceName: applicationName, - Host: host, - Port: int(port), - ID: host + constant.KeySeparator + url.Port, - Enable: true, - Healthy: true, - Metadata: metadata, - Tag: tag, - } - - for _, cus := range extension.GetCustomizers() { - cus.Customize(instance) - } - - return instance, nil -} - -// selectMetadataServiceExportedURL get already be exported url -func selectMetadataServiceExportedURL() *common.URL { - var selectedUrl *common.URL - metaDataService, err := extension.GetLocalMetadataService(constant.DefaultKey) - if err != nil { - logger.Warnf("get metadata service exporter failed, pls check if you import _ \"dubbo.apache.org/dubbo-go/v3/metadata/service/local\"") - return nil - } - urlList, err := metaDataService.GetExportedURLs(constant.AnyValue, constant.AnyValue, constant.AnyValue, constant.AnyValue) - if err != nil { - panic(err) - } - if len(urlList) == 0 { - return nil - } - for _, url := range urlList { - selectedUrl = url - // rest first - if url.Protocol == "rest" { - break + }() + protocol := extension.GetProtocol(constant.RegistryKey) + if rf, ok := protocol.(registry.RegistryFactory); ok { + for _, r := range rf.GetRegistries() { + if sdr, ok := r.(registry.ServiceDiscoveryRegistry); ok { + if err := sdr.RegisterService(); err != nil { + return err + } + } } } - return selectedUrl + return nil } diff --git a/registry/nacos/service_discovery_test.go b/registry/nacos/service_discovery_test.go index 81fb233401..9f6a335b9b 100644 --- a/registry/nacos/service_discovery_test.go +++ b/registry/nacos/service_discovery_test.go @@ -240,13 +240,10 @@ func (m *mockInvoker) Destroy() { } func (m *mockInvoker) Invoke(context.Context, protocol.Invocation) protocol.Result { - // for getMetadataInfo and ServiceInstancesChangedListenerImpl onEvent - serviceInfo := &common.ServiceInfo{ServiceKey: "test", MatchKey: "test"} - services := make(map[string]*common.ServiceInfo) - services["test"] = serviceInfo return &protocol.RPCResult{ - Rest: &common.MetadataInfo{ - Services: services, - }, + Rest: &mockResult{}, } } + +type mockResult struct { +} diff --git a/registry/service_discovery.go b/registry/service_discovery.go index 60019982c4..13641f1360 100644 --- a/registry/service_discovery.go +++ b/registry/service_discovery.go @@ -33,7 +33,7 @@ type ServiceDiscovery interface { fmt.Stringer // Destroy will destroy the service discovery. - // If the discovery cannot be destroy, it will return an error. + // If the discovery cannot be destroyed, it will return an error. Destroy() error // Register will register an instance of ServiceInstance to registry @@ -70,3 +70,10 @@ type ServiceDiscovery interface { // see addServiceInstancesChangedListener in Java AddListener(listener ServiceInstancesChangedListener) error } + +type ServiceDiscoveryRegistry interface { + // RegisterService register service instance use metadata info + RegisterService() error + // UnRegisterService unregister service instance + UnRegisterService() error +} diff --git a/registry/service_discovery_holder.go b/registry/service_discovery_holder.go deleted file mode 100644 index efe113fa18..0000000000 --- a/registry/service_discovery_holder.go +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package registry - -// ServiceDiscoveryHolder is the interface of getting a service discovery -// it always be a service discovery registry -type ServiceDiscoveryHolder interface { - // GetServiceDiscovery get service discovery - GetServiceDiscovery() ServiceDiscovery -} diff --git a/registry/service_instance.go b/registry/service_instance.go index 7c3c57c4a0..8863b8e967 100644 --- a/registry/service_instance.go +++ b/registry/service_instance.go @@ -31,6 +31,7 @@ import ( import ( "dubbo.apache.org/dubbo-go/v3/common" "dubbo.apache.org/dubbo-go/v3/common/constant" + "dubbo.apache.org/dubbo-go/v3/metadata/info" ) // ServiceInstance is the interface which is used for service registration and discovery. @@ -58,7 +59,7 @@ type ServiceInstance interface { GetMetadata() map[string]string // ToURLs will return a list of url - ToURLs(service *common.ServiceInfo) []*common.URL + ToURLs(service *info.ServiceInfo) []*common.URL // GetEndPoints will get end points from metadata GetEndPoints() []*Endpoint @@ -70,7 +71,8 @@ type ServiceInstance interface { GetAddress() string // SetServiceMetadata saves metadata in instance - SetServiceMetadata(info *common.MetadataInfo) + SetServiceMetadata(info *info.MetadataInfo) + GetServiceMetadata() *info.MetadataInfo // GetTag will return the tag of the instance GetTag() string @@ -92,7 +94,7 @@ type DefaultServiceInstance struct { Enable bool Healthy bool Metadata map[string]string - ServiceMetadata *common.MetadataInfo + ServiceMetadata *info.MetadataInfo Address string GroupName string endpoints []*Endpoint `json:"-"` @@ -143,16 +145,21 @@ func (d *DefaultServiceInstance) GetAddress() string { } // SetServiceMetadata save metadata in instance -func (d *DefaultServiceInstance) SetServiceMetadata(m *common.MetadataInfo) { +func (d *DefaultServiceInstance) SetServiceMetadata(m *info.MetadataInfo) { d.ServiceMetadata = m } +// GetServiceMetadata save metadata in instance +func (d *DefaultServiceInstance) GetServiceMetadata() *info.MetadataInfo { + return d.ServiceMetadata +} + func (d *DefaultServiceInstance) GetTag() string { return d.Tag } // ToURLs return a list of url. -func (d *DefaultServiceInstance) ToURLs(service *common.ServiceInfo) []*common.URL { +func (d *DefaultServiceInstance) ToURLs(service *info.ServiceInfo) []*common.URL { urls := make([]*common.URL, 0, 8) if d.endpoints == nil { err := json.Unmarshal([]byte(d.Metadata[constant.ServiceInstanceEndpoints]), &d.endpoints) diff --git a/registry/service_mapping_changed_listener.go b/registry/service_mapping_changed_listener.go deleted file mode 100644 index 3c75ac115f..0000000000 --- a/registry/service_mapping_changed_listener.go +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package registry - -import ( - "github.com/dubbogo/gost/gof/observer" -) - -type MappingListener interface { - OnEvent(e observer.Event) error - Stop() -} diff --git a/registry/event/metadata_service_url_params_customizer.go b/registry/servicediscovery/customizer/metadata_service_url_params_customizer.go similarity index 86% rename from registry/event/metadata_service_url_params_customizer.go rename to registry/servicediscovery/customizer/metadata_service_url_params_customizer.go index 48d8beddae..adddd0c640 100644 --- a/registry/event/metadata_service_url_params_customizer.go +++ b/registry/servicediscovery/customizer/metadata_service_url_params_customizer.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package event +package customizer import ( "encoding/json" @@ -30,7 +30,8 @@ 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/metadata/service/local" + "dubbo.apache.org/dubbo-go/v3/metadata" + "dubbo.apache.org/dubbo-go/v3/metadata/info" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -55,14 +56,9 @@ func (m *metadataServiceURLParamsMetadataCustomizer) GetPriority() int { } func (m *metadataServiceURLParamsMetadataCustomizer) Customize(instance registry.ServiceInstance) { - ms, err := local.GetLocalMetadataService() - if err != nil { - logger.Errorf("could not find the metadata service", err) - return - } - url, err := ms.GetMetadataServiceURL() - if url == nil || err != nil { - logger.Errorf("the metadata service url is nil") + url, _ := metadata.GetMetadataService().GetMetadataServiceURL() + if url == nil { + // when metadata service is not exported the url will be nil,this is because metadata type is remote return } ps := m.convertToParams(url) @@ -79,7 +75,7 @@ func (m *metadataServiceURLParamsMetadataCustomizer) convertToParams(url *common p := make(map[string]string, len(url.GetParams())) for k, v := range url.GetParams() { // we will ignore that - if !common.IncludeKeys.Contains(k) || len(v) == 0 || len(v[0]) == 0 { + if !info.IncludeKeys.Contains(k) || len(v) == 0 || len(v[0]) == 0 { continue } p[k] = v[0] diff --git a/registry/event/metadata_service_url_params_customizer_test.go b/registry/servicediscovery/customizer/metadata_service_url_params_customizer_test.go similarity index 99% rename from registry/event/metadata_service_url_params_customizer_test.go rename to registry/servicediscovery/customizer/metadata_service_url_params_customizer_test.go index 212c3ed57d..3b213ad66c 100644 --- a/registry/event/metadata_service_url_params_customizer_test.go +++ b/registry/servicediscovery/customizer/metadata_service_url_params_customizer_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package event +package customizer import ( "testing" diff --git a/registry/event/protocol_ports_metadata_customizer.go b/registry/servicediscovery/customizer/protocol_ports_metadata_customizer.go similarity index 84% rename from registry/event/protocol_ports_metadata_customizer.go rename to registry/servicediscovery/customizer/protocol_ports_metadata_customizer.go index 8eeda26e0e..a5d757ebfa 100644 --- a/registry/event/protocol_ports_metadata_customizer.go +++ b/registry/servicediscovery/customizer/protocol_ports_metadata_customizer.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package event +package customizer import ( "encoding/json" @@ -29,7 +29,7 @@ import ( import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/common/extension" - "dubbo.apache.org/dubbo-go/v3/metadata/service/local" + "dubbo.apache.org/dubbo-go/v3/metadata" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -48,24 +48,19 @@ func (p *ProtocolPortsMetadataCustomizer) GetPriority() int { // Customize put the the string like [{"protocol": "dubbo", "port": 123}] into instance's metadata func (p *ProtocolPortsMetadataCustomizer) Customize(instance registry.ServiceInstance) { - metadataService, err := local.GetLocalMetadataService() + list, err := metadata.GetMetadataService().GetExportedServiceURLs() if err != nil { - logger.Errorf("Could not init the MetadataService", err) + logger.Errorf("get metadata exported service url is error, %v", err) return } - - // 4 is enough... we don't have many protocol - protocolMap := make(map[string]int, 4) - - list, err := metadataService.GetExportedServiceURLs() - if list == nil || len(list) == 0 || err != nil { - logger.Debugf("Could not find exported urls", err) + if list == nil || len(list) == 0 { + // client side return } - + // 4 is enough... we don't have many protocol + protocolMap := make(map[string]int, 4) for _, u := range list { - if err != nil || len(u.Protocol) == 0 { - logger.Errorf("the url string is invalid: %s", u, err) + if len(u.Protocol) == 0 { continue } diff --git a/registry/servicediscovery/customizer/service_instance_host_port_customizer.go b/registry/servicediscovery/customizer/service_instance_host_port_customizer.go new file mode 100644 index 0000000000..f33023f45b --- /dev/null +++ b/registry/servicediscovery/customizer/service_instance_host_port_customizer.go @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package customizer + +import ( + "strconv" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common/extension" + "dubbo.apache.org/dubbo-go/v3/registry" +) + +func init() { + extension.AddCustomizers(&hostPortCustomizer{}) +} + +type hostPortCustomizer struct{} + +// GetPriority will return 1 so that it will be invoked in front of user defining Customizer +func (e *hostPortCustomizer) GetPriority() int { + return 1 +} + +// Customize calculate the revision for exported urls and then put it into instance metadata +func (e *hostPortCustomizer) Customize(instance registry.ServiceInstance) { + if instance.GetPort() > 0 { + return + } + if instance.GetServiceMetadata() == nil || len(instance.GetServiceMetadata().GetExportedServiceURLs()) == 0 { + return + } + for _, url := range instance.GetServiceMetadata().GetExportedServiceURLs() { + if ins, ok := instance.(*registry.DefaultServiceInstance); ok { + ins.Host = url.Ip + p, err := strconv.Atoi(url.Port) + if err == nil { + ins.Port = p + } + break + } + } +} diff --git a/registry/event/service_revision_customizer.go b/registry/servicediscovery/customizer/service_revision_customizer.go similarity index 86% rename from registry/event/service_revision_customizer.go rename to registry/servicediscovery/customizer/service_revision_customizer.go index 62b5a91f79..e8e3eaa090 100644 --- a/registry/event/service_revision_customizer.go +++ b/registry/servicediscovery/customizer/service_revision_customizer.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package event +package customizer import ( "fmt" @@ -31,7 +31,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/metadata/service/local" + "dubbo.apache.org/dubbo-go/v3/metadata" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -51,17 +51,11 @@ func (e *exportedServicesRevisionMetadataCustomizer) GetPriority() int { // Customize calculate the revision for exported urls and then put it into instance metadata func (e *exportedServicesRevisionMetadataCustomizer) Customize(instance registry.ServiceInstance) { - ms, err := local.GetLocalMetadataService() + urls, err := metadata.GetMetadataService().GetExportedServiceURLs() if err != nil { - logger.Errorf("could not get metadata service", err) + logger.Errorf("get metadata service url is error, %v", err) return } - - urls, err := ms.GetExportedURLs(constant.AnyValue, constant.AnyValue, constant.AnyValue, constant.AnyValue) - if err != nil { - logger.Errorf("could not find the exported url", err) - } - revision := resolveRevision(urls) if len(revision) == 0 { revision = defaultRevision @@ -78,17 +72,11 @@ func (e *subscribedServicesRevisionMetadataCustomizer) GetPriority() int { // Customize calculate the revision for subscribed urls and then put it into instance metadata func (e *subscribedServicesRevisionMetadataCustomizer) Customize(instance registry.ServiceInstance) { - ms, err := local.GetLocalMetadataService() + urls, err := metadata.GetMetadataService().GetSubscribedURLs() if err != nil { - logger.Errorf("could not get metadata service", err) + logger.Errorf("get metadata subscribed url is error, %v", err) return } - - urls, err := ms.GetSubscribedURLs() - if err != nil { - logger.Errorf("could not find the subscribed url", err) - } - revision := resolveRevision(urls) if len(revision) == 0 { revision = defaultRevision diff --git a/registry/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go index 6618544cde..a27f012dbf 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -29,22 +29,21 @@ import ( "github.com/dubbogo/gost/log/logger" perrors "github.com/pkg/errors" - - "go.uber.org/atomic" ) 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/metadata" + "dubbo.apache.org/dubbo-go/v3/metadata/info" "dubbo.apache.org/dubbo-go/v3/metadata/mapping" - "dubbo.apache.org/dubbo-go/v3/metadata/service" - "dubbo.apache.org/dubbo-go/v3/metadata/service/local" + "dubbo.apache.org/dubbo-go/v3/metadata/report" "dubbo.apache.org/dubbo-go/v3/metrics" - metricMetadata "dubbo.apache.org/dubbo-go/v3/metrics/metadata" + metricsMetadata "dubbo.apache.org/dubbo-go/v3/metrics/metadata" metricsRegistry "dubbo.apache.org/dubbo-go/v3/metrics/registry" "dubbo.apache.org/dubbo-go/v3/registry" - _ "dubbo.apache.org/dubbo-go/v3/registry/event" + _ "dubbo.apache.org/dubbo-go/v3/registry/servicediscovery/customizer" "dubbo.apache.org/dubbo-go/v3/registry/servicediscovery/synthesizer" ) @@ -52,67 +51,94 @@ func init() { extension.SetRegistry(constant.ServiceRegistryProtocol, newServiceDiscoveryRegistry) } -// ServiceDiscoveryRegistry is the implementation of application-level registry. +// serviceDiscoveryRegistry is the implementation of application-level registry. // It's completely different from other registry implementations -// This implementation is based on ServiceDiscovery abstraction and ServiceNameMapping +// This implementation is based on ServiceDiscovery abstraction and ServiceNameMapping and metadata // In order to keep compatible with interface-level registry, -// this implementation is -type ServiceDiscoveryRegistry struct { - lock sync.RWMutex - url *common.URL - serviceDiscovery registry.ServiceDiscovery - subscribedServices *gxset.HashSet - serviceNameMapping mapping.ServiceNameMapping - metaDataService service.MetadataService - registeredListeners *gxset.HashSet - subscribedURLsSynthesizers []synthesizer.SubscribedURLsSynthesizer - serviceRevisionExportedURLsCache map[string]map[string][]*common.URL - serviceListeners map[string]registry.ServiceInstancesChangedListener - serviceMappingListeners map[string]registry.MappingListener +// serviceDiscoveryRegistry = ServiceDiscovery + metadata +type serviceDiscoveryRegistry struct { + lock sync.RWMutex + url *common.URL + serviceDiscovery registry.ServiceDiscovery + instance registry.ServiceInstance + serviceNameMapping mapping.ServiceNameMapping + metadataReport report.MetadataReport + serviceListeners map[string]registry.ServiceInstancesChangedListener + serviceMappingListeners map[string]mapping.MappingListener } func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { - serviceDiscovery, err := creatServiceDiscovery(url) - if err != nil { - return nil, err - } - subscribedServices := parseServices(url.GetParam(constant.SubscribedServiceNamesKey, "")) - subscribedURLsSynthesizers := synthesizer.GetAllSynthesizer() - serviceNameMapping := extension.GetGlobalServiceNameMapping() - metaDataService, err := local.GetLocalMetadataService() + serviceDiscovery, err := extension.GetServiceDiscovery(url) if err != nil { - return nil, perrors.WithMessage(err, "could not init metadata service") + return nil, perrors.WithMessage(err, "Create service discovery failed") } - return &ServiceDiscoveryRegistry{ - url: url, - serviceDiscovery: serviceDiscovery, - subscribedServices: subscribedServices, - subscribedURLsSynthesizers: subscribedURLsSynthesizers, - registeredListeners: gxset.NewSet(), - serviceRevisionExportedURLsCache: make(map[string]map[string][]*common.URL, 8), - serviceNameMapping: serviceNameMapping, - metaDataService: metaDataService, - serviceListeners: make(map[string]registry.ServiceInstancesChangedListener), + return &serviceDiscoveryRegistry{ + url: url, + serviceDiscovery: serviceDiscovery, + serviceNameMapping: extension.GetGlobalServiceNameMapping(), + metadataReport: metadata.GetMetadataReportByRegistry(url.GetParam(constant.RegistryIdKey, "")), + serviceListeners: make(map[string]registry.ServiceInstancesChangedListener), // cache for mapping listener - serviceMappingListeners: make(map[string]registry.MappingListener), + serviceMappingListeners: make(map[string]mapping.MappingListener), }, nil } -func (s *ServiceDiscoveryRegistry) UnRegister(url *common.URL) error { +func (s *serviceDiscoveryRegistry) RegisterService() error { + metaInfo := metadata.GetMetadataInfo(s.url.GetParam(constant.RegistryIdKey, "")) + if metaInfo == nil { + panic("no metada info found of registry id " + s.url.GetParam(constant.RegistryIdKey, "")) + } + s.instance = createInstance(metaInfo) + // consumer has no host and port, so it will not register service + if s.instance.GetHost() != "" && s.instance.GetPort() != 0 { + metaInfo.CalAndGetRevision() + if metadata.GetMetadataType() == constant.RemoteMetadataStorageType { + if s.metadataReport == nil { + return perrors.New("can not publish app metadata cause report instance not found") + } + err := s.metadataReport.PublishAppMetadata(metaInfo.App, metaInfo.Revision, metaInfo) + if err != nil { + return err + } + } + return s.serviceDiscovery.Register(s.instance) + } + return nil +} + +func createInstance(meta *info.MetadataInfo) registry.ServiceInstance { + params := make(map[string]string, 8) + params[constant.MetadataStorageTypePropertyName] = metadata.GetMetadataType() + instance := ®istry.DefaultServiceInstance{ + ServiceName: meta.App, + Enable: true, + Healthy: true, + Metadata: params, + ServiceMetadata: meta, + Tag: meta.Tag, + } + + for _, cus := range extension.GetCustomizers() { + cus.Customize(instance) + } + return instance +} + +func (s *serviceDiscoveryRegistry) UnRegisterService() error { + return s.serviceDiscovery.Unregister(s.instance) +} + +func (s *serviceDiscoveryRegistry) UnRegister(url *common.URL) error { if !shouldRegister(url) { return nil } - return s.metaDataService.UnexportURL(url) + return nil } -func (s *ServiceDiscoveryRegistry) UnSubscribe(url *common.URL, listener registry.NotifyListener) error { +func (s *serviceDiscoveryRegistry) UnSubscribe(url *common.URL, listener registry.NotifyListener) error { if !shouldSubscribe(url) { return nil } - err := s.metaDataService.UnsubscribeURL(url) - if err != nil { - return err - } services := s.getServices(url, nil) if services == nil { return nil @@ -122,21 +148,13 @@ func (s *ServiceDiscoveryRegistry) UnSubscribe(url *common.URL, listener registr l := s.serviceListeners[serviceNamesKey] l.RemoveListener(url.ServiceKey()) s.stopListen(url) - err = s.serviceNameMapping.Remove(url) + err := s.serviceNameMapping.Remove(url) if err != nil { return err } return nil } -func creatServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, error) { - originServiceDiscovery, err := extension.GetServiceDiscovery(url) - if err != nil { - return nil, perrors.WithMessage(err, "Create service discovery fialed") - } - return originServiceDiscovery, nil -} - func parseServices(literalServices string) *gxset.HashSet { set := gxset.NewSet() if len(literalServices) == 0 { @@ -151,69 +169,58 @@ func parseServices(literalServices string) *gxset.HashSet { return set } -func (s *ServiceDiscoveryRegistry) GetServiceDiscovery() registry.ServiceDiscovery { +func (s *serviceDiscoveryRegistry) GetServiceDiscovery() registry.ServiceDiscovery { return s.serviceDiscovery } -func (s *ServiceDiscoveryRegistry) GetURL() *common.URL { +func (s *serviceDiscoveryRegistry) GetURL() *common.URL { return s.url } -func (s *ServiceDiscoveryRegistry) IsAvailable() bool { +func (s *serviceDiscoveryRegistry) IsAvailable() bool { if s.serviceDiscovery.GetServices() == nil { return false } return len(s.serviceDiscovery.GetServices().Values()) > 0 } -func (s *ServiceDiscoveryRegistry) Destroy() { +func (s *serviceDiscoveryRegistry) Destroy() { err := s.serviceDiscovery.Destroy() if err != nil { logger.Errorf("destroy serviceDiscovery catch error:%s", err.Error()) } } -func (s *ServiceDiscoveryRegistry) Register(url *common.URL) error { +func (s *serviceDiscoveryRegistry) Register(url *common.URL) error { if !shouldRegister(url) { return nil } common.HandleRegisterIPAndPort(url) - - start := time.Now() - ok, err := s.metaDataService.ExportURL(url) - metrics.Publish(metricsRegistry.NewServerRegisterEvent(ok && err == nil, start)) - - if err != nil { - logger.Errorf("The URL[%s] registry catch error:%s!", url.String(), err.Error()) - return err - } - if !ok { - logger.Warnf("The URL[%s] has been registry!", url.String()) + if id, exist := s.url.GetNonDefaultParam(constant.RegistryIdKey); exist { + metadata.AddService(id, url) } - + metrics.Publish(metricsRegistry.NewServerRegisterEvent(true, time.Now())) return s.serviceNameMapping.Map(url) } func shouldRegister(url *common.URL) bool { side := url.GetParam(constant.SideKey, "") - if side == constant.ProviderProtocol { + if side == constant.SideProvider { return true } logger.Debugf("The URL should not be register.", url.String()) return false } -func (s *ServiceDiscoveryRegistry) Subscribe(url *common.URL, notify registry.NotifyListener) error { +func (s *serviceDiscoveryRegistry) Subscribe(url *common.URL, notify registry.NotifyListener) error { if !shouldSubscribe(url) { logger.Infof("Service %s is set to not subscribe to instances.", url.ServiceKey()) return nil } - _, err := s.metaDataService.SubscribeURL(url) - if err != nil { - return perrors.WithMessage(err, "subscribe url error: "+url.String()) + if id, exist := s.url.GetNonDefaultParam(constant.RegistryIdKey); exist { + metadata.AddSubscribeURL(id, url) } - - mappingListener := NewMappingListener(s.url, url, s.subscribedServices, notify) + mappingListener := NewMappingListener(s.url, url, parseServices(url.GetParam(constant.ProvidedBy, "")), notify) services := s.getServices(url, mappingListener) if services.Empty() { return perrors.Errorf("Should has at least one way to know which services this interface belongs to,"+ @@ -225,7 +232,7 @@ func (s *ServiceDiscoveryRegistry) Subscribe(url *common.URL, notify registry.No return nil } -func (s *ServiceDiscoveryRegistry) SubscribeURL(url *common.URL, notify registry.NotifyListener, services *gxset.HashSet) { +func (s *serviceDiscoveryRegistry) SubscribeURL(url *common.URL, notify registry.NotifyListener, services *gxset.HashSet) { // FIXME ServiceNames.String() is not good var err error serviceNamesKey := services.String() @@ -246,13 +253,13 @@ func (s *ServiceDiscoveryRegistry) SubscribeURL(url *common.URL, notify registry Instances: instances, }) if err != nil { - logger.Warnf("[ServiceDiscoveryRegistry] ServiceInstancesChangedListenerImpl handle error:%v", err) + logger.Warnf("[serviceDiscoveryRegistry] ServiceInstancesChangedListenerImpl handle error:%v", err) } } } s.serviceListeners[serviceNamesKey] = listener listener.AddListenerAndNotify(protocolServiceKey, notify) - event := metricMetadata.NewMetadataMetricTimeEvent(metricMetadata.SubscribeServiceRt) + event := metricsMetadata.NewMetadataMetricTimeEvent(metricsMetadata.SubscribeServiceRt) logger.Infof("Start subscribing to registry for applications :%s with a new go routine.", serviceNamesKey) go func() { @@ -269,7 +276,7 @@ func (s *ServiceDiscoveryRegistry) SubscribeURL(url *common.URL, notify registry } // LoadSubscribeInstances load subscribe instance -func (s *ServiceDiscoveryRegistry) LoadSubscribeInstances(url *common.URL, notify registry.NotifyListener) error { +func (s *serviceDiscoveryRegistry) LoadSubscribeInstances(url *common.URL, notify registry.NotifyListener) error { return nil } @@ -301,9 +308,9 @@ func appendParam(buffer bytes.Buffer, paramKey string, url *common.URL) { buffer.WriteString(url.GetParam(paramKey, "")) } -func (s *ServiceDiscoveryRegistry) synthesizeSubscribedURLs(subscribedURL *common.URL, serviceInstances []registry.ServiceInstance) []*common.URL { +func (s *serviceDiscoveryRegistry) synthesizeSubscribedURLs(subscribedURL *common.URL, serviceInstances []registry.ServiceInstance) []*common.URL { var urls []*common.URL - for _, syn := range s.subscribedURLsSynthesizers { + for _, syn := range synthesizer.GetAllSynthesizer() { if syn.Support(subscribedURL) { urls = append(urls, syn.Synthesize(subscribedURL, serviceInstances)...) } @@ -315,7 +322,7 @@ func shouldSubscribe(url *common.URL) bool { return !shouldRegister(url) } -func (s *ServiceDiscoveryRegistry) getServices(url *common.URL, listener registry.MappingListener) *gxset.HashSet { +func (s *serviceDiscoveryRegistry) getServices(url *common.URL, listener mapping.MappingListener) *gxset.HashSet { services := gxset.NewSet() serviceNames := url.GetParam(constant.ProvidedBy, "") if len(serviceNames) > 0 { @@ -323,14 +330,11 @@ func (s *ServiceDiscoveryRegistry) getServices(url *common.URL, listener registr } if services.Empty() { services = s.findMappedServices(url, listener) - if services.Empty() { - return s.subscribedServices - } } return services } -func (s *ServiceDiscoveryRegistry) findMappedServices(url *common.URL, listener registry.MappingListener) *gxset.HashSet { +func (s *serviceDiscoveryRegistry) findMappedServices(url *common.URL, listener mapping.MappingListener) *gxset.HashSet { serviceNames, err := s.serviceNameMapping.Get(url, listener) if err != nil { logger.Errorf("get service names catch error, url:%s, err:%s ", url.String(), err.Error()) @@ -343,11 +347,7 @@ func (s *ServiceDiscoveryRegistry) findMappedServices(url *common.URL, listener return serviceNames } -var ( - exporting = &atomic.Bool{} -) - -func (s *ServiceDiscoveryRegistry) stopListen(url *common.URL) { +func (s *serviceDiscoveryRegistry) stopListen(url *common.URL) { protocolServiceKey := url.ServiceKey() + ":" + url.Protocol listener := s.serviceMappingListeners[protocolServiceKey] if listener != nil { diff --git a/registry/servicediscovery/service_instances_changed_listener_impl.go b/registry/servicediscovery/service_instances_changed_listener_impl.go index f5123cebd6..b091102f98 100644 --- a/registry/servicediscovery/service_instances_changed_listener_impl.go +++ b/registry/servicediscovery/service_instances_changed_listener_impl.go @@ -33,9 +33,8 @@ import ( 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/metadata/service" - "dubbo.apache.org/dubbo-go/v3/metadata/service/local" + "dubbo.apache.org/dubbo-go/v3/metadata" + "dubbo.apache.org/dubbo-go/v3/metadata/info" "dubbo.apache.org/dubbo-go/v3/registry" "dubbo.apache.org/dubbo-go/v3/registry/servicediscovery/store" "dubbo.apache.org/dubbo-go/v3/remoting" @@ -47,7 +46,7 @@ var ( ) func initCache(app string) { - gob.Register(&common.MetadataInfo{}) + gob.Register(&info.MetadataInfo{}) fileName := constant.DefaultMetaFileName + app cache, err := store.NewCacheManager(constant.DefaultMetaCacheName, fileName, time.Minute*10, constant.DefaultEntrySize, true) if err != nil { @@ -62,7 +61,7 @@ type ServiceInstancesChangedListenerImpl struct { serviceNames *gxset.HashSet listeners map[string]registry.NotifyListener serviceUrls map[string][]*common.URL - revisionToMetadata map[string]*common.MetadataInfo + revisionToMetadata map[string]*info.MetadataInfo allInstances map[string][]registry.ServiceInstance mutex sync.Mutex } @@ -76,7 +75,7 @@ func NewServiceInstancesChangedListener(app string, services *gxset.HashSet) reg serviceNames: services, listeners: make(map[string]registry.NotifyListener), serviceUrls: make(map[string][]*common.URL), - revisionToMetadata: make(map[string]*common.MetadataInfo), + revisionToMetadata: make(map[string]*info.MetadataInfo), allInstances: make(map[string][]registry.ServiceInstance), } } @@ -94,8 +93,8 @@ func (lstn *ServiceInstancesChangedListenerImpl) OnEvent(e observer.Event) error lstn.allInstances[ce.ServiceName] = ce.Instances revisionToInstances := make(map[string][]registry.ServiceInstance) - newRevisionToMetadata := make(map[string]*common.MetadataInfo) - localServiceToRevisions := make(map[*common.ServiceInfo]*gxset.HashSet) + newRevisionToMetadata := make(map[string]*info.MetadataInfo) + localServiceToRevisions := make(map[*info.ServiceInfo]*gxset.HashSet) protocolRevisionsToUrls := make(map[string]map[*gxset.HashSet][]*common.URL) newServiceURLs := make(map[string][]*common.URL) @@ -120,7 +119,7 @@ func (lstn *ServiceInstancesChangedListenerImpl) OnEvent(e observer.Event) error metadataInfo := lstn.revisionToMetadata[revision] if metadataInfo == nil { if val, ok := metaCache.Get(revision); ok { - metadataInfo = val.(*common.MetadataInfo) + metadataInfo = val.(*info.MetadataInfo) } else { metadataInfo, err = GetMetadataInfo(lstn.app, instance, revision) if err != nil { @@ -225,55 +224,33 @@ func (lstn *ServiceInstancesChangedListenerImpl) GetEventType() reflect.Type { } // GetMetadataInfo get metadata info when MetadataStorageTypePropertyName is null -func GetMetadataInfo(app string, instance registry.ServiceInstance, revision string) (*common.MetadataInfo, error) { +func GetMetadataInfo(app string, instance registry.ServiceInstance, revision string) (*info.MetadataInfo, error) { cacheOnce.Do(func() { initCache(app) }) if metadataInfo, ok := metaCache.Get(revision); ok { - return metadataInfo.(*common.MetadataInfo), nil + return metadataInfo.(*info.MetadataInfo), nil } var metadataStorageType string - var metadataInfo *common.MetadataInfo + var metadataInfo *info.MetadataInfo + var err error if instance.GetMetadata() == nil { metadataStorageType = constant.DefaultMetadataStorageType } else { metadataStorageType = instance.GetMetadata()[constant.MetadataStorageTypePropertyName] } if metadataStorageType == constant.RemoteMetadataStorageType { - remoteMetadataServiceImpl, err := extension.GetRemoteMetadataService() - if err != nil { - return nil, err - } - metadataInfo, err = remoteMetadataServiceImpl.GetMetadata(instance) + metadataInfo, err = metadata.GetMetadataFromMetadataReport(revision, instance) if err != nil { return nil, err } } else { - var err error - proxyFactory := extension.GetMetadataServiceProxyFactory(constant.DefaultKey) - metadataService := proxyFactory.GetProxy(instance) - defer destroyInvoker(metadataService) - metadataInfo, err = metadataService.GetMetadataInfo(revision) + metadataInfo, err = metadata.GetMetadataFromRpc(revision, instance) if err != nil { return nil, err } } - metaCache.Set(revision, metadataInfo) - return metadataInfo, nil } - -func destroyInvoker(metadataService service.MetadataService) { - if metadataService == nil { - return - } - - proxy := metadataService.(*local.MetadataServiceProxy) - if proxy.Invoker == nil { - return - } - - proxy.Invoker.Destroy() -} diff --git a/registry/servicediscovery/service_mapping_change_listener_impl.go b/registry/servicediscovery/service_mapping_change_listener_impl.go index 2f64f6973b..8a04eca969 100644 --- a/registry/servicediscovery/service_mapping_change_listener_impl.go +++ b/registry/servicediscovery/service_mapping_change_listener_impl.go @@ -87,7 +87,7 @@ func (lstn *ServiceMappingChangedListenerImpl) OnEvent(e observer.Event) error { if reg, err = extension.GetRegistry(lstn.registryUrl.Protocol, lstn.registryUrl); err != nil { return err } - if sdreg, ok := reg.(*ServiceDiscoveryRegistry); ok { + if sdreg, ok := reg.(*serviceDiscoveryRegistry); ok { sdreg.SubscribeURL(lstn.serviceUrl, lstn.listener, newServiceNames) } lstn.oldServiceNames = newServiceNames @@ -101,7 +101,7 @@ func (lstn *ServiceMappingChangedListenerImpl) OnEvent(e observer.Event) error { if reg, err = extension.GetRegistry(lstn.registryUrl.Protocol, lstn.registryUrl); err != nil { return err } - if sdreg, ok := reg.(*ServiceDiscoveryRegistry); ok { + if sdreg, ok := reg.(*serviceDiscoveryRegistry); ok { sdreg.SubscribeURL(lstn.serviceUrl, lstn.listener, newServiceNames) } lstn.oldServiceNames = newServiceNames diff --git a/registry/servicediscovery/store/cache_manager_test.go b/registry/servicediscovery/store/cache_manager_test.go index 3ca24dc2f1..36541d7ea9 100644 --- a/registry/servicediscovery/store/cache_manager_test.go +++ b/registry/servicediscovery/store/cache_manager_test.go @@ -23,7 +23,7 @@ import ( ) import ( - "dubbo.apache.org/dubbo-go/v3/common" + "dubbo.apache.org/dubbo-go/v3/metadata/info" ) var defaultTime = time.Second * 3 @@ -86,15 +86,15 @@ func TestCacheManager(t *testing.T) { func TestMetaInfoCacheManager(t *testing.T) { - serverInfo := make(map[string]*common.ServiceInfo) - serverInfo["1"] = common.NewServiceInfo("1", "1", "1", "1", "1", make(map[string]string)) - serverInfo["2"] = common.NewServiceInfo("2", "2", "2", "2", "2", make(map[string]string)) - serverInfo["3"] = common.NewServiceInfo("3", "3", "3", "3", "3", make(map[string]string)) + serverInfo := make(map[string]*info.ServiceInfo) + serverInfo["1"] = info.NewServiceInfo("1", "1", "1", "1", "1", make(map[string]string)) + serverInfo["2"] = info.NewServiceInfo("2", "2", "2", "2", "2", make(map[string]string)) + serverInfo["3"] = info.NewServiceInfo("3", "3", "3", "3", "3", make(map[string]string)) - metadataInfo1 := common.NewMetadataInfo("1", "1", serverInfo) - metadataInfo2 := common.NewMetadataInfo("2", "2", serverInfo) - metadataInfo3 := common.NewMetadataInfo("3", "3", serverInfo) - metadataInfo4 := common.NewMetadataInfo("4", "4", serverInfo) + metadataInfo1 := info.NewMetadataInfoWithParams("1", "1", serverInfo) + metadataInfo2 := info.NewMetadataInfoWithParams("2", "2", serverInfo) + metadataInfo3 := info.NewMetadataInfoWithParams("3", "3", serverInfo) + metadataInfo4 := info.NewMetadataInfoWithParams("4", "4", serverInfo) cm, err := NewCacheManager("metaTest1", "test_meta_cache", defaultTime, 10, true) if err != nil { diff --git a/registry/zookeeper/service_discovery_test.go b/registry/zookeeper/service_discovery_test.go index 00868b3c8a..2aca0ec852 100644 --- a/registry/zookeeper/service_discovery_test.go +++ b/registry/zookeeper/service_discovery_test.go @@ -136,13 +136,10 @@ func (m *mockInvoker) Destroy() { } func (m *mockInvoker) Invoke(context.Context, protocol.Invocation) protocol.Result { - // for getMetadataInfo and ServiceInstancesChangedListenerImpl onEvent - serviceInfo := &common.ServiceInfo{ServiceKey: "test", MatchKey: "test"} - services := make(map[string]*common.ServiceInfo) - services["test"] = serviceInfo return &protocol.RPCResult{ - Rest: &common.MetadataInfo{ - Services: services, - }, + Rest: &mockResult{}, } } + +type mockResult struct { +} diff --git a/remoting/nacos/builder.go b/remoting/nacos/builder.go index efecef78a9..2cd88bb72d 100644 --- a/remoting/nacos/builder.go +++ b/remoting/nacos/builder.go @@ -36,7 +36,6 @@ import ( import ( "dubbo.apache.org/dubbo-go/v3/common" "dubbo.apache.org/dubbo-go/v3/common/constant" - "dubbo.apache.org/dubbo-go/v3/config" ) // NewNacosConfigClientByUrl read the config from url and build an instance @@ -108,24 +107,6 @@ func GetNacosConfig(url *common.URL) ([]nacosConstant.ServerConfig, nacosConstan return serverConfigs, clientConfig, nil } -// NewNacosClient create an instance with the config -func NewNacosClient(rc *config.RemoteConfig) (*nacosClient.NacosNamingClient, error) { - url, err := rc.ToURL() - if err != nil { - return nil, err - } - scs, cc, err := GetNacosConfig(url) - - if err != nil { - return nil, err - } - clientName := url.GetParam(constant.ClientNameKey, "") - if len(clientName) <= 0 { - return nil, perrors.New("nacos client name must set") - } - return nacosClient.NewNacosNamingClient(clientName, true, scs, cc) -} - // NewNacosClientByURL created func NewNacosClientByURL(url *common.URL) (*nacosClient.NacosNamingClient, error) { scs, cc, err := GetNacosConfig(url) diff --git a/server/action.go b/server/action.go index 98a40ec613..784ac8bf31 100644 --- a/server/action.go +++ b/server/action.go @@ -194,6 +194,7 @@ func (svcOpts *ServiceOptions) export(info *ServiceInfo) error { common.WithPort(port), common.WithParams(urlMap), common.WithParamsValue(constant.BeanNameKey, svcOpts.Id), + common.WithParamsValue(constant.ApplicationTagKey, svcOpts.Application.Tag), //common.WithParamsValue(constant.SslEnabledKey, strconv.FormatBool(config.GetSslEnabled())), common.WithMethods(strings.Split(methods, ",")), // todo(DMwangnima): remove this @@ -233,16 +234,6 @@ func (svcOpts *ServiceOptions) export(info *ServiceInfo) error { svcOpts.exporters = append(svcOpts.exporters, exporter) } } else { - if ivkURL.GetParam(constant.InterfaceKey, "") == constant.MetadataServiceName { - ms, err := extension.GetLocalMetadataService("") - if err != nil { - logger.Warnf("needExport org.apache.dubbo.metadata.MetadataService failed beacause of %svcOpts ! pls check if you import _ \"dubbo.apache.org/dubbo-go/v3/metadata/service/local\"", err) - return nil - } - if err := ms.SetMetadataServiceURL(ivkURL); err != nil { - logger.Warnf("SetMetadataServiceURL error = %svcOpts", err) - } - } invoker = svcOpts.generatorInvoker(ivkURL, info) exporter := extension.GetProtocol(protocolwrapper.FILTER).Export(invoker) if exporter == nil { @@ -250,7 +241,6 @@ func (svcOpts *ServiceOptions) export(info *ServiceInfo) error { } svcOpts.exporters = append(svcOpts.exporters, exporter) } - publishServiceDefinition(ivkURL) // this protocol would be destroyed in graceful_shutdown // please refer to (https://github.com/apache/dubbo-go/issues/2429) graceful_shutdown.RegisterProtocol(proto.Name) @@ -426,21 +416,6 @@ func (svcOpts *ServiceOptions) postProcessConfig(url *common.URL) { } } -func publishServiceDefinition(url *common.URL) { - localService, err := extension.GetLocalMetadataService(constant.DefaultKey) - if err != nil { - logger.Warnf("get local metadata service failed, please check if you have imported _ \"dubbo.apache.org/dubbo-go/v3/metadata/service/local\"") - return - } - localService.PublishServiceDefinition(url) - if url.GetParam(constant.MetadataTypeKey, "") != constant.RemoteMetadataStorageType { - return - } - if remoteMetadataService, err := extension.GetRemoteMetadataService(); err == nil && remoteMetadataService != nil { - remoteMetadataService.PublishServiceDefinition(url) - } -} - // todo(DMwangnima): think about moving this function to a common place(e.g. /common/config) func getRegistryIds(registries map[string]*global.RegistryConfig) []string { ids := make([]string, 0) diff --git a/server/server.go b/server/server.go index 0258a940d3..e634aad6d0 100644 --- a/server/server.go +++ b/server/server.go @@ -22,7 +22,9 @@ import ( "context" "fmt" "sort" + "strconv" "sync" + "time" ) import ( @@ -33,9 +35,11 @@ import ( import ( "dubbo.apache.org/dubbo-go/v3/common" + "dubbo.apache.org/dubbo-go/v3/common/constant" + "dubbo.apache.org/dubbo-go/v3/global" "dubbo.apache.org/dubbo-go/v3/metadata" "dubbo.apache.org/dubbo-go/v3/protocol" - registry_exposed "dubbo.apache.org/dubbo-go/v3/registry/exposed_tmp" + "dubbo.apache.org/dubbo-go/v3/registry/exposed_tmp" ) // proServices are for internal services @@ -192,14 +196,28 @@ func (s *Server) exportServices() (err error) { } func (s *Server) Serve() error { + // the registryConfig in ServiceOptions and ServerOptions all need to init a metadataReporter, + // when ServiceOptions.init() is called we don't know if a new registry config is set in the future use serviceOption + if err := s.initRegistryMetadataReport(); err != nil { + return err + } + opts := metadata.NewOptions( + metadata.WithAppName(s.cfg.Application.Name), + metadata.WithMetadataType(s.cfg.Application.MetadataType), + metadata.WithPort(getMetadataPort(s.cfg)), + ) + if err := opts.Init(); err != nil { + return err + } if err := s.exportServices(); err != nil { return err } if err := s.exportInternalServices(); err != nil { return err } - metadata.ExportMetadataService() - registry_exposed.RegisterServiceInstance(s.cfg.Application.Name, s.cfg.Application.Tag, s.cfg.Application.MetadataType) + if err := exposed_tmp.RegisterServiceInstance(); err != nil { + return err + } select {} } @@ -277,6 +295,67 @@ type InternalService struct { Priority int } +func getMetadataPort(opts *ServerOptions) int { + port := opts.Application.MetadataServicePort + if port == "" { + protocolConfig, ok := opts.Protocols[constant.DefaultProtocol] + if ok { + port = protocolConfig.Port + } else { + logger.Warnf("[Metadata Service] Dubbo-go %s version's MetadataService only support dubbo protocol,"+ + "MetadataService will use random port", + constant.Version) + } + } + if port == "" { + return 0 + } + p, err := strconv.Atoi(port) + if err != nil { + logger.Error("MetadataService port parse error %v, MetadataService will use random port", err) + return 0 + } + return p +} + +func (s *Server) initRegistryMetadataReport() error { + if len(s.cfg.Registries) > 0 { + for id, reg := range s.cfg.Registries { + if reg.UseAsMetaReport { + opts, err := registryToReportOptions(id, reg) + if err != nil { + return err + } + if err := opts.Init(); err != nil { + return err + } + } + } + } + return nil +} + +func registryToReportOptions(id string, rc *global.RegistryConfig) (*metadata.ReportOptions, error) { + opts := metadata.NewReportOptions( + metadata.WithRegistryId(id), + metadata.WithProtocol(rc.Protocol), + metadata.WithAddress(rc.Address), + metadata.WithUsername(rc.Username), + metadata.WithPassword(rc.Password), + metadata.WithGroup(rc.Group), + metadata.WithNamespace(rc.Namespace), + metadata.WithParams(rc.Params), + ) + if rc.Timeout != "" { + timeout, err := time.ParseDuration(rc.Timeout) + if err != nil { + return nil, err + } + metadata.WithTimeout(timeout)(opts) + } + return opts, nil +} + type MethodInfo struct { Name string Type string