From 664e03fc8622a5957aaef84e63fb5d457ff0e754 Mon Sep 17 00:00:00 2001 From: foghost Date: Wed, 22 Nov 2023 12:52:50 +0800 Subject: [PATCH 01/26] refact metadata fmt --- .gitignore | 1 + client/action.go | 2 - common/extension/metadata_remote.go | 51 --- common/extension/metadata_report_factory.go | 8 +- common/extension/metadata_service.go | 55 --- common/extension/metadata_service_exporter.go | 46 --- .../metadata_service_proxy_factory.go | 50 --- config/config_loader.go | 18 +- config/instance/metadata_report.go | 66 ---- config/instance/metadata_report_test.go | 112 ------ config/instance/registry_metadata_report.go | 71 ---- .../instance/registry_metadata_report_test.go | 64 ---- config/metadata_report_config.go | 93 ++--- config/metadata_report_config_test.go | 1 - config/reference_config.go | 2 - config/registry_config.go | 8 - config/root_config.go | 61 ---- config/service_config.go | 13 +- go.mod | 1 - graceful_shutdown/common.go | 2 - imports/imports.go | 3 - metadata/definition/definition.go | 188 ----------- metadata/definition/definition_test.go | 53 --- metadata/definition/mock.go | 45 --- .../identifier/base_metadata_identifier.go | 113 ------- .../base_metadata_identifier_test.go | 41 --- metadata/identifier/metadata_identifier.go | 34 -- .../identifier/metadata_identifier_test.go | 44 --- .../identifier/service_metadata_identifier.go | 54 --- .../service_metadata_identifier_test.go | 45 --- .../subscribe_metadata_identifier.go | 48 --- .../subscribe_metadata_identifier_test.go | 42 --- {common => metadata/info}/metadata_info.go | 106 ++++-- metadata/info/metadata_info_test.go | 30 ++ .../mapping/memory/service_name_mapping.go | 64 ---- .../mapping/metadata/service_name_mapping.go | 15 +- metadata/mapping/mock_service_name_mapping.go | 45 --- metadata/mapping/service_name_mapping.go | 16 +- metadata/metadata.go | 81 ++--- metadata/report/delegate/delegate_report.go | 319 ------------------ .../report/delegate/delegate_report_test.go | 119 ------- metadata/report/etcd/report.go | 111 +----- metadata/report/instance/metadata_report.go | 118 +++++++ metadata/report/nacos/report.go | 128 +------ metadata/report/nacos/report_test.go | 242 +------------ metadata/report/report.go | 44 +-- .../report/{factory => }/report_factory.go | 5 +- metadata/report/reporter_metric.go | 66 ---- metadata/report/zookeeper/listener.go | 13 +- metadata/report/zookeeper/report.go | 106 +----- .../service/exporter/configurable/exporter.go | 121 ------- .../exporter/configurable/exporter_test.go | 130 ------- metadata/service/exporter/exporter.go | 30 -- .../local/metadata_service_proxy_factory.go | 114 ------- .../metadata_service_proxy_factory_test.go | 95 ------ metadata/service/local/service.go | 282 ---------------- metadata/service/local/service_proxy.go | 190 ----------- metadata/service/local/service_proxy_test.go | 94 ------ metadata/service/local/service_test.go | 119 ------- metadata/service/local_service.go | 137 -------- metadata/service/metadata_service.go | 51 +++ metadata/service/remote/service.go | 153 +++------ metadata/service/remote/service_test.go | 182 ++-------- metadata/service/remote_service.go | 32 -- metadata/service/service.go | 174 ++++++++++ protocol/dubbo3/dubbo3_invoker_test.go | 4 - registry/etcdv3/service_discovery.go | 14 +- registry/etcdv3/service_discovery_test.go | 11 +- .../metadata_service_url_params_customizer.go | 14 +- .../protocol_ports_metadata_customizer.go | 19 +- registry/event/service_revision_customizer.go | 29 +- registry/exposed_tmp/exposed.go | 17 +- registry/nacos/service_discovery.go | 12 +- registry/nacos/service_discovery_test.go | 11 +- registry/polaris/service_discovery.go | 16 +- registry/service_discovery.go | 45 +++ registry/service_instance.go | 11 +- registry/service_mapping_changed_listener.go | 27 -- .../service_discovery_registry.go | 50 +-- ...service_instances_changed_listener_impl.go | 76 ++--- .../store/cache_manager_test.go | 18 +- registry/zookeeper/service_discovery.go | 8 +- registry/zookeeper/service_discovery_test.go | 11 +- server/action.go | 11 - server/server.go | 2 +- 85 files changed, 862 insertions(+), 4511 deletions(-) delete mode 100644 common/extension/metadata_remote.go delete mode 100644 common/extension/metadata_service.go delete mode 100644 common/extension/metadata_service_exporter.go delete mode 100644 common/extension/metadata_service_proxy_factory.go delete mode 100644 config/instance/metadata_report.go delete mode 100644 config/instance/metadata_report_test.go delete mode 100644 config/instance/registry_metadata_report.go delete mode 100644 config/instance/registry_metadata_report_test.go delete mode 100644 metadata/definition/definition.go delete mode 100644 metadata/definition/definition_test.go delete mode 100644 metadata/definition/mock.go delete mode 100644 metadata/identifier/base_metadata_identifier.go delete mode 100644 metadata/identifier/base_metadata_identifier_test.go delete mode 100644 metadata/identifier/metadata_identifier.go delete mode 100644 metadata/identifier/metadata_identifier_test.go delete mode 100644 metadata/identifier/service_metadata_identifier.go delete mode 100644 metadata/identifier/service_metadata_identifier_test.go delete mode 100644 metadata/identifier/subscribe_metadata_identifier.go delete mode 100644 metadata/identifier/subscribe_metadata_identifier_test.go rename {common => metadata/info}/metadata_info.go (62%) create mode 100644 metadata/info/metadata_info_test.go delete mode 100644 metadata/mapping/memory/service_name_mapping.go delete mode 100644 metadata/mapping/mock_service_name_mapping.go delete mode 100644 metadata/report/delegate/delegate_report.go delete mode 100644 metadata/report/delegate/delegate_report_test.go create mode 100644 metadata/report/instance/metadata_report.go rename metadata/report/{factory => }/report_factory.go (89%) delete mode 100644 metadata/report/reporter_metric.go delete mode 100644 metadata/service/exporter/configurable/exporter.go delete mode 100644 metadata/service/exporter/configurable/exporter_test.go delete mode 100644 metadata/service/exporter/exporter.go delete mode 100644 metadata/service/local/metadata_service_proxy_factory.go delete mode 100644 metadata/service/local/metadata_service_proxy_factory_test.go delete mode 100644 metadata/service/local/service.go delete mode 100644 metadata/service/local/service_proxy.go delete mode 100644 metadata/service/local/service_proxy_test.go delete mode 100644 metadata/service/local/service_test.go delete mode 100644 metadata/service/local_service.go create mode 100644 metadata/service/metadata_service.go delete mode 100644 metadata/service/remote_service.go create mode 100644 metadata/service/service.go delete mode 100644 registry/service_mapping_changed_listener.go 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..da521c22b6 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 { 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/config/config_loader.go b/config/config_loader.go index eda382d8fd..7bd86d17a8 100644 --- a/config/config_loader.go +++ b/config/config_loader.go @@ -36,6 +36,7 @@ import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/common/extension" "dubbo.apache.org/dubbo-go/v3/logger/zap" + "dubbo.apache.org/dubbo-go/v3/metadata/service" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -104,12 +105,21 @@ func registerServiceInstance() { panic(err) } } - // publish metadata to remote - if GetApplicationConfig().MetadataType == constant.RemoteMetadataStorageType { - if remoteMetadataService, err := extension.GetRemoteMetadataService(); err == nil && remoteMetadataService != nil { - remoteMetadataService.PublishMetadata(GetApplicationConfig().Name) +} +func selectMetadataServiceExportedURL() *common.URL { + var selectedUrl *common.URL + urlList := service.GlobalMetadataService.GetExportedServiceURLs() + if len(urlList) == 0 { + return nil + } + for _, url := range urlList { + selectedUrl = url + // rest first + if url.Protocol == "rest" { + break } } + return selectedUrl } // // nolint 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_report_config.go index bde3732e7f..6db749fe43 100644 --- a/config/metadata_report_config.go +++ b/config/metadata_report_config.go @@ -18,7 +18,7 @@ package config import ( - "github.com/dubbogo/gost/log/logger" + "github.com/nacos-group/nacos-sdk-go/v2/common/logger" perrors "github.com/pkg/errors" ) @@ -26,8 +26,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/report/instance" ) // MetadataReportConfig is app level configuration @@ -54,7 +53,36 @@ func (mc *MetadataReportConfig) Init(rc *RootConfig) error { return nil } mc.metadataType = rc.Application.MetadataType - return mc.StartMetadataReport() + if mc.Address != "" { + tmpUrl, err := mc.ToUrl() + if err != nil { + logger.Errorf("MetadataReport init failed, err: %v", err) + return err + } + // if metadata report config is avaible, then init metadata report instance + instance.Init([]*common.URL{tmpUrl}) + return nil + } + if rc.Registries != nil && len(rc.Registries) > 0 { + // if metadata report config is not avaible, then init metadata report instance with registries + urls := make([]*common.URL, 0) + for id, reg := range rc.Registries { + if reg.UseAsMetaReport && isValid(reg.Address) { + if tmpUrl, err := reg.toMetadataReportUrl(); err == nil { + tmpUrl.AddParam(constant.RegistryKey, id) + urls = append(urls, tmpUrl) + } else { + logger.Warnf("use registry as metadata report failed, err: %v", err) + } + } + } + if len(urls) > 0 { + instance.Init(urls) + } else { + logger.Warnf("No metadata report config found, skip init metadata report instance.") + } + } + return nil } func (mc *MetadataReportConfig) ToUrl() (*common.URL, error) { @@ -79,63 +107,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_report_config_test.go index 1b1a9ac809..1e7caf5ace 100644 --- a/config/metadata_report_config_test.go +++ b/config/metadata_report_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/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..62feccd415 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) } diff --git a/config/root_config.go b/config/root_config.go index 8239b1e109..be7e9f02b7 100644 --- a/config/root_config.go +++ b/config/root_config.go @@ -27,23 +27,16 @@ 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" ) var ( startOnce sync.Once - exporting = &atomic.Bool{} ) // RootConfig is the root config @@ -122,8 +115,6 @@ func (rc *RootConfig) getRegistryIds() []string { return removeDuplicateElement(ids) } func registerPOJO() { - hessian.RegisterPOJO(&common.MetadataInfo{}) - hessian.RegisterPOJO(&common.ServiceInfo{}) hessian.RegisterPOJO(&common.URL{}) } @@ -212,8 +203,6 @@ func (rc *RootConfig) Start() { gracefulShutdownInit() rc.Consumer.Load() rc.Provider.Load() - // todo if register consumer instance or has exported services - exportMetadataService() registerServiceInstance() }) } @@ -346,56 +335,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..6fa195191e 100644 --- a/config/service_config.go +++ b/config/service_config.go @@ -327,24 +327,13 @@ 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) + invoker := proxyFactory.GetInvoker(ivkURL) exporter := extension.GetProtocol(protocolwrapper.FILTER).Export(invoker) if exporter == nil { return perrors.New(fmt.Sprintf("Filter protocol without registry new exporter error, url is {%v}", ivkURL)) } s.exporters = append(s.exporters, exporter) } - publishServiceDefinition(ivkURL) } s.exported.Store(true) return nil 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/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/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/common/metadata_info.go b/metadata/info/metadata_info.go similarity index 62% rename from common/metadata_info.go rename to metadata/info/metadata_info.go index aad5926b70..4db5a87d35 100644 --- a/common/metadata_info.go +++ b/metadata/info/metadata_info.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package common +package info import ( "fmt" @@ -26,13 +26,21 @@ import ( ) 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, @@ -50,10 +58,12 @@ var IncludeKeys = gxset.NewSet( // 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"` + Reported bool `json:"-"` + App string `json:"app,omitempty" hessian:"app"` + Revision string `json:"revision,omitempty" hessian:"revision"` + Services map[string]*ServiceInfo `json:"services,omitempty" hessian:"services"` + exportedServiceURLs map[string][]*common.URL // server exported service urls + subscribedServiceURLs map[string][]*common.URL // client subscribed service urls } // nolint @@ -126,37 +136,79 @@ func (mi *MetadataInfo) MarkReported() { } // nolint -func (mi *MetadataInfo) AddService(service *ServiceInfo) { - if service == nil { - return - } +// AddService add provider service info to MetadataInfo +func (mi *MetadataInfo) AddService(url *common.URL) { + service := NewServiceInfoWithURL(url) mi.Services[service.GetMatchKey()] = service + addUrl(mi.exportedServiceURLs, url) +} + +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 _, ok := m[url.ServiceKey()]; ok { + // m[url.ServiceKey()].Remove(url) + // if m[url.ServiceKey()].Size() == 0 { + // delete(m, url.ServiceKey()) + // } + // } } // nolint -func (mi *MetadataInfo) RemoveService(service *ServiceInfo) { - if service == nil { - return +func (mi *MetadataInfo) RemoveService(url *common.URL) { + service := NewServiceInfoWithURL(url) + delete(mi.Services, service.GetMatchKey()) + removeUrl(mi.exportedServiceURLs, url) +} + +// 客户端 订阅一个 服务的 url 元数据 +func (info *MetadataInfo) AddSubscribeURL(url *common.URL) { + addUrl(info.subscribedServiceURLs, url) +} + +// 客户端 删除一个订阅的服务 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...) } - delete(mi.Services, service.MatchKey) + return res } // 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"` + 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:"-"` - MatchKey string `json:"-"` - URL *URL `json:"-"` + ServiceKey string `json:"-"` + MatchKey string `json:"-"` + URL *common.URL `json:"-"` } // nolint -func NewServiceInfoWithURL(url *URL) *ServiceInfo { +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 @@ -180,8 +232,8 @@ func NewServiceInfoWithURL(url *URL) *ServiceInfo { // nolint func NewServiceInfo(name, group, version, protocol, path string, params map[string]string) *ServiceInfo { - serviceKey := ServiceKey(name, group, version) - matchKey := MatchKey(serviceKey, protocol) + serviceKey := common.ServiceKey(name, group, version) + matchKey := common.MatchKey(serviceKey, protocol) return &ServiceInfo{ Name: name, Group: group, @@ -235,7 +287,7 @@ func (si *ServiceInfo) GetMatchKey() string { return si.MatchKey } serviceKey := si.GetServiceKey() - si.MatchKey = MatchKey(serviceKey, si.Protocol) + si.MatchKey = common.MatchKey(serviceKey, si.Protocol) return si.MatchKey } @@ -244,6 +296,6 @@ func (si *ServiceInfo) GetServiceKey() string { if si.ServiceKey != "" { return si.ServiceKey } - si.ServiceKey = ServiceKey(si.Name, si.Group, si.Version) + 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..c3d537aef8 --- /dev/null +++ b/metadata/info/metadata_info_test.go @@ -0,0 +1,30 @@ +package info + +import ( + "testing" +) + +import ( + "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) +} 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..b0b4552cef 100644 --- a/metadata/mapping/metadata/service_name_mapping.go +++ b/metadata/mapping/metadata/service_name_mapping.go @@ -33,10 +33,9 @@ import ( "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/mapping" "dubbo.apache.org/dubbo-go/v3/metadata/report" - "dubbo.apache.org/dubbo-go/v3/registry" + "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" ) const ( @@ -62,7 +61,7 @@ func (d *MetadataServiceNameMapping) Map(url *common.URL) error { return nil } - appName := config.GetApplicationConfig().Name + appName := url.GetParam(constant.ApplicationKey, "") metadataReport := getMetaDataReport(url.GetParam(constant.RegistryKey, "")) if metadataReport == nil { @@ -77,15 +76,15 @@ func (d *MetadataServiceNameMapping) Map(url *common.URL) error { } // 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 *MetadataServiceNameMapping) Get(url *common.URL, listener mapping.MappingListener) (*gxset.HashSet, error) { serviceInterface := url.GetParam(constant.InterfaceKey, "") - metadataReport := instance.GetMetadataReportInstance() + metadataReport := instance.GetMetadataReport() return metadataReport.GetServiceAppMapping(serviceInterface, DefaultGroup, listener) } func (d *MetadataServiceNameMapping) Remove(url *common.URL) error { serviceInterface := url.GetParam(constant.InterfaceKey, "") - metadataReport := instance.GetMetadataReportInstance() + metadataReport := instance.GetMetadataReport() return metadataReport.RemoveServiceAppMappingListener(serviceInterface, DefaultGroup) } @@ -114,8 +113,8 @@ func GetNameMappingInstance() mapping.ServiceNameMapping { func getMetaDataReport(protocol string) report.MetadataReport { var metadataReport report.MetadataReport if config.GetApplicationConfig().MetadataType == constant.RemoteMetadataStorageType { - metadataReport = instance.GetMetadataReportInstance() + metadataReport = instance.GetMetadataReport() return metadataReport } - return instance.GetMetadataReportByRegistryProtocol(protocol) + return instance.GetMetadataReport() } 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..b7523aaf23 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -19,69 +19,44 @@ package metadata import ( - "github.com/dubbogo/gost/log/logger" - - perrors "github.com/pkg/errors" + "sync" +) - "go.uber.org/atomic" +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/metadata/service/exporter" + "dubbo.apache.org/dubbo-go/v3/metadata/service" ) var ( - exporting = &atomic.Bool{} + exportOnce sync.Once ) -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 - } - - err = expt.Export(nil) - if err != nil { - logger.Errorf("could not export the metadata service, err = %s", err.Error()) - return - } +func ExportMetadataService(app, metadataType string) { + exportOnce.Do(func() { + err := service.Exporter.Export(app, metadataType) + if err != nil { + logger.Errorf("export metadata service failed, got error %#v", err) + } + }) - // report interface-app mapping - err = publishMapping(expt) - if err != nil { - logger.Errorf("Publish interface-application mapping failed, got error %#v", err) - } + // 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 -} +// 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 +// } 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..6f90491c8a 100644 --- a/metadata/report/etcd/report.go +++ b/metadata/report/etcd/report.go @@ -34,24 +34,23 @@ 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 + client *gxetcd.Client + rootDir string } func (e *etcdMetadataReport) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { @@ -60,20 +59,20 @@ func (e *etcdMetadataReport) GetConfigKeysByGroup(group string) (*gxset.HashSet, } // 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 +81,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 +98,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 +127,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/instance/metadata_report.go b/metadata/report/instance/metadata_report.go new file mode 100644 index 0000000000..1bc5bad0f4 --- /dev/null +++ b/metadata/report/instance/metadata_report.go @@ -0,0 +1,118 @@ +/* + * 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" + "time" +) + +import ( + "github.com/dubbogo/gost/container/set" + "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/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) + once sync.Once +) + +func Init(urls []*common.URL) { + once.Do(func() { + if len(urls) == 0 { + for _, url := range urls { + fac := extension.GetMetadataReportFactory(url.Protocol) + if fac != nil { + key := url.GetParam(constant.RegistryKey, constant.DefaultKey) + instances[key] = &DelegateMetadataReport{instance: fac.CreateMetadataReport(url)} + } else { + logger.Warnf("metadata report instance is not found, url: %s", url.String()) + } + } + } + }) +} + +func GetMetadataReport() report.MetadataReport { + if len(instances) > 0 { + for _, v := range instances { + return v + } + } + return nil +} + +func GetMetadataReports() []report.MetadataReport { + reports := make([]report.MetadataReport, len(instances)) + for _, metadataReport := range instances { + reports = append(reports, metadataReport) + } + return reports +} + +// 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, p1 string, p2 mapping.MappingListener) (*gxset.HashSet, error) { + panic("TODO") +} + +func (d *DelegateMetadataReport) RegisterServiceAppMapping(p0 string, p1 string, p2 string) error { + panic("TODO") +} + +func (d *DelegateMetadataReport) RemoveServiceAppMappingListener(p0 string, p1 string) error { + panic("TODO") +} + +func (d *DelegateMetadataReport) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { + panic("TODO: Implement") +} diff --git a/metadata/report/nacos/report.go b/metadata/report/nacos/report.go index fd8182c270..caa65052fb 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,10 +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/nacos" ) @@ -55,7 +54,7 @@ const ( func init() { mf := &nacosMetadataReportFactory{} - extension.SetMetadataReportFactory("nacos", func() factory.MetadataReportFactory { + extension.SetMetadataReportFactory("nacos", func() report.MetadataReportFactory { return mf }) } @@ -64,20 +63,18 @@ 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, }) 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 +83,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 +108,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 +118,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 +128,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 +171,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 { @@ -349,5 +247,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..33786f0941 100644 --- a/metadata/report/report.go +++ b/metadata/report/report.go @@ -22,57 +22,23 @@ 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(application, revision string, info *info.MetadataInfo) error // RegisterServiceAppMapping map the specified Dubbo service interface to current Dubbo app name RegisterServiceAppMapping(string, string, string) error // GetServiceAppMapping get the app names from the specified Dubbo service interface - GetServiceAppMapping(string, string, registry.MappingListener) (*gxset.HashSet, error) + GetServiceAppMapping(string, string, mapping.MappingListener) (*gxset.HashSet, error) // RemoveServiceAppMappingListener remove the serviceMapping listener by key and group RemoveServiceAppMappingListener(string, 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..04fe1d3898 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 @@ -200,7 +132,7 @@ func (m *zookeeperMetadataReport) GetServiceAppMapping(key string, group string, // GetConfigKeysByGroup will return all keys with the group func (m *zookeeperMetadataReport) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { - path := m.getPath("", group) + path := m.rootDir + group result, err := m.client.GetChildren(path) if err != nil { return nil, perrors.WithStack(err) @@ -220,20 +152,6 @@ func (m *zookeeperMetadataReport) RemoveServiceAppMappingListener(key string, gr 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/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/metadata_service.go b/metadata/service/metadata_service.go new file mode 100644 index 0000000000..2a760cfbec --- /dev/null +++ b/metadata/service/metadata_service.go @@ -0,0 +1,51 @@ +/* + * 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/metadata/info" +) + +// MetadataService is used to define meta data related behaviors +// usually the implementation should be singleton +type MetadataService interface { + // GetExportedURLs will get the target exported url in metadata, the url should be unique + GetExportedURLs(serviceInterface string, group string, version string, protocol string) []*common.URL + // GetExportedServiceURLs will return exported service urls + GetExportedServiceURLs() []*common.URL + // GetSubscribedURLs will get the exported urls in metadata + GetSubscribedURLs() []*common.URL + Version() string + // GetMetadataInfo will return metadata info + GetMetadataInfo(revision string) *info.MetadataInfo + // GetMetadataServiceURL will return the url of metadata service + GetMetadataServiceURL() *common.URL + // MethodMapper only for rename exported function, for example: rename the function GetMetadataInfo to getMetadataInfo + MethodMapper() map[string]string +} + +type BaseMetadataService struct { +} + +func (mts *BaseMetadataService) MethodMapper() map[string]string { + return map[string]string{ + "GetExportedURLs": "getExportedURLs", + "GetMetadataInfo": "getMetadataInfo", + } +} diff --git a/metadata/service/remote/service.go b/metadata/service/remote/service.go index 9b2ba3909f..a02496f328 100644 --- a/metadata/service/remote/service.go +++ b/metadata/service/remote/service.go @@ -15,130 +15,77 @@ * limitations under the License. */ -package remote +package client import ( - "sync" -) - -import ( - "github.com/dubbogo/gost/log/logger" - - "go.uber.org/atomic" + "context" + "strconv" + "time" ) 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/metadata/info" + metadataInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" "dubbo.apache.org/dubbo-go/v3/registry" ) -type MetadataService struct { - *local.MetadataService - exportedRevision atomic.String - subscribedRevision atomic.String - delegateReport *delegate.MetadataReport +// GetMetadata get the medata info of service from report +func GetRemoteMetadata(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { + meta, err := getMetadataFromCache(revision, instance) + if err != nil || meta == nil { + meta, err = getMetadataFromMetadataReport(revision, instance) + if err != nil || meta == nil { + meta, err = getMetadataFromRpc(revision, instance) + } + // TODO: need to update cache + } + return meta, err } -var ( - metadataServiceOnce sync.Once - remoteMetadataServiceInstance service.RemoteMetadataService -) - -func init() { - extension.SetRemoteMetadataService(GetRemoteMetadataService) +func getMetadataFromCache(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { + // TODO + return nil, nil } -// 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 +func getMetadataFromMetadataReport(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { + report := metadataInstance.GetMetadataReport() + return report.GetAppMetadata(instance.GetServiceName(), revision) } -// PublishMetadata publishes the metadata info of @service to remote metadata center -func (s *MetadataService) PublishMetadata(service string) { - info, err := s.MetadataService.GetMetadataInfo("") +func getMetadataFromRpc(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { + url := common.NewURLWithOptions( + common.WithProtocol(constant.Dubbo), + common.WithIp(instance.GetHost()), + common.WithPort(strconv.Itoa(instance.GetPort())), + ) + url.SetParam(constant.SideKey, constant.Consumer) + url.SetParam(constant.VersionKey, "1.0.0") + url.SetParam(constant.InterfaceKey, constant.MetadataServiceName) + url.SetParam(constant.GroupKey, instance.GetServiceName()) + service, err := createRpcClient(url) if err != nil { - logger.Errorf("GetMetadataInfo error[%v]", err) - return - } - if info.HasReported() { - return + return nil, err } - 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() + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5000)) + defer cancel() + return service.GetMetadataInfo(ctx, revision) } -// 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) +type metadataService struct { + GetExportedURLs func(context context.Context, serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) `dubbo:"getExportedURLs"` + GetMetadataInfo func(context context.Context, revision string) (*info.MetadataInfo, error) `dubbo:"getMetadataInfo"` + GetMetadataServiceURL func(context context.Context) (*common.URL, error) + GetSubscribedURLs func(context context.Context) ([]*common.URL, error) + Version func(context context.Context) (string, error) } -// 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 +func createRpcClient(url *common.URL) (*metadataService, error) { + rpcService := &metadataService{} + invoker := extension.GetProtocol(constant.Dubbo).Refer(url) + proxy := extension.GetProxyFactory("").GetProxy(invoker, url) + proxy.Implement(rpcService) + return rpcService, nil } diff --git a/metadata/service/remote/service_test.go b/metadata/service/remote/service_test.go index 10f2b1e53f..6284dfcc99 100644 --- a/metadata/service/remote/service_test.go +++ b/metadata/service/remote/service_test.go @@ -1,174 +1,36 @@ -/* - * 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 +package client import ( - "fmt" + "context" "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) + "dubbo.apache.org/dubbo-go/v3/common/constant" + _ "dubbo.apache.org/dubbo-go/v3/imports" ) -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) +func TestCreateRpcClient(t *testing.T) { + url, err := common.NewURL("dubbo://10.253.77.49:20881") + if err != nil { + t.Error(err) + } + url.SetParam(constant.SideKey, constant.Consumer) + url.SetParam(constant.VersionKey, "1.0.0") + url.SetParam(constant.InterfaceKey, constant.MetadataServiceName) + url.SetParam(constant.GroupKey, "metrics-provider") + rpcService, err := createRpcClient(url) + if err != nil { + t.Error(err) + } + metadataInfo, err := rpcService.GetMetadataInfo(context.Background(), "d7c522bd296066ecde5c32e631bcdbe9") + if err != nil { + t.Error(err) + } + assert.NotNil(t, metadataInfo) } 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/metadata/service/service.go b/metadata/service/service.go new file mode 100644 index 0000000000..d3b9bf5b0b --- /dev/null +++ b/metadata/service/service.go @@ -0,0 +1,174 @@ +/* + * 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 ( + "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/info" + _ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo" + "dubbo.apache.org/dubbo-go/v3/registry" +) + +// version will be used by Version func +const ( + version = "1.0.0" + allServiceInterfaces = "*" +) + +var ( + metadataServiceUrl *common.URL + GlobalMetadataService MetadataService = &metadataService{} + Exporter = &MetadataServiceExporter{} +) + +// MetadataService is store and query the metadata info in memory when each service registry +type metadataService struct { + BaseMetadataService +} + +// GetExportedURLs get all exported urls +func (mts *metadataService) GetExportedURLs(serviceInterface string, group string, version string, protocol string) []*common.URL { + if allServiceInterfaces == serviceInterface { + return mts.GetExportedServiceURLs() + } + all := mts.GetExportedServiceURLs() + 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 +} + +// GetMetadataInfo can get metadata in memory +func (mts *metadataService) GetMetadataInfo(revision string) *info.MetadataInfo { + if revision == "" { + return nil + } + for _, sd := range mts.getServiceDiscoveries() { + meta := sd.GetLocalMetadata() + if meta.Revision == revision { + return meta + } + } + logger.Warnf("metadata not found for revision: %s", revision) + return nil +} + +func (mts *metadataService) getServiceDiscoveries() []registry.ServiceDiscovery { + sds := make([]registry.ServiceDiscovery, 0) + p := extension.GetProtocol(constant.RegistryProtocol) + if factory, ok := p.(registry.RegistryFactory); ok { + for _, v := range factory.GetRegistries() { + if sd, ok := v.(registry.ServiceDiscoveryHolder); ok { + sds = append(sds, sd.GetServiceDiscovery()) + } + } + } + return sds +} + +// GetExportedServiceURLs get exported service urls +func (mts *metadataService) GetExportedServiceURLs() []*common.URL { + urls := make([]*common.URL, 0) + for _, sd := range mts.getServiceDiscoveries() { + urls = append(urls, sd.GetLocalMetadata().GetExportedServiceURLs()...) + } + return urls +} + +// Version will return the version of metadata service +func (mts *metadataService) Version() string { + return version +} + +// GetMetadataServiceURL get url of MetadataService +func (mts *metadataService) GetMetadataServiceURL() *common.URL { + return metadataServiceUrl +} + +func (mts *metadataService) GetSubscribedURLs() []*common.URL { + urls := make([]*common.URL, 0) + for _, sd := range mts.getServiceDiscoveries() { + urls = append(urls, sd.GetLocalMetadata().GetSubscribedURLs()...) + } + return urls +} + +// MetadataServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface +type MetadataServiceExporter struct { + ServiceConfig *config.ServiceConfig + lock sync.RWMutex +} + +// Export will export the metadataService +func (exporter *MetadataServiceExporter) Export(app, metadataType string) error { + if !exporter.IsExported() { + exporter.lock.Lock() + defer exporter.lock.Unlock() + exporter.ServiceConfig = config.NewServiceConfigBuilder(). + SetServiceID(constant.SimpleMetadataServiceName). + SetProtocolIDs(constant.DefaultProtocol). + AddRCProtocol(constant.DefaultProtocol, config.NewProtocolConfigBuilder(). + SetName(constant.DefaultProtocol). + Build()). + SetRegistryIDs("N/A"). + SetInterface(constant.MetadataServiceName). + SetGroup(app). + SetVersion(version). + SetProxyFactoryKey(constant.DefaultKey). + SetMetadataType(metadataType). + Build() + exporter.ServiceConfig.Implement(GlobalMetadataService) + err := exporter.ServiceConfig.Export() + metadataServiceUrl = exporter.ServiceConfig.GetExportedUrls()[0] + logger.Infof("[Metadata Service] The MetadataService exports urls : %v ", exporter.ServiceConfig.GetExportedUrls()) + return err + } + logger.Warnf("[Metadata Service] The MetadataService has been exported : %v ", exporter.ServiceConfig.GetExportedUrls()) + return nil +} + +// UnExport will unExport the metadataService +func (exporter *MetadataServiceExporter) UnExport() { + if exporter.IsExported() { + exporter.ServiceConfig.Unexport() + } +} + +// 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/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.go b/registry/etcdv3/service_discovery.go index 59cb096c9a..3089d3798e 100644 --- a/registry/etcdv3/service_discovery.go +++ b/registry/etcdv3/service_discovery.go @@ -53,6 +53,7 @@ func init() { // new etcd service discovery struct type etcdV3ServiceDiscovery struct { + registry.BaseServiceDiscovery // descriptor is a short string about the basic information of this instance descriptor string // client is current Etcdv3 client @@ -318,10 +319,11 @@ func newEtcdV3ServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, erro descriptor := fmt.Sprintf("etcd-service-discovery[%s]", url.Location) return &etcdV3ServiceDiscovery{ - descriptor: descriptor, - client: client, - serviceInstance: nil, - services: gxset.NewSet(), - childListenerMap: make(map[string]*etcdv3.EventListener), - instanceListenerMap: make(map[string]*gxset.HashSet)}, nil + BaseServiceDiscovery: registry.NewBaseServiceDiscovery(url.GetParam(constant.ApplicationKey, "")), + descriptor: descriptor, + client: client, + serviceInstance: nil, + services: gxset.NewSet(), + childListenerMap: make(map[string]*etcdv3.EventListener), + instanceListenerMap: make(map[string]*gxset.HashSet)}, nil } 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/event/metadata_service_url_params_customizer.go b/registry/event/metadata_service_url_params_customizer.go index 48d8beddae..bc859e921d 100644 --- a/registry/event/metadata_service_url_params_customizer.go +++ b/registry/event/metadata_service_url_params_customizer.go @@ -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/info" + metadataService "dubbo.apache.org/dubbo-go/v3/metadata/service" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -55,13 +56,8 @@ 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 { + url := metadataService.GlobalMetadataService.GetMetadataServiceURL() + if url == nil { logger.Errorf("the metadata service url is nil") return } @@ -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/protocol_ports_metadata_customizer.go b/registry/event/protocol_ports_metadata_customizer.go index 8eeda26e0e..d4b6dde4c1 100644 --- a/registry/event/protocol_ports_metadata_customizer.go +++ b/registry/event/protocol_ports_metadata_customizer.go @@ -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" + metadataService "dubbo.apache.org/dubbo-go/v3/metadata/service" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -48,24 +48,15 @@ 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() - if err != nil { - logger.Errorf("Could not init the MetadataService", err) + list := metadataService.GlobalMetadataService.GetExportedServiceURLs() + if list == nil || len(list) == 0 { + logger.Warn("Could not find exported urls") 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) - return - } - 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/event/service_revision_customizer.go b/registry/event/service_revision_customizer.go index 62b5a91f79..baa207156b 100644 --- a/registry/event/service_revision_customizer.go +++ b/registry/event/service_revision_customizer.go @@ -23,15 +23,11 @@ import ( "sort" ) -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/local" + metadataService "dubbo.apache.org/dubbo-go/v3/metadata/service" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -51,16 +47,7 @@ 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() - if err != nil { - logger.Errorf("could not get metadata service", 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) - } + urls := metadataService.GlobalMetadataService.GetExportedServiceURLs() revision := resolveRevision(urls) if len(revision) == 0 { @@ -78,17 +65,7 @@ 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() - if err != nil { - logger.Errorf("could not get metadata service", err) - return - } - - urls, err := ms.GetSubscribedURLs() - if err != nil { - logger.Errorf("could not find the subscribed url", err) - } - + urls := metadataService.GlobalMetadataService.GetSubscribedURLs() revision := resolveRevision(urls) if len(revision) == 0 { revision = defaultRevision diff --git a/registry/exposed_tmp/exposed.go b/registry/exposed_tmp/exposed.go index f6861e068d..c2c3e5af0e 100644 --- a/registry/exposed_tmp/exposed.go +++ b/registry/exposed_tmp/exposed.go @@ -32,6 +32,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" + metadataService "dubbo.apache.org/dubbo-go/v3/metadata/service" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -64,12 +65,6 @@ func RegisterServiceInstance(applicationName string, tag string, metadataType st panic(err) } } - // publish metadata to remote - if metadataType == constant.RemoteMetadataStorageType { - if remoteMetadataService, err := extension.GetRemoteMetadataService(); err == nil && remoteMetadataService != nil { - remoteMetadataService.PublishMetadata(applicationName) - } - } } // // nolint @@ -109,15 +104,7 @@ func createInstance(url *common.URL, applicationName string, tag string, metadat // 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) - } + urlList := metadataService.GlobalMetadataService.GetExportedServiceURLs() if len(urlList) == 0 { return nil } diff --git a/registry/nacos/service_discovery.go b/registry/nacos/service_discovery.go index 7f2f2cae38..fbc497a5db 100644 --- a/registry/nacos/service_discovery.go +++ b/registry/nacos/service_discovery.go @@ -55,6 +55,7 @@ func init() { // There is a problem, the go namingClient for nacos does not support the id field. // we will use the metadata to store the id of ServiceInstance type nacosServiceDiscovery struct { + registry.BaseServiceDiscovery group string // descriptor is a short string about the basic information of this instance descriptor string @@ -347,11 +348,12 @@ func newNacosServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, error group := url.GetParam(constant.RegistryGroupKey, defaultGroup) newInstance := &nacosServiceDiscovery{ - group: group, - namingClient: client, - descriptor: descriptor, - registryInstances: []registry.ServiceInstance{}, - instanceListenerMap: make(map[string]*gxset.HashSet), + BaseServiceDiscovery: registry.NewBaseServiceDiscovery(url.GetParam(constant.ApplicationKey, "")), + group: group, + namingClient: client, + descriptor: descriptor, + registryInstances: []registry.ServiceInstance{}, + instanceListenerMap: make(map[string]*gxset.HashSet), } return newInstance, 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/polaris/service_discovery.go b/registry/polaris/service_discovery.go index 81398ce2c5..249d9271f2 100644 --- a/registry/polaris/service_discovery.go +++ b/registry/polaris/service_discovery.go @@ -77,18 +77,20 @@ func newPolarisServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, err descriptor := fmt.Sprintf("polaris-service-discovery[%s]", discoveryURL.Location) newInstance := &polarisServiceDiscovery{ - namespace: discoveryURL.GetParam(constant.RegistryNamespaceKey, constant.PolarisDefaultNamespace), - descriptor: descriptor, - consumer: consumerApi, - provider: providerApi, - services: gxset.NewSet(), - registryInstances: make(map[string]*PolarisInstanceInfo), - watchers: make(map[string]*PolarisServiceWatcher), + BaseServiceDiscovery: registry.NewBaseServiceDiscovery(url.GetParam(constant.ApplicationKey, "")), + namespace: discoveryURL.GetParam(constant.RegistryNamespaceKey, constant.PolarisDefaultNamespace), + descriptor: descriptor, + consumer: consumerApi, + provider: providerApi, + services: gxset.NewSet(), + registryInstances: make(map[string]*PolarisInstanceInfo), + watchers: make(map[string]*PolarisServiceWatcher), } return newInstance, nil } type polarisServiceDiscovery struct { + registry.BaseServiceDiscovery namespace string descriptor string provider api.ProviderAPI diff --git a/registry/service_discovery.go b/registry/service_discovery.go index 60019982c4..7adba7b001 100644 --- a/registry/service_discovery.go +++ b/registry/service_discovery.go @@ -26,6 +26,11 @@ import ( gxpage "github.com/dubbogo/gost/hash/page" ) +import ( + "dubbo.apache.org/dubbo-go/v3/metadata/info" + metadataInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" +) + const DefaultPageSize = 100 // ServiceDiscovery is the interface that wraps common operations of Service Discovery @@ -69,4 +74,44 @@ type ServiceDiscovery interface { // AddListener adds a new ServiceInstancesChangedListenerImpl // see addServiceInstancesChangedListener in Java AddListener(listener ServiceInstancesChangedListener) error + + // GetLocalMetadata get local metadata info + GetLocalMetadata() *info.MetadataInfo + // GetRemoteMetadata get remote server metadata info + GetRemoteMetadata(revision string) *info.MetadataInfo +} + +type BaseServiceDiscovery struct { + metadataInfo *info.MetadataInfo +} + +func NewBaseServiceDiscovery(app string) BaseServiceDiscovery { + return BaseServiceDiscovery{metadataInfo: info.NewMetadataInfWithApp(app)} +} + +func (sd *BaseServiceDiscovery) GetLocalMetadata() *info.MetadataInfo { + return sd.metadataInfo +} + +func (sd *BaseServiceDiscovery) GetRemoteMetadata(revision string) *info.MetadataInfo { + return sd.metadataInfo +} + +func (sd *BaseServiceDiscovery) Register(instance ServiceInstance) error { + revisionUpdated := sd.calOrUpdateInstanceRevision(instance) + if revisionUpdated { + err := metadataInstance.GetMetadataReport().PublishAppMetadata(sd.metadataInfo.App, sd.metadataInfo.Revision, sd.metadataInfo) + if err != nil { + return err + } + } + return nil +} + +func (sd *BaseServiceDiscovery) Unregister(instance ServiceInstance) error { + return nil +} + +func (sd *BaseServiceDiscovery) calOrUpdateInstanceRevision(instance ServiceInstance) bool { + return true } diff --git a/registry/service_instance.go b/registry/service_instance.go index 7c3c57c4a0..1eb75e3aad 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,7 @@ type ServiceInstance interface { GetAddress() string // SetServiceMetadata saves metadata in instance - SetServiceMetadata(info *common.MetadataInfo) + SetServiceMetadata(info *info.MetadataInfo) // GetTag will return the tag of the instance GetTag() string @@ -92,7 +93,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,7 +144,7 @@ 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 } @@ -152,7 +153,7 @@ func (d *DefaultServiceInstance) GetTag() string { } // 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/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go index 6618544cde..24ca08a59f 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -38,8 +38,7 @@ import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/common/extension" "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" + metadataService "dubbo.apache.org/dubbo-go/v3/metadata/service" "dubbo.apache.org/dubbo-go/v3/metrics" metricMetadata "dubbo.apache.org/dubbo-go/v3/metrics/metadata" metricsRegistry "dubbo.apache.org/dubbo-go/v3/metrics/registry" @@ -63,12 +62,12 @@ type ServiceDiscoveryRegistry struct { serviceDiscovery registry.ServiceDiscovery subscribedServices *gxset.HashSet serviceNameMapping mapping.ServiceNameMapping - metaDataService service.MetadataService + metaDataService metadataService.MetadataService registeredListeners *gxset.HashSet subscribedURLsSynthesizers []synthesizer.SubscribedURLsSynthesizer serviceRevisionExportedURLsCache map[string]map[string][]*common.URL serviceListeners map[string]registry.ServiceInstancesChangedListener - serviceMappingListeners map[string]registry.MappingListener + serviceMappingListeners map[string]mapping.MappingListener } func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { @@ -79,7 +78,7 @@ func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { subscribedServices := parseServices(url.GetParam(constant.SubscribedServiceNamesKey, "")) subscribedURLsSynthesizers := synthesizer.GetAllSynthesizer() serviceNameMapping := extension.GetGlobalServiceNameMapping() - metaDataService, err := local.GetLocalMetadataService() + // metaDataService, err := local.GetLocalMetadataService() if err != nil { return nil, perrors.WithMessage(err, "could not init metadata service") } @@ -91,10 +90,10 @@ func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { registeredListeners: gxset.NewSet(), serviceRevisionExportedURLsCache: make(map[string]map[string][]*common.URL, 8), serviceNameMapping: serviceNameMapping, - metaDataService: metaDataService, - serviceListeners: make(map[string]registry.ServiceInstancesChangedListener), + // metaDataService: metaDataService, + serviceListeners: make(map[string]registry.ServiceInstancesChangedListener), // cache for mapping listener - serviceMappingListeners: make(map[string]registry.MappingListener), + serviceMappingListeners: make(map[string]mapping.MappingListener), }, nil } @@ -102,17 +101,15 @@ func (s *ServiceDiscoveryRegistry) UnRegister(url *common.URL) error { if !shouldRegister(url) { return nil } - return s.metaDataService.UnexportURL(url) + s.serviceDiscovery.GetLocalMetadata().RemoveService(url) + return nil } 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 - } + s.serviceDiscovery.GetLocalMetadata().RemoveSubscribeURL(url) services := s.getServices(url, nil) if services == nil { return nil @@ -122,7 +119,7 @@ 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 } @@ -178,19 +175,8 @@ func (s *ServiceDiscoveryRegistry) Register(url *common.URL) error { 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()) - } - + s.serviceDiscovery.GetLocalMetadata().AddService(url) + metrics.Publish(metricsRegistry.NewServerRegisterEvent(true, time.Now())) return s.serviceNameMapping.Map(url) } @@ -208,11 +194,7 @@ func (s *ServiceDiscoveryRegistry) Subscribe(url *common.URL, notify registry.No 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()) - } - + s.serviceDiscovery.GetLocalMetadata().AddSubscribeURL(url) mappingListener := NewMappingListener(s.url, url, s.subscribedServices, notify) services := s.getServices(url, mappingListener) if services.Empty() { @@ -315,7 +297,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 { @@ -330,7 +312,7 @@ func (s *ServiceDiscoveryRegistry) getServices(url *common.URL, listener registr 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()) diff --git a/registry/servicediscovery/service_instances_changed_listener_impl.go b/registry/servicediscovery/service_instances_changed_listener_impl.go index f5123cebd6..cbabd8e917 100644 --- a/registry/servicediscovery/service_instances_changed_listener_impl.go +++ b/registry/servicediscovery/service_instances_changed_listener_impl.go @@ -33,9 +33,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/metadata/service" - "dubbo.apache.org/dubbo-go/v3/metadata/service/local" + "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 +45,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 +60,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 +74,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 +92,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 +118,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,39 +223,39 @@ 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 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) - 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) - if err != nil { - return nil, err - } + // remoteMetadataServiceImpl, err := extension.GetRemoteMetadataService() + // if err != nil { + // return nil, err + // } + // metadataInfo, err = remoteMetadataServiceImpl.GetMetadata(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) + // if err != nil { + // return nil, err + // } } metaCache.Set(revision, metadataInfo) @@ -265,15 +263,15 @@ func GetMetadataInfo(app string, instance registry.ServiceInstance, revision str return metadataInfo, nil } -func destroyInvoker(metadataService service.MetadataService) { - if metadataService == nil { - return - } +// func destroyInvoker(metadataService service.MetadataService) { +// if metadataService == nil { +// return +// } - proxy := metadataService.(*local.MetadataServiceProxy) - if proxy.Invoker == nil { - return - } +// proxy := metadataService.(*local.MetadataServiceProxy) +// if proxy.Invoker == nil { +// return +// } - proxy.Invoker.Destroy() -} +// proxy.Invoker.Destroy() +// } diff --git a/registry/servicediscovery/store/cache_manager_test.go b/registry/servicediscovery/store/cache_manager_test.go index 3ca24dc2f1..f3125f7c6b 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.NewMetadataInfo("1", "1", serverInfo) + metadataInfo2 := info.NewMetadataInfo("2", "2", serverInfo) + metadataInfo3 := info.NewMetadataInfo("3", "3", serverInfo) + metadataInfo4 := info.NewMetadataInfo("4", "4", serverInfo) cm, err := NewCacheManager("metaTest1", "test_meta_cache", defaultTime, 10, true) if err != nil { diff --git a/registry/zookeeper/service_discovery.go b/registry/zookeeper/service_discovery.go index 915f10b2bb..c7dd5e033c 100644 --- a/registry/zookeeper/service_discovery.go +++ b/registry/zookeeper/service_discovery.go @@ -50,6 +50,7 @@ func init() { } type zookeeperServiceDiscovery struct { + registry.BaseServiceDiscovery client *gxzookeeper.ZookeeperClient csd *curator_discovery.ServiceDiscovery url *common.URL @@ -70,9 +71,10 @@ func newZookeeperServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, e } zksd := &zookeeperServiceDiscovery{ - url: url, - rootPath: group, - instanceListenerMap: make(map[string]*gxset.HashSet), + BaseServiceDiscovery: registry.NewBaseServiceDiscovery(url.GetParam(constant.ApplicationKey, "")), + url: url, + rootPath: group, + instanceListenerMap: make(map[string]*gxset.HashSet), } if err := zookeeper.ValidateZookeeperClient(zksd, url.Location); err != nil { return nil, err 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/server/action.go b/server/action.go index 98a40ec613..d66224e20a 100644 --- a/server/action.go +++ b/server/action.go @@ -233,16 +233,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 +240,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) diff --git a/server/server.go b/server/server.go index a76d9cd59a..a85ded5c75 100644 --- a/server/server.go +++ b/server/server.go @@ -187,7 +187,7 @@ func (s *Server) Serve() error { if err := s.exportServices(); err != nil { return err } - metadata.ExportMetadataService() + metadata.ExportMetadataService(s.cfg.Application.Name, s.cfg.Application.MetadataType) registry_exposed.RegisterServiceInstance(s.cfg.Application.Name, s.cfg.Application.Tag, s.cfg.Application.MetadataType) select {} } From bf150ce47a6ac4f80d2997d96a3e6b4521b74c03 Mon Sep 17 00:00:00 2001 From: foghost Date: Fri, 24 Nov 2023 17:18:02 +0800 Subject: [PATCH 02/26] fix cycle import --- config/config_loader.go | 89 ------------- config/root_config.go | 1 - imports/imports.go | 1 + metadata/exporter/service_exporter.go | 50 ++++++++ metadata/metadata.go | 31 +++-- .../service.go => metadata_service.go} | 99 ++++++--------- metadata/service/metadata_service.go | 51 -------- metadata/service/remote/service_test.go | 36 ------ .../metadata_service_url_params_customizer.go | 2 +- .../protocol_ports_metadata_customizer.go | 2 +- registry/event/service_revision_customizer.go | 2 +- registry/exposed_tmp/exposed.go | 101 --------------- registry/service_discovery.go | 40 +----- registry/service_discovery_holder.go | 1 + .../service_discovery_metadata.go | 120 ++++++++++++++---- .../service_discovery_registry.go | 8 +- server/server.go | 3 +- 17 files changed, 217 insertions(+), 420 deletions(-) create mode 100644 metadata/exporter/service_exporter.go rename metadata/{service/service.go => metadata_service.go} (60%) delete mode 100644 metadata/service/metadata_service.go delete mode 100644 metadata/service/remote/service_test.go rename metadata/service/remote/service.go => registry/servicediscovery/service_discovery_metadata.go (50%) diff --git a/config/config_loader.go b/config/config_loader.go index 7bd86d17a8..128720949e 100644 --- a/config/config_loader.go +++ b/config/config_loader.go @@ -19,25 +19,17 @@ package config import ( "errors" - "reflect" - "strconv" ) import ( "github.com/dubbogo/gost/log/logger" "github.com/knadh/koanf" - - perrors "github.com/pkg/errors" ) import ( "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/common/constant" - "dubbo.apache.org/dubbo-go/v3/common/extension" "dubbo.apache.org/dubbo-go/v3/logger/zap" - "dubbo.apache.org/dubbo-go/v3/metadata/service" - "dubbo.apache.org/dubbo-go/v3/registry" ) var ( @@ -76,87 +68,6 @@ func check() error { return nil } -// registerServiceInstance register service instance -func registerServiceInstance() { - url := selectMetadataServiceExportedURL() - if url == nil { - return - } - instance, err := createInstance(url) - if err != nil { - panic(err) - } - p := extension.GetProtocol(constant.RegistryProtocol) - var rp registry.RegistryFactory - var ok bool - if rp, ok = p.(registry.RegistryFactory); !ok { - panic("dubbo registry protocol{" + reflect.TypeOf(p).String() + "} is invalid") - } - rs := rp.GetRegistries() - for _, r := range rs { - var sdr registry.ServiceDiscoveryHolder - if sdr, ok = r.(registry.ServiceDiscoveryHolder); !ok { - continue - } - // publish app level data to registry - logger.Infof("Starting register instance address %v", instance) - err := sdr.GetServiceDiscovery().Register(instance) - if err != nil { - panic(err) - } - } -} -func selectMetadataServiceExportedURL() *common.URL { - var selectedUrl *common.URL - urlList := service.GlobalMetadataService.GetExportedServiceURLs() - if len(urlList) == 0 { - return nil - } - for _, url := range urlList { - selectedUrl = url - // rest first - if url.Protocol == "rest" { - break - } - } - return selectedUrl -} - -// // nolint -func createInstance(url *common.URL) (registry.ServiceInstance, error) { - appConfig := GetApplicationConfig() - port, err := strconv.ParseInt(url.Port, 10, 32) - if err != nil { - return nil, perrors.WithMessage(err, "invalid port: "+url.Port) - } - - host := url.Ip - if len(host) == 0 { - host = common.GetLocalIp() - } - - // usually we will add more metadata - metadata := make(map[string]string, 8) - metadata[constant.MetadataStorageTypePropertyName] = appConfig.MetadataType - - instance := ®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/root_config.go b/config/root_config.go index be7e9f02b7..4ced0d0642 100644 --- a/config/root_config.go +++ b/config/root_config.go @@ -203,7 +203,6 @@ func (rc *RootConfig) Start() { gracefulShutdownInit() rc.Consumer.Load() rc.Provider.Load() - registerServiceInstance() }) } diff --git a/imports/imports.go b/imports/imports.go index 1a9fa18254..cf1296ec74 100644 --- a/imports/imports.go +++ b/imports/imports.go @@ -59,6 +59,7 @@ import ( _ "dubbo.apache.org/dubbo-go/v3/filter/tps/limiter" _ "dubbo.apache.org/dubbo-go/v3/filter/tps/strategy" _ "dubbo.apache.org/dubbo-go/v3/filter/tracing" + _ "dubbo.apache.org/dubbo-go/v3/metadata/exporter" _ "dubbo.apache.org/dubbo-go/v3/metadata/mapping/metadata" _ "dubbo.apache.org/dubbo-go/v3/metadata/report/etcd" _ "dubbo.apache.org/dubbo-go/v3/metadata/report/nacos" diff --git a/metadata/exporter/service_exporter.go b/metadata/exporter/service_exporter.go new file mode 100644 index 0000000000..b0f60e8842 --- /dev/null +++ b/metadata/exporter/service_exporter.go @@ -0,0 +1,50 @@ +package exporter + +import ( + "dubbo.apache.org/dubbo-go/v3/common/constant" + "dubbo.apache.org/dubbo-go/v3/config" + "dubbo.apache.org/dubbo-go/v3/metadata" + "github.com/dubbogo/gost/log/logger" +) + +func init() { + metadata.SetExporterFactory(NewMetadataServiceExporter) +} + +// MetadataServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface +type MetadataServiceExporter struct { + app, metadataType string + ServiceConfig *config.ServiceConfig +} + +func NewMetadataServiceExporter(app, metadataType string) metadata.MetadataServiceExporter { + return &MetadataServiceExporter{app: app, metadataType: metadataType} +} + +// Export will export the metadataService +func (exporter *MetadataServiceExporter) Export() error { + exporter.ServiceConfig = config.NewServiceConfigBuilder(). + SetServiceID(constant.SimpleMetadataServiceName). + SetProtocolIDs(constant.DefaultProtocol). + AddRCProtocol(constant.DefaultProtocol, config.NewProtocolConfigBuilder(). + SetName(constant.DefaultProtocol). + Build()). + SetRegistryIDs("N/A"). + SetInterface(constant.MetadataServiceName). + SetGroup(exporter.app). + SetVersion(metadata.GlobalMetadataService.Version()). + SetProxyFactoryKey(constant.DefaultKey). + SetMetadataType(exporter.metadataType). + Build() + exporter.ServiceConfig.Implement(metadata.GlobalMetadataService) + err := exporter.ServiceConfig.Export() + url := exporter.ServiceConfig.GetExportedUrls()[0] + metadata.GlobalMetadataService.SetMetadataServiceURL(url) + logger.Infof("[Metadata Service] The MetadataService exports urls : %v ", exporter.ServiceConfig.GetExportedUrls()) + return err +} + +// UnExport will unExport the metadataService +func (exporter *MetadataServiceExporter) UnExport() { + exporter.ServiceConfig.Unexport() +} diff --git a/metadata/metadata.go b/metadata/metadata.go index b7523aaf23..bce89b1422 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -26,21 +26,30 @@ import ( "github.com/dubbogo/gost/log/logger" ) -import ( - "dubbo.apache.org/dubbo-go/v3/metadata/service" -) - var ( - exportOnce sync.Once + GlobalMetadataService MetadataService = &metadataService{} + exportOnce sync.Once + Factory ExporterFactory ) +// ExporterFactory to create MetadataServiceExporter, avoid cycle import +type ExporterFactory func(app, metadataType string) MetadataServiceExporter + +func SetExporterFactory(factory ExporterFactory) { + Factory = factory +} + func ExportMetadataService(app, metadataType string) { - exportOnce.Do(func() { - err := service.Exporter.Export(app, metadataType) - if err != nil { - logger.Errorf("export metadata service failed, got error %#v", err) - } - }) + if Factory != nil { + exportOnce.Do(func() { + err := Factory(app, metadataType).Export() + if err != nil { + logger.Errorf("export metadata service failed, got error %#v", err) + } + }) + } else { + logger.Warn("no metadata service exporter found, MetadataService will not be Exported") + } // err = publishMapping(expt) // if err != nil { diff --git a/metadata/service/service.go b/metadata/metadata_service.go similarity index 60% rename from metadata/service/service.go rename to metadata/metadata_service.go index d3b9bf5b0b..ebca042512 100644 --- a/metadata/service/service.go +++ b/metadata/metadata_service.go @@ -15,11 +15,7 @@ * limitations under the License. */ -package service - -import ( - "sync" -) +package metadata import ( "github.com/dubbogo/gost/log/logger" @@ -29,9 +25,7 @@ import ( "dubbo.apache.org/dubbo-go/v3/common" "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/common/extension" - "dubbo.apache.org/dubbo-go/v3/config" "dubbo.apache.org/dubbo-go/v3/metadata/info" - _ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -41,15 +35,36 @@ const ( allServiceInterfaces = "*" ) -var ( - metadataServiceUrl *common.URL - GlobalMetadataService MetadataService = &metadataService{} - Exporter = &MetadataServiceExporter{} -) +// MetadataService is used to define meta data related behaviors +// usually the implementation should be singleton +type MetadataService interface { + // GetExportedURLs will get the target exported url in metadata, the url should be unique + GetExportedURLs(serviceInterface string, group string, version string, protocol string) []*common.URL + // GetExportedServiceURLs will return exported service urls + GetExportedServiceURLs() []*common.URL + // GetSubscribedURLs will get the exported urls in metadata + GetSubscribedURLs() []*common.URL + Version() string + // GetMetadataInfo will return metadata info + GetMetadataInfo(revision string) *info.MetadataInfo + // GetMetadataServiceURL will return the url of metadata service + GetMetadataServiceURL() *common.URL + // SetMetadataServiceURL exporter to set url of metadata service + SetMetadataServiceURL(*common.URL) +} + +type MetadataServiceExporter interface { + Export() error + UnExport() +} // MetadataService is store and query the metadata info in memory when each service registry type metadataService struct { - BaseMetadataService + url *common.URL +} + +func (mts *metadataService) SetMetadataServiceURL(url *common.URL) { + mts.url = url } // GetExportedURLs get all exported urls @@ -85,13 +100,13 @@ func (mts *metadataService) GetMetadataInfo(revision string) *info.MetadataInfo return nil } -func (mts *metadataService) getServiceDiscoveries() []registry.ServiceDiscovery { - sds := make([]registry.ServiceDiscovery, 0) +func (mts *metadataService) getServiceDiscoveries() []registry.ServiceDiscoveryMetadata { + sds := make([]registry.ServiceDiscoveryMetadata, 0) p := extension.GetProtocol(constant.RegistryProtocol) if factory, ok := p.(registry.RegistryFactory); ok { for _, v := range factory.GetRegistries() { if sd, ok := v.(registry.ServiceDiscoveryHolder); ok { - sds = append(sds, sd.GetServiceDiscovery()) + sds = append(sds, sd.GetServiceDiscoverMetadata()) } } } @@ -114,7 +129,7 @@ func (mts *metadataService) Version() string { // GetMetadataServiceURL get url of MetadataService func (mts *metadataService) GetMetadataServiceURL() *common.URL { - return metadataServiceUrl + return mts.url } func (mts *metadataService) GetSubscribedURLs() []*common.URL { @@ -125,50 +140,10 @@ func (mts *metadataService) GetSubscribedURLs() []*common.URL { return urls } -// MetadataServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface -type MetadataServiceExporter struct { - ServiceConfig *config.ServiceConfig - lock sync.RWMutex -} - -// Export will export the metadataService -func (exporter *MetadataServiceExporter) Export(app, metadataType string) error { - if !exporter.IsExported() { - exporter.lock.Lock() - defer exporter.lock.Unlock() - exporter.ServiceConfig = config.NewServiceConfigBuilder(). - SetServiceID(constant.SimpleMetadataServiceName). - SetProtocolIDs(constant.DefaultProtocol). - AddRCProtocol(constant.DefaultProtocol, config.NewProtocolConfigBuilder(). - SetName(constant.DefaultProtocol). - Build()). - SetRegistryIDs("N/A"). - SetInterface(constant.MetadataServiceName). - SetGroup(app). - SetVersion(version). - SetProxyFactoryKey(constant.DefaultKey). - SetMetadataType(metadataType). - Build() - exporter.ServiceConfig.Implement(GlobalMetadataService) - err := exporter.ServiceConfig.Export() - metadataServiceUrl = exporter.ServiceConfig.GetExportedUrls()[0] - logger.Infof("[Metadata Service] The MetadataService exports urls : %v ", exporter.ServiceConfig.GetExportedUrls()) - return err - } - logger.Warnf("[Metadata Service] The MetadataService has been exported : %v ", exporter.ServiceConfig.GetExportedUrls()) - return nil -} - -// UnExport will unExport the metadataService -func (exporter *MetadataServiceExporter) UnExport() { - if exporter.IsExported() { - exporter.ServiceConfig.Unexport() +// MethodMapper only for rename exported function, for example: rename the function GetMetadataInfo to getMetadataInfo +func (mts *metadataService) MethodMapper() map[string]string { + return map[string]string{ + "GetExportedURLs": "getExportedURLs", + "GetMetadataInfo": "getMetadataInfo", } } - -// IsExported will return is metadataServiceExporter exported or not -func (exporter *MetadataServiceExporter) IsExported() bool { - exporter.lock.RLock() - defer exporter.lock.RUnlock() - return exporter.ServiceConfig != nil && exporter.ServiceConfig.IsExport() -} diff --git a/metadata/service/metadata_service.go b/metadata/service/metadata_service.go deleted file mode 100644 index 2a760cfbec..0000000000 --- a/metadata/service/metadata_service.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 service - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/metadata/info" -) - -// MetadataService is used to define meta data related behaviors -// usually the implementation should be singleton -type MetadataService interface { - // GetExportedURLs will get the target exported url in metadata, the url should be unique - GetExportedURLs(serviceInterface string, group string, version string, protocol string) []*common.URL - // GetExportedServiceURLs will return exported service urls - GetExportedServiceURLs() []*common.URL - // GetSubscribedURLs will get the exported urls in metadata - GetSubscribedURLs() []*common.URL - Version() string - // GetMetadataInfo will return metadata info - GetMetadataInfo(revision string) *info.MetadataInfo - // GetMetadataServiceURL will return the url of metadata service - GetMetadataServiceURL() *common.URL - // MethodMapper only for rename exported function, for example: rename the function GetMetadataInfo to getMetadataInfo - MethodMapper() map[string]string -} - -type BaseMetadataService struct { -} - -func (mts *BaseMetadataService) MethodMapper() map[string]string { - return map[string]string{ - "GetExportedURLs": "getExportedURLs", - "GetMetadataInfo": "getMetadataInfo", - } -} diff --git a/metadata/service/remote/service_test.go b/metadata/service/remote/service_test.go deleted file mode 100644 index 6284dfcc99..0000000000 --- a/metadata/service/remote/service_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package client - -import ( - "context" - "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/imports" -) - -func TestCreateRpcClient(t *testing.T) { - url, err := common.NewURL("dubbo://10.253.77.49:20881") - if err != nil { - t.Error(err) - } - url.SetParam(constant.SideKey, constant.Consumer) - url.SetParam(constant.VersionKey, "1.0.0") - url.SetParam(constant.InterfaceKey, constant.MetadataServiceName) - url.SetParam(constant.GroupKey, "metrics-provider") - rpcService, err := createRpcClient(url) - if err != nil { - t.Error(err) - } - metadataInfo, err := rpcService.GetMetadataInfo(context.Background(), "d7c522bd296066ecde5c32e631bcdbe9") - if err != nil { - t.Error(err) - } - assert.NotNil(t, metadataInfo) -} diff --git a/registry/event/metadata_service_url_params_customizer.go b/registry/event/metadata_service_url_params_customizer.go index bc859e921d..6b779e4ac2 100644 --- a/registry/event/metadata_service_url_params_customizer.go +++ b/registry/event/metadata_service_url_params_customizer.go @@ -18,6 +18,7 @@ package event import ( + metadataService "dubbo.apache.org/dubbo-go/v3/metadata" "encoding/json" ) @@ -31,7 +32,6 @@ import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/common/extension" "dubbo.apache.org/dubbo-go/v3/metadata/info" - metadataService "dubbo.apache.org/dubbo-go/v3/metadata/service" "dubbo.apache.org/dubbo-go/v3/registry" ) diff --git a/registry/event/protocol_ports_metadata_customizer.go b/registry/event/protocol_ports_metadata_customizer.go index d4b6dde4c1..7cdfb430b6 100644 --- a/registry/event/protocol_ports_metadata_customizer.go +++ b/registry/event/protocol_ports_metadata_customizer.go @@ -18,6 +18,7 @@ package event import ( + metadataService "dubbo.apache.org/dubbo-go/v3/metadata" "encoding/json" "strconv" ) @@ -29,7 +30,6 @@ import ( import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/common/extension" - metadataService "dubbo.apache.org/dubbo-go/v3/metadata/service" "dubbo.apache.org/dubbo-go/v3/registry" ) diff --git a/registry/event/service_revision_customizer.go b/registry/event/service_revision_customizer.go index baa207156b..a9f305da8b 100644 --- a/registry/event/service_revision_customizer.go +++ b/registry/event/service_revision_customizer.go @@ -18,6 +18,7 @@ package event import ( + metadataService "dubbo.apache.org/dubbo-go/v3/metadata" "fmt" "hash/crc32" "sort" @@ -27,7 +28,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" - metadataService "dubbo.apache.org/dubbo-go/v3/metadata/service" "dubbo.apache.org/dubbo-go/v3/registry" ) diff --git a/registry/exposed_tmp/exposed.go b/registry/exposed_tmp/exposed.go index c2c3e5af0e..7a4de196a0 100644 --- a/registry/exposed_tmp/exposed.go +++ b/registry/exposed_tmp/exposed.go @@ -16,104 +16,3 @@ */ 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" - metadataService "dubbo.apache.org/dubbo-go/v3/metadata/service" - "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 - } - // publish app level data to registry - logger.Infof("Starting register instance address %v", instance) - err := sdr.GetServiceDiscovery().Register(instance) - if err != nil { - panic(err) - } - } -} - -// // 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 - urlList := metadataService.GlobalMetadataService.GetExportedServiceURLs() - if len(urlList) == 0 { - return nil - } - for _, url := range urlList { - selectedUrl = url - // rest first - if url.Protocol == "rest" { - break - } - } - return selectedUrl -} diff --git a/registry/service_discovery.go b/registry/service_discovery.go index 7adba7b001..6e06994be5 100644 --- a/registry/service_discovery.go +++ b/registry/service_discovery.go @@ -28,7 +28,6 @@ import ( import ( "dubbo.apache.org/dubbo-go/v3/metadata/info" - metadataInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" ) const DefaultPageSize = 100 @@ -74,44 +73,11 @@ type ServiceDiscovery interface { // AddListener adds a new ServiceInstancesChangedListenerImpl // see addServiceInstancesChangedListener in Java AddListener(listener ServiceInstancesChangedListener) error +} +type ServiceDiscoveryMetadata interface { // GetLocalMetadata get local metadata info GetLocalMetadata() *info.MetadataInfo // GetRemoteMetadata get remote server metadata info - GetRemoteMetadata(revision string) *info.MetadataInfo -} - -type BaseServiceDiscovery struct { - metadataInfo *info.MetadataInfo -} - -func NewBaseServiceDiscovery(app string) BaseServiceDiscovery { - return BaseServiceDiscovery{metadataInfo: info.NewMetadataInfWithApp(app)} -} - -func (sd *BaseServiceDiscovery) GetLocalMetadata() *info.MetadataInfo { - return sd.metadataInfo -} - -func (sd *BaseServiceDiscovery) GetRemoteMetadata(revision string) *info.MetadataInfo { - return sd.metadataInfo -} - -func (sd *BaseServiceDiscovery) Register(instance ServiceInstance) error { - revisionUpdated := sd.calOrUpdateInstanceRevision(instance) - if revisionUpdated { - err := metadataInstance.GetMetadataReport().PublishAppMetadata(sd.metadataInfo.App, sd.metadataInfo.Revision, sd.metadataInfo) - if err != nil { - return err - } - } - return nil -} - -func (sd *BaseServiceDiscovery) Unregister(instance ServiceInstance) error { - return nil -} - -func (sd *BaseServiceDiscovery) calOrUpdateInstanceRevision(instance ServiceInstance) bool { - return true + GetRemoteMetadata(revision string, instance ServiceInstance) (*info.MetadataInfo, error) } diff --git a/registry/service_discovery_holder.go b/registry/service_discovery_holder.go index efe113fa18..340309b04e 100644 --- a/registry/service_discovery_holder.go +++ b/registry/service_discovery_holder.go @@ -22,4 +22,5 @@ package registry type ServiceDiscoveryHolder interface { // GetServiceDiscovery get service discovery GetServiceDiscovery() ServiceDiscovery + GetServiceDiscoverMetadata() ServiceDiscoveryMetadata } diff --git a/metadata/service/remote/service.go b/registry/servicediscovery/service_discovery_metadata.go similarity index 50% rename from metadata/service/remote/service.go rename to registry/servicediscovery/service_discovery_metadata.go index a02496f328..e723e9098f 100644 --- a/metadata/service/remote/service.go +++ b/registry/servicediscovery/service_discovery_metadata.go @@ -1,37 +1,111 @@ -/* - * 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 client +package servicediscovery import ( "context" - "strconv" - "time" -) - -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" metadataInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" "dubbo.apache.org/dubbo-go/v3/registry" + "strconv" + "time" ) +type serviceDiscoveryMeta struct { + metadataInfo *info.MetadataInfo + instance registry.ServiceInstance +} + +func NewBaseServiceDiscovery(app string) serviceDiscoveryMeta { + return serviceDiscoveryMeta{metadataInfo: info.NewMetadataInfWithApp(app)} +} + +func (sd *serviceDiscoveryMeta) GetLocalMetadata() *info.MetadataInfo { + return sd.metadataInfo +} + +func (sd *serviceDiscoveryMeta) GetRemoteMetadata(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { + // TODO cache + return GetRemoteMetadata(revision, instance) +} + +func (sd *serviceDiscoveryMeta) Register() error { + revisionUpdated := sd.calOrUpdateInstanceRevision(sd.instance) + if revisionUpdated { + err := metadataInstance.GetMetadataReport().PublishAppMetadata(sd.metadataInfo.App, sd.metadataInfo.Revision, sd.metadataInfo) + if err != nil { + return err + } + } + return nil +} + +func (sd *serviceDiscoveryMeta) Unregister() error { + return nil +} + +func (sd *serviceDiscoveryMeta) calOrUpdateInstanceRevision(instance registry.ServiceInstance) bool { + return true +} + +//// registerServiceInstance register service instance +//func registerServiceInstance() { +// 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 +// err := sdr.GetServiceDiscovery().Register() +// if err != nil { +// panic(err) +// } +// } +//} + +// // 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 +//} + // GetMetadata get the medata info of service from report func GetRemoteMetadata(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { meta, err := getMetadataFromCache(revision, instance) diff --git a/registry/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go index 24ca08a59f..648f623a85 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -37,8 +37,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" "dubbo.apache.org/dubbo-go/v3/metadata/mapping" - metadataService "dubbo.apache.org/dubbo-go/v3/metadata/service" "dubbo.apache.org/dubbo-go/v3/metrics" metricMetadata "dubbo.apache.org/dubbo-go/v3/metrics/metadata" metricsRegistry "dubbo.apache.org/dubbo-go/v3/metrics/registry" @@ -53,16 +53,16 @@ func init() { // 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 +// ServiceDiscoveryRegistry = ServiceDiscovery + metadata type ServiceDiscoveryRegistry struct { lock sync.RWMutex url *common.URL serviceDiscovery registry.ServiceDiscovery subscribedServices *gxset.HashSet serviceNameMapping mapping.ServiceNameMapping - metaDataService metadataService.MetadataService + metaDataService metadata.MetadataService registeredListeners *gxset.HashSet subscribedURLsSynthesizers []synthesizer.SubscribedURLsSynthesizer serviceRevisionExportedURLsCache map[string]map[string][]*common.URL diff --git a/server/server.go b/server/server.go index a85ded5c75..48dbf28f0a 100644 --- a/server/server.go +++ b/server/server.go @@ -34,7 +34,6 @@ import ( "dubbo.apache.org/dubbo-go/v3/common" "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" ) // proServices are for internal services @@ -188,7 +187,7 @@ func (s *Server) Serve() error { return err } metadata.ExportMetadataService(s.cfg.Application.Name, s.cfg.Application.MetadataType) - registry_exposed.RegisterServiceInstance(s.cfg.Application.Name, s.cfg.Application.Tag, s.cfg.Application.MetadataType) + //registry_exposed.RegisterServiceInstance(s.cfg.Application.Name, s.cfg.Application.Tag, s.cfg.Application.MetadataType) select {} } From 54116b39350f0e7ea5e5e4d6f3de0d866d21689c Mon Sep 17 00:00:00 2001 From: foghost Date: Sat, 25 Nov 2023 19:19:39 +0800 Subject: [PATCH 03/26] Initially completed --- config/metadata_report_config.go | 4 +- metadata/exporter/service_exporter.go | 2 +- .../mapping/metadata/service_name_mapping.go | 80 +++------ metadata/metadata.go | 31 +--- metadata/metadata_service.go | 16 +- metadata/report/etcd/report.go | 5 - metadata/report/instance/metadata_report.go | 57 +++--- metadata/report/nacos/report.go | 40 ----- metadata/report/report.go | 8 +- metadata/report/zookeeper/report.go | 18 -- registry/etcdv3/service_discovery.go | 14 +- registry/exposed_tmp/exposed.go | 18 -- registry/nacos/service_discovery.go | 12 +- registry/polaris/service_discovery.go | 16 +- registry/service_discovery.go | 6 +- registry/service_discovery_holder.go | 26 --- registry/service_instance.go | 6 + .../service_discovery_metadata.go | 165 ------------------ .../service_discovery_registry.go | 127 ++++++++------ .../service_mapping_change_listener_impl.go | 4 +- registry/servicediscovery/service_metadata.go | 118 +++++++++++++ registry/zookeeper/service_discovery.go | 8 +- server/server.go | 6 +- 23 files changed, 318 insertions(+), 469 deletions(-) delete mode 100644 registry/exposed_tmp/exposed.go delete mode 100644 registry/service_discovery_holder.go delete mode 100644 registry/servicediscovery/service_discovery_metadata.go create mode 100644 registry/servicediscovery/service_metadata.go diff --git a/config/metadata_report_config.go b/config/metadata_report_config.go index 6db749fe43..6445614264 100644 --- a/config/metadata_report_config.go +++ b/config/metadata_report_config.go @@ -60,7 +60,7 @@ func (mc *MetadataReportConfig) Init(rc *RootConfig) error { return err } // if metadata report config is avaible, then init metadata report instance - instance.Init([]*common.URL{tmpUrl}) + instance.Init([]*common.URL{tmpUrl}, mc.metadataType) return nil } if rc.Registries != nil && len(rc.Registries) > 0 { @@ -77,7 +77,7 @@ func (mc *MetadataReportConfig) Init(rc *RootConfig) error { } } if len(urls) > 0 { - instance.Init(urls) + instance.Init(urls, mc.metadataType) } else { logger.Warnf("No metadata report config found, skip init metadata report instance.") } diff --git a/metadata/exporter/service_exporter.go b/metadata/exporter/service_exporter.go index b0f60e8842..1de82c520d 100644 --- a/metadata/exporter/service_exporter.go +++ b/metadata/exporter/service_exporter.go @@ -17,7 +17,7 @@ type MetadataServiceExporter struct { ServiceConfig *config.ServiceConfig } -func NewMetadataServiceExporter(app, metadataType string) metadata.MetadataServiceExporter { +func NewMetadataServiceExporter(app, metadataType string) metadata.ServiceExporter { return &MetadataServiceExporter{app: app, metadataType: metadataType} } diff --git a/metadata/mapping/metadata/service_name_mapping.go b/metadata/mapping/metadata/service_name_mapping.go index b0b4552cef..ebd216db14 100644 --- a/metadata/mapping/metadata/service_name_mapping.go +++ b/metadata/mapping/metadata/service_name_mapping.go @@ -24,97 +24,75 @@ import ( import ( gxset "github.com/dubbogo/gost/container/set" "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/config" "dubbo.apache.org/dubbo-go/v3/metadata/mapping" - "dubbo.apache.org/dubbo-go/v3/metadata/report" "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" ) 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, "") - - metadataReport := getMetaDataReport(url.GetParam(constant.RegistryKey, "")) + metadataReport := instance.GetMetadataReportByRegistry(url.GetParam(constant.RegistryKey, "")) if metadataReport == nil { - logger.Info("get metadata report instance is nil, metadata service will be enabled!") + logger.Warn("get metadata report instance is nil") } else { - err := metadataReport.RegisterServiceAppMapping(serviceInterface, DefaultGroup, appName) + var err error + for i := 0; i < retryTimes; i++ { + err = metadataReport.RegisterServiceAppMapping(serviceInterface, DefaultGroup, appName) + if err == nil { + break + } + } if err != nil { - return perrors.WithStack(err) + logger.Errorf("Failed registering mapping to remote, &v", 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 mapping.MappingListener) (*gxset.HashSet, error) { +func (d *ServiceNameMapping) Get(url *common.URL, listener mapping.MappingListener) (*gxset.HashSet, error) { serviceInterface := url.GetParam(constant.InterfaceKey, "") metadataReport := instance.GetMetadataReport() 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.GetMetadataReport() 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.GetMetadataReport() - return metadataReport - } - return instance.GetMetadataReport() -} diff --git a/metadata/metadata.go b/metadata/metadata.go index bce89b1422..b4074c2695 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -19,6 +19,7 @@ package metadata import ( + "dubbo.apache.org/dubbo-go/v3/common/constant" "sync" ) @@ -32,8 +33,8 @@ var ( Factory ExporterFactory ) -// ExporterFactory to create MetadataServiceExporter, avoid cycle import -type ExporterFactory func(app, metadataType string) MetadataServiceExporter +// ExporterFactory to create ServiceExporter, avoid cycle import +type ExporterFactory func(app, metadataType string) ServiceExporter func SetExporterFactory(factory ExporterFactory) { Factory = factory @@ -42,30 +43,14 @@ func SetExporterFactory(factory ExporterFactory) { func ExportMetadataService(app, metadataType string) { if Factory != nil { exportOnce.Do(func() { - err := Factory(app, metadataType).Export() - if err != nil { - logger.Errorf("export metadata service failed, got error %#v", err) + if metadataType != constant.RemoteMetadataStorageType { + err := Factory(app, metadataType).Export() + if err != nil { + logger.Errorf("export metadata service failed, got error %#v", err) + } } }) } else { logger.Warn("no metadata service exporter found, MetadataService will not be Exported") } - - // err = publishMapping(expt) - // if err != nil { - // 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 -// } diff --git a/metadata/metadata_service.go b/metadata/metadata_service.go index ebca042512..85a08a79d9 100644 --- a/metadata/metadata_service.go +++ b/metadata/metadata_service.go @@ -53,7 +53,7 @@ type MetadataService interface { SetMetadataServiceURL(*common.URL) } -type MetadataServiceExporter interface { +type ServiceExporter interface { Export() error UnExport() } @@ -90,7 +90,7 @@ func (mts *metadataService) GetMetadataInfo(revision string) *info.MetadataInfo if revision == "" { return nil } - for _, sd := range mts.getServiceDiscoveries() { + for _, sd := range mts.getServiceDiscoveryMetadata() { meta := sd.GetLocalMetadata() if meta.Revision == revision { return meta @@ -100,13 +100,13 @@ func (mts *metadataService) GetMetadataInfo(revision string) *info.MetadataInfo return nil } -func (mts *metadataService) getServiceDiscoveries() []registry.ServiceDiscoveryMetadata { - sds := make([]registry.ServiceDiscoveryMetadata, 0) +func (mts *metadataService) getServiceDiscoveryMetadata() []registry.ServiceDiscoveryRegistry { + sds := make([]registry.ServiceDiscoveryRegistry, 0) p := extension.GetProtocol(constant.RegistryProtocol) if factory, ok := p.(registry.RegistryFactory); ok { for _, v := range factory.GetRegistries() { - if sd, ok := v.(registry.ServiceDiscoveryHolder); ok { - sds = append(sds, sd.GetServiceDiscoverMetadata()) + if sd, ok := v.(registry.ServiceDiscoveryRegistry); ok { + sds = append(sds, sd) } } } @@ -116,7 +116,7 @@ func (mts *metadataService) getServiceDiscoveries() []registry.ServiceDiscoveryM // GetExportedServiceURLs get exported service urls func (mts *metadataService) GetExportedServiceURLs() []*common.URL { urls := make([]*common.URL, 0) - for _, sd := range mts.getServiceDiscoveries() { + for _, sd := range mts.getServiceDiscoveryMetadata() { urls = append(urls, sd.GetLocalMetadata().GetExportedServiceURLs()...) } return urls @@ -134,7 +134,7 @@ func (mts *metadataService) GetMetadataServiceURL() *common.URL { func (mts *metadataService) GetSubscribedURLs() []*common.URL { urls := make([]*common.URL, 0) - for _, sd := range mts.getServiceDiscoveries() { + for _, sd := range mts.getServiceDiscoveryMetadata() { urls = append(urls, sd.GetLocalMetadata().GetSubscribedURLs()...) } return urls diff --git a/metadata/report/etcd/report.go b/metadata/report/etcd/report.go index 6f90491c8a..0b325247bf 100644 --- a/metadata/report/etcd/report.go +++ b/metadata/report/etcd/report.go @@ -53,11 +53,6 @@ type etcdMetadataReport struct { rootDir string } -func (e *etcdMetadataReport) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { - //TODO implement me - panic("implement me") -} - // GetAppMetadata get metadata info from etcd func (e *etcdMetadataReport) GetAppMetadata(application, revision string) (*info.MetadataInfo, error) { key := e.rootDir + application + constant.PathSeparator + revision diff --git a/metadata/report/instance/metadata_report.go b/metadata/report/instance/metadata_report.go index 1bc5bad0f4..d004eaa396 100644 --- a/metadata/report/instance/metadata_report.go +++ b/metadata/report/instance/metadata_report.go @@ -39,12 +39,14 @@ import ( ) var ( - instances = make(map[string]report.MetadataReport) - once sync.Once + instances = make(map[string]report.MetadataReport) + once sync.Once + metadataType string ) -func Init(urls []*common.URL) { +func Init(urls []*common.URL, metaType string) { once.Do(func() { + metadataType = metaType if len(urls) == 0 { for _, url := range urls { fac := extension.GetMetadataReportFactory(url.Protocol) @@ -60,22 +62,37 @@ func Init(urls []*common.URL) { } func GetMetadataReport() report.MetadataReport { - if len(instances) > 0 { - for _, v := range instances { - return v - } + for _, v := range instances { + return v } return nil } -func GetMetadataReports() []report.MetadataReport { - reports := make([]report.MetadataReport, len(instances)) - for _, metadataReport := range instances { - reports = append(reports, metadataReport) +func GetMetadataReportByRegistry(registry string) report.MetadataReport { + if len(registry) == 0 { + registry = constant.DefaultKey + } + if r, ok := instances[registry]; ok { + return r + } + for _, r := range instances { + return r } - return reports + return nil } +func GetMetadataType() string { + return metadataType +} + +//func GetMetadataReports() []report.MetadataReport { +// reports := make([]report.MetadataReport, len(instances)) +// for _, metadataReport := range instances { +// reports = append(reports, metadataReport) +// } +// return reports +//} + // DelegateMetadataReport is a absolute delegate for DelegateMetadataReport type DelegateMetadataReport struct { instance report.MetadataReport @@ -101,18 +118,14 @@ func (d *DelegateMetadataReport) GetAppMetadata(application, revision string) (* return meta, err } -func (d *DelegateMetadataReport) GetServiceAppMapping(application string, p1 string, p2 mapping.MappingListener) (*gxset.HashSet, error) { - panic("TODO") -} - -func (d *DelegateMetadataReport) RegisterServiceAppMapping(p0 string, p1 string, p2 string) error { - panic("TODO") +func (d *DelegateMetadataReport) GetServiceAppMapping(application string, group string, listener mapping.MappingListener) (*gxset.HashSet, error) { + return d.instance.GetServiceAppMapping(application, group, listener) } -func (d *DelegateMetadataReport) RemoveServiceAppMappingListener(p0 string, p1 string) error { - panic("TODO") +func (d *DelegateMetadataReport) RegisterServiceAppMapping(interfaceName, group string, application string) error { + return d.instance.RegisterServiceAppMapping(interfaceName, group, application) } -func (d *DelegateMetadataReport) GetConfigKeysByGroup(group string) (*gxset.HashSet, error) { - panic("TODO: Implement") +func (d *DelegateMetadataReport) RemoveServiceAppMappingListener(interfaceName, group string) error { + return d.instance.RemoveServiceAppMappingListener(interfaceName, group) } diff --git a/metadata/report/nacos/report.go b/metadata/report/nacos/report.go index caa65052fb..f4563ca862 100644 --- a/metadata/report/nacos/report.go +++ b/metadata/report/nacos/report.go @@ -38,20 +38,11 @@ import ( "dubbo.apache.org/dubbo-go/v3/common/extension" "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/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() report.MetadataReportFactory { @@ -196,37 +187,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) diff --git a/metadata/report/report.go b/metadata/report/report.go index 33786f0941..392a8d8caa 100644 --- a/metadata/report/report.go +++ b/metadata/report/report.go @@ -35,13 +35,11 @@ type MetadataReport interface { 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, mapping.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/zookeeper/report.go b/metadata/report/zookeeper/report.go index 04fe1d3898..2d80f5541d 100644 --- a/metadata/report/zookeeper/report.go +++ b/metadata/report/zookeeper/report.go @@ -130,24 +130,6 @@ 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.rootDir + 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 } diff --git a/registry/etcdv3/service_discovery.go b/registry/etcdv3/service_discovery.go index 3089d3798e..59cb096c9a 100644 --- a/registry/etcdv3/service_discovery.go +++ b/registry/etcdv3/service_discovery.go @@ -53,7 +53,6 @@ func init() { // new etcd service discovery struct type etcdV3ServiceDiscovery struct { - registry.BaseServiceDiscovery // descriptor is a short string about the basic information of this instance descriptor string // client is current Etcdv3 client @@ -319,11 +318,10 @@ func newEtcdV3ServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, erro descriptor := fmt.Sprintf("etcd-service-discovery[%s]", url.Location) return &etcdV3ServiceDiscovery{ - BaseServiceDiscovery: registry.NewBaseServiceDiscovery(url.GetParam(constant.ApplicationKey, "")), - descriptor: descriptor, - client: client, - serviceInstance: nil, - services: gxset.NewSet(), - childListenerMap: make(map[string]*etcdv3.EventListener), - instanceListenerMap: make(map[string]*gxset.HashSet)}, nil + descriptor: descriptor, + client: client, + serviceInstance: nil, + services: gxset.NewSet(), + childListenerMap: make(map[string]*etcdv3.EventListener), + instanceListenerMap: make(map[string]*gxset.HashSet)}, nil } diff --git a/registry/exposed_tmp/exposed.go b/registry/exposed_tmp/exposed.go deleted file mode 100644 index 7a4de196a0..0000000000 --- a/registry/exposed_tmp/exposed.go +++ /dev/null @@ -1,18 +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 exposed_tmp diff --git a/registry/nacos/service_discovery.go b/registry/nacos/service_discovery.go index fbc497a5db..7f2f2cae38 100644 --- a/registry/nacos/service_discovery.go +++ b/registry/nacos/service_discovery.go @@ -55,7 +55,6 @@ func init() { // There is a problem, the go namingClient for nacos does not support the id field. // we will use the metadata to store the id of ServiceInstance type nacosServiceDiscovery struct { - registry.BaseServiceDiscovery group string // descriptor is a short string about the basic information of this instance descriptor string @@ -348,12 +347,11 @@ func newNacosServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, error group := url.GetParam(constant.RegistryGroupKey, defaultGroup) newInstance := &nacosServiceDiscovery{ - BaseServiceDiscovery: registry.NewBaseServiceDiscovery(url.GetParam(constant.ApplicationKey, "")), - group: group, - namingClient: client, - descriptor: descriptor, - registryInstances: []registry.ServiceInstance{}, - instanceListenerMap: make(map[string]*gxset.HashSet), + group: group, + namingClient: client, + descriptor: descriptor, + registryInstances: []registry.ServiceInstance{}, + instanceListenerMap: make(map[string]*gxset.HashSet), } return newInstance, nil } diff --git a/registry/polaris/service_discovery.go b/registry/polaris/service_discovery.go index 249d9271f2..81398ce2c5 100644 --- a/registry/polaris/service_discovery.go +++ b/registry/polaris/service_discovery.go @@ -77,20 +77,18 @@ func newPolarisServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, err descriptor := fmt.Sprintf("polaris-service-discovery[%s]", discoveryURL.Location) newInstance := &polarisServiceDiscovery{ - BaseServiceDiscovery: registry.NewBaseServiceDiscovery(url.GetParam(constant.ApplicationKey, "")), - namespace: discoveryURL.GetParam(constant.RegistryNamespaceKey, constant.PolarisDefaultNamespace), - descriptor: descriptor, - consumer: consumerApi, - provider: providerApi, - services: gxset.NewSet(), - registryInstances: make(map[string]*PolarisInstanceInfo), - watchers: make(map[string]*PolarisServiceWatcher), + namespace: discoveryURL.GetParam(constant.RegistryNamespaceKey, constant.PolarisDefaultNamespace), + descriptor: descriptor, + consumer: consumerApi, + provider: providerApi, + services: gxset.NewSet(), + registryInstances: make(map[string]*PolarisInstanceInfo), + watchers: make(map[string]*PolarisServiceWatcher), } return newInstance, nil } type polarisServiceDiscovery struct { - registry.BaseServiceDiscovery namespace string descriptor string provider api.ProviderAPI diff --git a/registry/service_discovery.go b/registry/service_discovery.go index 6e06994be5..8470d1a263 100644 --- a/registry/service_discovery.go +++ b/registry/service_discovery.go @@ -75,7 +75,11 @@ type ServiceDiscovery interface { AddListener(listener ServiceInstancesChangedListener) error } -type ServiceDiscoveryMetadata interface { +type ServiceDiscoveryRegistry interface { + // RegisterService register service instance use metadata info + RegisterService() error + // UnRegisterService unregister service instance + UnRegisterService() error // GetLocalMetadata get local metadata info GetLocalMetadata() *info.MetadataInfo // GetRemoteMetadata get remote server metadata info diff --git a/registry/service_discovery_holder.go b/registry/service_discovery_holder.go deleted file mode 100644 index 340309b04e..0000000000 --- a/registry/service_discovery_holder.go +++ /dev/null @@ -1,26 +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 - GetServiceDiscoverMetadata() ServiceDiscoveryMetadata -} diff --git a/registry/service_instance.go b/registry/service_instance.go index 1eb75e3aad..8863b8e967 100644 --- a/registry/service_instance.go +++ b/registry/service_instance.go @@ -72,6 +72,7 @@ type ServiceInstance interface { // SetServiceMetadata saves metadata in instance SetServiceMetadata(info *info.MetadataInfo) + GetServiceMetadata() *info.MetadataInfo // GetTag will return the tag of the instance GetTag() string @@ -148,6 +149,11 @@ 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 } diff --git a/registry/servicediscovery/service_discovery_metadata.go b/registry/servicediscovery/service_discovery_metadata.go deleted file mode 100644 index e723e9098f..0000000000 --- a/registry/servicediscovery/service_discovery_metadata.go +++ /dev/null @@ -1,165 +0,0 @@ -package servicediscovery - -import ( - "context" - "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" - metadataInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" - "dubbo.apache.org/dubbo-go/v3/registry" - "strconv" - "time" -) - -type serviceDiscoveryMeta struct { - metadataInfo *info.MetadataInfo - instance registry.ServiceInstance -} - -func NewBaseServiceDiscovery(app string) serviceDiscoveryMeta { - return serviceDiscoveryMeta{metadataInfo: info.NewMetadataInfWithApp(app)} -} - -func (sd *serviceDiscoveryMeta) GetLocalMetadata() *info.MetadataInfo { - return sd.metadataInfo -} - -func (sd *serviceDiscoveryMeta) GetRemoteMetadata(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { - // TODO cache - return GetRemoteMetadata(revision, instance) -} - -func (sd *serviceDiscoveryMeta) Register() error { - revisionUpdated := sd.calOrUpdateInstanceRevision(sd.instance) - if revisionUpdated { - err := metadataInstance.GetMetadataReport().PublishAppMetadata(sd.metadataInfo.App, sd.metadataInfo.Revision, sd.metadataInfo) - if err != nil { - return err - } - } - return nil -} - -func (sd *serviceDiscoveryMeta) Unregister() error { - return nil -} - -func (sd *serviceDiscoveryMeta) calOrUpdateInstanceRevision(instance registry.ServiceInstance) bool { - return true -} - -//// registerServiceInstance register service instance -//func registerServiceInstance() { -// 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 -// err := sdr.GetServiceDiscovery().Register() -// if err != nil { -// panic(err) -// } -// } -//} - -// // 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 -//} - -// GetMetadata get the medata info of service from report -func GetRemoteMetadata(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { - meta, err := getMetadataFromCache(revision, instance) - if err != nil || meta == nil { - meta, err = getMetadataFromMetadataReport(revision, instance) - if err != nil || meta == nil { - meta, err = getMetadataFromRpc(revision, instance) - } - // TODO: need to update cache - } - return meta, err -} - -func getMetadataFromCache(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { - // TODO - return nil, nil -} - -func getMetadataFromMetadataReport(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { - report := metadataInstance.GetMetadataReport() - return report.GetAppMetadata(instance.GetServiceName(), revision) -} - -func getMetadataFromRpc(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { - url := common.NewURLWithOptions( - common.WithProtocol(constant.Dubbo), - common.WithIp(instance.GetHost()), - common.WithPort(strconv.Itoa(instance.GetPort())), - ) - url.SetParam(constant.SideKey, constant.Consumer) - url.SetParam(constant.VersionKey, "1.0.0") - url.SetParam(constant.InterfaceKey, constant.MetadataServiceName) - url.SetParam(constant.GroupKey, instance.GetServiceName()) - service, err := createRpcClient(url) - if err != nil { - return nil, err - } - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5000)) - defer cancel() - return service.GetMetadataInfo(ctx, revision) -} - -type metadataService struct { - GetExportedURLs func(context context.Context, serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) `dubbo:"getExportedURLs"` - GetMetadataInfo func(context context.Context, revision string) (*info.MetadataInfo, error) `dubbo:"getMetadataInfo"` - GetMetadataServiceURL func(context context.Context) (*common.URL, error) - GetSubscribedURLs func(context context.Context) ([]*common.URL, error) - Version func(context context.Context) (string, error) -} - -func createRpcClient(url *common.URL) (*metadataService, error) { - rpcService := &metadataService{} - invoker := extension.GetProtocol(constant.Dubbo).Refer(url) - proxy := extension.GetProxyFactory("").GetProxy(invoker, url) - proxy.Implement(rpcService) - return rpcService, nil -} diff --git a/registry/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go index 648f623a85..664c0e28f1 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -19,6 +19,7 @@ package servicediscovery import ( "bytes" + "dubbo.apache.org/dubbo-go/v3/metadata/info" "strings" "sync" "time" @@ -29,15 +30,12 @@ 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/mapping" "dubbo.apache.org/dubbo-go/v3/metrics" metricMetadata "dubbo.apache.org/dubbo-go/v3/metrics/metadata" @@ -51,23 +49,37 @@ func init() { extension.SetRegistry(constant.ServiceRegistryProtocol, newServiceDiscoveryRegistry) } -// ServiceDiscoveryRegistry is the implementation of application-level registry. +func RegisterServiceInstance() error { + protocol := extension.GetProtocol(constant.RegistryKey) + if rf, ok := protocol.(registry.RegistryFactory); ok { + for _, r := range rf.GetRegistries() { + if sdr, ok := r.(registry.ServiceDiscoveryRegistry); ok { + err := sdr.RegisterService() + if err != nil { + return err + } + } + } + } + return nil +} + +// 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 and metadata // In order to keep compatible with interface-level registry, -// ServiceDiscoveryRegistry = ServiceDiscovery + metadata -type ServiceDiscoveryRegistry struct { - lock sync.RWMutex - url *common.URL - serviceDiscovery registry.ServiceDiscovery - subscribedServices *gxset.HashSet - serviceNameMapping mapping.ServiceNameMapping - metaDataService metadata.MetadataService - registeredListeners *gxset.HashSet - subscribedURLsSynthesizers []synthesizer.SubscribedURLsSynthesizer - serviceRevisionExportedURLsCache map[string]map[string][]*common.URL - serviceListeners map[string]registry.ServiceInstancesChangedListener - serviceMappingListeners map[string]mapping.MappingListener +// serviceDiscoveryRegistry = ServiceDiscovery + metadata +type serviceDiscoveryRegistry struct { + lock sync.RWMutex + url *common.URL + serviceDiscovery registry.ServiceDiscovery + serviceMeta *serviceMeta + subscribedServices *gxset.HashSet + serviceNameMapping mapping.ServiceNameMapping + registeredListeners *gxset.HashSet + subscribedURLsSynthesizers []synthesizer.SubscribedURLsSynthesizer + serviceListeners map[string]registry.ServiceInstancesChangedListener + serviceMappingListeners map[string]mapping.MappingListener } func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { @@ -78,38 +90,53 @@ func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { subscribedServices := parseServices(url.GetParam(constant.SubscribedServiceNamesKey, "")) subscribedURLsSynthesizers := synthesizer.GetAllSynthesizer() serviceNameMapping := extension.GetGlobalServiceNameMapping() - // metaDataService, err := local.GetLocalMetadataService() if err != nil { return nil, perrors.WithMessage(err, "could not init metadata service") } - 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, + serviceMeta: NewServiceMeta(url.GetParam(constant.ApplicationKey, "")), + subscribedServices: subscribedServices, + subscribedURLsSynthesizers: subscribedURLsSynthesizers, + registeredListeners: gxset.NewSet(), + serviceNameMapping: serviceNameMapping, + serviceListeners: make(map[string]registry.ServiceInstancesChangedListener), // cache for mapping listener serviceMappingListeners: make(map[string]mapping.MappingListener), }, nil } -func (s *ServiceDiscoveryRegistry) UnRegister(url *common.URL) error { +func (s *serviceDiscoveryRegistry) RegisterService() error { + s.serviceMeta.createInstance() + return s.serviceDiscovery.Register(s.serviceMeta.instance) +} + +func (s *serviceDiscoveryRegistry) UnRegisterService() error { + return s.serviceDiscovery.Unregister(s.serviceMeta.instance) +} + +func (s *serviceDiscoveryRegistry) GetLocalMetadata() *info.MetadataInfo { + return s.serviceMeta.metadataInfo +} + +func (s *serviceDiscoveryRegistry) GetRemoteMetadata(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { + return s.serviceMeta.GetRemoteMetadata(revision, instance) +} + +func (s *serviceDiscoveryRegistry) UnRegister(url *common.URL) error { if !shouldRegister(url) { return nil } - s.serviceDiscovery.GetLocalMetadata().RemoveService(url) + s.serviceMeta.GetLocalMetadata().RemoveService(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 } - s.serviceDiscovery.GetLocalMetadata().RemoveSubscribeURL(url) + s.serviceMeta.GetLocalMetadata().RemoveSubscribeURL(url) services := s.getServices(url, nil) if services == nil { return nil @@ -129,7 +156,7 @@ func (s *ServiceDiscoveryRegistry) UnSubscribe(url *common.URL, listener registr 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 nil, perrors.WithMessage(err, "Create service discovery failed") } return originServiceDiscovery, nil } @@ -148,34 +175,34 @@ 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) - s.serviceDiscovery.GetLocalMetadata().AddService(url) + s.serviceMeta.GetLocalMetadata().AddService(url) metrics.Publish(metricsRegistry.NewServerRegisterEvent(true, time.Now())) return s.serviceNameMapping.Map(url) } @@ -189,12 +216,12 @@ func shouldRegister(url *common.URL) bool { 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 } - s.serviceDiscovery.GetLocalMetadata().AddSubscribeURL(url) + s.serviceMeta.GetLocalMetadata().AddSubscribeURL(url) mappingListener := NewMappingListener(s.url, url, s.subscribedServices, notify) services := s.getServices(url, mappingListener) if services.Empty() { @@ -207,7 +234,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() @@ -228,7 +255,7 @@ 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) } } } @@ -251,7 +278,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 } @@ -283,7 +310,7 @@ 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 { if syn.Support(subscribedURL) { @@ -297,7 +324,7 @@ func shouldSubscribe(url *common.URL) bool { return !shouldRegister(url) } -func (s *ServiceDiscoveryRegistry) getServices(url *common.URL, listener mapping.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 { @@ -312,7 +339,7 @@ func (s *ServiceDiscoveryRegistry) getServices(url *common.URL, listener mapping return services } -func (s *ServiceDiscoveryRegistry) findMappedServices(url *common.URL, listener mapping.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()) @@ -325,11 +352,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_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/service_metadata.go b/registry/servicediscovery/service_metadata.go new file mode 100644 index 0000000000..1e98647a52 --- /dev/null +++ b/registry/servicediscovery/service_metadata.go @@ -0,0 +1,118 @@ +package servicediscovery + +import ( + "context" + "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" + metadataInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" + "dubbo.apache.org/dubbo-go/v3/registry" + "strconv" + "time" +) + +type serviceMeta struct { + metadataInfo *info.MetadataInfo + instance registry.ServiceInstance +} + +func NewServiceMeta(app string) *serviceMeta { + return &serviceMeta{metadataInfo: info.NewMetadataInfWithApp(app)} +} + +func (sd *serviceMeta) GetLocalMetadata() *info.MetadataInfo { + return sd.metadataInfo +} + +func (sd *serviceMeta) GetRemoteMetadata(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { + meta, err := getMetadataFromCache(revision) + if err != nil || meta == nil { + meta, err = getMetadataFromMetadataReport(revision, instance) + if err != nil || meta == nil { + meta, err = getMetadataFromRpc(revision, instance) + } + // TODO : need to update cache + } + return meta, err +} + +func (sd *serviceMeta) createInstance() { + metadata := make(map[string]string, 8) + metadata[constant.MetadataStorageTypePropertyName] = metadataInstance.GetMetadataType() + instance := ®istry.DefaultServiceInstance{ + ServiceName: sd.metadataInfo.App, + Enable: true, + Healthy: true, + Metadata: metadata, + ServiceMetadata: sd.metadataInfo, + } + + for _, cus := range extension.GetCustomizers() { + cus.Customize(instance) + } + sd.instance = instance +} + +//func (sd *serviceMeta) calOrUpdateInstanceRevision(instance registry.ServiceInstance) bool { +// oldRevision := getRevision(instance) +// newRevision := instance.GetServiceMetadata().CalAndGetRevision() +// if oldRevision != newRevision { +// instance.GetMetadata()[constant.ExportedServicesRevisionPropertyName] = newRevision +// return true +// } +// return false +//} + +//func getRevision(instance registry.ServiceInstance) string { +// if instance.GetServiceMetadata() != nil && instance.GetServiceMetadata().Revision != "" { +// return instance.GetServiceMetadata().Revision +// } +// return instance.GetMetadata()[constant.ExportedServicesRevisionPropertyName] +//} + +func getMetadataFromCache(revision string) (*info.MetadataInfo, error) { + // TODO metadata cache + return nil, nil +} + +func getMetadataFromMetadataReport(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { + report := metadataInstance.GetMetadataReport() + return report.GetAppMetadata(instance.GetServiceName(), revision) +} + +func getMetadataFromRpc(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { + service, destroy := createRpcClient(instance) + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5000)) + defer cancel() + defer destroy() + return service.GetMetadataInfo(ctx, revision) +} + +type metadataService struct { + GetExportedURLs func(context context.Context, serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) `dubbo:"getExportedURLs"` + GetMetadataInfo func(context context.Context, revision string) (*info.MetadataInfo, error) `dubbo:"getMetadataInfo"` + GetMetadataServiceURL func(context context.Context) (*common.URL, error) + GetSubscribedURLs func(context context.Context) ([]*common.URL, error) + Version func(context context.Context) (string, error) +} + +func createRpcClient(instance registry.ServiceInstance) (*metadataService, func()) { + url := common.NewURLWithOptions( + common.WithProtocol(constant.Dubbo), + common.WithIp(instance.GetHost()), + common.WithPort(strconv.Itoa(instance.GetPort())), + ) + url.SetParam(constant.SideKey, constant.Consumer) + url.SetParam(constant.VersionKey, "1.0.0") + url.SetParam(constant.InterfaceKey, constant.MetadataServiceName) + url.SetParam(constant.GroupKey, instance.GetServiceName()) + rpcService := &metadataService{} + invoker := extension.GetProtocol(constant.Dubbo).Refer(url) + proxy := extension.GetProxyFactory("").GetProxy(invoker, url) + proxy.Implement(rpcService) + destroy := func() { + invoker.Destroy() + } + return rpcService, destroy +} diff --git a/registry/zookeeper/service_discovery.go b/registry/zookeeper/service_discovery.go index c7dd5e033c..915f10b2bb 100644 --- a/registry/zookeeper/service_discovery.go +++ b/registry/zookeeper/service_discovery.go @@ -50,7 +50,6 @@ func init() { } type zookeeperServiceDiscovery struct { - registry.BaseServiceDiscovery client *gxzookeeper.ZookeeperClient csd *curator_discovery.ServiceDiscovery url *common.URL @@ -71,10 +70,9 @@ func newZookeeperServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, e } zksd := &zookeeperServiceDiscovery{ - BaseServiceDiscovery: registry.NewBaseServiceDiscovery(url.GetParam(constant.ApplicationKey, "")), - url: url, - rootPath: group, - instanceListenerMap: make(map[string]*gxset.HashSet), + url: url, + rootPath: group, + instanceListenerMap: make(map[string]*gxset.HashSet), } if err := zookeeper.ValidateZookeeperClient(zksd, url.Location); err != nil { return nil, err diff --git a/server/server.go b/server/server.go index 48dbf28f0a..477fe4ac8d 100644 --- a/server/server.go +++ b/server/server.go @@ -20,6 +20,7 @@ package server import ( "context" + "dubbo.apache.org/dubbo-go/v3/registry/servicediscovery" "fmt" "sync" ) @@ -187,7 +188,10 @@ func (s *Server) Serve() error { return err } metadata.ExportMetadataService(s.cfg.Application.Name, s.cfg.Application.MetadataType) - //registry_exposed.RegisterServiceInstance(s.cfg.Application.Name, s.cfg.Application.Tag, s.cfg.Application.MetadataType) + err := servicediscovery.RegisterServiceInstance() + if err != nil { + return err + } select {} } From dda0ba85e16bbc741cfa5cb5596651b2c3c6c0fb Mon Sep 17 00:00:00 2001 From: foghost Date: Tue, 28 Nov 2023 16:47:23 +0800 Subject: [PATCH 04/26] register success --- config/root_config.go | 7 ++ metadata/exporter/service_exporter.go | 17 ++-- metadata/info/metadata_info.go | 78 +++++++++++-------- metadata/metadata.go | 6 +- metadata/metadata_service.go | 53 +++++++------ metadata/report/instance/metadata_report.go | 2 +- .../metadata_service_url_params_customizer.go | 6 +- .../protocol_ports_metadata_customizer.go | 6 +- registry/event/service_revision_customizer.go | 14 +++- .../service_discovery_registry.go | 9 ++- 10 files changed, 125 insertions(+), 73 deletions(-) diff --git a/config/root_config.go b/config/root_config.go index 4ced0d0642..98ab1e42d5 100644 --- a/config/root_config.go +++ b/config/root_config.go @@ -18,6 +18,7 @@ package config import ( + "dubbo.apache.org/dubbo-go/v3/registry/servicediscovery" "sync" ) @@ -33,6 +34,7 @@ import ( "dubbo.apache.org/dubbo-go/v3/common" "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/config_center" + "dubbo.apache.org/dubbo-go/v3/metadata" ) var ( @@ -203,6 +205,11 @@ func (rc *RootConfig) Start() { gracefulShutdownInit() rc.Consumer.Load() rc.Provider.Load() + metadata.ExportMetadataService(rc.Application.Name, rc.Application.MetadataType) + err := servicediscovery.RegisterServiceInstance() + if err != nil { + panic(err) + } }) } diff --git a/metadata/exporter/service_exporter.go b/metadata/exporter/service_exporter.go index 1de82c520d..c00f6e9d7f 100644 --- a/metadata/exporter/service_exporter.go +++ b/metadata/exporter/service_exporter.go @@ -15,14 +15,16 @@ func init() { type MetadataServiceExporter struct { app, metadataType string ServiceConfig *config.ServiceConfig + service metadata.MetadataService } -func NewMetadataServiceExporter(app, metadataType string) metadata.ServiceExporter { - return &MetadataServiceExporter{app: app, metadataType: metadataType} +func NewMetadataServiceExporter(app, metadataType string, service metadata.MetadataService) metadata.ServiceExporter { + return &MetadataServiceExporter{app: app, metadataType: metadataType, service: service} } // Export will export the metadataService func (exporter *MetadataServiceExporter) Export() error { + version, _ := exporter.service.Version() exporter.ServiceConfig = config.NewServiceConfigBuilder(). SetServiceID(constant.SimpleMetadataServiceName). SetProtocolIDs(constant.DefaultProtocol). @@ -32,16 +34,19 @@ func (exporter *MetadataServiceExporter) Export() error { SetRegistryIDs("N/A"). SetInterface(constant.MetadataServiceName). SetGroup(exporter.app). - SetVersion(metadata.GlobalMetadataService.Version()). + SetVersion(version). SetProxyFactoryKey(constant.DefaultKey). SetMetadataType(exporter.metadataType). Build() - exporter.ServiceConfig.Implement(metadata.GlobalMetadataService) + exporter.ServiceConfig.Implement(exporter.service) err := exporter.ServiceConfig.Export() + if err != nil { + return err + } url := exporter.ServiceConfig.GetExportedUrls()[0] - metadata.GlobalMetadataService.SetMetadataServiceURL(url) + exporter.service.SetMetadataServiceURL(url) logger.Infof("[Metadata Service] The MetadataService exports urls : %v ", exporter.ServiceConfig.GetExportedUrls()) - return err + return nil } // UnExport will unExport the metadataService diff --git a/metadata/info/metadata_info.go b/metadata/info/metadata_info.go index 4db5a87d35..f93c683efb 100644 --- a/metadata/info/metadata_info.go +++ b/metadata/info/metadata_info.go @@ -74,15 +74,17 @@ func NewMetadataInfWithApp(app string) *MetadataInfo { // nolint func NewMetadataInfo(app string, revision string, services map[string]*ServiceInfo) *MetadataInfo { return &MetadataInfo{ - App: app, - Revision: revision, - Services: services, - Reported: false, + App: app, + Revision: revision, + Services: services, + Reported: false, + exportedServiceURLs: make(map[string][]*common.URL), + subscribedServiceURLs: make(map[string][]*common.URL), } } // nolint -func (mi *MetadataInfo) JavaClassName() string { +func (info *MetadataInfo) JavaClassName() string { return "org.apache.dubbo.metadata.MetadataInfo" } @@ -90,16 +92,16 @@ func (mi *MetadataInfo) JavaClassName() string { // 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 +func (info *MetadataInfo) CalAndGetRevision() string { + if info.Revision != "" && info.Reported { + return info.Revision } - if len(mi.Services) == 0 { + if len(info.Services) == 0 { return "0" } candidates := make([]string, 0, 8) - for _, s := range mi.Services { + for _, s := range info.Services { iface := s.URL.GetParam(constant.InterfaceKey, "") ms := s.URL.Methods if len(ms) == 0 { @@ -120,27 +122,29 @@ func (mi *MetadataInfo) CalAndGetRevision() string { for _, c := range candidates { res += uint64(crc32.ChecksumIEEE([]byte(c))) } - mi.Revision = fmt.Sprint(res) - return mi.Revision + info.Revision = fmt.Sprint(res) + return info.Revision } // nolint -func (mi *MetadataInfo) HasReported() bool { - return mi.Reported +func (info *MetadataInfo) HasReported() bool { + return info.Reported } // nolint -func (mi *MetadataInfo) MarkReported() { - mi.Reported = true +func (info *MetadataInfo) MarkReported() { + info.Reported = true } -// nolint // AddService add provider service info to MetadataInfo -func (mi *MetadataInfo) AddService(url *common.URL) { +func (info *MetadataInfo) AddService(url *common.URL) { service := NewServiceInfoWithURL(url) - mi.Services[service.GetMatchKey()] = service - addUrl(mi.exportedServiceURLs, 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) { @@ -151,27 +155,37 @@ func addUrl(m map[string][]*common.URL, url *common.URL) { } func removeUrl(m map[string][]*common.URL, url *common.URL) { - // if _, ok := m[url.ServiceKey()]; ok { - // m[url.ServiceKey()].Remove(url) - // if m[url.ServiceKey()].Size() == 0 { - // delete(m, url.ServiceKey()) - // } - // } + 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()) + } + } } -// nolint -func (mi *MetadataInfo) RemoveService(url *common.URL) { +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(mi.Services, service.GetMatchKey()) - removeUrl(mi.exportedServiceURLs, url) + delete(info.Services, service.GetMatchKey()) + removeUrl(info.exportedServiceURLs, url) } -// 客户端 订阅一个 服务的 url 元数据 +// AddSubscribeURL client subscribe a service url func (info *MetadataInfo) AddSubscribeURL(url *common.URL) { addUrl(info.subscribedServiceURLs, url) } -// 客户端 删除一个订阅的服务 url 元数据 +// RemoveSubscribeURL client unsubscribe a service url func (info *MetadataInfo) RemoveSubscribeURL(url *common.URL) { removeUrl(info.subscribedServiceURLs, url) } diff --git a/metadata/metadata.go b/metadata/metadata.go index b4074c2695..dab22df50d 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -28,13 +28,13 @@ import ( ) var ( - GlobalMetadataService MetadataService = &metadataService{} + GlobalMetadataService MetadataService = &DefaultMetadataService{} exportOnce sync.Once Factory ExporterFactory ) // ExporterFactory to create ServiceExporter, avoid cycle import -type ExporterFactory func(app, metadataType string) ServiceExporter +type ExporterFactory func(app, metadataType string, service MetadataService) ServiceExporter func SetExporterFactory(factory ExporterFactory) { Factory = factory @@ -44,7 +44,7 @@ func ExportMetadataService(app, metadataType string) { if Factory != nil { exportOnce.Do(func() { if metadataType != constant.RemoteMetadataStorageType { - err := Factory(app, metadataType).Export() + err := Factory(app, metadataType, GlobalMetadataService).Export() if err != nil { logger.Errorf("export metadata service failed, got error %#v", err) } diff --git a/metadata/metadata_service.go b/metadata/metadata_service.go index 85a08a79d9..1aae59d5c5 100644 --- a/metadata/metadata_service.go +++ b/metadata/metadata_service.go @@ -39,16 +39,16 @@ const ( // 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 + GetExportedURLs(serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) // GetExportedServiceURLs will return exported service urls - GetExportedServiceURLs() []*common.URL + GetExportedServiceURLs() ([]*common.URL, error) // GetSubscribedURLs will get the exported urls in metadata - GetSubscribedURLs() []*common.URL - Version() string + GetSubscribedURLs() ([]*common.URL, error) + Version() (string, error) // GetMetadataInfo will return metadata info - GetMetadataInfo(revision string) *info.MetadataInfo + GetMetadataInfo(revision string) (*info.MetadataInfo, error) // GetMetadataServiceURL will return the url of metadata service - GetMetadataServiceURL() *common.URL + GetMetadataServiceURL() (*common.URL, error) // SetMetadataServiceURL exporter to set url of metadata service SetMetadataServiceURL(*common.URL) } @@ -59,20 +59,23 @@ type ServiceExporter interface { } // MetadataService is store and query the metadata info in memory when each service registry -type metadataService struct { +type DefaultMetadataService struct { url *common.URL } -func (mts *metadataService) SetMetadataServiceURL(url *common.URL) { +func (mts *DefaultMetadataService) SetMetadataServiceURL(url *common.URL) { mts.url = url } // GetExportedURLs get all exported urls -func (mts *metadataService) GetExportedURLs(serviceInterface string, group string, version string, protocol string) []*common.URL { +func (mts *DefaultMetadataService) GetExportedURLs(serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) { if allServiceInterfaces == serviceInterface { return mts.GetExportedServiceURLs() } - all := 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 && @@ -82,25 +85,25 @@ func (mts *metadataService) GetExportedURLs(serviceInterface string, group strin urls = append(urls, url) } } - return urls + return urls, nil } // GetMetadataInfo can get metadata in memory -func (mts *metadataService) GetMetadataInfo(revision string) *info.MetadataInfo { +func (mts *DefaultMetadataService) GetMetadataInfo(revision string) (*info.MetadataInfo, error) { if revision == "" { - return nil + return nil, nil } for _, sd := range mts.getServiceDiscoveryMetadata() { meta := sd.GetLocalMetadata() if meta.Revision == revision { - return meta + return meta, nil } } logger.Warnf("metadata not found for revision: %s", revision) - return nil + return nil, nil } -func (mts *metadataService) getServiceDiscoveryMetadata() []registry.ServiceDiscoveryRegistry { +func (mts *DefaultMetadataService) getServiceDiscoveryMetadata() []registry.ServiceDiscoveryRegistry { sds := make([]registry.ServiceDiscoveryRegistry, 0) p := extension.GetProtocol(constant.RegistryProtocol) if factory, ok := p.(registry.RegistryFactory); ok { @@ -114,34 +117,34 @@ func (mts *metadataService) getServiceDiscoveryMetadata() []registry.ServiceDisc } // GetExportedServiceURLs get exported service urls -func (mts *metadataService) GetExportedServiceURLs() []*common.URL { +func (mts *DefaultMetadataService) GetExportedServiceURLs() ([]*common.URL, error) { urls := make([]*common.URL, 0) for _, sd := range mts.getServiceDiscoveryMetadata() { urls = append(urls, sd.GetLocalMetadata().GetExportedServiceURLs()...) } - return urls + return urls, nil } // Version will return the version of metadata service -func (mts *metadataService) Version() string { - return version +func (mts *DefaultMetadataService) Version() (string, error) { + return version, nil } // GetMetadataServiceURL get url of MetadataService -func (mts *metadataService) GetMetadataServiceURL() *common.URL { - return mts.url +func (mts *DefaultMetadataService) GetMetadataServiceURL() (*common.URL, error) { + return mts.url, nil } -func (mts *metadataService) GetSubscribedURLs() []*common.URL { +func (mts *DefaultMetadataService) GetSubscribedURLs() ([]*common.URL, error) { urls := make([]*common.URL, 0) for _, sd := range mts.getServiceDiscoveryMetadata() { urls = append(urls, sd.GetLocalMetadata().GetSubscribedURLs()...) } - return urls + return urls, nil } // MethodMapper only for rename exported function, for example: rename the function GetMetadataInfo to getMetadataInfo -func (mts *metadataService) MethodMapper() map[string]string { +func (mts *DefaultMetadataService) MethodMapper() map[string]string { return map[string]string{ "GetExportedURLs": "getExportedURLs", "GetMetadataInfo": "getMetadataInfo", diff --git a/metadata/report/instance/metadata_report.go b/metadata/report/instance/metadata_report.go index d004eaa396..5c8c1b881b 100644 --- a/metadata/report/instance/metadata_report.go +++ b/metadata/report/instance/metadata_report.go @@ -47,7 +47,7 @@ var ( func Init(urls []*common.URL, metaType string) { once.Do(func() { metadataType = metaType - if len(urls) == 0 { + if len(urls) != 0 { for _, url := range urls { fac := extension.GetMetadataReportFactory(url.Protocol) if fac != nil { diff --git a/registry/event/metadata_service_url_params_customizer.go b/registry/event/metadata_service_url_params_customizer.go index 6b779e4ac2..5cd142be81 100644 --- a/registry/event/metadata_service_url_params_customizer.go +++ b/registry/event/metadata_service_url_params_customizer.go @@ -56,7 +56,11 @@ func (m *metadataServiceURLParamsMetadataCustomizer) GetPriority() int { } func (m *metadataServiceURLParamsMetadataCustomizer) Customize(instance registry.ServiceInstance) { - url := metadataService.GlobalMetadataService.GetMetadataServiceURL() + url, err := metadataService.GlobalMetadataService.GetMetadataServiceURL() + if err != nil { + logger.Errorf("get metadata service url is error, %v", err) + return + } if url == nil { logger.Errorf("the metadata service url is nil") return diff --git a/registry/event/protocol_ports_metadata_customizer.go b/registry/event/protocol_ports_metadata_customizer.go index 7cdfb430b6..23ac124216 100644 --- a/registry/event/protocol_ports_metadata_customizer.go +++ b/registry/event/protocol_ports_metadata_customizer.go @@ -48,7 +48,11 @@ 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) { - list := metadataService.GlobalMetadataService.GetExportedServiceURLs() + list, err := metadataService.GlobalMetadataService.GetExportedServiceURLs() + if err != nil { + logger.Errorf("get metadata exported service url is error, %v", err) + return + } if list == nil || len(list) == 0 { logger.Warn("Could not find exported urls") return diff --git a/registry/event/service_revision_customizer.go b/registry/event/service_revision_customizer.go index a9f305da8b..ac4817b8c3 100644 --- a/registry/event/service_revision_customizer.go +++ b/registry/event/service_revision_customizer.go @@ -20,6 +20,7 @@ package event import ( metadataService "dubbo.apache.org/dubbo-go/v3/metadata" "fmt" + "github.com/dubbogo/gost/log/logger" "hash/crc32" "sort" ) @@ -47,8 +48,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) { - urls := metadataService.GlobalMetadataService.GetExportedServiceURLs() - + urls, err := metadataService.GlobalMetadataService.GetExportedServiceURLs() + if err != nil { + logger.Errorf("get metadata service url is error, %v", err) + return + } revision := resolveRevision(urls) if len(revision) == 0 { revision = defaultRevision @@ -65,7 +69,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) { - urls := metadataService.GlobalMetadataService.GetSubscribedURLs() + urls, err := metadataService.GlobalMetadataService.GetSubscribedURLs() + if err != nil { + logger.Errorf("get metadata subscribed url is error, %v", err) + return + } 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 664c0e28f1..5c4d535feb 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -20,6 +20,7 @@ package servicediscovery import ( "bytes" "dubbo.apache.org/dubbo-go/v3/metadata/info" + "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" "strings" "sync" "time" @@ -108,6 +109,12 @@ func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { } func (s *serviceDiscoveryRegistry) RegisterService() error { + meta := s.serviceMeta.metadataInfo + meta.CalAndGetRevision() + err := instance.GetMetadataReport().PublishAppMetadata(meta.App, meta.Revision, meta) + if err != nil { + return err + } s.serviceMeta.createInstance() return s.serviceDiscovery.Register(s.serviceMeta.instance) } @@ -146,7 +153,7 @@ 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) // TODO check is right if err != nil { return err } From f2134b4cebb6181062dc945cc197f144928ab2d1 Mon Sep 17 00:00:00 2001 From: foghost Date: Wed, 29 Nov 2023 11:31:07 +0800 Subject: [PATCH 05/26] refect serviceDiscoveryRegistry Server size api --- config/root_config.go | 2 +- metadata/exporter/service_exporter.go | 5 +- metadata/info/metadata_info.go | 21 +----- metadata/metadata.go | 5 +- metadata/metadata_service.go | 2 +- .../metadata_service_url_params_customizer.go | 4 +- .../protocol_ports_metadata_customizer.go | 4 +- registry/event/service_revision_customizer.go | 11 +-- registry/service_discovery.go | 2 - .../service_discovery_registry.go | 68 ++++++------------- registry/servicediscovery/service_metadata.go | 22 +++--- server/server.go | 2 +- 12 files changed, 58 insertions(+), 90 deletions(-) diff --git a/config/root_config.go b/config/root_config.go index 98ab1e42d5..84dc9744bf 100644 --- a/config/root_config.go +++ b/config/root_config.go @@ -18,7 +18,6 @@ package config import ( - "dubbo.apache.org/dubbo-go/v3/registry/servicediscovery" "sync" ) @@ -35,6 +34,7 @@ import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/config_center" "dubbo.apache.org/dubbo-go/v3/metadata" + "dubbo.apache.org/dubbo-go/v3/registry/servicediscovery" ) var ( diff --git a/metadata/exporter/service_exporter.go b/metadata/exporter/service_exporter.go index c00f6e9d7f..75b8e38fd1 100644 --- a/metadata/exporter/service_exporter.go +++ b/metadata/exporter/service_exporter.go @@ -1,10 +1,13 @@ package exporter +import ( + "github.com/dubbogo/gost/log/logger" +) + import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/config" "dubbo.apache.org/dubbo-go/v3/metadata" - "github.com/dubbogo/gost/log/logger" ) func init() { diff --git a/metadata/info/metadata_info.go b/metadata/info/metadata_info.go index f93c683efb..e42579b839 100644 --- a/metadata/info/metadata_info.go +++ b/metadata/info/metadata_info.go @@ -58,7 +58,6 @@ var IncludeKeys = gxset.NewSet( // MetadataInfo the metadata information of instance type MetadataInfo struct { - Reported bool `json:"-"` App string `json:"app,omitempty" hessian:"app"` Revision string `json:"revision,omitempty" hessian:"revision"` Services map[string]*ServiceInfo `json:"services,omitempty" hessian:"services"` @@ -66,24 +65,20 @@ type MetadataInfo struct { subscribedServiceURLs map[string][]*common.URL // client subscribed service urls } -// nolint -func NewMetadataInfWithApp(app string) *MetadataInfo { - return NewMetadataInfo(app, "", make(map[string]*ServiceInfo)) +func NewMetadataInfWithApp() *MetadataInfo { + return NewMetadataInfo("", "", 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, exportedServiceURLs: make(map[string][]*common.URL), subscribedServiceURLs: make(map[string][]*common.URL), } } -// nolint func (info *MetadataInfo) JavaClassName() string { return "org.apache.dubbo.metadata.MetadataInfo" } @@ -93,7 +88,7 @@ func (info *MetadataInfo) JavaClassName() string { // 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 != "" && info.Reported { + if info.Revision != "" { return info.Revision } if len(info.Services) == 0 { @@ -127,16 +122,6 @@ func (info *MetadataInfo) CalAndGetRevision() string { } -// nolint -func (info *MetadataInfo) HasReported() bool { - return info.Reported -} - -// nolint -func (info *MetadataInfo) MarkReported() { - info.Reported = true -} - // AddService add provider service info to MetadataInfo func (info *MetadataInfo) AddService(url *common.URL) { service := NewServiceInfoWithURL(url) diff --git a/metadata/metadata.go b/metadata/metadata.go index dab22df50d..bb2ac7f990 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -19,7 +19,6 @@ package metadata import ( - "dubbo.apache.org/dubbo-go/v3/common/constant" "sync" ) @@ -27,6 +26,10 @@ import ( "github.com/dubbogo/gost/log/logger" ) +import ( + "dubbo.apache.org/dubbo-go/v3/common/constant" +) + var ( GlobalMetadataService MetadataService = &DefaultMetadataService{} exportOnce sync.Once diff --git a/metadata/metadata_service.go b/metadata/metadata_service.go index 1aae59d5c5..c0dc468874 100644 --- a/metadata/metadata_service.go +++ b/metadata/metadata_service.go @@ -58,7 +58,7 @@ type ServiceExporter interface { UnExport() } -// MetadataService is store and query the metadata info in memory when each service registry +// DefaultMetadataService is store and query the metadata info in memory when each service registry type DefaultMetadataService struct { url *common.URL } diff --git a/registry/event/metadata_service_url_params_customizer.go b/registry/event/metadata_service_url_params_customizer.go index 5cd142be81..35d530dd50 100644 --- a/registry/event/metadata_service_url_params_customizer.go +++ b/registry/event/metadata_service_url_params_customizer.go @@ -18,7 +18,6 @@ package event import ( - metadataService "dubbo.apache.org/dubbo-go/v3/metadata" "encoding/json" ) @@ -31,6 +30,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" "dubbo.apache.org/dubbo-go/v3/metadata/info" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -56,7 +56,7 @@ func (m *metadataServiceURLParamsMetadataCustomizer) GetPriority() int { } func (m *metadataServiceURLParamsMetadataCustomizer) Customize(instance registry.ServiceInstance) { - url, err := metadataService.GlobalMetadataService.GetMetadataServiceURL() + url, err := metadata.GlobalMetadataService.GetMetadataServiceURL() if err != nil { logger.Errorf("get metadata service url is error, %v", err) return diff --git a/registry/event/protocol_ports_metadata_customizer.go b/registry/event/protocol_ports_metadata_customizer.go index 23ac124216..1d68b0a054 100644 --- a/registry/event/protocol_ports_metadata_customizer.go +++ b/registry/event/protocol_ports_metadata_customizer.go @@ -18,7 +18,6 @@ package event import ( - metadataService "dubbo.apache.org/dubbo-go/v3/metadata" "encoding/json" "strconv" ) @@ -30,6 +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" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -48,7 +48,7 @@ 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) { - list, err := metadataService.GlobalMetadataService.GetExportedServiceURLs() + list, err := metadata.GlobalMetadataService.GetExportedServiceURLs() if err != nil { logger.Errorf("get metadata exported service url is error, %v", err) return diff --git a/registry/event/service_revision_customizer.go b/registry/event/service_revision_customizer.go index ac4817b8c3..ebad0ae14a 100644 --- a/registry/event/service_revision_customizer.go +++ b/registry/event/service_revision_customizer.go @@ -18,17 +18,20 @@ package event import ( - metadataService "dubbo.apache.org/dubbo-go/v3/metadata" "fmt" - "github.com/dubbogo/gost/log/logger" "hash/crc32" "sort" ) +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" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -48,7 +51,7 @@ 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) { - urls, err := metadataService.GlobalMetadataService.GetExportedServiceURLs() + urls, err := metadata.GlobalMetadataService.GetExportedServiceURLs() if err != nil { logger.Errorf("get metadata service url is error, %v", err) return @@ -69,7 +72,7 @@ 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) { - urls, err := metadataService.GlobalMetadataService.GetSubscribedURLs() + urls, err := metadata.GlobalMetadataService.GetSubscribedURLs() if err != nil { logger.Errorf("get metadata subscribed url is error, %v", err) return diff --git a/registry/service_discovery.go b/registry/service_discovery.go index 8470d1a263..bc727adfb7 100644 --- a/registry/service_discovery.go +++ b/registry/service_discovery.go @@ -82,6 +82,4 @@ type ServiceDiscoveryRegistry interface { UnRegisterService() error // GetLocalMetadata get local metadata info GetLocalMetadata() *info.MetadataInfo - // GetRemoteMetadata get remote server metadata info - GetRemoteMetadata(revision string, instance ServiceInstance) (*info.MetadataInfo, error) } diff --git a/registry/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go index 5c4d535feb..662d10940e 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -19,8 +19,6 @@ package servicediscovery import ( "bytes" - "dubbo.apache.org/dubbo-go/v3/metadata/info" - "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" "strings" "sync" "time" @@ -37,7 +35,9 @@ 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/metadata/mapping" + "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" "dubbo.apache.org/dubbo-go/v3/metrics" metricMetadata "dubbo.apache.org/dubbo-go/v3/metrics/metadata" metricsRegistry "dubbo.apache.org/dubbo-go/v3/metrics/registry" @@ -71,38 +71,26 @@ func RegisterServiceInstance() error { // In order to keep compatible with interface-level registry, // serviceDiscoveryRegistry = ServiceDiscovery + metadata type serviceDiscoveryRegistry struct { - lock sync.RWMutex - url *common.URL - serviceDiscovery registry.ServiceDiscovery - serviceMeta *serviceMeta - subscribedServices *gxset.HashSet - serviceNameMapping mapping.ServiceNameMapping - registeredListeners *gxset.HashSet - subscribedURLsSynthesizers []synthesizer.SubscribedURLsSynthesizer - serviceListeners map[string]registry.ServiceInstancesChangedListener - serviceMappingListeners map[string]mapping.MappingListener + lock sync.RWMutex + url *common.URL + serviceDiscovery registry.ServiceDiscovery + serviceMeta *ServiceMeta + serviceNameMapping mapping.ServiceNameMapping + serviceListeners map[string]registry.ServiceInstancesChangedListener + serviceMappingListeners map[string]mapping.MappingListener } func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { - serviceDiscovery, err := creatServiceDiscovery(url) + serviceDiscovery, err := extension.GetServiceDiscovery(url) if err != nil { - return nil, err - } - subscribedServices := parseServices(url.GetParam(constant.SubscribedServiceNamesKey, "")) - subscribedURLsSynthesizers := synthesizer.GetAllSynthesizer() - serviceNameMapping := extension.GetGlobalServiceNameMapping() - 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, - serviceMeta: NewServiceMeta(url.GetParam(constant.ApplicationKey, "")), - subscribedServices: subscribedServices, - subscribedURLsSynthesizers: subscribedURLsSynthesizers, - registeredListeners: gxset.NewSet(), - serviceNameMapping: serviceNameMapping, - serviceListeners: make(map[string]registry.ServiceInstancesChangedListener), + url: url, + serviceDiscovery: serviceDiscovery, + serviceMeta: NewServiceMeta(), + serviceNameMapping: extension.GetGlobalServiceNameMapping(), + serviceListeners: make(map[string]registry.ServiceInstancesChangedListener), // cache for mapping listener serviceMappingListeners: make(map[string]mapping.MappingListener), }, nil @@ -115,8 +103,7 @@ func (s *serviceDiscoveryRegistry) RegisterService() error { if err != nil { return err } - s.serviceMeta.createInstance() - return s.serviceDiscovery.Register(s.serviceMeta.instance) + return s.serviceDiscovery.Register(s.serviceMeta.createInstance()) } func (s *serviceDiscoveryRegistry) UnRegisterService() error { @@ -127,10 +114,6 @@ func (s *serviceDiscoveryRegistry) GetLocalMetadata() *info.MetadataInfo { return s.serviceMeta.metadataInfo } -func (s *serviceDiscoveryRegistry) GetRemoteMetadata(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { - return s.serviceMeta.GetRemoteMetadata(revision, instance) -} - func (s *serviceDiscoveryRegistry) UnRegister(url *common.URL) error { if !shouldRegister(url) { return nil @@ -160,14 +143,6 @@ func (s *serviceDiscoveryRegistry) UnSubscribe(url *common.URL, listener registr 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 failed") - } - return originServiceDiscovery, nil -} - func parseServices(literalServices string) *gxset.HashSet { set := gxset.NewSet() if len(literalServices) == 0 { @@ -216,7 +191,7 @@ func (s *serviceDiscoveryRegistry) Register(url *common.URL) error { 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()) @@ -229,7 +204,7 @@ func (s *serviceDiscoveryRegistry) Subscribe(url *common.URL, notify registry.No return nil } s.serviceMeta.GetLocalMetadata().AddSubscribeURL(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,"+ @@ -319,7 +294,7 @@ func appendParam(buffer bytes.Buffer, paramKey string, url *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)...) } @@ -339,9 +314,6 @@ func (s *serviceDiscoveryRegistry) getServices(url *common.URL, listener mapping } if services.Empty() { services = s.findMappedServices(url, listener) - if services.Empty() { - return s.subscribedServices - } } return services } diff --git a/registry/servicediscovery/service_metadata.go b/registry/servicediscovery/service_metadata.go index 1e98647a52..9c1011ac35 100644 --- a/registry/servicediscovery/service_metadata.go +++ b/registry/servicediscovery/service_metadata.go @@ -2,30 +2,33 @@ package servicediscovery import ( "context" + "strconv" + "time" +) + +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" metadataInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" "dubbo.apache.org/dubbo-go/v3/registry" - "strconv" - "time" ) -type serviceMeta struct { +type ServiceMeta struct { metadataInfo *info.MetadataInfo instance registry.ServiceInstance } -func NewServiceMeta(app string) *serviceMeta { - return &serviceMeta{metadataInfo: info.NewMetadataInfWithApp(app)} +func NewServiceMeta() *ServiceMeta { + return &ServiceMeta{metadataInfo: info.NewMetadataInfWithApp()} } -func (sd *serviceMeta) GetLocalMetadata() *info.MetadataInfo { +func (sd *ServiceMeta) GetLocalMetadata() *info.MetadataInfo { return sd.metadataInfo } -func (sd *serviceMeta) GetRemoteMetadata(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { +func (sd *ServiceMeta) GetRemoteMetadata(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { meta, err := getMetadataFromCache(revision) if err != nil || meta == nil { meta, err = getMetadataFromMetadataReport(revision, instance) @@ -37,7 +40,7 @@ func (sd *serviceMeta) GetRemoteMetadata(revision string, instance registry.Serv return meta, err } -func (sd *serviceMeta) createInstance() { +func (sd *ServiceMeta) createInstance() registry.ServiceInstance { metadata := make(map[string]string, 8) metadata[constant.MetadataStorageTypePropertyName] = metadataInstance.GetMetadataType() instance := ®istry.DefaultServiceInstance{ @@ -52,9 +55,10 @@ func (sd *serviceMeta) createInstance() { cus.Customize(instance) } sd.instance = instance + return instance } -//func (sd *serviceMeta) calOrUpdateInstanceRevision(instance registry.ServiceInstance) bool { +//func (sd *ServiceMeta) calOrUpdateInstanceRevision(instance registry.ServiceInstance) bool { // oldRevision := getRevision(instance) // newRevision := instance.GetServiceMetadata().CalAndGetRevision() // if oldRevision != newRevision { diff --git a/server/server.go b/server/server.go index 477fe4ac8d..de10c16d4b 100644 --- a/server/server.go +++ b/server/server.go @@ -20,7 +20,6 @@ package server import ( "context" - "dubbo.apache.org/dubbo-go/v3/registry/servicediscovery" "fmt" "sync" ) @@ -35,6 +34,7 @@ import ( "dubbo.apache.org/dubbo-go/v3/common" "dubbo.apache.org/dubbo-go/v3/metadata" "dubbo.apache.org/dubbo-go/v3/protocol" + "dubbo.apache.org/dubbo-go/v3/registry/servicediscovery" ) // proServices are for internal services From 20d02c09aad530fa47568c8d339de2be20e90aad Mon Sep 17 00:00:00 2001 From: foghost Date: Wed, 29 Nov 2023 12:10:42 +0800 Subject: [PATCH 06/26] move customizer to service discovery dir add host port customizer add licence --- metadata/exporter/service_exporter.go | 17 ++++++ metadata/info/metadata_info_test.go | 17 ++++++ .../metadata_service_url_params_customizer.go | 2 +- ...data_service_url_params_customizer_test.go | 2 +- .../protocol_ports_metadata_customizer.go | 2 +- .../service_instance_host_port_customizer.go | 55 +++++++++++++++++++ .../service_revision_customizer.go | 2 +- .../service_discovery_registry.go | 6 +- registry/servicediscovery/service_metadata.go | 17 ++++++ 9 files changed, 113 insertions(+), 7 deletions(-) rename registry/{event => servicediscovery/customizer}/metadata_service_url_params_customizer.go (99%) rename registry/{event => servicediscovery/customizer}/metadata_service_url_params_customizer_test.go (99%) rename registry/{event => servicediscovery/customizer}/protocol_ports_metadata_customizer.go (99%) create mode 100644 registry/servicediscovery/customizer/service_instance_host_port_customizer.go rename registry/{event => servicediscovery/customizer}/service_revision_customizer.go (99%) diff --git a/metadata/exporter/service_exporter.go b/metadata/exporter/service_exporter.go index 75b8e38fd1..4359d6339e 100644 --- a/metadata/exporter/service_exporter.go +++ b/metadata/exporter/service_exporter.go @@ -1,3 +1,20 @@ +/* + * 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 ( diff --git a/metadata/info/metadata_info_test.go b/metadata/info/metadata_info_test.go index c3d537aef8..bc81bcc2fe 100644 --- a/metadata/info/metadata_info_test.go +++ b/metadata/info/metadata_info_test.go @@ -1,3 +1,20 @@ +/* + * 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 ( diff --git a/registry/event/metadata_service_url_params_customizer.go b/registry/servicediscovery/customizer/metadata_service_url_params_customizer.go similarity index 99% rename from registry/event/metadata_service_url_params_customizer.go rename to registry/servicediscovery/customizer/metadata_service_url_params_customizer.go index 35d530dd50..d88978f5ab 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" 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 99% rename from registry/event/protocol_ports_metadata_customizer.go rename to registry/servicediscovery/customizer/protocol_ports_metadata_customizer.go index 1d68b0a054..75bf9039e9 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" 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..f8cd91ec38 --- /dev/null +++ b/registry/servicediscovery/customizer/service_instance_host_port_customizer.go @@ -0,0 +1,55 @@ +/* + * 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 ( + "dubbo.apache.org/dubbo-go/v3/common/extension" + "dubbo.apache.org/dubbo-go/v3/registry" + "strconv" +) + +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 99% rename from registry/event/service_revision_customizer.go rename to registry/servicediscovery/customizer/service_revision_customizer.go index ebad0ae14a..b49e008913 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" diff --git a/registry/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go index 662d10940e..d9805a1357 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -39,10 +39,10 @@ import ( "dubbo.apache.org/dubbo-go/v3/metadata/mapping" "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" "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" ) @@ -243,7 +243,7 @@ func (s *serviceDiscoveryRegistry) SubscribeURL(url *common.URL, notify registry } 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() { diff --git a/registry/servicediscovery/service_metadata.go b/registry/servicediscovery/service_metadata.go index 9c1011ac35..d8c51fb3bd 100644 --- a/registry/servicediscovery/service_metadata.go +++ b/registry/servicediscovery/service_metadata.go @@ -1,3 +1,20 @@ +/* + * 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 servicediscovery import ( From d13afea85c8fbb1f8142c23c7193e8f1df71ee9f Mon Sep 17 00:00:00 2001 From: foghost Date: Sat, 2 Dec 2023 14:42:02 +0800 Subject: [PATCH 07/26] client call success --- config/root_config.go | 4 +- metadata/exporter/service_exporter_test.go | 140 ++++++++++++++++++ metadata/info/metadata_info.go | 16 +- metadata/info/metadata_info_test.go | 23 +++ .../mapping/metadata/service_name_mapping.go | 6 + registry/exposed_tmp/exposed.go | 40 +++++ .../service_instance_host_port_customizer.go | 5 +- .../service_discovery_registry.go | 35 ++--- ...service_instances_changed_listener_impl.go | 42 ++---- registry/servicediscovery/service_metadata.go | 76 ++++++---- .../servicediscovery/service_metadata_test.go | 27 ++++ server/server.go | 4 +- 12 files changed, 324 insertions(+), 94 deletions(-) create mode 100644 metadata/exporter/service_exporter_test.go create mode 100644 registry/exposed_tmp/exposed.go create mode 100644 registry/servicediscovery/service_metadata_test.go diff --git a/config/root_config.go b/config/root_config.go index 84dc9744bf..1278374ef0 100644 --- a/config/root_config.go +++ b/config/root_config.go @@ -34,7 +34,7 @@ import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/config_center" "dubbo.apache.org/dubbo-go/v3/metadata" - "dubbo.apache.org/dubbo-go/v3/registry/servicediscovery" + "dubbo.apache.org/dubbo-go/v3/registry/exposed_tmp" ) var ( @@ -206,7 +206,7 @@ func (rc *RootConfig) Start() { rc.Consumer.Load() rc.Provider.Load() metadata.ExportMetadataService(rc.Application.Name, rc.Application.MetadataType) - err := servicediscovery.RegisterServiceInstance() + err := exposed_tmp.RegisterServiceInstance() if err != nil { panic(err) } diff --git a/metadata/exporter/service_exporter_test.go b/metadata/exporter/service_exporter_test.go new file mode 100644 index 0000000000..4bce389929 --- /dev/null +++ b/metadata/exporter/service_exporter_test.go @@ -0,0 +1,140 @@ +package exporter + +import ( + "reflect" + "testing" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common" + "dubbo.apache.org/dubbo-go/v3/config" + "dubbo.apache.org/dubbo-go/v3/metadata" + "dubbo.apache.org/dubbo-go/v3/metadata/info" + _ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo" + _ "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory" +) + +func TestMetadataServiceExporter_Export(t *testing.T) { + type fields struct { + app string + metadataType string + ServiceConfig *config.ServiceConfig + service metadata.MetadataService + } + tests := []struct { + name string + fields fields + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + exporter := &MetadataServiceExporter{ + app: tt.fields.app, + metadataType: tt.fields.metadataType, + ServiceConfig: tt.fields.ServiceConfig, + service: tt.fields.service, + } + if err := exporter.Export(); (err != nil) != tt.wantErr { + t.Errorf("Export() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestMetadataServiceExporter_UnExport(t *testing.T) { + type fields struct { + app string + metadataType string + ServiceConfig *config.ServiceConfig + service metadata.MetadataService + } + tests := []struct { + name string + fields fields + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + exporter := &MetadataServiceExporter{ + app: tt.fields.app, + metadataType: tt.fields.metadataType, + ServiceConfig: tt.fields.ServiceConfig, + service: tt.fields.service, + } + exporter.UnExport() + }) + } +} + +func TestNewMetadataServiceExporter(t *testing.T) { + type args struct { + app string + metadataType string + service metadata.MetadataService + } + tests := []struct { + name string + args args + want metadata.ServiceExporter + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := NewMetadataServiceExporter(tt.args.app, tt.args.metadataType, tt.args.service); !reflect.DeepEqual(got, tt.want) { + t.Errorf("NewMetadataServiceExporter() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestExporter(t *testing.T) { + err := NewMetadataServiceExporter("test", "local", &Service{}).Export() + if err != nil { + panic(err) + } + select {} +} + +type Service struct { +} + +func (s Service) GetExportedURLs(serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) { + //TODO implement me + panic("implement me") +} + +func (s Service) GetExportedServiceURLs() ([]*common.URL, error) { + //TODO implement me + panic("implement me") +} + +func (s Service) GetSubscribedURLs() ([]*common.URL, error) { + //TODO implement me + panic("implement me") +} + +func (s Service) Version() (string, error) { + return "1.0.0", nil +} + +func (s Service) GetMetadataInfo(revision string) (*info.MetadataInfo, error) { + meta := info.NewMetadataInfo() + meta.App = "test" + meta.Revision = "1" + //meta.Services = make(map[string]*info.ServiceInfo) + //meta.Services["1"] = info.NewServiceInfo("test", "group", "1.0.0", "dubbo", "", make(map[string]string)) + return meta, nil +} + +func (s Service) GetMetadataServiceURL() (*common.URL, error) { + //TODO implement me + panic("implement me") +} + +func (s Service) SetMetadataServiceURL(url *common.URL) { + +} diff --git a/metadata/info/metadata_info.go b/metadata/info/metadata_info.go index e42579b839..5d9d96a486 100644 --- a/metadata/info/metadata_info.go +++ b/metadata/info/metadata_info.go @@ -61,15 +61,15 @@ type MetadataInfo struct { App string `json:"app,omitempty" hessian:"app"` Revision string `json:"revision,omitempty" hessian:"revision"` Services map[string]*ServiceInfo `json:"services,omitempty" hessian:"services"` - exportedServiceURLs map[string][]*common.URL // server exported service urls - subscribedServiceURLs map[string][]*common.URL // client subscribed service urls + exportedServiceURLs map[string][]*common.URL `hessian:"-"` // server exported service urls + subscribedServiceURLs map[string][]*common.URL `hessian:"-"` // client subscribed service urls } -func NewMetadataInfWithApp() *MetadataInfo { - return NewMetadataInfo("", "", make(map[string]*ServiceInfo)) +func NewMetadataInfo() *MetadataInfo { + return NewMetadataInfoWithParams("", "", make(map[string]*ServiceInfo)) } -func NewMetadataInfo(app string, revision string, services map[string]*ServiceInfo) *MetadataInfo { +func NewMetadataInfoWithParams(app string, revision string, services map[string]*ServiceInfo) *MetadataInfo { return &MetadataInfo{ App: app, Revision: revision, @@ -201,9 +201,9 @@ type ServiceInfo struct { Path string `json:"path,omitempty" hessian:"path"` Params map[string]string `json:"params,omitempty" hessian:"params"` - ServiceKey string `json:"-"` - MatchKey string `json:"-"` - URL *common.URL `json:"-"` + ServiceKey string `json:"-" hessian:"-"` + MatchKey string `json:"-" hessian:"-"` + URL *common.URL `json:"-" hessian:"-"` } // nolint diff --git a/metadata/info/metadata_info_test.go b/metadata/info/metadata_info_test.go index bc81bcc2fe..de7039cfdf 100644 --- a/metadata/info/metadata_info_test.go +++ b/metadata/info/metadata_info_test.go @@ -22,6 +22,8 @@ import ( ) import ( + hessian "github.com/apache/dubbo-go-hessian2" + "github.com/stretchr/testify/assert" ) @@ -45,3 +47,24 @@ func TestMetadataInfoAddService(t *testing.T) { 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) + if err != nil { + panic(err) + } + obj, err := hessian.NewDecoder(e.Buffer()).Decode() + if err != nil { + panic(err) + } + t.Log(obj) +} diff --git a/metadata/mapping/metadata/service_name_mapping.go b/metadata/mapping/metadata/service_name_mapping.go index ebd216db14..5f1beca479 100644 --- a/metadata/mapping/metadata/service_name_mapping.go +++ b/metadata/mapping/metadata/service_name_mapping.go @@ -88,11 +88,17 @@ func (d *ServiceNameMapping) Map(url *common.URL) error { func (d *ServiceNameMapping) Get(url *common.URL, listener mapping.MappingListener) (*gxset.HashSet, error) { serviceInterface := url.GetParam(constant.InterfaceKey, "") metadataReport := instance.GetMetadataReport() + if metadataReport == nil { + return gxset.NewSet(), nil + } return metadataReport.GetServiceAppMapping(serviceInterface, DefaultGroup, listener) } func (d *ServiceNameMapping) Remove(url *common.URL) error { serviceInterface := url.GetParam(constant.InterfaceKey, "") metadataReport := instance.GetMetadataReport() + if metadataReport == nil { + return nil + } return metadataReport.RemoveServiceAppMappingListener(serviceInterface, DefaultGroup) } diff --git a/registry/exposed_tmp/exposed.go b/registry/exposed_tmp/exposed.go new file mode 100644 index 0000000000..c8ca0310c3 --- /dev/null +++ b/registry/exposed_tmp/exposed.go @@ -0,0 +1,40 @@ +/* + * 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 exposed_tmp + +import ( + "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() error { + protocol := extension.GetProtocol(constant.RegistryKey) + if rf, ok := protocol.(registry.RegistryFactory); ok { + for _, r := range rf.GetRegistries() { + if sdr, ok := r.(registry.ServiceDiscoveryRegistry); ok { + err := sdr.RegisterService() + if err != nil { + return err + } + } + } + } + return nil +} diff --git a/registry/servicediscovery/customizer/service_instance_host_port_customizer.go b/registry/servicediscovery/customizer/service_instance_host_port_customizer.go index f8cd91ec38..f33023f45b 100644 --- a/registry/servicediscovery/customizer/service_instance_host_port_customizer.go +++ b/registry/servicediscovery/customizer/service_instance_host_port_customizer.go @@ -17,10 +17,13 @@ package customizer +import ( + "strconv" +) + import ( "dubbo.apache.org/dubbo-go/v3/common/extension" "dubbo.apache.org/dubbo-go/v3/registry" - "strconv" ) func init() { diff --git a/registry/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go index d9805a1357..a6585a0089 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -50,21 +50,6 @@ func init() { extension.SetRegistry(constant.ServiceRegistryProtocol, newServiceDiscoveryRegistry) } -func RegisterServiceInstance() error { - protocol := extension.GetProtocol(constant.RegistryKey) - if rf, ok := protocol.(registry.RegistryFactory); ok { - for _, r := range rf.GetRegistries() { - if sdr, ok := r.(registry.ServiceDiscoveryRegistry); ok { - err := sdr.RegisterService() - if err != nil { - return err - } - } - } - } - return nil -} - // 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 and metadata @@ -97,13 +82,21 @@ func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { } func (s *serviceDiscoveryRegistry) RegisterService() error { - meta := s.serviceMeta.metadataInfo - meta.CalAndGetRevision() - err := instance.GetMetadataReport().PublishAppMetadata(meta.App, meta.Revision, meta) - if err != nil { - return err + serviceInstance := s.serviceMeta.createInstance() + // consumer has not host and port, so it will not register service + if serviceInstance.GetHost() != "" && serviceInstance.GetPort() != 0 { + meta := s.serviceMeta.metadataInfo + meta.CalAndGetRevision() + metadataReport := instance.GetMetadataReport() + if metadataReport != nil { + err := metadataReport.PublishAppMetadata(meta.App, meta.Revision, meta) + if err != nil { + return err + } + } + return s.serviceDiscovery.Register(serviceInstance) } - return s.serviceDiscovery.Register(s.serviceMeta.createInstance()) + return nil } func (s *serviceDiscoveryRegistry) UnRegisterService() error { diff --git a/registry/servicediscovery/service_instances_changed_listener_impl.go b/registry/servicediscovery/service_instances_changed_listener_impl.go index cbabd8e917..06e35e6181 100644 --- a/registry/servicediscovery/service_instances_changed_listener_impl.go +++ b/registry/servicediscovery/service_instances_changed_listener_impl.go @@ -233,45 +233,23 @@ func GetMetadataInfo(app string, instance registry.ServiceInstance, revision str var metadataStorageType string 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) - // 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) - // if err != nil { - // return nil, err - // } + metadataInfo, err = getMetadataFromMetadataReport(revision, instance) + if err != nil { + return nil, err + } + } else { + metadataInfo, err = 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_metadata.go b/registry/servicediscovery/service_metadata.go index d8c51fb3bd..d59a6bcde4 100644 --- a/registry/servicediscovery/service_metadata.go +++ b/registry/servicediscovery/service_metadata.go @@ -19,16 +19,22 @@ package servicediscovery import ( "context" - "strconv" + "encoding/json" "time" ) +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/info" metadataInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" + _ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo" + _ "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -38,25 +44,13 @@ type ServiceMeta struct { } func NewServiceMeta() *ServiceMeta { - return &ServiceMeta{metadataInfo: info.NewMetadataInfWithApp()} + return &ServiceMeta{metadataInfo: info.NewMetadataInfo()} } func (sd *ServiceMeta) GetLocalMetadata() *info.MetadataInfo { return sd.metadataInfo } -func (sd *ServiceMeta) GetRemoteMetadata(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { - meta, err := getMetadataFromCache(revision) - if err != nil || meta == nil { - meta, err = getMetadataFromMetadataReport(revision, instance) - if err != nil || meta == nil { - meta, err = getMetadataFromRpc(revision, instance) - } - // TODO : need to update cache - } - return meta, err -} - func (sd *ServiceMeta) createInstance() registry.ServiceInstance { metadata := make(map[string]string, 8) metadata[constant.MetadataStorageTypePropertyName] = metadataInstance.GetMetadataType() @@ -92,11 +86,6 @@ func (sd *ServiceMeta) createInstance() registry.ServiceInstance { // return instance.GetMetadata()[constant.ExportedServicesRevisionPropertyName] //} -func getMetadataFromCache(revision string) (*info.MetadataInfo, error) { - // TODO metadata cache - return nil, nil -} - func getMetadataFromMetadataReport(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { report := metadataInstance.GetMetadataReport() return report.GetAppMetadata(instance.GetServiceName(), revision) @@ -119,15 +108,12 @@ type metadataService struct { } func createRpcClient(instance registry.ServiceInstance) (*metadataService, func()) { - url := common.NewURLWithOptions( - common.WithProtocol(constant.Dubbo), - common.WithIp(instance.GetHost()), - common.WithPort(strconv.Itoa(instance.GetPort())), - ) - url.SetParam(constant.SideKey, constant.Consumer) - url.SetParam(constant.VersionKey, "1.0.0") - url.SetParam(constant.InterfaceKey, constant.MetadataServiceName) - url.SetParam(constant.GroupKey, instance.GetServiceName()) + params := getMetadataServiceUrlParams(instance.GetMetadata()[constant.MetadataServiceURLParamsPropertyName]) + url := buildMetadataServiceURL(instance.GetServiceName(), instance.GetHost(), params) + return createRpcClientByUrl(url) +} + +func createRpcClientByUrl(url *common.URL) (*metadataService, func()) { rpcService := &metadataService{} invoker := extension.GetProtocol(constant.Dubbo).Refer(url) proxy := extension.GetProxyFactory("").GetProxy(invoker, url) @@ -137,3 +123,37 @@ func createRpcClient(instance registry.ServiceInstance) (*metadataService, func( } return rpcService, destroy } + +// buildMetadataServiceURL will use standard format to build the metadata service url. +func buildMetadataServiceURL(serviceName string, host string, ps map[string]string) *common.URL { + if ps[constant.ProtocolKey] == "" { + return nil + } + 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, 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/registry/servicediscovery/service_metadata_test.go b/registry/servicediscovery/service_metadata_test.go new file mode 100644 index 0000000000..bfaa063873 --- /dev/null +++ b/registry/servicediscovery/service_metadata_test.go @@ -0,0 +1,27 @@ +package servicediscovery + +import ( + "context" + "fmt" + "testing" +) + +import ( + "github.com/dubbogo/gost/log/logger" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common" + _ "dubbo.apache.org/dubbo-go/v3/metadata/info" +) + +func TestCreateRpcClientByUrl(t *testing.T) { + url, err := common.NewURL("dubbo://10.253.77.49:61086?group=dubbo.io&interface=org.apache.dubbo.metadata.MetadataService&side=consumer&version=1.0.0") + service, destory := createRpcClientByUrl(url) + defer destory() + meta, err := service.GetMetadataInfo(context.Background(), "4647984502") + if err != nil { + logger.Error(err) + } + fmt.Println(meta) +} diff --git a/server/server.go b/server/server.go index de10c16d4b..5e6f73818b 100644 --- a/server/server.go +++ b/server/server.go @@ -34,7 +34,7 @@ import ( "dubbo.apache.org/dubbo-go/v3/common" "dubbo.apache.org/dubbo-go/v3/metadata" "dubbo.apache.org/dubbo-go/v3/protocol" - "dubbo.apache.org/dubbo-go/v3/registry/servicediscovery" + "dubbo.apache.org/dubbo-go/v3/registry/exposed_tmp" ) // proServices are for internal services @@ -188,7 +188,7 @@ func (s *Server) Serve() error { return err } metadata.ExportMetadataService(s.cfg.Application.Name, s.cfg.Application.MetadataType) - err := servicediscovery.RegisterServiceInstance() + err := exposed_tmp.RegisterServiceInstance() if err != nil { return err } From 77983e5a19def2decf9f0b843e80b03e7329a57e Mon Sep 17 00:00:00 2001 From: foghost Date: Sat, 2 Dec 2023 18:22:16 +0800 Subject: [PATCH 08/26] mv metadata client to metadata dir --- metadata/client/client.go | 109 +++++++++++ metadata/client/client_test.go | 183 ++++++++++++++++++ ...service_instances_changed_listener_impl.go | 4 +- registry/servicediscovery/service_metadata.go | 83 -------- .../servicediscovery/service_metadata_test.go | 27 --- 5 files changed, 294 insertions(+), 112 deletions(-) create mode 100644 metadata/client/client.go create mode 100644 metadata/client/client_test.go delete mode 100644 registry/servicediscovery/service_metadata_test.go diff --git a/metadata/client/client.go b/metadata/client/client.go new file mode 100644 index 0000000000..648da57cd2 --- /dev/null +++ b/metadata/client/client.go @@ -0,0 +1,109 @@ +/* + * 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 client + +import ( + "context" + "encoding/json" + "time" +) + +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/info" + metadataInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" + "dubbo.apache.org/dubbo-go/v3/registry" +) + +func GetMetadataFromMetadataReport(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { + report := metadataInstance.GetMetadataReport() + return report.GetAppMetadata(instance.GetServiceName(), revision) +} + +func GetMetadataFromRpc(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { + service, destroy := createRpcClient(instance) + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5000)) + defer cancel() + defer destroy() + return service.GetMetadataInfo(ctx, revision) +} + +type metadataService struct { + GetExportedURLs func(context context.Context, serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) `dubbo:"getExportedURLs"` + GetMetadataInfo func(context context.Context, revision string) (*info.MetadataInfo, error) `dubbo:"getMetadataInfo"` + GetMetadataServiceURL func(context context.Context) (*common.URL, error) + GetSubscribedURLs func(context context.Context) ([]*common.URL, error) + Version func(context context.Context) (string, error) +} + +func createRpcClient(instance registry.ServiceInstance) (*metadataService, func()) { + params := getMetadataServiceUrlParams(instance.GetMetadata()[constant.MetadataServiceURLParamsPropertyName]) + url := buildMetadataServiceURL(instance.GetServiceName(), instance.GetHost(), params) + return createRpcClientByUrl(url) +} + +func createRpcClientByUrl(url *common.URL) (*metadataService, func()) { + rpcService := &metadataService{} + invoker := extension.GetProtocol(constant.Dubbo).Refer(url) + proxy := extension.GetProxyFactory("").GetProxy(invoker, url) + proxy.Implement(rpcService) + destroy := func() { + invoker.Destroy() + } + return rpcService, destroy +} + +// buildMetadataServiceURL will use standard format to build the metadata service url. +func buildMetadataServiceURL(serviceName string, host string, ps map[string]string) *common.URL { + if ps[constant.ProtocolKey] == "" { + return nil + } + 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, 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/client/client_test.go b/metadata/client/client_test.go new file mode 100644 index 0000000000..d1d92dd881 --- /dev/null +++ b/metadata/client/client_test.go @@ -0,0 +1,183 @@ +/* + * 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 client + +import ( + "dubbo.apache.org/dubbo-go/v3/common" + "dubbo.apache.org/dubbo-go/v3/metadata/info" + "dubbo.apache.org/dubbo-go/v3/registry" + "reflect" + "testing" +) + +func TestCreateRpcClientByUrl(t *testing.T) { + //url, err := common.NewURL("dubbo://10.253.77.49:61086?group=dubbo.io&interface=org.apache.dubbo.metadata.MetadataService&side=consumer&version=1.0.0") + //service, destory := createRpcClientByUrl(url) + //defer destory() + //meta, err := service.GetMetadataInfo(context.Background(), "4647984502") + //if err != nil { + // logger.Error(err) + //} + //fmt.Println(meta) +} + +func TestGetMetadataFromMetadataReport(t *testing.T) { + type args struct { + revision string + instance registry.ServiceInstance + } + tests := []struct { + name string + args args + want *info.MetadataInfo + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetMetadataFromMetadataReport(tt.args.revision, tt.args.instance) + if (err != nil) != tt.wantErr { + t.Errorf("GetMetadataFromMetadataReport() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetMetadataFromMetadataReport() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestGetMetadataFromRpc(t *testing.T) { + type args struct { + revision string + instance registry.ServiceInstance + } + tests := []struct { + name string + args args + want *info.MetadataInfo + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetMetadataFromRpc(tt.args.revision, tt.args.instance) + if (err != nil) != tt.wantErr { + t.Errorf("GetMetadataFromRpc() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetMetadataFromRpc() got = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_buildMetadataServiceURL(t *testing.T) { + type args struct { + serviceName string + host string + ps map[string]string + } + tests := []struct { + name string + args args + want *common.URL + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := buildMetadataServiceURL(tt.args.serviceName, tt.args.host, tt.args.ps); !reflect.DeepEqual(got, tt.want) { + t.Errorf("buildMetadataServiceURL() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_createRpcClient(t *testing.T) { + type args struct { + instance registry.ServiceInstance + } + tests := []struct { + name string + args args + want *metadataService + want1 func() + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, got1 := createRpcClient(tt.args.instance) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("createRpcClient() got = %v, want %v", got, tt.want) + } + if !reflect.DeepEqual(got1, tt.want1) { + t.Errorf("createRpcClient() got1 = %v, want %v", got1, tt.want1) + } + }) + } +} + +func Test_createRpcClientByUrl(t *testing.T) { + type args struct { + url *common.URL + } + tests := []struct { + name string + args args + want *metadataService + want1 func() + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, got1 := createRpcClientByUrl(tt.args.url) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("createRpcClientByUrl() got = %v, want %v", got, tt.want) + } + if !reflect.DeepEqual(got1, tt.want1) { + t.Errorf("createRpcClientByUrl() got1 = %v, want %v", got1, tt.want1) + } + }) + } +} + +func Test_getMetadataServiceUrlParams(t *testing.T) { + type args struct { + jsonStr string + } + tests := []struct { + name string + args args + want map[string]string + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := getMetadataServiceUrlParams(tt.args.jsonStr); !reflect.DeepEqual(got, tt.want) { + t.Errorf("getMetadataServiceUrlParams() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/registry/servicediscovery/service_instances_changed_listener_impl.go b/registry/servicediscovery/service_instances_changed_listener_impl.go index 06e35e6181..955b6ff720 100644 --- a/registry/servicediscovery/service_instances_changed_listener_impl.go +++ b/registry/servicediscovery/service_instances_changed_listener_impl.go @@ -240,12 +240,12 @@ func GetMetadataInfo(app string, instance registry.ServiceInstance, revision str metadataStorageType = instance.GetMetadata()[constant.MetadataStorageTypePropertyName] } if metadataStorageType == constant.RemoteMetadataStorageType { - metadataInfo, err = getMetadataFromMetadataReport(revision, instance) + metadataInfo, err = client.GetMetadataFromMetadataReport(revision, instance) if err != nil { return nil, err } } else { - metadataInfo, err = getMetadataFromRpc(revision, instance) + metadataInfo, err = client.GetMetadataFromRpc(revision, instance) if err != nil { return nil, err } diff --git a/registry/servicediscovery/service_metadata.go b/registry/servicediscovery/service_metadata.go index d59a6bcde4..17bc20346e 100644 --- a/registry/servicediscovery/service_metadata.go +++ b/registry/servicediscovery/service_metadata.go @@ -18,17 +18,6 @@ package servicediscovery import ( - "context" - "encoding/json" - "time" -) - -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/info" @@ -85,75 +74,3 @@ func (sd *ServiceMeta) createInstance() registry.ServiceInstance { // } // return instance.GetMetadata()[constant.ExportedServicesRevisionPropertyName] //} - -func getMetadataFromMetadataReport(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { - report := metadataInstance.GetMetadataReport() - return report.GetAppMetadata(instance.GetServiceName(), revision) -} - -func getMetadataFromRpc(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { - service, destroy := createRpcClient(instance) - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5000)) - defer cancel() - defer destroy() - return service.GetMetadataInfo(ctx, revision) -} - -type metadataService struct { - GetExportedURLs func(context context.Context, serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) `dubbo:"getExportedURLs"` - GetMetadataInfo func(context context.Context, revision string) (*info.MetadataInfo, error) `dubbo:"getMetadataInfo"` - GetMetadataServiceURL func(context context.Context) (*common.URL, error) - GetSubscribedURLs func(context context.Context) ([]*common.URL, error) - Version func(context context.Context) (string, error) -} - -func createRpcClient(instance registry.ServiceInstance) (*metadataService, func()) { - params := getMetadataServiceUrlParams(instance.GetMetadata()[constant.MetadataServiceURLParamsPropertyName]) - url := buildMetadataServiceURL(instance.GetServiceName(), instance.GetHost(), params) - return createRpcClientByUrl(url) -} - -func createRpcClientByUrl(url *common.URL) (*metadataService, func()) { - rpcService := &metadataService{} - invoker := extension.GetProtocol(constant.Dubbo).Refer(url) - proxy := extension.GetProxyFactory("").GetProxy(invoker, url) - proxy.Implement(rpcService) - destroy := func() { - invoker.Destroy() - } - return rpcService, destroy -} - -// buildMetadataServiceURL will use standard format to build the metadata service url. -func buildMetadataServiceURL(serviceName string, host string, ps map[string]string) *common.URL { - if ps[constant.ProtocolKey] == "" { - return nil - } - 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, 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/registry/servicediscovery/service_metadata_test.go b/registry/servicediscovery/service_metadata_test.go deleted file mode 100644 index bfaa063873..0000000000 --- a/registry/servicediscovery/service_metadata_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package servicediscovery - -import ( - "context" - "fmt" - "testing" -) - -import ( - "github.com/dubbogo/gost/log/logger" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - _ "dubbo.apache.org/dubbo-go/v3/metadata/info" -) - -func TestCreateRpcClientByUrl(t *testing.T) { - url, err := common.NewURL("dubbo://10.253.77.49:61086?group=dubbo.io&interface=org.apache.dubbo.metadata.MetadataService&side=consumer&version=1.0.0") - service, destory := createRpcClientByUrl(url) - defer destory() - meta, err := service.GetMetadataInfo(context.Background(), "4647984502") - if err != nil { - logger.Error(err) - } - fmt.Println(meta) -} From 41f7474395e571b818fb1134374ca96fd5392601 Mon Sep 17 00:00:00 2001 From: foghost Date: Tue, 5 Dec 2023 14:19:36 +0800 Subject: [PATCH 09/26] refactor --- client/action.go | 15 ---- config/service_config.go | 2 +- go.sum | 4 - metadata/client/client.go | 31 ++++---- metadata/client/client_test.go | 7 +- metadata/exporter/service_exporter.go | 2 + metadata/info/metadata_info.go | 24 +++--- metadata/metadata.go | 18 +++-- metadata/metadata_service.go | 23 +++--- metadata/report/instance/metadata_report.go | 2 +- registry/service_discovery.go | 2 +- .../metadata_service_url_params_customizer.go | 2 +- .../protocol_ports_metadata_customizer.go | 2 +- .../customizer/service_revision_customizer.go | 4 +- .../service_discovery_registry.go | 48 ++++++++---- ...service_instances_changed_listener_impl.go | 1 + registry/servicediscovery/service_metadata.go | 76 ------------------- server/action.go | 15 ---- 18 files changed, 96 insertions(+), 182 deletions(-) delete mode 100644 registry/servicediscovery/service_metadata.go diff --git a/client/action.go b/client/action.go index da521c22b6..c5ef02d74f 100644 --- a/client/action.go +++ b/client/action.go @@ -277,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/config/service_config.go b/config/service_config.go index 6fa195191e..2fc8a4f5a5 100644 --- a/config/service_config.go +++ b/config/service_config.go @@ -327,7 +327,7 @@ func (s *ServiceConfig) Export() error { s.exporters = append(s.exporters, exporter) } } else { - invoker := proxyFactory.GetInvoker(ivkURL) + invoker = s.generatorInvoker(ivkURL, info) exporter := extension.GetProtocol(protocolwrapper.FILTER).Export(invoker) if exporter == nil { return perrors.New(fmt.Sprintf("Filter protocol without registry new exporter error, url is {%v}", ivkURL)) 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/metadata/client/client.go b/metadata/client/client.go index 648da57cd2..d0b8b5d28e 100644 --- a/metadata/client/client.go +++ b/metadata/client/client.go @@ -32,29 +32,32 @@ import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/common/extension" "dubbo.apache.org/dubbo-go/v3/metadata/info" - metadataInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" + reportInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" "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 := metadataInstance.GetMetadataReport() + report := reportInstance.GetMetadataReport() return report.GetAppMetadata(instance.GetServiceName(), revision) } func GetMetadataFromRpc(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { service, destroy := createRpcClient(instance) - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5000)) + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(metadataProxyDefaultTimeout)) defer cancel() defer destroy() return service.GetMetadataInfo(ctx, revision) } type metadataService struct { - GetExportedURLs func(context context.Context, serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) `dubbo:"getExportedURLs"` - GetMetadataInfo func(context context.Context, revision string) (*info.MetadataInfo, error) `dubbo:"getMetadataInfo"` - GetMetadataServiceURL func(context context.Context) (*common.URL, error) - GetSubscribedURLs func(context context.Context) ([]*common.URL, error) - Version func(context context.Context) (string, error) + //GetExportedURLs func(context context.Context, serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) `dubbo:"getExportedURLs"` + GetMetadataInfo func(context context.Context, revision string) (*info.MetadataInfo, error) `dubbo:"getMetadataInfo"` + //GetMetadataServiceURL func(context context.Context) (*common.URL, error) + //GetSubscribedURLs func(context context.Context) ([]*common.URL, error) + //Version func(context context.Context) (string, error) } func createRpcClient(instance registry.ServiceInstance) (*metadataService, func()) { @@ -75,18 +78,18 @@ func createRpcClientByUrl(url *common.URL) (*metadataService, func()) { } // buildMetadataServiceURL will use standard format to build the metadata service url. -func buildMetadataServiceURL(serviceName string, host string, ps map[string]string) *common.URL { - if ps[constant.ProtocolKey] == "" { +func buildMetadataServiceURL(serviceName string, host string, params map[string]string) *common.URL { + if params[constant.ProtocolKey] == "" { return nil } - convertedParams := make(map[string][]string, len(ps)) - for k, v := range ps { + 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(ps[constant.ProtocolKey]), - common.WithPort(ps[constant.PortKey]), + common.WithProtocol(params[constant.ProtocolKey]), + common.WithPort(params[constant.PortKey]), common.WithParams(convertedParams), common.WithParamsValue(constant.GroupKey, serviceName), common.WithParamsValue(constant.InterfaceKey, constant.MetadataServiceName)) diff --git a/metadata/client/client_test.go b/metadata/client/client_test.go index d1d92dd881..740230eab7 100644 --- a/metadata/client/client_test.go +++ b/metadata/client/client_test.go @@ -17,12 +17,15 @@ package client +import ( + "reflect" + "testing" +) + import ( "dubbo.apache.org/dubbo-go/v3/common" "dubbo.apache.org/dubbo-go/v3/metadata/info" "dubbo.apache.org/dubbo-go/v3/registry" - "reflect" - "testing" ) func TestCreateRpcClientByUrl(t *testing.T) { diff --git a/metadata/exporter/service_exporter.go b/metadata/exporter/service_exporter.go index 4359d6339e..fe7cc88ae1 100644 --- a/metadata/exporter/service_exporter.go +++ b/metadata/exporter/service_exporter.go @@ -25,6 +25,8 @@ import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/config" "dubbo.apache.org/dubbo-go/v3/metadata" + _ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo" + _ "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory" ) func init() { diff --git a/metadata/info/metadata_info.go b/metadata/info/metadata_info.go index 5d9d96a486..c11764aa53 100644 --- a/metadata/info/metadata_info.go +++ b/metadata/info/metadata_info.go @@ -225,6 +225,7 @@ func NewServiceInfoWithURL(url *common.URL) *ServiceInfo { } } } + p[constant.MethodsKey] = strings.Join(url.Methods, ",") service.Params = p return service } @@ -245,33 +246,26 @@ func NewServiceInfo(name, group, version, protocol, path string, params map[stri } } -// 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 + 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(strings.Split(si.Params[constant.MethodsKey], ",")) for k, p := range si.Params { ms := strings.Index(k, ".") - if ms > 0 { + if ms > 0 && methods.Contains(k[0:ms]) { v.Set("methods."+k, p) } else { v.Set(k, p) diff --git a/metadata/metadata.go b/metadata/metadata.go index bb2ac7f990..eb57bd3cc0 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -31,23 +31,23 @@ import ( ) var ( - GlobalMetadataService MetadataService = &DefaultMetadataService{} - exportOnce sync.Once - Factory ExporterFactory + metadataService MetadataService = &DefaultMetadataService{} + exportOnce sync.Once + factory ExporterFactory ) // ExporterFactory to create ServiceExporter, avoid cycle import type ExporterFactory func(app, metadataType string, service MetadataService) ServiceExporter -func SetExporterFactory(factory ExporterFactory) { - Factory = factory +func SetExporterFactory(f ExporterFactory) { + factory = f } func ExportMetadataService(app, metadataType string) { - if Factory != nil { + if factory != nil { exportOnce.Do(func() { if metadataType != constant.RemoteMetadataStorageType { - err := Factory(app, metadataType, GlobalMetadataService).Export() + err := factory(app, metadataType, metadataService).Export() if err != nil { logger.Errorf("export metadata service failed, got error %#v", err) } @@ -57,3 +57,7 @@ func ExportMetadataService(app, metadataType string) { logger.Warn("no metadata service exporter found, MetadataService will not be Exported") } } + +func GetMetadataService() MetadataService { + return metadataService +} diff --git a/metadata/metadata_service.go b/metadata/metadata_service.go index c0dc468874..5e9cd4fc55 100644 --- a/metadata/metadata_service.go +++ b/metadata/metadata_service.go @@ -49,7 +49,7 @@ type MetadataService interface { 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 + // SetMetadataServiceURL exporter to set url of metadata service, will not be exported by exporter,cause no error return SetMetadataServiceURL(*common.URL) } @@ -93,23 +93,22 @@ func (mts *DefaultMetadataService) GetMetadataInfo(revision string) (*info.Metad if revision == "" { return nil, nil } - for _, sd := range mts.getServiceDiscoveryMetadata() { - meta := sd.GetLocalMetadata() - if meta.Revision == revision { - return meta, nil + for _, metadataInfo := range mts.getAllMetadata() { + if metadataInfo.Revision == revision { + return metadataInfo, nil } } logger.Warnf("metadata not found for revision: %s", revision) return nil, nil } -func (mts *DefaultMetadataService) getServiceDiscoveryMetadata() []registry.ServiceDiscoveryRegistry { - sds := make([]registry.ServiceDiscoveryRegistry, 0) +func (mts *DefaultMetadataService) getAllMetadata() []*info.MetadataInfo { + sds := make([]*info.MetadataInfo, 0) p := extension.GetProtocol(constant.RegistryProtocol) if factory, ok := p.(registry.RegistryFactory); ok { for _, v := range factory.GetRegistries() { if sd, ok := v.(registry.ServiceDiscoveryRegistry); ok { - sds = append(sds, sd) + sds = append(sds, sd.GetLocalMetadata()) } } } @@ -119,8 +118,8 @@ func (mts *DefaultMetadataService) getServiceDiscoveryMetadata() []registry.Serv // GetExportedServiceURLs get exported service urls func (mts *DefaultMetadataService) GetExportedServiceURLs() ([]*common.URL, error) { urls := make([]*common.URL, 0) - for _, sd := range mts.getServiceDiscoveryMetadata() { - urls = append(urls, sd.GetLocalMetadata().GetExportedServiceURLs()...) + for _, metadataInfo := range mts.getAllMetadata() { + urls = append(urls, metadataInfo.GetExportedServiceURLs()...) } return urls, nil } @@ -137,8 +136,8 @@ func (mts *DefaultMetadataService) GetMetadataServiceURL() (*common.URL, error) func (mts *DefaultMetadataService) GetSubscribedURLs() ([]*common.URL, error) { urls := make([]*common.URL, 0) - for _, sd := range mts.getServiceDiscoveryMetadata() { - urls = append(urls, sd.GetLocalMetadata().GetSubscribedURLs()...) + for _, metadataInfo := range mts.getAllMetadata() { + urls = append(urls, metadataInfo.GetSubscribedURLs()...) } return urls, nil } diff --git a/metadata/report/instance/metadata_report.go b/metadata/report/instance/metadata_report.go index 5c8c1b881b..4d58970d3e 100644 --- a/metadata/report/instance/metadata_report.go +++ b/metadata/report/instance/metadata_report.go @@ -41,7 +41,7 @@ import ( var ( instances = make(map[string]report.MetadataReport) once sync.Once - metadataType string + metadataType = constant.DefaultMetadataStorageType ) func Init(urls []*common.URL, metaType string) { diff --git a/registry/service_discovery.go b/registry/service_discovery.go index bc727adfb7..5626cbdf08 100644 --- a/registry/service_discovery.go +++ b/registry/service_discovery.go @@ -37,7 +37,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 diff --git a/registry/servicediscovery/customizer/metadata_service_url_params_customizer.go b/registry/servicediscovery/customizer/metadata_service_url_params_customizer.go index d88978f5ab..690858f4d9 100644 --- a/registry/servicediscovery/customizer/metadata_service_url_params_customizer.go +++ b/registry/servicediscovery/customizer/metadata_service_url_params_customizer.go @@ -56,7 +56,7 @@ func (m *metadataServiceURLParamsMetadataCustomizer) GetPriority() int { } func (m *metadataServiceURLParamsMetadataCustomizer) Customize(instance registry.ServiceInstance) { - url, err := metadata.GlobalMetadataService.GetMetadataServiceURL() + url, err := metadata.GetMetadataService().GetMetadataServiceURL() if err != nil { logger.Errorf("get metadata service url is error, %v", err) return diff --git a/registry/servicediscovery/customizer/protocol_ports_metadata_customizer.go b/registry/servicediscovery/customizer/protocol_ports_metadata_customizer.go index 75bf9039e9..9b883c0464 100644 --- a/registry/servicediscovery/customizer/protocol_ports_metadata_customizer.go +++ b/registry/servicediscovery/customizer/protocol_ports_metadata_customizer.go @@ -48,7 +48,7 @@ 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) { - list, err := metadata.GlobalMetadataService.GetExportedServiceURLs() + list, err := metadata.GetMetadataService().GetExportedServiceURLs() if err != nil { logger.Errorf("get metadata exported service url is error, %v", err) return diff --git a/registry/servicediscovery/customizer/service_revision_customizer.go b/registry/servicediscovery/customizer/service_revision_customizer.go index b49e008913..e8e3eaa090 100644 --- a/registry/servicediscovery/customizer/service_revision_customizer.go +++ b/registry/servicediscovery/customizer/service_revision_customizer.go @@ -51,7 +51,7 @@ 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) { - urls, err := metadata.GlobalMetadataService.GetExportedServiceURLs() + urls, err := metadata.GetMetadataService().GetExportedServiceURLs() if err != nil { logger.Errorf("get metadata service url is error, %v", err) return @@ -72,7 +72,7 @@ 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) { - urls, err := metadata.GlobalMetadataService.GetSubscribedURLs() + urls, err := metadata.GetMetadataService().GetSubscribedURLs() if err != nil { logger.Errorf("get metadata subscribed url is error, %v", err) return diff --git a/registry/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go index a6585a0089..71764fef76 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -37,7 +37,7 @@ import ( "dubbo.apache.org/dubbo-go/v3/common/extension" "dubbo.apache.org/dubbo-go/v3/metadata/info" "dubbo.apache.org/dubbo-go/v3/metadata/mapping" - "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" + reportInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" "dubbo.apache.org/dubbo-go/v3/metrics" metricsMetadata "dubbo.apache.org/dubbo-go/v3/metrics/metadata" metricsRegistry "dubbo.apache.org/dubbo-go/v3/metrics/registry" @@ -59,7 +59,8 @@ type serviceDiscoveryRegistry struct { lock sync.RWMutex url *common.URL serviceDiscovery registry.ServiceDiscovery - serviceMeta *ServiceMeta + metadataInfo *info.MetadataInfo + instance registry.ServiceInstance serviceNameMapping mapping.ServiceNameMapping serviceListeners map[string]registry.ServiceInstancesChangedListener serviceMappingListeners map[string]mapping.MappingListener @@ -73,7 +74,7 @@ func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { return &serviceDiscoveryRegistry{ url: url, serviceDiscovery: serviceDiscovery, - serviceMeta: NewServiceMeta(), + metadataInfo: info.NewMetadataInfo(), serviceNameMapping: extension.GetGlobalServiceNameMapping(), serviceListeners: make(map[string]registry.ServiceInstancesChangedListener), // cache for mapping listener @@ -82,36 +83,53 @@ func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { } func (s *serviceDiscoveryRegistry) RegisterService() error { - serviceInstance := s.serviceMeta.createInstance() - // consumer has not host and port, so it will not register service - if serviceInstance.GetHost() != "" && serviceInstance.GetPort() != 0 { - meta := s.serviceMeta.metadataInfo + s.instance = createInstance(s.metadataInfo) + // consumer has no host and port, so it will not register service + if s.instance.GetHost() != "" && s.instance.GetPort() != 0 { + meta := s.metadataInfo meta.CalAndGetRevision() - metadataReport := instance.GetMetadataReport() + metadataReport := reportInstance.GetMetadataReport() if metadataReport != nil { err := metadataReport.PublishAppMetadata(meta.App, meta.Revision, meta) if err != nil { return err } } - return s.serviceDiscovery.Register(serviceInstance) + return s.serviceDiscovery.Register(s.instance) } return nil } +func createInstance(meta *info.MetadataInfo) registry.ServiceInstance { + metadata := make(map[string]string, 8) + metadata[constant.MetadataStorageTypePropertyName] = reportInstance.GetMetadataType() + instance := ®istry.DefaultServiceInstance{ + ServiceName: meta.App, + Enable: true, + Healthy: true, + Metadata: metadata, + ServiceMetadata: meta, + } + + for _, cus := range extension.GetCustomizers() { + cus.Customize(instance) + } + return instance +} + func (s *serviceDiscoveryRegistry) UnRegisterService() error { - return s.serviceDiscovery.Unregister(s.serviceMeta.instance) + return s.serviceDiscovery.Unregister(s.instance) } func (s *serviceDiscoveryRegistry) GetLocalMetadata() *info.MetadataInfo { - return s.serviceMeta.metadataInfo + return s.metadataInfo } func (s *serviceDiscoveryRegistry) UnRegister(url *common.URL) error { if !shouldRegister(url) { return nil } - s.serviceMeta.GetLocalMetadata().RemoveService(url) + s.GetLocalMetadata().RemoveService(url) return nil } @@ -119,7 +137,7 @@ func (s *serviceDiscoveryRegistry) UnSubscribe(url *common.URL, listener registr if !shouldSubscribe(url) { return nil } - s.serviceMeta.GetLocalMetadata().RemoveSubscribeURL(url) + s.GetLocalMetadata().RemoveSubscribeURL(url) services := s.getServices(url, nil) if services == nil { return nil @@ -177,7 +195,7 @@ func (s *serviceDiscoveryRegistry) Register(url *common.URL) error { return nil } common.HandleRegisterIPAndPort(url) - s.serviceMeta.GetLocalMetadata().AddService(url) + s.GetLocalMetadata().AddService(url) metrics.Publish(metricsRegistry.NewServerRegisterEvent(true, time.Now())) return s.serviceNameMapping.Map(url) } @@ -196,7 +214,7 @@ func (s *serviceDiscoveryRegistry) Subscribe(url *common.URL, notify registry.No logger.Infof("Service %s is set to not subscribe to instances.", url.ServiceKey()) return nil } - s.serviceMeta.GetLocalMetadata().AddSubscribeURL(url) + s.GetLocalMetadata().AddSubscribeURL(url) mappingListener := NewMappingListener(s.url, url, parseServices(url.GetParam(constant.ProvidedBy, "")), notify) services := s.getServices(url, mappingListener) if services.Empty() { diff --git a/registry/servicediscovery/service_instances_changed_listener_impl.go b/registry/servicediscovery/service_instances_changed_listener_impl.go index 955b6ff720..a9f174e196 100644 --- a/registry/servicediscovery/service_instances_changed_listener_impl.go +++ b/registry/servicediscovery/service_instances_changed_listener_impl.go @@ -33,6 +33,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/client" "dubbo.apache.org/dubbo-go/v3/metadata/info" "dubbo.apache.org/dubbo-go/v3/registry" "dubbo.apache.org/dubbo-go/v3/registry/servicediscovery/store" diff --git a/registry/servicediscovery/service_metadata.go b/registry/servicediscovery/service_metadata.go deleted file mode 100644 index 17bc20346e..0000000000 --- a/registry/servicediscovery/service_metadata.go +++ /dev/null @@ -1,76 +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 servicediscovery - -import ( - "dubbo.apache.org/dubbo-go/v3/common/constant" - "dubbo.apache.org/dubbo-go/v3/common/extension" - "dubbo.apache.org/dubbo-go/v3/metadata/info" - metadataInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" - _ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo" - _ "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory" - "dubbo.apache.org/dubbo-go/v3/registry" -) - -type ServiceMeta struct { - metadataInfo *info.MetadataInfo - instance registry.ServiceInstance -} - -func NewServiceMeta() *ServiceMeta { - return &ServiceMeta{metadataInfo: info.NewMetadataInfo()} -} - -func (sd *ServiceMeta) GetLocalMetadata() *info.MetadataInfo { - return sd.metadataInfo -} - -func (sd *ServiceMeta) createInstance() registry.ServiceInstance { - metadata := make(map[string]string, 8) - metadata[constant.MetadataStorageTypePropertyName] = metadataInstance.GetMetadataType() - instance := ®istry.DefaultServiceInstance{ - ServiceName: sd.metadataInfo.App, - Enable: true, - Healthy: true, - Metadata: metadata, - ServiceMetadata: sd.metadataInfo, - } - - for _, cus := range extension.GetCustomizers() { - cus.Customize(instance) - } - sd.instance = instance - return instance -} - -//func (sd *ServiceMeta) calOrUpdateInstanceRevision(instance registry.ServiceInstance) bool { -// oldRevision := getRevision(instance) -// newRevision := instance.GetServiceMetadata().CalAndGetRevision() -// if oldRevision != newRevision { -// instance.GetMetadata()[constant.ExportedServicesRevisionPropertyName] = newRevision -// return true -// } -// return false -//} - -//func getRevision(instance registry.ServiceInstance) string { -// if instance.GetServiceMetadata() != nil && instance.GetServiceMetadata().Revision != "" { -// return instance.GetServiceMetadata().Revision -// } -// return instance.GetMetadata()[constant.ExportedServicesRevisionPropertyName] -//} diff --git a/server/action.go b/server/action.go index d66224e20a..21645cd91a 100644 --- a/server/action.go +++ b/server/action.go @@ -415,21 +415,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) From 5eadaae21b98bc52a8825cf0fa4d43063febf115 Mon Sep 17 00:00:00 2001 From: foghost Date: Mon, 11 Dec 2023 14:08:26 +0800 Subject: [PATCH 10/26] refactor metadata exporter --- imports/imports.go | 1 - metadata/exporter/service_exporter.go | 77 ------------ metadata/exporter/service_exporter_test.go | 140 --------------------- metadata/metadata.go | 27 ++-- metadata/metadata_service.go | 72 +++++++++-- 5 files changed, 67 insertions(+), 250 deletions(-) delete mode 100644 metadata/exporter/service_exporter.go delete mode 100644 metadata/exporter/service_exporter_test.go diff --git a/imports/imports.go b/imports/imports.go index cf1296ec74..1a9fa18254 100644 --- a/imports/imports.go +++ b/imports/imports.go @@ -59,7 +59,6 @@ import ( _ "dubbo.apache.org/dubbo-go/v3/filter/tps/limiter" _ "dubbo.apache.org/dubbo-go/v3/filter/tps/strategy" _ "dubbo.apache.org/dubbo-go/v3/filter/tracing" - _ "dubbo.apache.org/dubbo-go/v3/metadata/exporter" _ "dubbo.apache.org/dubbo-go/v3/metadata/mapping/metadata" _ "dubbo.apache.org/dubbo-go/v3/metadata/report/etcd" _ "dubbo.apache.org/dubbo-go/v3/metadata/report/nacos" diff --git a/metadata/exporter/service_exporter.go b/metadata/exporter/service_exporter.go deleted file mode 100644 index fe7cc88ae1..0000000000 --- a/metadata/exporter/service_exporter.go +++ /dev/null @@ -1,77 +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 ( - "github.com/dubbogo/gost/log/logger" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common/constant" - "dubbo.apache.org/dubbo-go/v3/config" - "dubbo.apache.org/dubbo-go/v3/metadata" - _ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo" - _ "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory" -) - -func init() { - metadata.SetExporterFactory(NewMetadataServiceExporter) -} - -// MetadataServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface -type MetadataServiceExporter struct { - app, metadataType string - ServiceConfig *config.ServiceConfig - service metadata.MetadataService -} - -func NewMetadataServiceExporter(app, metadataType string, service metadata.MetadataService) metadata.ServiceExporter { - return &MetadataServiceExporter{app: app, metadataType: metadataType, service: service} -} - -// Export will export the metadataService -func (exporter *MetadataServiceExporter) Export() error { - version, _ := exporter.service.Version() - exporter.ServiceConfig = config.NewServiceConfigBuilder(). - SetServiceID(constant.SimpleMetadataServiceName). - SetProtocolIDs(constant.DefaultProtocol). - AddRCProtocol(constant.DefaultProtocol, config.NewProtocolConfigBuilder(). - SetName(constant.DefaultProtocol). - Build()). - SetRegistryIDs("N/A"). - SetInterface(constant.MetadataServiceName). - SetGroup(exporter.app). - SetVersion(version). - SetProxyFactoryKey(constant.DefaultKey). - SetMetadataType(exporter.metadataType). - Build() - exporter.ServiceConfig.Implement(exporter.service) - err := exporter.ServiceConfig.Export() - if err != nil { - return err - } - url := exporter.ServiceConfig.GetExportedUrls()[0] - exporter.service.SetMetadataServiceURL(url) - logger.Infof("[Metadata Service] The MetadataService exports urls : %v ", exporter.ServiceConfig.GetExportedUrls()) - return nil -} - -// UnExport will unExport the metadataService -func (exporter *MetadataServiceExporter) UnExport() { - exporter.ServiceConfig.Unexport() -} diff --git a/metadata/exporter/service_exporter_test.go b/metadata/exporter/service_exporter_test.go deleted file mode 100644 index 4bce389929..0000000000 --- a/metadata/exporter/service_exporter_test.go +++ /dev/null @@ -1,140 +0,0 @@ -package exporter - -import ( - "reflect" - "testing" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/config" - "dubbo.apache.org/dubbo-go/v3/metadata" - "dubbo.apache.org/dubbo-go/v3/metadata/info" - _ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo" - _ "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory" -) - -func TestMetadataServiceExporter_Export(t *testing.T) { - type fields struct { - app string - metadataType string - ServiceConfig *config.ServiceConfig - service metadata.MetadataService - } - tests := []struct { - name string - fields fields - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - exporter := &MetadataServiceExporter{ - app: tt.fields.app, - metadataType: tt.fields.metadataType, - ServiceConfig: tt.fields.ServiceConfig, - service: tt.fields.service, - } - if err := exporter.Export(); (err != nil) != tt.wantErr { - t.Errorf("Export() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestMetadataServiceExporter_UnExport(t *testing.T) { - type fields struct { - app string - metadataType string - ServiceConfig *config.ServiceConfig - service metadata.MetadataService - } - tests := []struct { - name string - fields fields - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - exporter := &MetadataServiceExporter{ - app: tt.fields.app, - metadataType: tt.fields.metadataType, - ServiceConfig: tt.fields.ServiceConfig, - service: tt.fields.service, - } - exporter.UnExport() - }) - } -} - -func TestNewMetadataServiceExporter(t *testing.T) { - type args struct { - app string - metadataType string - service metadata.MetadataService - } - tests := []struct { - name string - args args - want metadata.ServiceExporter - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := NewMetadataServiceExporter(tt.args.app, tt.args.metadataType, tt.args.service); !reflect.DeepEqual(got, tt.want) { - t.Errorf("NewMetadataServiceExporter() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestExporter(t *testing.T) { - err := NewMetadataServiceExporter("test", "local", &Service{}).Export() - if err != nil { - panic(err) - } - select {} -} - -type Service struct { -} - -func (s Service) GetExportedURLs(serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) { - //TODO implement me - panic("implement me") -} - -func (s Service) GetExportedServiceURLs() ([]*common.URL, error) { - //TODO implement me - panic("implement me") -} - -func (s Service) GetSubscribedURLs() ([]*common.URL, error) { - //TODO implement me - panic("implement me") -} - -func (s Service) Version() (string, error) { - return "1.0.0", nil -} - -func (s Service) GetMetadataInfo(revision string) (*info.MetadataInfo, error) { - meta := info.NewMetadataInfo() - meta.App = "test" - meta.Revision = "1" - //meta.Services = make(map[string]*info.ServiceInfo) - //meta.Services["1"] = info.NewServiceInfo("test", "group", "1.0.0", "dubbo", "", make(map[string]string)) - return meta, nil -} - -func (s Service) GetMetadataServiceURL() (*common.URL, error) { - //TODO implement me - panic("implement me") -} - -func (s Service) SetMetadataServiceURL(url *common.URL) { - -} diff --git a/metadata/metadata.go b/metadata/metadata.go index eb57bd3cc0..b71cf20153 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -33,29 +33,18 @@ import ( var ( metadataService MetadataService = &DefaultMetadataService{} exportOnce sync.Once - factory ExporterFactory ) -// ExporterFactory to create ServiceExporter, avoid cycle import -type ExporterFactory func(app, metadataType string, service MetadataService) ServiceExporter - -func SetExporterFactory(f ExporterFactory) { - factory = f -} - func ExportMetadataService(app, metadataType string) { - if factory != nil { - exportOnce.Do(func() { - if metadataType != constant.RemoteMetadataStorageType { - err := factory(app, metadataType, metadataService).Export() - if err != nil { - logger.Errorf("export metadata service failed, got error %#v", err) - } + exportOnce.Do(func() { + if metadataType != constant.RemoteMetadataStorageType { + exporter := &ServiceExporter{app: app, metadataType: metadataType, service: metadataService} + err := exporter.Export() + if err != nil { + logger.Errorf("export metadata service failed, got error %#v", err) } - }) - } else { - logger.Warn("no metadata service exporter found, MetadataService will not be Exported") - } + } + }) } func GetMetadataService() MetadataService { diff --git a/metadata/metadata_service.go b/metadata/metadata_service.go index 5e9cd4fc55..0c7d6dfdee 100644 --- a/metadata/metadata_service.go +++ b/metadata/metadata_service.go @@ -17,8 +17,16 @@ package metadata +import ( + "fmt" + "strings" +) + import ( "github.com/dubbogo/gost/log/logger" + gxnet "github.com/dubbogo/gost/net" + + perrors "github.com/pkg/errors" ) import ( @@ -26,6 +34,7 @@ import ( "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" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -35,6 +44,8 @@ const ( allServiceInterfaces = "*" ) +var metadataUrl *common.URL + // MetadataService is used to define meta data related behaviors // usually the implementation should be singleton type MetadataService interface { @@ -49,22 +60,10 @@ type MetadataService interface { 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) -} - -type ServiceExporter interface { - Export() error - UnExport() } // DefaultMetadataService is store and query the metadata info in memory when each service registry type DefaultMetadataService struct { - url *common.URL -} - -func (mts *DefaultMetadataService) SetMetadataServiceURL(url *common.URL) { - mts.url = url } // GetExportedURLs get all exported urls @@ -131,7 +130,7 @@ func (mts *DefaultMetadataService) Version() (string, error) { // GetMetadataServiceURL get url of MetadataService func (mts *DefaultMetadataService) GetMetadataServiceURL() (*common.URL, error) { - return mts.url, nil + return metadataUrl, nil } func (mts *DefaultMetadataService) GetSubscribedURLs() ([]*common.URL, error) { @@ -149,3 +148,50 @@ func (mts *DefaultMetadataService) MethodMapper() map[string]string { "GetMetadataInfo": "getMetadataInfo", } } + +// ServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface +type ServiceExporter struct { + app, metadataType string + service MetadataService + protocolExporter protocol.Exporter +} + +// Export will export the metadataService +func (exporter *ServiceExporter) Export() error { + version, _ := exporter.service.Version() + tcp, err := gxnet.ListenOnTCPRandomPort("") + if err != nil { + panic(perrors.New(fmt.Sprintf("Get tcp port error, err is {%v}", err))) + } + ivkURL := common.NewURLWithOptions( + common.WithPath(constant.MetadataServiceName), + common.WithProtocol(constant.DefaultProtocol), + common.WithPort(strings.Split(tcp.Addr().String(), ":")[1]), + common.WithParamsValue(constant.GroupKey, exporter.app), + 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, exporter.metadataType), + ) + methods, err := common.ServiceMap.Register(ivkURL.Interface(), ivkURL.Protocol, ivkURL.Group(), ivkURL.Version(), exporter.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) + exporter.protocolExporter = extension.GetProtocol(ivkURL.Protocol).Export(invoker) + metadataUrl = exporter.protocolExporter.GetInvoker().GetURL() + logger.Infof("[Metadata Service] The MetadataService exports urls : %v ", exporter.protocolExporter.GetInvoker().GetURL()) + return nil +} + +// UnExport will unExport the metadataService +func (exporter *ServiceExporter) UnExport() { + exporter.protocolExporter.UnExport() +} From 2ac1b8ebed317e900dd8544935cfe08a6f27b7fa Mon Sep 17 00:00:00 2001 From: foghost Date: Mon, 11 Dec 2023 14:19:11 +0800 Subject: [PATCH 11/26] mv metadata client to metadata dir --- metadata/{client => }/client.go | 10 +++++----- metadata/{client => }/client_test.go | 2 +- .../service_instances_changed_listener_impl.go | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) rename metadata/{client => }/client.go (94%) rename metadata/{client => }/client_test.go (99%) diff --git a/metadata/client/client.go b/metadata/client.go similarity index 94% rename from metadata/client/client.go rename to metadata/client.go index d0b8b5d28e..d1dd34aa72 100644 --- a/metadata/client/client.go +++ b/metadata/client.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package client +package metadata import ( "context" @@ -52,7 +52,7 @@ func GetMetadataFromRpc(revision string, instance registry.ServiceInstance) (*in return service.GetMetadataInfo(ctx, revision) } -type metadataService struct { +type remoteMetadataService struct { //GetExportedURLs func(context context.Context, serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) `dubbo:"getExportedURLs"` GetMetadataInfo func(context context.Context, revision string) (*info.MetadataInfo, error) `dubbo:"getMetadataInfo"` //GetMetadataServiceURL func(context context.Context) (*common.URL, error) @@ -60,14 +60,14 @@ type metadataService struct { //Version func(context context.Context) (string, error) } -func createRpcClient(instance registry.ServiceInstance) (*metadataService, func()) { +func createRpcClient(instance registry.ServiceInstance) (*remoteMetadataService, func()) { params := getMetadataServiceUrlParams(instance.GetMetadata()[constant.MetadataServiceURLParamsPropertyName]) url := buildMetadataServiceURL(instance.GetServiceName(), instance.GetHost(), params) return createRpcClientByUrl(url) } -func createRpcClientByUrl(url *common.URL) (*metadataService, func()) { - rpcService := &metadataService{} +func createRpcClientByUrl(url *common.URL) (*remoteMetadataService, func()) { + rpcService := &remoteMetadataService{} invoker := extension.GetProtocol(constant.Dubbo).Refer(url) proxy := extension.GetProxyFactory("").GetProxy(invoker, url) proxy.Implement(rpcService) diff --git a/metadata/client/client_test.go b/metadata/client_test.go similarity index 99% rename from metadata/client/client_test.go rename to metadata/client_test.go index 740230eab7..e950c9dc34 100644 --- a/metadata/client/client_test.go +++ b/metadata/client_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package client +package metadata import ( "reflect" diff --git a/registry/servicediscovery/service_instances_changed_listener_impl.go b/registry/servicediscovery/service_instances_changed_listener_impl.go index a9f174e196..b091102f98 100644 --- a/registry/servicediscovery/service_instances_changed_listener_impl.go +++ b/registry/servicediscovery/service_instances_changed_listener_impl.go @@ -33,7 +33,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/client" + "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" @@ -241,12 +241,12 @@ func GetMetadataInfo(app string, instance registry.ServiceInstance, revision str metadataStorageType = instance.GetMetadata()[constant.MetadataStorageTypePropertyName] } if metadataStorageType == constant.RemoteMetadataStorageType { - metadataInfo, err = client.GetMetadataFromMetadataReport(revision, instance) + metadataInfo, err = metadata.GetMetadataFromMetadataReport(revision, instance) if err != nil { return nil, err } } else { - metadataInfo, err = client.GetMetadataFromRpc(revision, instance) + metadataInfo, err = metadata.GetMetadataFromRpc(revision, instance) if err != nil { return nil, err } From 6b2fd9d976140fad4986f7f01dc00febad030645 Mon Sep 17 00:00:00 2001 From: foghost Date: Mon, 11 Dec 2023 17:13:19 +0800 Subject: [PATCH 12/26] mv metadata info to metadata module add app tag to service instance --- common/constant/key.go | 2 + config/registry_config.go | 5 +- config/service_config.go | 3 + global/registry_config.go | 2 +- metadata/info/metadata_info.go | 9 +- metadata/metadata.go | 60 ++++++++++++ metadata/metadata_service.go | 92 +++++-------------- registry/service_discovery.go | 6 -- .../service_discovery_registry.go | 35 +++---- .../store/cache_manager_test.go | 8 +- server/action.go | 1 + 11 files changed, 122 insertions(+), 101 deletions(-) diff --git a/common/constant/key.go b/common/constant/key.go index e3dfa87606..8368cd9e41 100644 --- a/common/constant/key.go +++ b/common/constant/key.go @@ -164,6 +164,7 @@ const ( // registry keys const ( RegistryKey = "registry" + RegistryIdKey = "registry.id" RegistryProtocol = "registry" ServiceRegistryProtocol = "service-discovery-registry" RegistryRoleKey = "registry.role" @@ -186,6 +187,7 @@ const ( const ( ApplicationKey = "application" + ApplicationTagKey = "application.tag" OrganizationKey = "organization" NameKey = "name" ModuleKey = "module" diff --git a/config/registry_config.go b/config/registry_config.go index 62feccd415..752d64e53a 100644 --- a/config/registry_config.go +++ b/config/registry_config.go @@ -53,7 +53,7 @@ type RegistryConfig struct { Zone string `yaml:"zone" json:"zone,omitempty" property:"zone"` // The region where the registry belongs, usually used to isolate traffics Weight int64 `yaml:"weight" json:"weight,omitempty" property:"weight"` // Affects traffic distribution among registriesConfig, useful when subscribe to multiple registriesConfig Take effect only when no preferred registry is specified. Params map[string]string `yaml:"params" json:"params,omitempty" property:"params"` - RegistryType string `yaml:"registry-type"` + RegistryType string `default:"all" yaml:"registry-type"` UseAsMetaReport bool `default:"true" yaml:"use-as-meta-report" json:"use-as-meta-report,omitempty" property:"use-as-meta-report"` UseAsConfigCenter bool `default:"true" yaml:"use-as-config-center" json:"use-as-config-center,omitempty" property:"use-as-config-center"` } @@ -229,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/service_config.go b/config/service_config.go index 2fc8a4f5a5..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), diff --git a/global/registry_config.go b/global/registry_config.go index 783dccd2b0..089fa249c8 100644 --- a/global/registry_config.go +++ b/global/registry_config.go @@ -36,7 +36,7 @@ type RegistryConfig struct { Zone string `yaml:"zone" json:"zone,omitempty" property:"zone"` // The region where the registry belongs, usually used to isolate traffics Weight int64 `yaml:"weight" json:"weight,omitempty" property:"weight"` // Affects traffic distribution among registriesConfig, useful when subscribe to multiple registriesConfig Take effect only when no preferred registry is specified. Params map[string]string `yaml:"params" json:"params,omitempty" property:"params"` - RegistryType string `yaml:"registry-type"` + RegistryType string `default:"all" yaml:"registry-type"` UseAsMetaReport bool `default:"true" yaml:"use-as-meta-report" json:"use-as-meta-report,omitempty" property:"use-as-meta-report"` UseAsConfigCenter bool `default:"true" yaml:"use-as-config-center" json:"use-as-config-center,omitempty" property:"use-as-config-center"` } diff --git a/metadata/info/metadata_info.go b/metadata/info/metadata_info.go index c11764aa53..99f2585c86 100644 --- a/metadata/info/metadata_info.go +++ b/metadata/info/metadata_info.go @@ -58,15 +58,16 @@ var IncludeKeys = gxset.NewSet( // MetadataInfo the metadata information of instance type MetadataInfo struct { - App string `json:"app,omitempty" hessian:"app"` - Revision string `json:"revision,omitempty" hessian:"revision"` + 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() *MetadataInfo { - return NewMetadataInfoWithParams("", "", make(map[string]*ServiceInfo)) +func NewMetadataInfo(app, tag string) *MetadataInfo { + return NewMetadataInfoWithParams(app, "", make(map[string]*ServiceInfo)) } func NewMetadataInfoWithParams(app string, revision string, services map[string]*ServiceInfo) *MetadataInfo { diff --git a/metadata/metadata.go b/metadata/metadata.go index b71cf20153..356d445654 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -19,15 +19,23 @@ package metadata import ( + "fmt" + "strings" "sync" ) 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/protocol" ) var ( @@ -50,3 +58,55 @@ func ExportMetadataService(app, metadataType string) { func GetMetadataService() MetadataService { return metadataService } + +// ServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface +type ServiceExporter struct { + app, metadataType string + service MetadataService + protocolExporter protocol.Exporter +} + +// Export will export the metadataService +func (exporter *ServiceExporter) Export() error { + version, _ := exporter.service.Version() + 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))) + } + ivkURL := common.NewURLWithOptions( + common.WithPath(constant.MetadataServiceName), + common.WithProtocol(constant.DefaultProtocol), + common.WithPort(strings.Split(tcp.Addr().String(), ":")[1]), + common.WithParamsValue(constant.GroupKey, exporter.app), + 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, exporter.metadataType), + common.WithParamsValue(constant.SideKey, constant.SideProvider), + ) + methods, err := common.ServiceMap.Register(ivkURL.Interface(), ivkURL.Protocol, ivkURL.Group(), ivkURL.Version(), exporter.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) + exporter.protocolExporter = extension.GetProtocol(ivkURL.Protocol).Export(invoker) + metadataUrl = exporter.protocolExporter.GetInvoker().GetURL() + logger.Infof("[Metadata Service] The MetadataService exports urls : %v ", exporter.protocolExporter.GetInvoker().GetURL()) + return nil +} + +// UnExport will unExport the metadataService +func (exporter *ServiceExporter) UnExport() { + exporter.protocolExporter.UnExport() +} diff --git a/metadata/metadata_service.go b/metadata/metadata_service.go index 0c7d6dfdee..411792f75a 100644 --- a/metadata/metadata_service.go +++ b/metadata/metadata_service.go @@ -17,25 +17,14 @@ package metadata -import ( - "fmt" - "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" - "dubbo.apache.org/dubbo-go/v3/registry" ) // version will be used by Version func @@ -44,7 +33,10 @@ const ( allServiceInterfaces = "*" ) -var metadataUrl *common.URL +var ( + appMetadataInfoMap = make(map[string]*info.MetadataInfo) + metadataUrl *common.URL +) // MetadataService is used to define meta data related behaviors // usually the implementation should be singleton @@ -92,7 +84,7 @@ func (mts *DefaultMetadataService) GetMetadataInfo(revision string) (*info.Metad if revision == "" { return nil, nil } - for _, metadataInfo := range mts.getAllMetadata() { + for _, metadataInfo := range appMetadataInfoMap { if metadataInfo.Revision == revision { return metadataInfo, nil } @@ -101,23 +93,10 @@ func (mts *DefaultMetadataService) GetMetadataInfo(revision string) (*info.Metad return nil, nil } -func (mts *DefaultMetadataService) getAllMetadata() []*info.MetadataInfo { - sds := make([]*info.MetadataInfo, 0) - p := extension.GetProtocol(constant.RegistryProtocol) - if factory, ok := p.(registry.RegistryFactory); ok { - for _, v := range factory.GetRegistries() { - if sd, ok := v.(registry.ServiceDiscoveryRegistry); ok { - sds = append(sds, sd.GetLocalMetadata()) - } - } - } - return sds -} - // GetExportedServiceURLs get exported service urls func (mts *DefaultMetadataService) GetExportedServiceURLs() ([]*common.URL, error) { urls := make([]*common.URL, 0) - for _, metadataInfo := range mts.getAllMetadata() { + for _, metadataInfo := range appMetadataInfoMap { urls = append(urls, metadataInfo.GetExportedServiceURLs()...) } return urls, nil @@ -135,7 +114,7 @@ func (mts *DefaultMetadataService) GetMetadataServiceURL() (*common.URL, error) func (mts *DefaultMetadataService) GetSubscribedURLs() ([]*common.URL, error) { urls := make([]*common.URL, 0) - for _, metadataInfo := range mts.getAllMetadata() { + for _, metadataInfo := range appMetadataInfoMap { urls = append(urls, metadataInfo.GetSubscribedURLs()...) } return urls, nil @@ -149,49 +128,26 @@ func (mts *DefaultMetadataService) MethodMapper() map[string]string { } } -// ServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface -type ServiceExporter struct { - app, metadataType string - service MetadataService - protocolExporter protocol.Exporter +func GetMetadataInfo(id string) *info.MetadataInfo { + return appMetadataInfoMap[id] } -// Export will export the metadataService -func (exporter *ServiceExporter) Export() error { - version, _ := exporter.service.Version() - tcp, err := gxnet.ListenOnTCPRandomPort("") - if err != nil { - panic(perrors.New(fmt.Sprintf("Get tcp port error, err is {%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, ""), + ) } - ivkURL := common.NewURLWithOptions( - common.WithPath(constant.MetadataServiceName), - common.WithProtocol(constant.DefaultProtocol), - common.WithPort(strings.Split(tcp.Addr().String(), ":")[1]), - common.WithParamsValue(constant.GroupKey, exporter.app), - 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, exporter.metadataType), - ) - methods, err := common.ServiceMap.Register(ivkURL.Interface(), ivkURL.Protocol, ivkURL.Group(), ivkURL.Version(), exporter.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) - exporter.protocolExporter = extension.GetProtocol(ivkURL.Protocol).Export(invoker) - metadataUrl = exporter.protocolExporter.GetInvoker().GetURL() - logger.Infof("[Metadata Service] The MetadataService exports urls : %v ", exporter.protocolExporter.GetInvoker().GetURL()) - return nil + appMetadataInfoMap[registryId].AddService(url) } -// UnExport will unExport the metadataService -func (exporter *ServiceExporter) UnExport() { - exporter.protocolExporter.UnExport() +func AddSubscribeURL(registryId string, url *common.URL) { + if _, exist := appMetadataInfoMap[registryId]; !exist { + appMetadataInfoMap[registryId] = info.NewMetadataInfo( + url.GetParam(constant.ApplicationKey, ""), + url.GetParam(constant.ApplicationTagKey, ""), + ) + } + appMetadataInfoMap[registryId].AddSubscribeURL(url) } diff --git a/registry/service_discovery.go b/registry/service_discovery.go index 5626cbdf08..13641f1360 100644 --- a/registry/service_discovery.go +++ b/registry/service_discovery.go @@ -26,10 +26,6 @@ import ( gxpage "github.com/dubbogo/gost/hash/page" ) -import ( - "dubbo.apache.org/dubbo-go/v3/metadata/info" -) - const DefaultPageSize = 100 // ServiceDiscovery is the interface that wraps common operations of Service Discovery @@ -80,6 +76,4 @@ type ServiceDiscoveryRegistry interface { RegisterService() error // UnRegisterService unregister service instance UnRegisterService() error - // GetLocalMetadata get local metadata info - GetLocalMetadata() *info.MetadataInfo } diff --git a/registry/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go index 71764fef76..998fa84dd2 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -35,6 +35,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" "dubbo.apache.org/dubbo-go/v3/metadata/info" "dubbo.apache.org/dubbo-go/v3/metadata/mapping" reportInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" @@ -59,7 +60,6 @@ type serviceDiscoveryRegistry struct { lock sync.RWMutex url *common.URL serviceDiscovery registry.ServiceDiscovery - metadataInfo *info.MetadataInfo instance registry.ServiceInstance serviceNameMapping mapping.ServiceNameMapping serviceListeners map[string]registry.ServiceInstancesChangedListener @@ -74,7 +74,6 @@ func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { return &serviceDiscoveryRegistry{ url: url, serviceDiscovery: serviceDiscovery, - metadataInfo: info.NewMetadataInfo(), serviceNameMapping: extension.GetGlobalServiceNameMapping(), serviceListeners: make(map[string]registry.ServiceInstancesChangedListener), // cache for mapping listener @@ -83,14 +82,17 @@ func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { } func (s *serviceDiscoveryRegistry) RegisterService() error { - s.instance = createInstance(s.metadataInfo) + 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 { - meta := s.metadataInfo - meta.CalAndGetRevision() + metaInfo.CalAndGetRevision() metadataReport := reportInstance.GetMetadataReport() if metadataReport != nil { - err := metadataReport.PublishAppMetadata(meta.App, meta.Revision, meta) + err := metadataReport.PublishAppMetadata(metaInfo.App, metaInfo.Revision, metaInfo) if err != nil { return err } @@ -101,14 +103,15 @@ func (s *serviceDiscoveryRegistry) RegisterService() error { } func createInstance(meta *info.MetadataInfo) registry.ServiceInstance { - metadata := make(map[string]string, 8) - metadata[constant.MetadataStorageTypePropertyName] = reportInstance.GetMetadataType() + params := make(map[string]string, 8) + params[constant.MetadataStorageTypePropertyName] = reportInstance.GetMetadataType() instance := ®istry.DefaultServiceInstance{ ServiceName: meta.App, Enable: true, Healthy: true, - Metadata: metadata, + Metadata: params, ServiceMetadata: meta, + Tag: meta.Tag, } for _, cus := range extension.GetCustomizers() { @@ -121,15 +124,10 @@ func (s *serviceDiscoveryRegistry) UnRegisterService() error { return s.serviceDiscovery.Unregister(s.instance) } -func (s *serviceDiscoveryRegistry) GetLocalMetadata() *info.MetadataInfo { - return s.metadataInfo -} - func (s *serviceDiscoveryRegistry) UnRegister(url *common.URL) error { if !shouldRegister(url) { return nil } - s.GetLocalMetadata().RemoveService(url) return nil } @@ -137,7 +135,6 @@ func (s *serviceDiscoveryRegistry) UnSubscribe(url *common.URL, listener registr if !shouldSubscribe(url) { return nil } - s.GetLocalMetadata().RemoveSubscribeURL(url) services := s.getServices(url, nil) if services == nil { return nil @@ -195,7 +192,9 @@ func (s *serviceDiscoveryRegistry) Register(url *common.URL) error { return nil } common.HandleRegisterIPAndPort(url) - s.GetLocalMetadata().AddService(url) + 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) } @@ -214,7 +213,9 @@ func (s *serviceDiscoveryRegistry) Subscribe(url *common.URL, notify registry.No logger.Infof("Service %s is set to not subscribe to instances.", url.ServiceKey()) return nil } - s.GetLocalMetadata().AddSubscribeURL(url) + if id, exist := s.url.GetNonDefaultParam(constant.RegistryIdKey); exist { + metadata.AddSubscribeURL(id, url) + } mappingListener := NewMappingListener(s.url, url, parseServices(url.GetParam(constant.ProvidedBy, "")), notify) services := s.getServices(url, mappingListener) if services.Empty() { diff --git a/registry/servicediscovery/store/cache_manager_test.go b/registry/servicediscovery/store/cache_manager_test.go index f3125f7c6b..36541d7ea9 100644 --- a/registry/servicediscovery/store/cache_manager_test.go +++ b/registry/servicediscovery/store/cache_manager_test.go @@ -91,10 +91,10 @@ func TestMetaInfoCacheManager(t *testing.T) { 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 := info.NewMetadataInfo("1", "1", serverInfo) - metadataInfo2 := info.NewMetadataInfo("2", "2", serverInfo) - metadataInfo3 := info.NewMetadataInfo("3", "3", serverInfo) - metadataInfo4 := info.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/server/action.go b/server/action.go index 21645cd91a..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 From 157e9c2af5198ce47ed21764b90e4f0671263a06 Mon Sep 17 00:00:00 2001 From: foghost Date: Tue, 2 Jan 2024 12:17:07 +0800 Subject: [PATCH 13/26] associate metadataReport with registry --- .../mapping/metadata/service_name_mapping.go | 6 +- metadata/metadata.go | 75 +++++----------- metadata/metadata_service.go | 87 ++++++++++++++----- metadata/report/nacos/report.go | 5 ++ .../protocol_ports_metadata_customizer.go | 2 +- .../service_discovery_registry.go | 8 +- 6 files changed, 99 insertions(+), 84 deletions(-) diff --git a/metadata/mapping/metadata/service_name_mapping.go b/metadata/mapping/metadata/service_name_mapping.go index 5f1beca479..8d70ee8e24 100644 --- a/metadata/mapping/metadata/service_name_mapping.go +++ b/metadata/mapping/metadata/service_name_mapping.go @@ -66,7 +66,7 @@ type ServiceNameMapping struct { func (d *ServiceNameMapping) Map(url *common.URL) error { serviceInterface := url.GetParam(constant.InterfaceKey, "") appName := url.GetParam(constant.ApplicationKey, "") - metadataReport := instance.GetMetadataReportByRegistry(url.GetParam(constant.RegistryKey, "")) + metadataReport := instance.GetMetadataReportByRegistry(url.GetParam(constant.RegistryIdKey, "")) if metadataReport == nil { logger.Warn("get metadata report instance is nil") } else { @@ -87,7 +87,7 @@ func (d *ServiceNameMapping) Map(url *common.URL) error { // Get will return the application-level services. If not found, the empty set will be returned. func (d *ServiceNameMapping) Get(url *common.URL, listener mapping.MappingListener) (*gxset.HashSet, error) { serviceInterface := url.GetParam(constant.InterfaceKey, "") - metadataReport := instance.GetMetadataReport() + metadataReport := instance.GetMetadataReportByRegistry(url.GetParam(constant.RegistryIdKey, "")) if metadataReport == nil { return gxset.NewSet(), nil } @@ -96,7 +96,7 @@ func (d *ServiceNameMapping) Get(url *common.URL, listener mapping.MappingListen func (d *ServiceNameMapping) Remove(url *common.URL) error { serviceInterface := url.GetParam(constant.InterfaceKey, "") - metadataReport := instance.GetMetadataReport() + metadataReport := instance.GetMetadataReportByRegistry(url.GetParam(constant.RegistryIdKey, "")) if metadataReport == nil { return nil } diff --git a/metadata/metadata.go b/metadata/metadata.go index 356d445654..54e99ea868 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -19,28 +19,23 @@ package metadata import ( - "fmt" - "strings" "sync" ) 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/protocol" + "dubbo.apache.org/dubbo-go/v3/metadata/info" ) var ( - metadataService MetadataService = &DefaultMetadataService{} - exportOnce sync.Once + metadataService MetadataService = &DefaultMetadataService{} + exportOnce sync.Once + appMetadataInfoMap = make(map[string]*info.MetadataInfo) ) func ExportMetadataService(app, metadataType string) { @@ -59,54 +54,26 @@ func GetMetadataService() MetadataService { return metadataService } -// ServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface -type ServiceExporter struct { - app, metadataType string - service MetadataService - protocolExporter protocol.Exporter +func GetMetadataInfo(registryId string) *info.MetadataInfo { + return appMetadataInfoMap[registryId] } -// Export will export the metadataService -func (exporter *ServiceExporter) Export() error { - version, _ := exporter.service.Version() - 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))) +func AddService(registryId string, url *common.URL) { + if _, exist := appMetadataInfoMap[registryId]; !exist { + appMetadataInfoMap[registryId] = info.NewMetadataInfo( + url.GetParam(constant.ApplicationKey, ""), + url.GetParam(constant.ApplicationTagKey, ""), + ) } - ivkURL := common.NewURLWithOptions( - common.WithPath(constant.MetadataServiceName), - common.WithProtocol(constant.DefaultProtocol), - common.WithPort(strings.Split(tcp.Addr().String(), ":")[1]), - common.WithParamsValue(constant.GroupKey, exporter.app), - 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, exporter.metadataType), - common.WithParamsValue(constant.SideKey, constant.SideProvider), - ) - methods, err := common.ServiceMap.Register(ivkURL.Interface(), ivkURL.Protocol, ivkURL.Group(), ivkURL.Version(), exporter.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) - exporter.protocolExporter = extension.GetProtocol(ivkURL.Protocol).Export(invoker) - metadataUrl = exporter.protocolExporter.GetInvoker().GetURL() - logger.Infof("[Metadata Service] The MetadataService exports urls : %v ", exporter.protocolExporter.GetInvoker().GetURL()) - return nil + appMetadataInfoMap[registryId].AddService(url) } -// UnExport will unExport the metadataService -func (exporter *ServiceExporter) UnExport() { - exporter.protocolExporter.UnExport() +func AddSubscribeURL(registryId string, url *common.URL) { + if _, exist := appMetadataInfoMap[registryId]; !exist { + appMetadataInfoMap[registryId] = info.NewMetadataInfo( + url.GetParam(constant.ApplicationKey, ""), + url.GetParam(constant.ApplicationTagKey, ""), + ) + } + appMetadataInfoMap[registryId].AddSubscribeURL(url) } diff --git a/metadata/metadata_service.go b/metadata/metadata_service.go index 411792f75a..b814532624 100644 --- a/metadata/metadata_service.go +++ b/metadata/metadata_service.go @@ -17,14 +17,24 @@ package metadata +import ( + "fmt" + "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 @@ -33,11 +43,6 @@ const ( allServiceInterfaces = "*" ) -var ( - appMetadataInfoMap = make(map[string]*info.MetadataInfo) - metadataUrl *common.URL -) - // MetadataService is used to define meta data related behaviors // usually the implementation should be singleton type MetadataService interface { @@ -52,10 +57,17 @@ type MetadataService interface { 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 @@ -109,7 +121,7 @@ func (mts *DefaultMetadataService) Version() (string, error) { // GetMetadataServiceURL get url of MetadataService func (mts *DefaultMetadataService) GetMetadataServiceURL() (*common.URL, error) { - return metadataUrl, nil + return mts.metadataUrl, nil } func (mts *DefaultMetadataService) GetSubscribedURLs() ([]*common.URL, error) { @@ -128,26 +140,55 @@ func (mts *DefaultMetadataService) MethodMapper() map[string]string { } } -func GetMetadataInfo(id string) *info.MetadataInfo { - return appMetadataInfoMap[id] +// ServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface +type ServiceExporter struct { + app, metadataType string + service MetadataService + protocolExporter protocol.Exporter } -func AddService(registryId string, url *common.URL) { - if _, exist := appMetadataInfoMap[registryId]; !exist { - appMetadataInfoMap[registryId] = info.NewMetadataInfo( - url.GetParam(constant.ApplicationKey, ""), - url.GetParam(constant.ApplicationTagKey, ""), - ) +// Export will export the metadataService +func (e *ServiceExporter) Export() error { + version, _ := e.service.Version() + 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))) } - appMetadataInfoMap[registryId].AddService(url) + port := strings.Split(tcp.Addr().String(), ":")[1] + ivkURL := common.NewURLWithOptions( + common.WithPath(constant.MetadataServiceName), + common.WithProtocol(constant.DefaultProtocol), + common.WithPort(port), + common.WithParamsValue(constant.GroupKey, e.app), + 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.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 AddSubscribeURL(registryId string, url *common.URL) { - if _, exist := appMetadataInfoMap[registryId]; !exist { - appMetadataInfoMap[registryId] = info.NewMetadataInfo( - url.GetParam(constant.ApplicationKey, ""), - url.GetParam(constant.ApplicationTagKey, ""), - ) - } - appMetadataInfoMap[registryId].AddSubscribeURL(url) +// UnExport will unExport the metadataService +func (e *ServiceExporter) UnExport() { + e.protocolExporter.UnExport() } diff --git a/metadata/report/nacos/report.go b/metadata/report/nacos/report.go index f4563ca862..e7f64c3e8e 100644 --- a/metadata/report/nacos/report.go +++ b/metadata/report/nacos/report.go @@ -61,6 +61,11 @@ func (n *nacosMetadataReport) GetAppMetadata(application, revision string) (*inf data, err := n.getConfig(vo.ConfigParam{ 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 } diff --git a/registry/servicediscovery/customizer/protocol_ports_metadata_customizer.go b/registry/servicediscovery/customizer/protocol_ports_metadata_customizer.go index 9b883c0464..a5d757ebfa 100644 --- a/registry/servicediscovery/customizer/protocol_ports_metadata_customizer.go +++ b/registry/servicediscovery/customizer/protocol_ports_metadata_customizer.go @@ -54,7 +54,7 @@ func (p *ProtocolPortsMetadataCustomizer) Customize(instance registry.ServiceIns return } if list == nil || len(list) == 0 { - logger.Warn("Could not find exported urls") + // client side return } // 4 is enough... we don't have many protocol diff --git a/registry/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go index 998fa84dd2..089ff3c813 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -38,6 +38,7 @@ import ( "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/report" reportInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" "dubbo.apache.org/dubbo-go/v3/metrics" metricsMetadata "dubbo.apache.org/dubbo-go/v3/metrics/metadata" @@ -62,6 +63,7 @@ type serviceDiscoveryRegistry struct { serviceDiscovery registry.ServiceDiscovery instance registry.ServiceInstance serviceNameMapping mapping.ServiceNameMapping + metadataReport report.MetadataReport serviceListeners map[string]registry.ServiceInstancesChangedListener serviceMappingListeners map[string]mapping.MappingListener } @@ -75,6 +77,7 @@ func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { url: url, serviceDiscovery: serviceDiscovery, serviceNameMapping: extension.GetGlobalServiceNameMapping(), + metadataReport: reportInstance.GetMetadataReportByRegistry(url.GetParam(constant.RegistryIdKey, "")), serviceListeners: make(map[string]registry.ServiceInstancesChangedListener), // cache for mapping listener serviceMappingListeners: make(map[string]mapping.MappingListener), @@ -90,9 +93,8 @@ func (s *serviceDiscoveryRegistry) RegisterService() error { // consumer has no host and port, so it will not register service if s.instance.GetHost() != "" && s.instance.GetPort() != 0 { metaInfo.CalAndGetRevision() - metadataReport := reportInstance.GetMetadataReport() - if metadataReport != nil { - err := metadataReport.PublishAppMetadata(metaInfo.App, metaInfo.Revision, metaInfo) + if s.metadataReport != nil { + err := s.metadataReport.PublishAppMetadata(metaInfo.App, metaInfo.Revision, metaInfo) if err != nil { return err } From 8ab355779e0653e83e0398db8b87c8d6a62ba325 Mon Sep 17 00:00:00 2001 From: foghost Date: Thu, 4 Jan 2024 19:30:13 +0800 Subject: [PATCH 14/26] unit test not done --- metadata/client_test.go | 186 ---------------------------------------- 1 file changed, 186 deletions(-) delete mode 100644 metadata/client_test.go diff --git a/metadata/client_test.go b/metadata/client_test.go deleted file mode 100644 index e950c9dc34..0000000000 --- a/metadata/client_test.go +++ /dev/null @@ -1,186 +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 metadata - -import ( - "reflect" - "testing" -) - -import ( - "dubbo.apache.org/dubbo-go/v3/common" - "dubbo.apache.org/dubbo-go/v3/metadata/info" - "dubbo.apache.org/dubbo-go/v3/registry" -) - -func TestCreateRpcClientByUrl(t *testing.T) { - //url, err := common.NewURL("dubbo://10.253.77.49:61086?group=dubbo.io&interface=org.apache.dubbo.metadata.MetadataService&side=consumer&version=1.0.0") - //service, destory := createRpcClientByUrl(url) - //defer destory() - //meta, err := service.GetMetadataInfo(context.Background(), "4647984502") - //if err != nil { - // logger.Error(err) - //} - //fmt.Println(meta) -} - -func TestGetMetadataFromMetadataReport(t *testing.T) { - type args struct { - revision string - instance registry.ServiceInstance - } - tests := []struct { - name string - args args - want *info.MetadataInfo - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetMetadataFromMetadataReport(tt.args.revision, tt.args.instance) - if (err != nil) != tt.wantErr { - t.Errorf("GetMetadataFromMetadataReport() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetMetadataFromMetadataReport() got = %v, want %v", got, tt.want) - } - }) - } -} - -func TestGetMetadataFromRpc(t *testing.T) { - type args struct { - revision string - instance registry.ServiceInstance - } - tests := []struct { - name string - args args - want *info.MetadataInfo - wantErr bool - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := GetMetadataFromRpc(tt.args.revision, tt.args.instance) - if (err != nil) != tt.wantErr { - t.Errorf("GetMetadataFromRpc() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("GetMetadataFromRpc() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_buildMetadataServiceURL(t *testing.T) { - type args struct { - serviceName string - host string - ps map[string]string - } - tests := []struct { - name string - args args - want *common.URL - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := buildMetadataServiceURL(tt.args.serviceName, tt.args.host, tt.args.ps); !reflect.DeepEqual(got, tt.want) { - t.Errorf("buildMetadataServiceURL() = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_createRpcClient(t *testing.T) { - type args struct { - instance registry.ServiceInstance - } - tests := []struct { - name string - args args - want *metadataService - want1 func() - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, got1 := createRpcClient(tt.args.instance) - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("createRpcClient() got = %v, want %v", got, tt.want) - } - if !reflect.DeepEqual(got1, tt.want1) { - t.Errorf("createRpcClient() got1 = %v, want %v", got1, tt.want1) - } - }) - } -} - -func Test_createRpcClientByUrl(t *testing.T) { - type args struct { - url *common.URL - } - tests := []struct { - name string - args args - want *metadataService - want1 func() - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, got1 := createRpcClientByUrl(tt.args.url) - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("createRpcClientByUrl() got = %v, want %v", got, tt.want) - } - if !reflect.DeepEqual(got1, tt.want1) { - t.Errorf("createRpcClientByUrl() got1 = %v, want %v", got1, tt.want1) - } - }) - } -} - -func Test_getMetadataServiceUrlParams(t *testing.T) { - type args struct { - jsonStr string - } - tests := []struct { - name string - args args - want map[string]string - }{ - // TODO: Add test cases. - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := getMetadataServiceUrlParams(tt.args.jsonStr); !reflect.DeepEqual(got, tt.want) { - t.Errorf("getMetadataServiceUrlParams() = %v, want %v", got, tt.want) - } - }) - } -} From a179a3d092bafd86a0cb4cfa995f26d765417cf9 Mon Sep 17 00:00:00 2001 From: foghost Date: Fri, 5 Jan 2024 12:25:45 +0800 Subject: [PATCH 15/26] update warn log --- metadata/report/instance/metadata_report.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metadata/report/instance/metadata_report.go b/metadata/report/instance/metadata_report.go index 4d58970d3e..d7b34582ec 100644 --- a/metadata/report/instance/metadata_report.go +++ b/metadata/report/instance/metadata_report.go @@ -54,7 +54,7 @@ func Init(urls []*common.URL, metaType string) { key := url.GetParam(constant.RegistryKey, constant.DefaultKey) instances[key] = &DelegateMetadataReport{instance: fac.CreateMetadataReport(url)} } else { - logger.Warnf("metadata report instance is not found, url: %s", url.String()) + logger.Warnf("metadata report factory is not found, url: %s", url.String()) } } } From 1a11af186dfafba05536eb3f062d583e58e8f58b Mon Sep 17 00:00:00 2001 From: foghost Date: Fri, 5 Jan 2024 17:31:57 +0800 Subject: [PATCH 16/26] unit test tmp fix --- metadata/metadata.go | 8 ++++++++ registry/exposed_tmp/exposed.go | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/metadata/metadata.go b/metadata/metadata.go index 54e99ea868..67ec931041 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -42,6 +42,14 @@ func ExportMetadataService(app, metadataType string) { exportOnce.Do(func() { if metadataType != constant.RemoteMetadataStorageType { exporter := &ServiceExporter{app: app, metadataType: metadataType, service: metadataService} + 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() if err != nil { logger.Errorf("export metadata service failed, got error %#v", err) diff --git a/registry/exposed_tmp/exposed.go b/registry/exposed_tmp/exposed.go index c8ca0310c3..997c60e4f8 100644 --- a/registry/exposed_tmp/exposed.go +++ b/registry/exposed_tmp/exposed.go @@ -21,10 +21,19 @@ import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/common/extension" "dubbo.apache.org/dubbo-go/v3/registry" + "github.com/dubbogo/gost/log/logger" ) // RegisterServiceInstance register service instance 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) + } + }() protocol := extension.GetProtocol(constant.RegistryKey) if rf, ok := protocol.(registry.RegistryFactory); ok { for _, r := range rf.GetRegistries() { From c29963b54471da7efe254e2bc6cf786be93c7079 Mon Sep 17 00:00:00 2001 From: foghost Date: Fri, 5 Jan 2024 18:33:17 +0800 Subject: [PATCH 17/26] fix integrate_test --- metadata/info/metadata_info.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/metadata/info/metadata_info.go b/metadata/info/metadata_info.go index 99f2585c86..471a97d81e 100644 --- a/metadata/info/metadata_info.go +++ b/metadata/info/metadata_info.go @@ -263,7 +263,10 @@ func (si *ServiceInfo) GetParams() url.Values { if len(methodNames) == 0 { return v } - methods := gxset.NewSet(strings.Split(si.Params[constant.MethodsKey], ",")) + 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]) { From 25d8711de42dc50d0435720865ef01eda83de288 Mon Sep 17 00:00:00 2001 From: foghost Date: Mon, 8 Jan 2024 12:42:20 +0800 Subject: [PATCH 18/26] fix some logic --- config/registry_config.go | 2 +- metadata/client.go | 22 ++++++----- metadata/info/metadata_info_test.go | 13 +++---- .../mapping/metadata/service_name_mapping.go | 37 +++++++++++-------- metadata/report/instance/metadata_report.go | 9 ++++- metadata/report/report.go | 2 +- registry/exposed_tmp/exposed.go | 5 ++- .../metadata_service_url_params_customizer.go | 8 +--- .../service_discovery_registry.go | 5 ++- server/server.go | 3 +- 10 files changed, 60 insertions(+), 46 deletions(-) diff --git a/config/registry_config.go b/config/registry_config.go index 752d64e53a..887bf706bb 100644 --- a/config/registry_config.go +++ b/config/registry_config.go @@ -53,7 +53,7 @@ type RegistryConfig struct { Zone string `yaml:"zone" json:"zone,omitempty" property:"zone"` // The region where the registry belongs, usually used to isolate traffics Weight int64 `yaml:"weight" json:"weight,omitempty" property:"weight"` // Affects traffic distribution among registriesConfig, useful when subscribe to multiple registriesConfig Take effect only when no preferred registry is specified. Params map[string]string `yaml:"params" json:"params,omitempty" property:"params"` - RegistryType string `default:"all" yaml:"registry-type"` + RegistryType string `yaml:"registry-type"` UseAsMetaReport bool `default:"true" yaml:"use-as-meta-report" json:"use-as-meta-report,omitempty" property:"use-as-meta-report"` UseAsConfigCenter bool `default:"true" yaml:"use-as-config-center" json:"use-as-config-center,omitempty" property:"use-as-config-center"` } diff --git a/metadata/client.go b/metadata/client.go index d1dd34aa72..ed13b35655 100644 --- a/metadata/client.go +++ b/metadata/client.go @@ -25,6 +25,8 @@ import ( import ( "github.com/dubbogo/gost/log/logger" + + perrors "github.com/pkg/errors" ) import ( @@ -45,7 +47,10 @@ func GetMetadataFromMetadataReport(revision string, instance registry.ServiceIns } func GetMetadataFromRpc(revision string, instance registry.ServiceInstance) (*info.MetadataInfo, error) { - service, destroy := createRpcClient(instance) + service, destroy, err := createRpcClient(instance) + if err != nil { + return nil, err + } ctx, cancel := context.WithTimeout(context.Background(), time.Duration(metadataProxyDefaultTimeout)) defer cancel() defer destroy() @@ -53,28 +58,27 @@ func GetMetadataFromRpc(revision string, instance registry.ServiceInstance) (*in } type remoteMetadataService struct { - //GetExportedURLs func(context context.Context, serviceInterface string, group string, version string, protocol string) ([]*common.URL, error) `dubbo:"getExportedURLs"` GetMetadataInfo func(context context.Context, revision string) (*info.MetadataInfo, error) `dubbo:"getMetadataInfo"` - //GetMetadataServiceURL func(context context.Context) (*common.URL, error) - //GetSubscribedURLs func(context context.Context) ([]*common.URL, error) - //Version func(context context.Context) (string, error) } -func createRpcClient(instance registry.ServiceInstance) (*remoteMetadataService, func()) { +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()) { +func createRpcClientByUrl(url *common.URL) (*remoteMetadataService, func(), error) { rpcService := &remoteMetadataService{} invoker := extension.GetProtocol(constant.Dubbo).Refer(url) - proxy := extension.GetProxyFactory("").GetProxy(invoker, 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 + return rpcService, destroy, nil } // buildMetadataServiceURL will use standard format to build the metadata service url. diff --git a/metadata/info/metadata_info_test.go b/metadata/info/metadata_info_test.go index de7039cfdf..41889bfdc6 100644 --- a/metadata/info/metadata_info_test.go +++ b/metadata/info/metadata_info_test.go @@ -18,6 +18,7 @@ package info import ( + "encoding/json" "testing" ) @@ -59,12 +60,10 @@ func TestHessian(t *testing.T) { metadataInfo.Services["1"] = NewServiceInfo("dubbo.io", "default", "1.0.0", "dubbo", "", make(map[string]string)) e := hessian.NewEncoder() err := e.Encode(metadataInfo) - if err != nil { - panic(err) - } + assert.Nil(t, err) obj, err := hessian.NewDecoder(e.Buffer()).Decode() - if err != nil { - panic(err) - } - t.Log(obj) + assert.Nil(t, err) + objJson, _ := json.Marshal(obj) + metaJson, _ := json.Marshal(metadataInfo) + assert.Equal(t, objJson, metaJson) } diff --git a/metadata/mapping/metadata/service_name_mapping.go b/metadata/mapping/metadata/service_name_mapping.go index 8d70ee8e24..3e8d533d53 100644 --- a/metadata/mapping/metadata/service_name_mapping.go +++ b/metadata/mapping/metadata/service_name_mapping.go @@ -24,6 +24,8 @@ import ( import ( gxset "github.com/dubbogo/gost/container/set" "github.com/dubbogo/gost/log/logger" + + perrors "github.com/pkg/errors" ) import ( @@ -36,7 +38,6 @@ import ( const ( DefaultGroup = "mapping" - slash = "/" retryTimes = 10 ) @@ -66,19 +67,23 @@ type ServiceNameMapping struct { func (d *ServiceNameMapping) Map(url *common.URL) error { serviceInterface := url.GetParam(constant.InterfaceKey, "") appName := url.GetParam(constant.ApplicationKey, "") - metadataReport := instance.GetMetadataReportByRegistry(url.GetParam(constant.RegistryIdKey, "")) - if metadataReport == nil { - logger.Warn("get metadata report instance is nil") + // url is the service url,not the registry url,this url has no registry id info,can not got where to write mapping,so write all + // if the mapping can hold a report instance, it can write once + metadataReports := instance.GetMetadataReports() + if len(metadataReports) == 0 { + return perrors.New("can not registering mapping to remote cause no metadata report instance found") } else { - var err error - for i := 0; i < retryTimes; i++ { - err = metadataReport.RegisterServiceAppMapping(serviceInterface, DefaultGroup, appName) - if err == nil { - break + for _, metadataReport := range metadataReports { + var err error + for i := 0; i < retryTimes; i++ { + err = metadataReport.RegisterServiceAppMapping(serviceInterface, DefaultGroup, appName) + if err == nil { + break + } + } + if err != nil { + logger.Errorf("Failed registering mapping to remote, &v", err) } - } - if err != nil { - logger.Errorf("Failed registering mapping to remote, &v", err) } } return nil @@ -87,18 +92,18 @@ func (d *ServiceNameMapping) Map(url *common.URL) error { // Get will return the application-level services. If not found, the empty set will be returned. func (d *ServiceNameMapping) Get(url *common.URL, listener mapping.MappingListener) (*gxset.HashSet, error) { serviceInterface := url.GetParam(constant.InterfaceKey, "") - metadataReport := instance.GetMetadataReportByRegistry(url.GetParam(constant.RegistryIdKey, "")) + metadataReport := instance.GetMetadataReport() if metadataReport == nil { - return gxset.NewSet(), 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 *ServiceNameMapping) Remove(url *common.URL) error { serviceInterface := url.GetParam(constant.InterfaceKey, "") - metadataReport := instance.GetMetadataReportByRegistry(url.GetParam(constant.RegistryIdKey, "")) + metadataReport := instance.GetMetadataReport() if metadataReport == nil { - return nil + return perrors.New("can not remove mapping in remote cause no metadata report instance found") } return metadataReport.RemoveServiceAppMappingListener(serviceInterface, DefaultGroup) } diff --git a/metadata/report/instance/metadata_report.go b/metadata/report/instance/metadata_report.go index d7b34582ec..a576773cf5 100644 --- a/metadata/report/instance/metadata_report.go +++ b/metadata/report/instance/metadata_report.go @@ -75,10 +75,15 @@ func GetMetadataReportByRegistry(registry string) report.MetadataReport { if r, ok := instances[registry]; ok { return r } + return GetMetadataReport() +} + +func GetMetadataReports() []report.MetadataReport { + reports := make([]report.MetadataReport, 0) for _, r := range instances { - return r + reports = append(reports, r) } - return nil + return reports } func GetMetadataType() string { diff --git a/metadata/report/report.go b/metadata/report/report.go index 392a8d8caa..edabce0a60 100644 --- a/metadata/report/report.go +++ b/metadata/report/report.go @@ -31,7 +31,7 @@ type MetadataReport interface { // GetAppMetadata get metadata info from report GetAppMetadata(application, revision string) (*info.MetadataInfo, error) - // PublishAppMetadata publish metadata info to reportss + // 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 diff --git a/registry/exposed_tmp/exposed.go b/registry/exposed_tmp/exposed.go index 997c60e4f8..3fded551f9 100644 --- a/registry/exposed_tmp/exposed.go +++ b/registry/exposed_tmp/exposed.go @@ -17,11 +17,14 @@ package exposed_tmp +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/registry" - "github.com/dubbogo/gost/log/logger" ) // RegisterServiceInstance register service instance diff --git a/registry/servicediscovery/customizer/metadata_service_url_params_customizer.go b/registry/servicediscovery/customizer/metadata_service_url_params_customizer.go index 690858f4d9..adddd0c640 100644 --- a/registry/servicediscovery/customizer/metadata_service_url_params_customizer.go +++ b/registry/servicediscovery/customizer/metadata_service_url_params_customizer.go @@ -56,13 +56,9 @@ func (m *metadataServiceURLParamsMetadataCustomizer) GetPriority() int { } func (m *metadataServiceURLParamsMetadataCustomizer) Customize(instance registry.ServiceInstance) { - url, err := metadata.GetMetadataService().GetMetadataServiceURL() - if err != nil { - logger.Errorf("get metadata service url is error, %v", err) - return - } + url, _ := metadata.GetMetadataService().GetMetadataServiceURL() if url == nil { - logger.Errorf("the metadata service url is nil") + // when metadata service is not exported the url will be nil,this is because metadata type is remote return } ps := m.convertToParams(url) diff --git a/registry/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go index 089ff3c813..9817099a87 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -93,7 +93,10 @@ func (s *serviceDiscoveryRegistry) RegisterService() error { // consumer has no host and port, so it will not register service if s.instance.GetHost() != "" && s.instance.GetPort() != 0 { metaInfo.CalAndGetRevision() - if s.metadataReport != nil { + if reportInstance.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 diff --git a/server/server.go b/server/server.go index 5e6f73818b..fe02485e47 100644 --- a/server/server.go +++ b/server/server.go @@ -188,8 +188,7 @@ func (s *Server) Serve() error { return err } metadata.ExportMetadataService(s.cfg.Application.Name, s.cfg.Application.MetadataType) - err := exposed_tmp.RegisterServiceInstance() - if err != nil { + if err := exposed_tmp.RegisterServiceInstance(); err != nil { return err } select {} From e3ef66eb691cf31969946de61ce28fb4195ea05d Mon Sep 17 00:00:00 2001 From: foghost Date: Tue, 9 Jan 2024 12:14:36 +0800 Subject: [PATCH 19/26] change RegistryType default value --- global/registry_config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global/registry_config.go b/global/registry_config.go index 089fa249c8..783dccd2b0 100644 --- a/global/registry_config.go +++ b/global/registry_config.go @@ -36,7 +36,7 @@ type RegistryConfig struct { Zone string `yaml:"zone" json:"zone,omitempty" property:"zone"` // The region where the registry belongs, usually used to isolate traffics Weight int64 `yaml:"weight" json:"weight,omitempty" property:"weight"` // Affects traffic distribution among registriesConfig, useful when subscribe to multiple registriesConfig Take effect only when no preferred registry is specified. Params map[string]string `yaml:"params" json:"params,omitempty" property:"params"` - RegistryType string `default:"all" yaml:"registry-type"` + RegistryType string `yaml:"registry-type"` UseAsMetaReport bool `default:"true" yaml:"use-as-meta-report" json:"use-as-meta-report,omitempty" property:"use-as-meta-report"` UseAsConfigCenter bool `default:"true" yaml:"use-as-config-center" json:"use-as-config-center,omitempty" property:"use-as-config-center"` } From 88a537d1944a0f00f071c34437c464647d809e4b Mon Sep 17 00:00:00 2001 From: foghost Date: Mon, 15 Jan 2024 12:13:22 +0800 Subject: [PATCH 20/26] code review fix --- .../mapping/metadata/service_name_mapping.go | 20 +++++++--------- metadata/report/instance/metadata_report.go | 24 +++++++------------ registry/exposed_tmp/exposed.go | 3 +-- 3 files changed, 19 insertions(+), 28 deletions(-) diff --git a/metadata/mapping/metadata/service_name_mapping.go b/metadata/mapping/metadata/service_name_mapping.go index 3e8d533d53..a7120839ac 100644 --- a/metadata/mapping/metadata/service_name_mapping.go +++ b/metadata/mapping/metadata/service_name_mapping.go @@ -72,19 +72,17 @@ func (d *ServiceNameMapping) Map(url *common.URL) error { metadataReports := instance.GetMetadataReports() if len(metadataReports) == 0 { return perrors.New("can not registering mapping to remote cause no metadata report instance found") - } else { - for _, metadataReport := range metadataReports { - var err error - for i := 0; i < retryTimes; i++ { - err = metadataReport.RegisterServiceAppMapping(serviceInterface, DefaultGroup, appName) - if err == nil { - break - } - } - if err != nil { - logger.Errorf("Failed registering mapping to remote, &v", err) + } + 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 { + logger.Errorf("Failed registering mapping to remote, &v", err) + } } return nil } diff --git a/metadata/report/instance/metadata_report.go b/metadata/report/instance/metadata_report.go index a576773cf5..0c4024d21e 100644 --- a/metadata/report/instance/metadata_report.go +++ b/metadata/report/instance/metadata_report.go @@ -50,12 +50,12 @@ func Init(urls []*common.URL, metaType string) { if len(urls) != 0 { for _, url := range urls { fac := extension.GetMetadataReportFactory(url.Protocol) - if fac != nil { - key := url.GetParam(constant.RegistryKey, constant.DefaultKey) - instances[key] = &DelegateMetadataReport{instance: fac.CreateMetadataReport(url)} - } else { - logger.Warnf("metadata report factory is not found, url: %s", url.String()) + if fac == nil { + logger.Warnf("no metadata report factory of protocol %s found!", url.Protocol) + continue } + key := url.GetParam(constant.RegistryKey, constant.DefaultKey) + instances[key] = &DelegateMetadataReport{instance: fac.CreateMetadataReport(url)} } } }) @@ -79,9 +79,11 @@ func GetMetadataReportByRegistry(registry string) report.MetadataReport { } func GetMetadataReports() []report.MetadataReport { - reports := make([]report.MetadataReport, 0) + reports := make([]report.MetadataReport, len(instances)) + index := 0 for _, r := range instances { - reports = append(reports, r) + reports[index] = r + index++ } return reports } @@ -90,14 +92,6 @@ func GetMetadataType() string { return metadataType } -//func GetMetadataReports() []report.MetadataReport { -// reports := make([]report.MetadataReport, len(instances)) -// for _, metadataReport := range instances { -// reports = append(reports, metadataReport) -// } -// return reports -//} - // DelegateMetadataReport is a absolute delegate for DelegateMetadataReport type DelegateMetadataReport struct { instance report.MetadataReport diff --git a/registry/exposed_tmp/exposed.go b/registry/exposed_tmp/exposed.go index 3fded551f9..d4a1b77c45 100644 --- a/registry/exposed_tmp/exposed.go +++ b/registry/exposed_tmp/exposed.go @@ -41,8 +41,7 @@ func RegisterServiceInstance() error { if rf, ok := protocol.(registry.RegistryFactory); ok { for _, r := range rf.GetRegistries() { if sdr, ok := r.(registry.ServiceDiscoveryRegistry); ok { - err := sdr.RegisterService() - if err != nil { + if err := sdr.RegisterService(); err != nil { return err } } From 328eeebe2b6ea3f31946f7ea59e8ea0ed0f5668b Mon Sep 17 00:00:00 2001 From: foghost Date: Tue, 16 Jan 2024 12:13:06 +0800 Subject: [PATCH 21/26] change metadata to options style --- client/client.go | 45 +++++ ...ta_report_config.go => metadata_config.go} | 113 ++++++++++-- ...config_test.go => metadata_config_test.go} | 0 config/root_config.go | 8 +- global/application_config.go | 5 +- global/metadata_report_config.go | 2 - global/registry_config.go | 2 +- metadata/client.go | 3 +- .../mapping/metadata/service_name_mapping.go | 11 +- metadata/metadata.go | 31 +--- metadata/metadata_service.go | 36 ++-- metadata/options.go | 164 ++++++++++++++---- .../metadata_report.go => report_instance.go} | 48 ++--- options.go | 92 +++++++++- .../service_discovery_registry.go | 9 +- server/server.go | 80 ++++++++- 16 files changed, 500 insertions(+), 149 deletions(-) rename config/{metadata_report_config.go => metadata_config.go} (65%) rename config/{metadata_report_config_test.go => metadata_config_test.go} (100%) rename metadata/{report/instance/metadata_report.go => report_instance.go} (77%) diff --git a/client/client.go b/client/client.go index 4a9a722691..01ec989334 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), @@ -120,6 +126,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/config/metadata_report_config.go b/config/metadata_config.go similarity index 65% rename from config/metadata_report_config.go rename to config/metadata_config.go index 6445614264..f25b597758 100644 --- a/config/metadata_report_config.go +++ b/config/metadata_config.go @@ -18,7 +18,12 @@ package config import ( - "github.com/nacos-group/nacos-sdk-go/v2/common/logger" + "strconv" + "time" +) + +import ( + "github.com/dubbogo/gost/log/logger" perrors "github.com/pkg/errors" ) @@ -26,7 +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/report/instance" + "dubbo.apache.org/dubbo-go/v3/metadata" ) // MetadataReportConfig is app level configuration @@ -43,44 +48,114 @@ 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 if mc.Address != "" { - tmpUrl, err := mc.ToUrl() + // if metadata report config is avaible, then init metadata report instance + opts, err := mc.toReportOptions() if err != nil { - logger.Errorf("MetadataReport init failed, err: %v", err) return err } - // if metadata report config is avaible, then init metadata report instance - instance.Init([]*common.URL{tmpUrl}, mc.metadataType) - return nil + 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 - urls := make([]*common.URL, 0) for id, reg := range rc.Registries { if reg.UseAsMetaReport && isValid(reg.Address) { - if tmpUrl, err := reg.toMetadataReportUrl(); err == nil { - tmpUrl.AddParam(constant.RegistryKey, id) - urls = append(urls, tmpUrl) - } else { - logger.Warnf("use registry as metadata report failed, err: %v", err) + opts, err := registryToReportOptions(id, reg) + if err != nil { + return err + } + if err = opts.Init(); err != nil { + return err } } } - if len(urls) > 0 { - instance.Init(urls, mc.metadataType) - } else { - logger.Warnf("No metadata report config found, skip init metadata report instance.") - } } return nil } diff --git a/config/metadata_report_config_test.go b/config/metadata_config_test.go similarity index 100% rename from config/metadata_report_config_test.go rename to config/metadata_config_test.go diff --git a/config/root_config.go b/config/root_config.go index 1278374ef0..ca4a6bb4ca 100644 --- a/config/root_config.go +++ b/config/root_config.go @@ -33,7 +33,6 @@ import ( "dubbo.apache.org/dubbo-go/v3/common" "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/config_center" - "dubbo.apache.org/dubbo-go/v3/metadata" "dubbo.apache.org/dubbo-go/v3/registry/exposed_tmp" ) @@ -205,9 +204,10 @@ func (rc *RootConfig) Start() { gracefulShutdownInit() rc.Consumer.Load() rc.Provider.Load() - metadata.ExportMetadataService(rc.Application.Name, rc.Application.MetadataType) - err := exposed_tmp.RegisterServiceInstance() - if err != nil { + if err := initMetadata(rc); err != nil { + panic(err) + } + if err := exposed_tmp.RegisterServiceInstance(); err != nil { panic(err) } }) 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/metadata/client.go b/metadata/client.go index ed13b35655..6b9120e1ea 100644 --- a/metadata/client.go +++ b/metadata/client.go @@ -34,7 +34,6 @@ import ( "dubbo.apache.org/dubbo-go/v3/common/constant" "dubbo.apache.org/dubbo-go/v3/common/extension" "dubbo.apache.org/dubbo-go/v3/metadata/info" - reportInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" "dubbo.apache.org/dubbo-go/v3/registry" ) @@ -42,7 +41,7 @@ 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 := reportInstance.GetMetadataReport() + report := GetMetadataReport() return report.GetAppMetadata(instance.GetServiceName(), revision) } diff --git a/metadata/mapping/metadata/service_name_mapping.go b/metadata/mapping/metadata/service_name_mapping.go index a7120839ac..95e588ef91 100644 --- a/metadata/mapping/metadata/service_name_mapping.go +++ b/metadata/mapping/metadata/service_name_mapping.go @@ -32,8 +32,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" "dubbo.apache.org/dubbo-go/v3/metadata/mapping" - "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" ) const ( @@ -67,9 +67,9 @@ type ServiceNameMapping struct { func (d *ServiceNameMapping) Map(url *common.URL) error { serviceInterface := url.GetParam(constant.InterfaceKey, "") appName := url.GetParam(constant.ApplicationKey, "") - // url is the service url,not the registry url,this url has no registry id info,can not got where to write mapping,so write all + // 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 := instance.GetMetadataReports() + metadataReports := metadata.GetMetadataReports() if len(metadataReports) == 0 { return perrors.New("can not registering mapping to remote cause no metadata report instance found") } @@ -82,6 +82,7 @@ func (d *ServiceNameMapping) Map(url *common.URL) error { } if err != nil { logger.Errorf("Failed registering mapping to remote, &v", err) + return err } } return nil @@ -90,7 +91,7 @@ func (d *ServiceNameMapping) Map(url *common.URL) error { // Get will return the application-level services. If not found, the empty set will be returned. func (d *ServiceNameMapping) Get(url *common.URL, listener mapping.MappingListener) (*gxset.HashSet, error) { serviceInterface := url.GetParam(constant.InterfaceKey, "") - metadataReport := instance.GetMetadataReport() + metadataReport := metadata.GetMetadataReport() if metadataReport == nil { return nil, perrors.New("can not get mapping in remote cause no metadata report instance found") } @@ -99,7 +100,7 @@ func (d *ServiceNameMapping) Get(url *common.URL, listener mapping.MappingListen func (d *ServiceNameMapping) Remove(url *common.URL) error { serviceInterface := url.GetParam(constant.InterfaceKey, "") - metadataReport := instance.GetMetadataReport() + metadataReport := metadata.GetMetadataReport() if metadataReport == nil { return perrors.New("can not remove mapping in remote cause no metadata report instance found") } diff --git a/metadata/metadata.go b/metadata/metadata.go index 67ec931041..6fbbfcd8ca 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -18,14 +18,6 @@ // Package metadata collects and exposes information of all services for service discovery purpose. package metadata -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" @@ -34,30 +26,9 @@ import ( var ( metadataService MetadataService = &DefaultMetadataService{} - exportOnce sync.Once - appMetadataInfoMap = make(map[string]*info.MetadataInfo) + appMetadataInfoMap = make(map[string]*info.MetadataInfo) ) -func ExportMetadataService(app, metadataType string) { - exportOnce.Do(func() { - if metadataType != constant.RemoteMetadataStorageType { - exporter := &ServiceExporter{app: app, metadataType: metadataType, service: metadataService} - 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() - if err != nil { - logger.Errorf("export metadata service failed, got error %#v", err) - } - } - }) -} - func GetMetadataService() MetadataService { return metadataService } diff --git a/metadata/metadata_service.go b/metadata/metadata_service.go index b814532624..5a3591c54c 100644 --- a/metadata/metadata_service.go +++ b/metadata/metadata_service.go @@ -19,6 +19,7 @@ package metadata import ( "fmt" + "strconv" "strings" ) @@ -142,34 +143,31 @@ func (mts *DefaultMetadataService) MethodMapper() map[string]string { // ServiceExporter is the ConfigurableMetadataServiceExporter which implement MetadataServiceExporter interface type ServiceExporter struct { - app, metadataType string - service MetadataService - protocolExporter protocol.Exporter + opts *Options + service MetadataService + protocolExporter protocol.Exporter } // Export will export the metadataService func (e *ServiceExporter) Export() error { version, _ := e.service.Version() - tcp, err := gxnet.ListenOnTCPRandomPort("") - if err != nil { - panic(perrors.New(fmt.Sprintf("Get tcp port error, err is {%v}", err))) + var port string + if e.opts.port == 0 { + port = randomPort() + } else { + port = strconv.Itoa(e.opts.port) } - err = tcp.Close() - if err != nil { - panic(perrors.New(fmt.Sprintf("Close tcp port error, err is {%v}", err))) - } - port := strings.Split(tcp.Addr().String(), ":")[1] ivkURL := common.NewURLWithOptions( common.WithPath(constant.MetadataServiceName), common.WithProtocol(constant.DefaultProtocol), common.WithPort(port), - common.WithParamsValue(constant.GroupKey, e.app), + 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.metadataType), + 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) @@ -188,6 +186,18 @@ func (e *ServiceExporter) Export() error { 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..eead2828af 100644 --- a/metadata/options.go +++ b/metadata/options.go @@ -20,20 +20,35 @@ package metadata import ( "strconv" "strings" + "sync" "time" ) +import ( + "github.com/dubbogo/gost/log/logger" + + perrors "github.com/pkg/errors" +) + 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 +59,150 @@ 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 WithMetadataType(typ string) Option { + return func(options *Options) { + options.metadataType = typ + } +} + +func WithPort(port int) Option { + return func(options *Options) { + options.port = port + } +} + +type ReportOptions struct { + registryId string + *global.MetadataReportConfig +} + +func (opts *ReportOptions) Init() error { + fac := extension.GetMetadataReportFactory(opts.Protocol) + if fac == nil { + return perrors.Errorf("no metadata report factory of protocol %s found!", opts.Protocol) } + 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 WithNacos() Option { - return func(opts *Options) { - opts.Metadata.Protocol = constant.NacosKey +func defaultReportOptions() *ReportOptions { + return &ReportOptions{MetadataReportConfig: global.DefaultMetadataReportConfig()} +} + +func NewReportOptions(opts ...ReportOption) *ReportOptions { + reportOptions := defaultReportOptions() + for _, opt := range opts { + opt(reportOptions) } + return reportOptions } -func WithEtcdV3() Option { - return func(opts *Options) { - opts.Metadata.Protocol = constant.EtcdV3Key +type ReportOption func(*ReportOptions) + +func WithZookeeper() ReportOption { + return func(opts *ReportOptions) { + opts.Protocol = constant.ZookeeperKey } } -func WithMetadata(meta string) Option { - return func(opts *Options) { - opts.Metadata.Protocol = meta +func WithNacos() ReportOption { + return func(opts *ReportOptions) { + opts.Protocol = constant.NacosKey } } -func WithAddress(address string) Option { - return func(opts *Options) { +func WithEtcdV3() ReportOption { + return func(opts *ReportOptions) { + opts.Protocol = constant.EtcdV3Key + } +} + +func WithProtocol(meta string) ReportOption { + return func(opts *ReportOptions) { + opts.Protocol = meta + } +} + +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/instance/metadata_report.go b/metadata/report_instance.go similarity index 77% rename from metadata/report/instance/metadata_report.go rename to metadata/report_instance.go index 0c4024d21e..2238144f09 100644 --- a/metadata/report/instance/metadata_report.go +++ b/metadata/report_instance.go @@ -15,22 +15,22 @@ * limitations under the License. */ -package instance +package metadata import ( - "sync" + "strings" "time" ) import ( "github.com/dubbogo/gost/container/set" - "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/metadata/mapping" "dubbo.apache.org/dubbo-go/v3/metadata/report" @@ -39,26 +39,28 @@ import ( ) var ( - instances = make(map[string]report.MetadataReport) - once sync.Once - metadataType = constant.DefaultMetadataStorageType + instances = make(map[string]report.MetadataReport) ) -func Init(urls []*common.URL, metaType string) { - once.Do(func() { - metadataType = metaType - if len(urls) != 0 { - for _, url := range urls { - fac := extension.GetMetadataReportFactory(url.Protocol) - if fac == nil { - logger.Warnf("no metadata report factory of protocol %s found!", url.Protocol) - continue - } - key := url.GetParam(constant.RegistryKey, constant.DefaultKey) - instances[key] = &DelegateMetadataReport{instance: fac.CreateMetadataReport(url)} - } - } - }) +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 { @@ -89,7 +91,7 @@ func GetMetadataReports() []report.MetadataReport { } func GetMetadataType() string { - return metadataType + return metadataOptions.metadataType } // DelegateMetadataReport is a absolute delegate for DelegateMetadataReport 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/registry/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go index 9817099a87..a27f012dbf 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -39,7 +39,6 @@ import ( "dubbo.apache.org/dubbo-go/v3/metadata/info" "dubbo.apache.org/dubbo-go/v3/metadata/mapping" "dubbo.apache.org/dubbo-go/v3/metadata/report" - reportInstance "dubbo.apache.org/dubbo-go/v3/metadata/report/instance" "dubbo.apache.org/dubbo-go/v3/metrics" metricsMetadata "dubbo.apache.org/dubbo-go/v3/metrics/metadata" metricsRegistry "dubbo.apache.org/dubbo-go/v3/metrics/registry" @@ -77,7 +76,7 @@ func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) { url: url, serviceDiscovery: serviceDiscovery, serviceNameMapping: extension.GetGlobalServiceNameMapping(), - metadataReport: reportInstance.GetMetadataReportByRegistry(url.GetParam(constant.RegistryIdKey, "")), + metadataReport: metadata.GetMetadataReportByRegistry(url.GetParam(constant.RegistryIdKey, "")), serviceListeners: make(map[string]registry.ServiceInstancesChangedListener), // cache for mapping listener serviceMappingListeners: make(map[string]mapping.MappingListener), @@ -93,7 +92,7 @@ func (s *serviceDiscoveryRegistry) RegisterService() error { // consumer has no host and port, so it will not register service if s.instance.GetHost() != "" && s.instance.GetPort() != 0 { metaInfo.CalAndGetRevision() - if reportInstance.GetMetadataType() == constant.RemoteMetadataStorageType { + if metadata.GetMetadataType() == constant.RemoteMetadataStorageType { if s.metadataReport == nil { return perrors.New("can not publish app metadata cause report instance not found") } @@ -109,7 +108,7 @@ func (s *serviceDiscoveryRegistry) RegisterService() error { func createInstance(meta *info.MetadataInfo) registry.ServiceInstance { params := make(map[string]string, 8) - params[constant.MetadataStorageTypePropertyName] = reportInstance.GetMetadataType() + params[constant.MetadataStorageTypePropertyName] = metadata.GetMetadataType() instance := ®istry.DefaultServiceInstance{ ServiceName: meta.App, Enable: true, @@ -149,7 +148,7 @@ 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) // TODO check is right + err := s.serviceNameMapping.Remove(url) if err != nil { return err } diff --git a/server/server.go b/server/server.go index fe02485e47..84d7e29a65 100644 --- a/server/server.go +++ b/server/server.go @@ -21,7 +21,9 @@ package server import ( "context" "fmt" + "strconv" "sync" + "time" ) import ( @@ -32,6 +34,8 @@ 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" "dubbo.apache.org/dubbo-go/v3/registry/exposed_tmp" @@ -184,16 +188,90 @@ 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 } - metadata.ExportMetadataService(s.cfg.Application.Name, s.cfg.Application.MetadataType) + if err := exposed_tmp.RegisterServiceInstance(); err != nil { return err } select {} } +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 From d7097b139d0dea372db42e7818a32956d2450318 Mon Sep 17 00:00:00 2001 From: foghost Date: Tue, 16 Jan 2024 12:52:05 +0800 Subject: [PATCH 22/26] fix unit test --- config/application_config_test.go | 1 + config/custom_config_test.go | 1 + config/provider_config_test.go | 1 + .../config/provider/registry_application.yaml | 2 +- remoting/nacos/builder.go | 19 ------------------- 5 files changed, 4 insertions(+), 20 deletions(-) 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/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/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/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/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) From 5c6e8d762d89eb2c0676faa2ee3759d94ad89a69 Mon Sep 17 00:00:00 2001 From: foghost Date: Tue, 16 Jan 2024 19:16:21 +0800 Subject: [PATCH 23/26] fix integration test --- metadata/options.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/metadata/options.go b/metadata/options.go index eead2828af..8379f39ce7 100644 --- a/metadata/options.go +++ b/metadata/options.go @@ -26,8 +26,6 @@ import ( import ( "github.com/dubbogo/gost/log/logger" - - perrors "github.com/pkg/errors" ) import ( @@ -107,7 +105,8 @@ type ReportOptions struct { func (opts *ReportOptions) Init() error { fac := extension.GetMetadataReportFactory(opts.Protocol) if fac == nil { - return perrors.Errorf("no metadata report factory of protocol %s found!", opts.Protocol) + logger.Errorf("no metadata report factory of protocol %s found!", opts.Protocol) + return nil } url, err := toUrl(opts) if err != nil { From e461a39511193db7f77d2c588fb9977ca597f45b Mon Sep 17 00:00:00 2001 From: foghost Date: Mon, 22 Jan 2024 19:11:26 +0800 Subject: [PATCH 24/26] add doc for metadata report option WithAddress --- metadata/options.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/metadata/options.go b/metadata/options.go index 8379f39ce7..30e866abee 100644 --- a/metadata/options.go +++ b/metadata/options.go @@ -155,6 +155,8 @@ func WithProtocol(meta string) ReportOption { } } +// 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 { From b0c75a5ee9b2072e98451e98e756df87a98c097b Mon Sep 17 00:00:00 2001 From: finalt Date: Fri, 26 Jan 2024 12:45:19 +0800 Subject: [PATCH 25/26] modify README.md (#2585) --- cmd/protoc-gen-go-triple/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/protoc-gen-go-triple/README.md b/cmd/protoc-gen-go-triple/README.md index e04c7058fe..26655b82c6 100644 --- a/cmd/protoc-gen-go-triple/README.md +++ b/cmd/protoc-gen-go-triple/README.md @@ -40,10 +40,10 @@ Make sure to add the resulting binary to your system's PATH. To generate Triple code from your Protocol Buffer files, use the `protoc` compiler with the `protoc-gen-go-triple` plugin. Here's an example command: ```shell -protoc --go_out=. --go_opt=paths=source_relative --triple-go_out=. your_file.proto +protoc --go_out=. --go_opt=paths=source_relative --go-triple_out=. your_file.proto ``` -Both the `--go_out` flag and `--triple-go_out` flag should be set to `.`. Please set the generated file path in the proto file using the `go_package` option. +Both the `--go_out` flag and `--go-triple_out` flag should be set to `.`. Please set the generated file path in the proto file using the `go_package` option. ## Example @@ -94,7 +94,7 @@ The package for `greet.pb.go` and `greet.triple.go` are `greet` You can use the following command to generate the Go code: ```shell -protoc --go_out=. --go_opt=paths=source_relative --triple-go_out=. greet.proto +protoc --go_out=. --go_opt=paths=source_relative --go-triple_out=. greet.proto ``` This will generate the Go code for the Protobuf code in `greet.pb.go` and the Triple code in `greet.triple.go`. From a134e1b55c5bb9d9d5a3ffffbc8f0d30b2e547cf Mon Sep 17 00:00:00 2001 From: finalt Date: Fri, 26 Jan 2024 16:05:39 +0800 Subject: [PATCH 26/26] feat:add internal service extension (#2577) fixes #2565, #2539 --- common/constant/key.go | 10 ++ protocol/triple/health/healthServer.go | 18 +-- .../triple/reflection/serverreflection.go | 16 ++- server/server.go | 117 +++++++++++++++--- 4 files changed, 132 insertions(+), 29 deletions(-) diff --git a/common/constant/key.go b/common/constant/key.go index e3dfa87606..5dd9dcb0cd 100644 --- a/common/constant/key.go +++ b/common/constant/key.go @@ -17,6 +17,8 @@ package constant +import "math" + type DubboCtxKey string const ( @@ -439,3 +441,11 @@ const ( DefaultMetaFileName = "dubbo.metadata." DefaultEntrySize = 100 ) + +// priority +const ( + DefaultPriority = 0 + HighestPriority = math.MinInt32 + // LowestPriority for metadata service + LowestPriority = math.MaxInt32 +) diff --git a/protocol/triple/health/healthServer.go b/protocol/triple/health/healthServer.go index aca420d27d..5bd89063e3 100644 --- a/protocol/triple/health/healthServer.go +++ b/protocol/triple/health/healthServer.go @@ -172,14 +172,18 @@ func (srv *HealthTripleServer) Resume() { func init() { healthServer = NewServer() - internal.HealthSetServingStatusServing = SetServingStatusServing - - server.SetProServices(&server.ServiceDefinition{ - Handler: healthServer, - Info: &triple_health.Health_ServiceInfo, - Opts: []server.ServiceOption{server.WithNotRegister(), - server.WithInterface(constant.HealthCheckServiceInterface)}, + server.SetProServices(&server.InternalService{ + Name: "healthCheck", + Init: func(options *server.ServiceOptions) (*server.ServiceDefinition, bool) { + return &server.ServiceDefinition{ + Handler: healthServer, + Info: &triple_health.Health_ServiceInfo, + Opts: []server.ServiceOption{server.WithNotRegister(), + server.WithInterface(constant.HealthCheckServiceInterface)}, + }, true + }, + Priority: constant.DefaultPriority, }) // In order to adapt config.Load diff --git a/protocol/triple/reflection/serverreflection.go b/protocol/triple/reflection/serverreflection.go index dff18213b7..a3f6ac1346 100644 --- a/protocol/triple/reflection/serverreflection.go +++ b/protocol/triple/reflection/serverreflection.go @@ -263,11 +263,17 @@ var reflectionServer *ReflectionServer func init() { reflectionServer = NewServer() internal.ReflectionRegister = Register - server.SetProServices(&server.ServiceDefinition{ - Handler: reflectionServer, - Info: &rpb.ServerReflection_ServiceInfo, - Opts: []server.ServiceOption{server.WithNotRegister(), - server.WithInterface(constant.ReflectionServiceInterface)}, + server.SetProServices(&server.InternalService{ + Name: "reflection", + Init: func(options *server.ServiceOptions) (*server.ServiceDefinition, bool) { + return &server.ServiceDefinition{ + Handler: reflectionServer, + Info: &rpb.ServerReflection_ServiceInfo, + Opts: []server.ServiceOption{server.WithNotRegister(), + server.WithInterface(constant.ReflectionServiceInterface)}, + }, true + }, + Priority: constant.DefaultPriority, }) // In order to adapt config.Load // Plans for future removal diff --git a/server/server.go b/server/server.go index a76d9cd59a..0258a940d3 100644 --- a/server/server.go +++ b/server/server.go @@ -21,6 +21,7 @@ package server import ( "context" "fmt" + "sort" "sync" ) @@ -38,7 +39,8 @@ import ( ) // proServices are for internal services -var proServices = map[string]*ServiceDefinition{} +var proServices = make([]*InternalService, 0, 16) +var proLock sync.Mutex type Server struct { invoker protocol.Invoker @@ -123,8 +125,17 @@ func newInfoInvoker(url *common.URL, info *ServiceInfo, svc common.RPCService) p // Register assemble invoker chains like ProviderConfig.Load, init a service per call func (s *Server) Register(handler interface{}, info *ServiceInfo, opts ...ServiceOption) error { + newSvcOpts, err := s.genSvcOpts(handler, opts...) + if err != nil { + return err + } + s.svcOptsMap.Store(newSvcOpts, info) + return nil +} + +func (s *Server) genSvcOpts(handler interface{}, opts ...ServiceOption) (*ServiceOptions, error) { if s.cfg == nil { - return errors.New("Server has not been initialized, please use NewServer() to create Server") + return nil, errors.New("Server has not been initialized, please use NewServer() to create Server") } var svcOpts []ServiceOption appCfg := s.cfg.Application @@ -153,16 +164,13 @@ func (s *Server) Register(handler interface{}, info *ServiceInfo, opts ...Servic SetRegistries(regsCfg), ) } - // options passed by users have higher priority svcOpts = append(svcOpts, opts...) if err := newSvcOpts.init(s, svcOpts...); err != nil { - return err + return nil, err } newSvcOpts.Implement(handler) - s.svcOptsMap.Store(newSvcOpts, info) - - return nil + return newSvcOpts, nil } func (s *Server) exportServices() (err error) { @@ -187,11 +195,88 @@ func (s *Server) Serve() error { 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) select {} } +// In order to expose internal services +func (s *Server) exportInternalServices() error { + cfg := &ServiceOptions{} + cfg.Application = s.cfg.Application + cfg.Provider = s.cfg.Provider + cfg.Protocols = s.cfg.Protocols + cfg.Registries = s.cfg.Registries + + services := make([]*InternalService, 0, len(proServices)) + + proLock.Lock() + defer proLock.Unlock() + for _, service := range proServices { + if service.Init == nil { + return errors.New("[internal service]internal service init func is empty, please set the init func correctly") + } + sd, ok := service.Init(cfg) + if !ok { + logger.Infof("[internal service]%s service will not expose", service.Name) + continue + } + newSvcOpts, err := s.genSvcOpts(sd.Handler, sd.Opts...) + if err != nil { + return err + } + service.svcOpts = newSvcOpts + service.info = sd.Info + services = append(services, service) + } + + sort.Slice(services, func(i, j int) bool { + return services[i].Priority < services[j].Priority + }) + + for _, service := range services { + if service.BeforeExport != nil { + service.BeforeExport(service.svcOpts) + } + err := service.svcOpts.ExportWithInfo(service.info) + if service.AfterExport != nil { + service.AfterExport(service.svcOpts, err) + } + if err != nil { + logger.Errorf("[internal service]export %s service failed, err: %s", service.Name, err) + return err + } + } + + return nil +} + +// InternalService for dubbo internal services +type InternalService struct { + // This is required + // internal service name + Name string + svcOpts *ServiceOptions + info *ServiceInfo + // This is required + // This options is service configuration + // Return serviceDefinition and bool, where bool indicates whether it is exported + Init func(options *ServiceOptions) (*ServiceDefinition, bool) + // This options is InternalService.svcOpts itself + BeforeExport func(options *ServiceOptions) + // This options is InternalService.svcOpts itself + AfterExport func(options *ServiceOptions, err error) + // Priority of service exposure + // Lower numbers have the higher priority + // The default priority is 0 + // The metadata service is exposed at the end + // If you have no requirements for the order of service exposure, you can use the default priority or not set + Priority int +} + type MethodInfo struct { Name string Type string @@ -210,17 +295,15 @@ func NewServer(opts ...ServerOption) (*Server, error) { srv := &Server{ cfg: newSrvOpts, } - - for _, service := range proServices { - err := srv.Register(service.Handler, service.Info, service.Opts...) - if err != nil { - return nil, err - } - } - return srv, nil } -func SetProServices(sd *ServiceDefinition) { - proServices[sd.Info.InterfaceName] = sd +func SetProServices(sd *InternalService) { + if sd.Name == "" { + logger.Warnf("[internal service]internal name is empty, please set internal name") + return + } + proLock.Lock() + defer proLock.Unlock() + proServices = append(proServices, sd) }