From 804056674a7c6b0317978c3dc77d9bf4de1f908c Mon Sep 17 00:00:00 2001 From: shen <zhangqi1@cmiot.chinamobile.com> Date: Thu, 13 Aug 2020 09:11:20 +0800 Subject: [PATCH] optimized code for consul service discovery --- registry/consul/service_discovery.go | 2 +- registry/consul/service_discovery_test.go | 4 ++-- registry/etcdv3/service_discovery.go | 2 +- .../event/event_publishing_service_deiscovery_test.go | 2 +- registry/event/event_publishing_service_discovery.go | 10 +++++----- registry/nacos/service_discovery.go | 2 +- registry/service_discovery.go | 2 +- .../servicediscovery/service_discovery_registry.go | 2 +- .../service_discovery_registry_test.go | 2 +- registry/zookeeper/service_discovery.go | 2 +- 10 files changed, 15 insertions(+), 15 deletions(-) diff --git a/registry/consul/service_discovery.go b/registry/consul/service_discovery.go index d2e65be88..662b2bca0 100644 --- a/registry/consul/service_discovery.go +++ b/registry/consul/service_discovery.go @@ -136,7 +136,7 @@ type consulServiceDiscovery struct { *consul.Config } -func (csd *consulServiceDiscovery) Initialize(registryURL common.URL) error { +func (csd *consulServiceDiscovery) Init(registryURL common.URL) error { csd.serviceUrl = registryURL csd.checkPassInterval = registryURL.GetParamInt(CHECK_PASS_INTERVAL, DEFAULT_CHECK_PASS_INTERVAL) csd.tag = registryURL.GetParam(QUERY_TAG, "") diff --git a/registry/consul/service_discovery_test.go b/registry/consul/service_discovery_test.go index ec85d4aa9..19e2a3df0 100644 --- a/registry/consul/service_discovery_test.go +++ b/registry/consul/service_discovery_test.go @@ -81,7 +81,7 @@ func TestConsulServiceDiscovery_Destroy(t *testing.T) { prepareData() serviceDiscovery, err := extension.GetServiceDiscovery(constant.CONSUL_KEY, testName) _, registryUrl := prepareService() - serviceDiscovery.Initialize(registryUrl) + serviceDiscovery.Init(registryUrl) assert.Nil(t, err) assert.NotNil(t, serviceDiscovery) err = serviceDiscovery.Destroy() @@ -109,7 +109,7 @@ func TestConsulServiceDiscovery_CRUD(t *testing.T) { serviceDiscovery, err := extension.GetServiceDiscovery(constant.CONSUL_KEY, testName) assert.Nil(t, err) - err = serviceDiscovery.Initialize(registryUrl) + err = serviceDiscovery.Init(registryUrl) assert.Nil(t, err) err = serviceDiscovery.Unregister(instance) diff --git a/registry/etcdv3/service_discovery.go b/registry/etcdv3/service_discovery.go index 010adff59..6a99a3922 100644 --- a/registry/etcdv3/service_discovery.go +++ b/registry/etcdv3/service_discovery.go @@ -67,7 +67,7 @@ type etcdV3ServiceDiscovery struct { childListenerMap map[string]*etcdv3.EventListener } -func (e *etcdV3ServiceDiscovery) Initialize(registryURL common.URL) error { +func (e *etcdV3ServiceDiscovery) Init(registryURL common.URL) error { return nil } diff --git a/registry/event/event_publishing_service_deiscovery_test.go b/registry/event/event_publishing_service_deiscovery_test.go index 0f1053696..2b4bf05f1 100644 --- a/registry/event/event_publishing_service_deiscovery_test.go +++ b/registry/event/event_publishing_service_deiscovery_test.go @@ -117,7 +117,7 @@ func (tel *TestServiceInstancePreRegisteredEventListener) GetEventType() reflect type ServiceDiscoveryA struct { } -func (msd *ServiceDiscoveryA) Initialize(registryURL common.URL) error { +func (msd *ServiceDiscoveryA) Init(registryURL common.URL) error { return nil } diff --git a/registry/event/event_publishing_service_discovery.go b/registry/event/event_publishing_service_discovery.go index 5742a18f2..14ee18997 100644 --- a/registry/event/event_publishing_service_discovery.go +++ b/registry/event/event_publishing_service_discovery.go @@ -38,8 +38,8 @@ import ( // EventPublishingServiceDiscovery will enhance Service Discovery // Publish some event about service discovery type EventPublishingServiceDiscovery struct { - serviceDiscovery registry.ServiceDiscovery - once sync.Once + serviceDiscovery registry.ServiceDiscovery + serviceDiscoveryInitOnce sync.Once } // NewEventPublishingServiceDiscovery is a constructor @@ -54,10 +54,10 @@ func (epsd *EventPublishingServiceDiscovery) String() string { return epsd.serviceDiscovery.String() } -func (epsd *EventPublishingServiceDiscovery) Initialize(registryURL common.URL) error { +func (epsd *EventPublishingServiceDiscovery) Init(registryURL common.URL) error { var err error - epsd.once.Do(func() { - err = epsd.serviceDiscovery.Initialize(registryURL) + epsd.serviceDiscoveryInitOnce.Do(func() { + err = epsd.serviceDiscovery.Init(registryURL) }) return err } diff --git a/registry/nacos/service_discovery.go b/registry/nacos/service_discovery.go index 78c6c4c4f..ad28c1a0f 100644 --- a/registry/nacos/service_discovery.go +++ b/registry/nacos/service_discovery.go @@ -63,7 +63,7 @@ type nacosServiceDiscovery struct { namingClient naming_client.INamingClient } -func (n *nacosServiceDiscovery) Initialize(registryURL common.URL) error { +func (n *nacosServiceDiscovery) Init(registryURL common.URL) error { return nil } diff --git a/registry/service_discovery.go b/registry/service_discovery.go index e4c4cc53a..a4e9e2421 100644 --- a/registry/service_discovery.go +++ b/registry/service_discovery.go @@ -42,7 +42,7 @@ type ServiceDiscovery interface { * Initializes the ServiceDiscovery * */ - Initialize(registryURL common.URL) error + Init(registryURL common.URL) error // Destroy will destroy the service discovery. // If the discovery cannot be destroy, it will return an error. diff --git a/registry/servicediscovery/service_discovery_registry.go b/registry/servicediscovery/service_discovery_registry.go index a1d3f120c..0dff3473b 100644 --- a/registry/servicediscovery/service_discovery_registry.go +++ b/registry/servicediscovery/service_discovery_registry.go @@ -126,7 +126,7 @@ func creatServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, error) { return nil, perrors.WithMessage(err, "Create service discovery fialed") } serviceDiscovery := event.NewEventPublishingServiceDiscovery(originServiceDiscovery) - serviceDiscovery.Initialize(*url) + serviceDiscovery.Init(*url) return serviceDiscovery, nil } diff --git a/registry/servicediscovery/service_discovery_registry_test.go b/registry/servicediscovery/service_discovery_registry_test.go index bf16ebdc1..b42158c1f 100644 --- a/registry/servicediscovery/service_discovery_registry_test.go +++ b/registry/servicediscovery/service_discovery_registry_test.go @@ -126,7 +126,7 @@ func (m *mockServiceNameMapping) Get(serviceInterface string, group string, vers type mockServiceDiscovery struct { } -func (m *mockServiceDiscovery) Initialize(registryURL common.URL) error { +func (m *mockServiceDiscovery) Init(registryURL common.URL) error { return nil } diff --git a/registry/zookeeper/service_discovery.go b/registry/zookeeper/service_discovery.go index 314f56bde..bf73691c2 100644 --- a/registry/zookeeper/service_discovery.go +++ b/registry/zookeeper/service_discovery.go @@ -163,7 +163,7 @@ func (zksd *zookeeperServiceDiscovery) String() string { return fmt.Sprintf("zookeeper-service-discovery[%s]", zksd.url) } -func (zksd *zookeeperServiceDiscovery) Initialize(registryURL common.URL) error { +func (zksd *zookeeperServiceDiscovery) Init(registryURL common.URL) error { return nil } -- GitLab