diff --git a/common/constant/default.go b/common/constant/default.go index 211bfc06bd5241ab3b8e591e06691f87c2d372f0..45fc38f13e05a45e650cffcfe50177d71c8f797e 100644 --- a/common/constant/default.go +++ b/common/constant/default.go @@ -77,6 +77,7 @@ const ( const ( SIMPLE_METADATA_SERVICE_NAME = "MetadataService" + DEFAULT_REVIESION = "N/A" ) const ( diff --git a/common/extension/metadata_service.go b/common/extension/metadata_service.go index c6a165c487b11781d1dbdc6d0369eaf06b0b7329..93ff40aa763100ece3b557598e24eeb8f81454a9 100644 --- a/common/extension/metadata_service.go +++ b/common/extension/metadata_service.go @@ -36,7 +36,7 @@ func GetMetadataService(msType string) (service.MetadataService, error) { if creator, ok := metadataServiceInsMap[msType]; ok { return creator() } - panic(fmt.Sprintf("could not find the creator for metadataType: %s, please check whether you have imported relative packages, \n"+ + panic(fmt.Sprintf("could not find the metadata service creator for metadataType: %s, please check whether you have imported relative packages, \n"+ "local - github.com/apache/dubbo-go/metadata/service/inmemory, \n"+ "remote - github.com/apache/dubbo-go/metadata/service/remote", msType)) } diff --git a/common/extension/metadata_service_proxy_factory.go b/common/extension/metadata_service_proxy_factory.go new file mode 100644 index 0000000000000000000000000000000000000000..35f63c5fa9a15916739f32e7c1810c04e7ed4b2c --- /dev/null +++ b/common/extension/metadata_service_proxy_factory.go @@ -0,0 +1,41 @@ +/* + * 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" + + "github.com/apache/dubbo-go/metadata/service" +) + +var ( + metadataServiceProxyFactoryMap = make(map[string]func() service.MetadataServiceProxyFactory) +) + +func SetMetadataServiceProxyFactory(name string, creator func() service.MetadataServiceProxyFactory) { + metadataServiceProxyFactoryMap[name] = creator +} + +func GetMetadataServiceProxyFactory(name string) service.MetadataServiceProxyFactory { + 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, \n"+ + "local - github.com/apache/dubbo-go/metadata/service/inmemory, \n"+ + "remote - github.com/apache/dubbo-go/metadata/service/remote", name)) +} diff --git a/config/base_config.go b/config/base_config.go index dad4d7f7ef9a44e125556974480731519249293f..f04a09f498133a84be4043aada7c5821dfe98b56 100644 --- a/config/base_config.go +++ b/config/base_config.go @@ -45,6 +45,10 @@ type BaseConfig struct { ConfigCenterConfig *ConfigCenterConfig `yaml:"config_center" json:"config_center,omitempty"` Remotes map[string]*RemoteConfig `yaml:"remote" json:"remote,omitempty"` ServiceDiscoveries map[string]*ServiceDiscoveryConfig `yaml:"service_discovery" json:"service_discovery,omitempty"` + + Registry *RegistryConfig `yaml:"registry" json:"registry,omitempty" property:"registry"` + Registries map[string]*RegistryConfig `yaml:"registries" json:"registries,omitempty" property:"registries"` + // application config ApplicationConfig *ApplicationConfig `yaml:"application" json:"application,omitempty" property:"application"` diff --git a/config/config_loader.go b/config/config_loader.go index 4a293e7a9f7696641fc9280ca2b0ae5452e8914c..7d279ff08369829f348ba1cfafaf0316ee7bca94 100644 --- a/config/config_loader.go +++ b/config/config_loader.go @@ -148,7 +148,7 @@ func loadConsumerConfig() { if count > maxWait { errMsg := fmt.Sprintf("Failed to check the status of the service %v . No provider available for the service to the consumer use dubbo version %v", refconfig.InterfaceName, constant.Version) logger.Error(errMsg) - panic(errMsg) + // panic(errMsg) } time.Sleep(time.Second * 1) break @@ -222,13 +222,11 @@ func Load() { extension.SetAndInitGlobalDispatcher(GetBaseConfig().EventDispatcherType) // start the metadata report if config set - if err := startMetadataReport(providerConfig.ApplicationConfig.MetadataType, providerConfig.MetadataReportConfig); err != nil { + if err := startMetadataReport(GetApplicationConfig().MetadataType, GetProviderConfig().MetadataReportConfig); err != nil { logger.Errorf("Provider starts metadata report error, and the error is {%#v}", err) return } - logger.Debugf("provider config{%#v}\n", providerConfig) - // reference config loadConsumerConfig() @@ -323,3 +321,7 @@ func GetBaseConfig() *BaseConfig { } return baseConfig } + +func IsProvider() bool { + return providerConfig != nil +} diff --git a/config/consumer_config.go b/config/consumer_config.go index bee9b1e3fcf6e9d95a285c8613bb9c8f4c6c858b..fd3d30e6fca2d537da47cce79134e749060f511e 100644 --- a/config/consumer_config.go +++ b/config/consumer_config.go @@ -51,8 +51,6 @@ type ConsumerConfig struct { ProxyFactory string `yaml:"proxy_factory" default:"default" json:"proxy_factory,omitempty" property:"proxy_factory"` Check *bool `yaml:"check" json:"check,omitempty" property:"check"` - Registry *RegistryConfig `yaml:"registry" json:"registry,omitempty" property:"registry"` - Registries map[string]*RegistryConfig `yaml:"registries" json:"registries,omitempty" property:"registries"` References map[string]*ReferenceConfig `yaml:"references" json:"references,omitempty" property:"references"` ProtocolConf interface{} `yaml:"protocol_conf" json:"protocol_conf,omitempty" property:"protocol_conf"` FilterConf interface{} `yaml:"filter_conf" json:"filter_conf,omitempty" property:"filter_conf" ` diff --git a/config/provider_config.go b/config/provider_config.go index 0f14c9f1d36219c72fdd81124ab773c8bdfcb09b..99c532a383ad75285edc7e7200f6edbeac936f9d 100644 --- a/config/provider_config.go +++ b/config/provider_config.go @@ -42,8 +42,6 @@ type ProviderConfig struct { ProxyFactory string `yaml:"proxy_factory" default:"default" json:"proxy_factory,omitempty" property:"proxy_factory"` // metadata-report MetadataReportConfig *MetadataReportConfig `yaml:"metadata_report" json:"metadata_report,omitempty" property:"metadata_report"` - Registry *RegistryConfig `yaml:"registry" json:"registry,omitempty" property:"registry"` - Registries map[string]*RegistryConfig `yaml:"registries" json:"registries,omitempty" property:"registries"` Services map[string]*ServiceConfig `yaml:"services" json:"services,omitempty" property:"services"` Protocols map[string]*ProtocolConfig `yaml:"protocols" json:"protocols,omitempty" property:"protocols"` ProtocolConf interface{} `yaml:"protocol_conf" json:"protocol_conf,omitempty" property:"protocol_conf" ` diff --git a/config/reference_config.go b/config/reference_config.go index f343a9a2362928d4d87874d7d1ddc5d7ad40316c..e935c7646d7ba9d7a75fed3bed68c88ed7ed104d 100644 --- a/config/reference_config.go +++ b/config/reference_config.go @@ -55,6 +55,7 @@ type ReferenceConfig struct { Retries string `yaml:"retries" json:"retries,omitempty" property:"retries"` Group string `yaml:"group" json:"group,omitempty" property:"group"` Version string `yaml:"version" json:"version,omitempty" property:"version"` + ProvideBy string `yaml:"provide_by" json:"provide_by,omitempty" property:"provide_by"` Methods []*MethodConfig `yaml:"methods" json:"methods,omitempty" property:"methods"` Async bool `yaml:"async" json:"async,omitempty" property:"async"` Params map[string]string `yaml:"params" json:"params,omitempty" property:"params"` @@ -190,6 +191,7 @@ func (c *ReferenceConfig) getUrlMap() url.Values { urlMap.Set(constant.VERSION_KEY, c.Version) urlMap.Set(constant.GENERIC_KEY, strconv.FormatBool(c.Generic)) urlMap.Set(constant.ROLE_KEY, strconv.Itoa(common.CONSUMER)) + urlMap.Set(constant.PROVIDER_BY, c.ProvideBy) urlMap.Set(constant.RELEASE_KEY, "dubbo-golang-"+constant.Version) urlMap.Set(constant.SIDE_KEY, (common.RoleType(common.CONSUMER)).Role()) diff --git a/config_center/nacos/impl.go b/config_center/nacos/impl.go index 0b8aceb23c561756c41be1f88f54e8c36d27456f..b4a7a5467978404387c0c9667dc308a2e68d59ed 100644 --- a/config_center/nacos/impl.go +++ b/config_center/nacos/impl.go @@ -125,7 +125,7 @@ func (n *nacosDynamicConfiguration) GetConfigKeysByGroup(group string) (*gxset.H return result, perrors.WithMessage(err, "can not find the client config") } for _, itm := range page.PageItems { - result.Add(itm.Content) + result.Add(itm.Appname) } return result, nil } diff --git a/metadata/service/inmemory/metadata_service_proxy_factory.go b/metadata/service/inmemory/metadata_service_proxy_factory.go new file mode 100644 index 0000000000000000000000000000000000000000..813ed75136da3badaa7edb26e0d1816f7be9e740 --- /dev/null +++ b/metadata/service/inmemory/metadata_service_proxy_factory.go @@ -0,0 +1,35 @@ +/* + * 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 inmemory + +import ( + "github.com/apache/dubbo-go/common/extension" + "github.com/apache/dubbo-go/metadata/service" + "github.com/apache/dubbo-go/registry" +) + +func init() { + factory := service.NewBaseMetadataServiceProxyFactory(createProxy) + extension.SetMetadataServiceProxyFactory(local, func() service.MetadataServiceProxyFactory { + return factory + }) +} + +func createProxy(ins registry.ServiceInstance) service.MetadataService { + return nil +} diff --git a/metadata/service/inmemory/service.go b/metadata/service/inmemory/service.go index c9d2ed47b28222a950548d781f367da8d42bcf5b..b0782fa3deb190b0577f84ce5979284900da9965 100644 --- a/metadata/service/inmemory/service.go +++ b/metadata/service/inmemory/service.go @@ -37,13 +37,16 @@ import ( "github.com/apache/dubbo-go/metadata/service" ) +// version will be used by Version func +const ( + version = "1.0.0" + local = "local" +) + func init() { - extension.SetMetadataService("local", NewMetadataService) + extension.SetMetadataService(local, NewMetadataService) } -// 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 diff --git a/metadata/service/remote/metadata_service_proxy_factory.go b/metadata/service/remote/metadata_service_proxy_factory.go new file mode 100644 index 0000000000000000000000000000000000000000..a1a8594282c581913d97586630f3e5e74305642d --- /dev/null +++ b/metadata/service/remote/metadata_service_proxy_factory.go @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package remote + +import ( + "github.com/apache/dubbo-go/common/extension" + "github.com/apache/dubbo-go/metadata/service" +) + +func init() { + factory := service.NewBaseMetadataServiceProxyFactory(newMetadataServiceProxy) + extension.SetMetadataServiceProxyFactory(remote, func() service.MetadataServiceProxyFactory { + return factory + }) +} diff --git a/metadata/service/remote/service.go b/metadata/service/remote/service.go index 2e8f66444f346aba8491ad02292eb2d60c97d07b..19f0c021d3002ba8274a0909746da8f0dca79d3e 100644 --- a/metadata/service/remote/service.go +++ b/metadata/service/remote/service.go @@ -38,10 +38,13 @@ import ( ) // version will be used by Version func -const version = "1.0.0" +const ( + version = "1.0.0" + remote = "remote" +) func init() { - extension.SetMetadataService("remote", newMetadataService) + extension.SetMetadataService(remote, newMetadataService) } // MetadataService is a implement of metadata service which will delegate the remote metadata report diff --git a/metadata/service/remote/service_proxy.go b/metadata/service/remote/service_proxy.go new file mode 100644 index 0000000000000000000000000000000000000000..090e8edc3d7ccd0167a27b7afb52becfde9914a5 --- /dev/null +++ b/metadata/service/remote/service_proxy.go @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package remote + +import ( + "strings" + + "github.com/apache/dubbo-go/common" + "github.com/apache/dubbo-go/common/constant" + "github.com/apache/dubbo-go/common/logger" + "github.com/apache/dubbo-go/config/instance" + "github.com/apache/dubbo-go/metadata/identifier" + "github.com/apache/dubbo-go/metadata/report" + "github.com/apache/dubbo-go/metadata/service" + "github.com/apache/dubbo-go/registry" +) + +type metadataServiceProxy struct { + serviceName string + revision string + report report.MetadataReport +} + +func (m *metadataServiceProxy) Reference() string { + return constant.METADATA_SERVICE_NAME +} + +func (m *metadataServiceProxy) ServiceName() (string, error) { + return m.serviceName, nil +} + +func (m *metadataServiceProxy) ExportURL(url common.URL) (bool, error) { + logger.Errorf("you should never invoke this implementation") + return true, nil +} + +func (m *metadataServiceProxy) UnexportURL(url common.URL) error { + logger.Errorf("you should never invoke this implementation") + return nil +} + +func (m *metadataServiceProxy) SubscribeURL(url common.URL) (bool, error) { + logger.Errorf("you should never invoke this implementation") + return true, nil +} + +func (m *metadataServiceProxy) UnsubscribeURL(url common.URL) error { + logger.Errorf("you should never invoke this implementation") + return nil +} + +func (m *metadataServiceProxy) PublishServiceDefinition(url common.URL) error { + logger.Errorf("you should never invoke this implementation") + return nil +} + +func (m *metadataServiceProxy) GetExportedURLs(serviceInterface string, group string, version string, protocol string) ([]common.URL, error) { + urls := m.report.GetExportedURLs(&identifier.ServiceMetadataIdentifier{ + BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{ + ServiceInterface: serviceInterface, + Version: version, + Group: group, + Side: constant.PROVIDER_PROTOCOL, + }, + Revision: m.revision, + Protocol: protocol, + }) + res := make([]common.URL, 0, len(urls)) + for _, s := range urls { + u, err := common.NewURL(s) + if err != nil { + logger.Errorf("could not parse the url string to URL structure", err) + continue + } + res = append(res, u) + } + return res, nil +} + +func (m *metadataServiceProxy) GetSubscribedURLs() ([]common.URL, error) { + logger.Errorf("you should never invoke this implementation") + return []common.URL{}, nil +} + +func (m *metadataServiceProxy) GetServiceDefinition(interfaceName string, group string, version string) (string, error) { + return m.report.GetServiceDefinition(&identifier.MetadataIdentifier{ + BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{ + ServiceInterface: interfaceName, + Group: group, + Version: version, + Side: constant.PROVIDER_PROTOCOL, + }, + Application: m.serviceName, + }), nil +} + +func (m *metadataServiceProxy) GetServiceDefinitionByServiceKey(serviceKey string) (string, error) { + params := parse(serviceKey) + return m.GetServiceDefinition(params[0], params[1], params[2]) +} + +func (m *metadataServiceProxy) RefreshMetadata(exportedRevision string, subscribedRevision string) (bool, error) { + logger.Errorf("you should never invoke this implementation") + return true, nil +} + +func (m metadataServiceProxy) Version() (string, error) { + return version, nil +} + +func newMetadataServiceProxy(ins registry.ServiceInstance) service.MetadataService { + revision := ins.GetMetadata()[constant.EXPORTED_SERVICES_REVISION_PROPERTY_NAME] + if len(revision) == 0 { + revision = constant.DEFAULT_REVIESION + } + + return &metadataServiceProxy{ + serviceName: ins.GetServiceName(), + revision: revision, + report: instance.GetMetadataReportInstance(), + } +} + +func parse(key string) []string { + arr := make([]string, 0, 3) + tmp := strings.SplitN(key, "/", 2) + if len(tmp) > 1 { + arr[0] = tmp[0] + key = tmp[1] + } + tmp = strings.SplitN(key, "/", 2) + if len(tmp) > 1 { + arr[2] = tmp[1] + key = tmp[0] + } + arr[1] = key + return arr +} diff --git a/metadata/service/service.go b/metadata/service/service.go index e05b634f8c18fa09876a3292ccb8a4151bf4a7d8..fc76eca68f1584e22f2c3665bf609956a0650a06 100644 --- a/metadata/service/service.go +++ b/metadata/service/service.go @@ -18,8 +18,11 @@ package service import ( + "sync" + "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" + "github.com/apache/dubbo-go/registry" ) // MetadataService is used to define meta data related behaviors @@ -74,3 +77,34 @@ func (mts *BaseMetadataService) ServiceName() (string, error) { func (mts *BaseMetadataService) Reference() string { return constant.SIMPLE_METADATA_SERVICE_NAME } + +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 { + key := ins.GetServiceName() + "##" + getExportedServicesRevision(ins) + if proxy, ok := b.proxies.Load(key); ok { + return proxy.(MetadataService) + } + v, _ := b.proxies.LoadOrStore(key, b.creator(ins)) + return v.(MetadataService) +} + +func getExportedServicesRevision(serviceInstance registry.ServiceInstance) string { + metaData := serviceInstance.GetMetadata() + return metaData[constant.EXPORTED_SERVICES_REVISION_PROPERTY_NAME] +} diff --git a/registry/servicediscovery/proxy/metadata_service_proxy_factory.go b/registry/servicediscovery/proxy/metadata_service_proxy_factory.go index 7e8d891bf3566c298bb62527df1040ef82ad8fe9..6851f4bb7c1abe082d8310cdd92636f89b6721a6 100644 --- a/registry/servicediscovery/proxy/metadata_service_proxy_factory.go +++ b/registry/servicediscovery/proxy/metadata_service_proxy_factory.go @@ -17,15 +17,7 @@ package proxy -var ( - serviceProxy = make(map[string]func() BaseMetadataServiceProxy) +import ( + "github.com/apache/dubbo-go/metadata/service" + "github.com/apache/dubbo-go/registry" ) - -func SetMetadataServiceProxy(name string, creator func() BaseMetadataServiceProxy) { - //TODO -} - -func GetMetadataServiceProxy(name string) BaseMetadataServiceProxy { - //TODO - return nil -} diff --git a/registry/servicediscovery/proxy/service_proxy.go b/registry/servicediscovery/proxy/service_proxy.go index 7a14201fdd81dcf663d08260058c631e35f8002f..6555874dd6bc7b88ede2774405f4e1b20cfbbee5 100644 --- a/registry/servicediscovery/proxy/service_proxy.go +++ b/registry/servicediscovery/proxy/service_proxy.go @@ -16,14 +16,3 @@ */ package proxy - -import ( - "github.com/apache/dubbo-go/metadata/service" - "github.com/apache/dubbo-go/registry" -) - -type BaseMetadataServiceProxy interface { - GetProxy(serviceInstance registry.ServiceInstance) service.MetadataService - - CreateProxy(serviceInstance registry.ServiceInstance) service.MetadataService -} diff --git a/registry/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go index aad4b26416248bfc3e54f0d4acef10989ef0750d..133827d5eb1f53e0ce4c7579d0b3a6b8f15c46aa 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -40,7 +40,6 @@ import ( "github.com/apache/dubbo-go/metadata/service/exporter/configurable" "github.com/apache/dubbo-go/registry" "github.com/apache/dubbo-go/registry/event" - "github.com/apache/dubbo-go/registry/servicediscovery/proxy" "github.com/apache/dubbo-go/registry/servicediscovery/synthesizer" "github.com/apache/dubbo-go/remoting" ) @@ -407,11 +406,11 @@ func (c comparator) Compare(comp cm.Comparator) int { func (s *serviceDiscoveryRegistry) getExportedUrlsByInst(serviceInstance registry.ServiceInstance) []common.URL { var urls []common.URL metadataStorageType := getExportedStoreType(serviceInstance) - metadataProxy := proxy.GetMetadataServiceProxy(metadataStorageType) - if metadataProxy == nil { + proxyFactory := extension.GetMetadataServiceProxyFactory(metadataStorageType) + if proxyFactory == nil { return urls } - metadataService := metadataProxy.GetProxy(serviceInstance) + metadataService := proxyFactory.GetProxy(serviceInstance) if metadataService == nil { return urls } @@ -665,7 +664,14 @@ func initMetadataService() { if err != nil { logger.Errorf("could not init metadata service", err) } + + // we don't need to expose the metadata service since this is a pure consumer application + if !config.IsProvider() { + return + } + expt := configurable.NewMetadataServiceExporter(ms) + err = expt.Export() if err != nil { logger.Errorf("could not export the metadata service", err)