You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2020/05/03 14:26:31 UTC

[GitHub] [dubbo-go] zouyx commented on a change in pull request #491: ServiceDiscoveryRegistry implementation

zouyx commented on a change in pull request #491:
URL: https://github.com/apache/dubbo-go/pull/491#discussion_r419110336



##########
File path: config/config_loader.go
##########
@@ -104,6 +104,8 @@ func loadConsumerConfig() {
 	applicationConfig = consumerConfig.ApplicationConfig
 	extension.SetAndInitGlobalDispatcher(consumerConfig.eventDispatcherType)
 
+	extension.SetAndInitGlobalDispatcher(consumerConfig.eventDispatcherType)

Review comment:
       the same to line 105?

##########
File path: config/config_loader.go
##########
@@ -181,6 +183,8 @@ func loadProviderConfig() {
 	applicationConfig = providerConfig.ApplicationConfig
 	extension.SetAndInitGlobalDispatcher(providerConfig.eventDispatcherType)
 
+	extension.SetAndInitGlobalDispatcher(consumerConfig.eventDispatcherType)

Review comment:
       the same to line 184?

##########
File path: registry/servicediscovery/proxy/metadata_service_proxy_factory.go
##########
@@ -0,0 +1,31 @@
+/*
+ * 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 proxy
+
+var (
+	serviceProxy = make(map[string]func() BaseMetadataServiceProxy)
+)
+
+func SetMetadataServiceProxy(name string, creator func() BaseMetadataServiceProxy) {
+	//TODO
+}
+
+func GetMetadataServiceProxy(name string) BaseMetadataServiceProxy {
+	//TODO
+	return nil
+}

Review comment:
       should implement?

##########
File path: registry/event_listener.go
##########
@@ -25,20 +25,34 @@ import (
 	"github.com/apache/dubbo-go/common/observer"
 )
 
-// TODO (implement ConditionalEventListener)
+//The Service Discovery Changed  Event Listener
 type ServiceInstancesChangedListener struct {
 	ServiceName string
-	observer.EventListener
+	observer.ConditionalEventListener
+	ChangedNotify ChangedNotify
 }
 
-func (sicl *ServiceInstancesChangedListener) OnEvent(e observer.Event) error {
+//On ServiceInstancesChangedEvent the service instances change event
+func (sicl *ServiceInstancesChangedListener) OnEvent(e ServiceInstancesChangedEvent) error {
+	sicl.ChangedNotify.Notify(e)
 	return nil
 }
 
+//get listener priority
 func (sicl *ServiceInstancesChangedListener) GetPriority() int {
 	return -1
 }
 
+//get event type
 func (sicl *ServiceInstancesChangedListener) GetEventType() reflect.Type {
 	return reflect.TypeOf(&ServiceInstancesChangedEvent{})
 }
+
+//If service name matches,return true or false
+func (sicl *ServiceInstancesChangedListener) Accept(e ServiceInstancesChangedEvent) bool {
+	return e.ServiceName == sicl.ServiceName
+}
+
+type ChangedNotify interface {

Review comment:
       should it move to parent package?

##########
File path: registry/servicediscovery/service_discovery_registry.go
##########
@@ -0,0 +1,596 @@
+/*
+ * 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 (
+	"bytes"
+	"encoding/json"
+	"strconv"
+	"strings"
+	"sync"
+)
+
+import (
+	cm "github.com/Workiva/go-datastructures/common"
+	gxset "github.com/dubbogo/gost/container/set"
+)
+
+import (
+	"github.com/apache/dubbo-go/common"
+	"github.com/apache/dubbo-go/common/constant"
+	"github.com/apache/dubbo-go/common/extension"
+	"github.com/apache/dubbo-go/common/logger"
+	"github.com/apache/dubbo-go/metadata/mapping"
+	"github.com/apache/dubbo-go/metadata/service"
+	"github.com/apache/dubbo-go/metadata/service/inmemory"
+	"github.com/apache/dubbo-go/registry"
+	"github.com/apache/dubbo-go/registry/servicediscovery/proxy"
+	"github.com/apache/dubbo-go/registry/servicediscovery/synthesizer"
+	"github.com/apache/dubbo-go/remoting"
+)
+
+const (
+	protocolName = "service-discovery"
+)
+
+func init() {
+	extension.SetRegistry(protocolName, newServiceDiscoveryRegistry)
+}
+
+// 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
+// In order to keep compatible with interface-level registry,
+// 1. when we registry the service, we should create the mapping from service name to application name
+// 2. when we sub
+type serviceDiscoveryRegistry struct {
+	lock               sync.RWMutex
+	url                *common.URL
+	serviceDiscovery   registry.ServiceDiscovery
+	subscribedServices *gxset.HashSet
+	serviceNameMapping mapping.ServiceNameMapping
+	metaDataService    service.MetadataService
+	//cache the registered listen
+	registeredListeners *gxset.HashSet
+	//all synthesize
+	subscribedURLsSynthesizers []synthesizer.SubscribedURLsSynthesizer
+	//cache exported  urls,   serviceName->revision->[]URL
+	serviceRevisionExportedURLsCache map[string]map[string][]common.URL
+}
+
+func newServiceDiscoveryRegistry(url *common.URL) (registry.Registry, error) {
+	serviceDiscovery, err := creatServiceDiscovery(url)
+	if err != nil {
+		return nil, err
+	}
+	subscribedServices := parseServices(url.GetParam(constant.SUBSCRIBED_SERVICE_NAMES_KEY, ""))
+	subscribedURLsSynthesizers := synthesizer.GetAllSynthesizer()
+	serviceNameMapping := extension.GetServiceNameMapping(url.GetParam(constant.SERVICE_NAME_MAPPING_KEY, ""))
+	//TODO it's need to get implement by factory
+	metaDataService := inmemory.NewMetadataService()
+	return &serviceDiscoveryRegistry{
+		url:                              url,
+		serviceDiscovery:                 serviceDiscovery,
+		subscribedServices:               subscribedServices,
+		subscribedURLsSynthesizers:       subscribedURLsSynthesizers,
+		registeredListeners:              gxset.NewSet(),
+		serviceRevisionExportedURLsCache: make(map[string]map[string][]common.URL),
+		serviceNameMapping:               serviceNameMapping,
+		metaDataService:                  metaDataService,
+	}, nil
+}
+
+func creatServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, error) {
+	return extension.GetServiceDiscovery(url.Protocol, url)
+}
+
+func parseServices(literalServices string) *gxset.HashSet {
+	set := gxset.NewSet()
+	if len(literalServices) == 0 {
+		return set
+	}
+	var splitServices = strings.Split(literalServices, ",")
+	for _, s := range splitServices {
+		if len(s) != 0 {
+			set.Add(s)
+		}
+	}
+	return set
+}
+
+//GetServiceDiscovery for get serviceDiscovery of the registry
+func (s *serviceDiscoveryRegistry) GetServiceDiscovery() registry.ServiceDiscovery {
+	return s.serviceDiscovery
+}
+
+//GetUrl for get url of the registry
+func (s *serviceDiscoveryRegistry) GetUrl() common.URL {
+	return *s.url
+}
+
+//IsAvailable for make sure is't available
+func (s *serviceDiscoveryRegistry) IsAvailable() bool {
+	return true

Review comment:
       always true?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org