Skip to content
Snippets Groups Projects
Commit d766f4b7 authored by zhangshen023's avatar zhangshen023
Browse files

delete the method 'Init' of interface ServiceDiscovery

parent 7cf64b76
No related branches found
No related tags found
No related merge requests found
......@@ -186,8 +186,6 @@ const (
ACL_TOKEN = "acl-token"
// default deregister critical server after
DEFAULT_DEREGISTER_TIME = "20s"
DEFAULT_WATCH_TIMEOUT = 60 * 1000
WATCH_TIMEOUT = "consul-watch-timeout"
DEREGISTER_AFTER = "consul-deregister-critical-service-after"
)
......
......@@ -503,10 +503,6 @@ func (m *mockServiceDiscovery) String() string {
panic("implement me")
}
func (m *mockServiceDiscovery) Init(registryURL common.URL) error {
panic("implement me")
}
func (m *mockServiceDiscovery) Destroy() error {
panic("implement me")
}
......
......@@ -21,7 +21,6 @@ import (
"encoding/base64"
"fmt"
"strconv"
"strings"
"sync"
"time"
)
......@@ -35,7 +34,6 @@ import (
)
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"
......@@ -71,43 +69,37 @@ func newConsulServiceDiscovery(name string) (registry.ServiceDiscovery, error) {
descriptor := fmt.Sprintf("consul-service-discovery[%s]", remoteConfig.Address)
config := &consul.Config{Address: remoteConfig.Address, Token: remoteConfig.Params[constant.ACL_TOKEN]}
client, err := consul.NewClient(config)
if err != nil {
return nil, perrors.WithMessage(err, "create consul client failed.")
}
return &consulServiceDiscovery{
address: remoteConfig.Address,
descriptor: descriptor,
address: remoteConfig.Address,
descriptor: descriptor,
checkPassInterval: getCheckPassInterval(remoteConfig.Params),
Config: config,
tag: remoteConfig.Params[constant.QUERY_TAG],
consulClient: client,
deregisterCriticalServiceAfter: getDeregisterAfter(remoteConfig.Params),
}, nil
}
// consulServiceDiscovery is the implementation of service discovery based on consul.
type consulServiceDiscovery struct {
group string
// descriptor is a short string about the basic information of this instance
descriptor string
// Consul client.
consulClient *consul.Client
serviceUrl common.URL
checkPassInterval int64
tag string
tags []string
address string
ttl sync.Map
consulClient *consul.Client
checkPassInterval int64
tag string
address string
deregisterCriticalServiceAfter string
ttl sync.Map
*consul.Config
}
func (csd *consulServiceDiscovery) Init(registryURL common.URL) error {
csd.serviceUrl = registryURL
csd.checkPassInterval = registryURL.GetParamInt(constant.CHECK_PASS_INTERVAL, constant.DEFAULT_CHECK_PASS_INTERVAL)
csd.tag = registryURL.GetParam(constant.QUERY_TAG, "")
csd.tags = strings.Split(registryURL.GetParam("tags", ""), ",")
aclToken := registryURL.GetParam(constant.ACL_TOKEN, "")
csd.Config = &consul.Config{Address: csd.address, Token: aclToken}
client, err := consul.NewClient(csd.Config)
if err != nil {
return perrors.WithMessage(err, "create consul client failed.")
}
csd.consulClient = client
return nil
}
func (csd *consulServiceDiscovery) String() string {
return csd.descriptor
}
......@@ -240,9 +232,8 @@ func decodeConsulMetadata(metadata map[string]string) map[string]string {
}
func (csd *consulServiceDiscovery) GetInstances(serviceName string) []registry.ServiceInstance {
waitTime := csd.serviceUrl.GetParamInt(constant.WATCH_TIMEOUT, constant.DEFAULT_WATCH_TIMEOUT) / 1000
instances, _, err := csd.consulClient.Health().Service(serviceName, csd.tag, true, &consul.QueryOptions{
WaitTime: time.Duration(waitTime),
WaitTime: time.Duration(csd.checkPassInterval),
})
if err != nil {
logger.Errorf("get instances for service %s,error: %v", serviceName, err)
......@@ -411,10 +402,32 @@ func (csd *consulServiceDiscovery) buildCheck(instance registry.ServiceInstance)
return consul.AgentServiceCheck{
//CheckID: buildID(instance),
TTL: strconv.FormatInt(csd.checkPassInterval/1000, 10) + "s",
DeregisterCriticalServiceAfter: deregister,
DeregisterCriticalServiceAfter: csd.deregisterCriticalServiceAfter,
}
}
// nolint
func getCheckPassInterval(params map[string]string) int64 {
checkPassIntervalStr, ok := params[constant.CHECK_PASS_INTERVAL]
if !ok {
return constant.DEFAULT_CHECK_PASS_INTERVAL
}
checkPassInterval, err := strconv.ParseInt(checkPassIntervalStr, 10, 64)
if err != nil {
logger.Warnf("consul service discovery remote config error:%s", checkPassIntervalStr)
return constant.DEFAULT_CHECK_PASS_INTERVAL
}
return checkPassInterval
}
// nolint
func getDeregisterAfter(metadata map[string]string) string {
deregister, ok := metadata[constant.DEREGISTER_AFTER]
if !ok || len(deregister) == 0 {
deregister = constant.DEFAULT_DEREGISTER_TIME
}
return deregister
}
func buildID(instance registry.ServiceInstance) string {
id := fmt.Sprintf("id:%s,serviceName:%s,host:%s,port:%d", instance.GetId(), instance.GetServiceName(), instance.GetHost(), instance.GetPort())
......
......@@ -80,8 +80,7 @@ func TestConsulServiceDiscovery_newConsulServiceDiscovery(t *testing.T) {
func TestConsulServiceDiscovery_Destroy(t *testing.T) {
prepareData()
serviceDiscovery, err := extension.GetServiceDiscovery(constant.CONSUL_KEY, testName)
_, registryUrl := prepareService()
serviceDiscovery.Init(registryUrl)
prepareService()
assert.Nil(t, err)
assert.NotNil(t, serviceDiscovery)
err = serviceDiscovery.Destroy()
......@@ -103,15 +102,12 @@ func TestConsulServiceDiscovery_CRUD(t *testing.T) {
extension.SetAndInitGlobalDispatcher("mock")
rand.Seed(time.Now().Unix())
instance, registryUrl := prepareService()
instance, _ := prepareService()
// clean data
serviceDiscovery, err := extension.GetServiceDiscovery(constant.CONSUL_KEY, testName)
assert.Nil(t, err)
err = serviceDiscovery.Init(registryUrl)
assert.Nil(t, err)
err = serviceDiscovery.Unregister(instance)
assert.Nil(t, err)
......
......@@ -31,7 +31,6 @@ import (
)
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"
......@@ -67,10 +66,6 @@ type etcdV3ServiceDiscovery struct {
childListenerMap map[string]*etcdv3.EventListener
}
func (e *etcdV3ServiceDiscovery) Init(registryURL common.URL) error {
return nil
}
// basic information of this instance
func (e *etcdV3ServiceDiscovery) String() string {
return e.descriptor
......
......@@ -30,7 +30,6 @@ import (
)
import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/observer"
dispatcher2 "github.com/apache/dubbo-go/common/observer/dispatcher"
......@@ -117,10 +116,6 @@ func (tel *TestServiceInstancePreRegisteredEventListener) GetEventType() reflect
type ServiceDiscoveryA struct {
}
func (msd *ServiceDiscoveryA) Init(registryURL common.URL) error {
return nil
}
// String return mockServiceDiscovery
func (msd *ServiceDiscoveryA) String() string {
return "testServiceDiscovery"
......
......@@ -17,17 +17,12 @@
package event
import (
"sync"
)
import (
gxset "github.com/dubbogo/gost/container/set"
gxpage "github.com/dubbogo/gost/page"
)
import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/observer"
"github.com/apache/dubbo-go/config"
......@@ -38,8 +33,7 @@ import (
// EventPublishingServiceDiscovery will enhance Service Discovery
// Publish some event about service discovery
type EventPublishingServiceDiscovery struct {
serviceDiscovery registry.ServiceDiscovery
serviceDiscoveryInitOnce sync.Once
serviceDiscovery registry.ServiceDiscovery
}
// NewEventPublishingServiceDiscovery is a constructor
......@@ -54,14 +48,6 @@ func (epsd *EventPublishingServiceDiscovery) String() string {
return epsd.serviceDiscovery.String()
}
func (epsd *EventPublishingServiceDiscovery) Init(registryURL common.URL) error {
var err error
epsd.serviceDiscoveryInitOnce.Do(func() {
err = epsd.serviceDiscovery.Init(registryURL)
})
return err
}
// Destroy delegate function
func (epsd *EventPublishingServiceDiscovery) Destroy() error {
f := func() error {
......
......@@ -32,7 +32,6 @@ import (
)
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"
......@@ -65,10 +64,6 @@ type nacosServiceDiscovery struct {
registryInstances []registry.ServiceInstance
}
func (n *nacosServiceDiscovery) Init(registryURL common.URL) error {
return nil
}
// Destroy will close the service discovery.
// Actually, it only marks the naming client as null and then return
func (n *nacosServiceDiscovery) Destroy() error {
......
......@@ -26,10 +26,6 @@ import (
gxpage "github.com/dubbogo/gost/page"
)
import (
"github.com/apache/dubbo-go/common"
)
const DefaultPageSize = 100
// ServiceDiscovery is the common operations of Service Discovery
......@@ -38,12 +34,6 @@ type ServiceDiscovery interface {
// ----------------- lifecycle -------------------
/**
* Initializes the ServiceDiscovery
*
*/
Init(registryURL common.URL) error
// Destroy will destroy the service discovery.
// If the discovery cannot be destroy, it will return an error.
Destroy() error
......
......@@ -125,7 +125,6 @@ func creatServiceDiscovery(url *common.URL) (registry.ServiceDiscovery, error) {
return nil, perrors.WithMessage(err, "Create service discovery fialed")
}
serviceDiscovery := event.NewEventPublishingServiceDiscovery(originServiceDiscovery)
serviceDiscovery.Init(*url)
return serviceDiscovery, nil
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment