diff --git a/common/extension/service_discovery.go b/common/extension/service_discovery.go index 4398dec39a8077f03faf51589ac3b9371b071dc4..25b80cf3353505c058bea40cc4c80712ad923d2d 100644 --- a/common/extension/service_discovery.go +++ b/common/extension/service_discovery.go @@ -17,6 +17,9 @@ package extension +import ( + perrors "github.com/pkg/errors" +) import ( "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/registry" @@ -26,14 +29,17 @@ var ( discoveryCreatorMap = make(map[string]func(url *common.URL) (registry.ServiceDiscovery, error), 4) ) +// SetServiceDiscovery will store the creator and name func SetServiceDiscovery(name string, creator func(url *common.URL) (registry.ServiceDiscovery, error)) { discoveryCreatorMap[name] = creator } +// GetServiceDiscovery will return the registry.ServiceDiscovery +// if not found, or initialize instance failed, it will return error. func GetServiceDiscovery(name string, url *common.URL) (registry.ServiceDiscovery, error) { creator, ok := discoveryCreatorMap[name] if !ok { - panic("Could not find the service discovery with name: " + name) + return nil, perrors.New("Could not find the service discovery with name: " + name) } return creator(url) } diff --git a/go.sum b/go.sum index ed0bb84d82ac4a46549df1b4a62263ac17194dab..e499992eb0a0335ca0a1e1f746caca3418af7655 100644 --- a/go.sum +++ b/go.sum @@ -387,6 +387,7 @@ github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8m github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nacos-group/nacos-sdk-go v0.0.0-20190723125407-0242d42e3dbb h1:lbmvw8r9W55w+aQgWn35W1nuleRIECMoqUrmwAOAvoI= github.com/nacos-group/nacos-sdk-go v0.0.0-20190723125407-0242d42e3dbb/go.mod h1:CEkSvEpoveoYjA81m4HNeYQ0sge0LFGKSEqO3JKHllo= +github.com/nacos-group/nacos-sdk-go v0.0.0-20191128082542-fe1b325b125c h1:WoCa3AvgQMVKNs+RIFlWPRgY9QVJwUxJDrGxHs0fcRo= github.com/nacos-group/nacos-sdk-go v0.0.0-20191128082542-fe1b325b125c/go.mod h1:CEkSvEpoveoYjA81m4HNeYQ0sge0LFGKSEqO3JKHllo= github.com/nicolai86/scaleway-sdk v1.10.2-0.20180628010248-798f60e20bb2 h1:BQ1HW7hr4IVovMwWg0E0PYcyW8CzqDcVmaew9cujU4s= github.com/nicolai86/scaleway-sdk v1.10.2-0.20180628010248-798f60e20bb2/go.mod h1:TLb2Sg7HQcgGdloNxkrmtgDNR9uVYF3lfdFIN4Ro6Sk= diff --git a/registry/event.go b/registry/event.go index da66ccb33dda6eac197d50ba0a2b908b9069a837..be9f11d00bb5a70b0d787d15bcdc98471aad0a4b 100644 --- a/registry/event.go +++ b/registry/event.go @@ -42,6 +42,7 @@ type ServiceEvent struct { Service common.URL } +// String return the description of event func (e ServiceEvent) String() string { return fmt.Sprintf("ServiceEvent{Action{%s}, Path{%s}}", e.Action, e.Service) } @@ -91,10 +92,12 @@ type ServiceInstancesChangedEvent struct { Instances []ServiceInstance } +// String return the description of the event func (s *ServiceInstancesChangedEvent) String() string { return fmt.Sprintf("ServiceInstancesChangedEvent[source=%s]", s.ServiceName) } +// NewServiceInstancesChangedEvent will create the ServiceInstanceChangedEvent instance func NewServiceInstancesChangedEvent(serviceName string, instances []ServiceInstance) *ServiceInstancesChangedEvent { return &ServiceInstancesChangedEvent{ baseEvent: baseEvent{ diff --git a/registry/nacos/base_registry.go b/registry/nacos/base_registry.go index a92df4cdc20471b8c61e44a13a1ce5670af67607..bd5dc86b4376a4cc81644df332c60009379a24ec 100644 --- a/registry/nacos/base_registry.go +++ b/registry/nacos/base_registry.go @@ -43,6 +43,7 @@ type baseRegistry struct { namingClient naming_client.INamingClient } +// newBaseRegistry will create new instance func newBaseRegistry(url *common.URL) (baseRegistry, error) { nacosConfig, err := getNacosConfig(url) if err != nil { @@ -59,6 +60,7 @@ func newBaseRegistry(url *common.URL) (baseRegistry, error) { return registry, nil } +// getNacosConfig will return the nacos config func getNacosConfig(url *common.URL) (map[string]interface{}, error) { if url == nil { return nil, perrors.New("url is empty!") diff --git a/registry/nacos/registry.go b/registry/nacos/registry.go index 697cbbeb83971e28f44044d016362e20f78d2bd7..b0f399862af6fff64b4db346648d1486ad9fafc7 100644 --- a/registry/nacos/registry.go +++ b/registry/nacos/registry.go @@ -56,6 +56,7 @@ type nacosRegistry struct { baseRegistry } +// newNacosRegistry will create an instance func newNacosRegistry(url *common.URL) (registry.Registry, error) { base, err := newBaseRegistry(url) if err != nil { diff --git a/registry/nacos/service_discovery.go b/registry/nacos/service_discovery.go index 5a730e935d1fe7ff852cb8af8ab0f0de2345f257..b89d4f611bd4193085164225cf1271e037ed7548 100644 --- a/registry/nacos/service_discovery.go +++ b/registry/nacos/service_discovery.go @@ -38,6 +38,7 @@ const ( idKey = "id" ) +// init will put the service discovery into extension func init() { extension.SetServiceDiscovery(constant.NACOS_KEY, newNacosServiceDiscovery) } @@ -224,19 +225,24 @@ func (n *nacosServiceDiscovery) AddListener(listener *registry.ServiceInstancesC }) } +// DispatchEventByServiceName will dispatch the event for the service with the service name func (n *nacosServiceDiscovery) DispatchEventByServiceName(serviceName string) error { return n.DispatchEventForInstances(serviceName, n.GetInstances(serviceName)) } +// DispatchEventForInstances will dispatch the event to those instances func (n *nacosServiceDiscovery) DispatchEventForInstances(serviceName string, instances []registry.ServiceInstance) error { return n.DispatchEvent(registry.NewServiceInstancesChangedEvent(serviceName, instances)) } +// DispatchEvent will dispatch the event func (n *nacosServiceDiscovery) DispatchEvent(event *registry.ServiceInstancesChangedEvent) error { // TODO(waiting for event dispatcher, another task) return nil } +// toRegisterInstance convert the ServiceInstance to RegisterInstanceParam +// the Ephemeral will be true func (n *nacosServiceDiscovery) toRegisterInstance(instance registry.ServiceInstance) vo.RegisterInstanceParam { metadata := instance.GetMetadata() if metadata == nil { @@ -255,6 +261,7 @@ func (n *nacosServiceDiscovery) toRegisterInstance(instance registry.ServiceInst } } +// toDeregisterInstance will convert the ServiceInstance to DeregisterInstanceParam func (n *nacosServiceDiscovery) toDeregisterInstance(instance registry.ServiceInstance) vo.DeregisterInstanceParam { return vo.DeregisterInstanceParam{ ServiceName: instance.GetServiceName(), @@ -264,7 +271,9 @@ func (n *nacosServiceDiscovery) toDeregisterInstance(instance registry.ServiceIn } } +// toDeregisterInstance will create new service discovery instance func newNacosServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, error) { + base, err := newBaseRegistry(url) if err != nil { return nil, perrors.WithStack(err)