From d9f57d5647f7f463317329ee9bc8e547a313fe48 Mon Sep 17 00:00:00 2001 From: guobin <596194783@qq.com> Date: Wed, 23 Oct 2019 15:50:06 +0800 Subject: [PATCH] =?UTF-8?q?zookeeper=E6=8E=A5=E5=85=A5kratos=20=20?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E6=B3=A8=E5=86=8C=E5=8F=91=E7=8E=B0=E4=B8=8E?= =?UTF-8?q?=E8=B4=9F=E8=BD=BD=E5=9D=87=E8=A1=A1=201.=20=E5=8A=A0=E5=85=A5c?= =?UTF-8?q?tx.Done=E7=9A=84=E6=83=85=E5=86=B5=E9=98=B2=E6=AD=A2groutine=20?= =?UTF-8?q?=E6=B3=84=E9=9C=B2=202.=20=E6=B3=A8=E5=86=8C=E7=9A=84=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE=EF=BC=8C=E5=85=81=E8=AE=B8=E6=B3=A8=E5=86=8C=E5=A4=9A?= =?UTF-8?q?=E7=A7=8D=E5=8D=8F=E8=AE=AE=EF=BC=8C=E5=85=B7=E4=BD=93=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E5=93=AA=E7=A7=8D=E5=8D=8F=E8=AE=AE=E6=9C=89client?= =?UTF-8?q?=E7=AB=AF=E9=80=89=E6=8B=A9=203.=20=E5=8E=BB=E6=8E=89RootPath?= =?UTF-8?q?=E5=B8=B8=E9=87=8F=EF=BC=8C=E9=81=BF=E5=85=8D=E6=AD=A7=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/naming/zookeeper/zookeeper.go | 69 ++++++++++--------------------- 1 file changed, 22 insertions(+), 47 deletions(-) diff --git a/pkg/naming/zookeeper/zookeeper.go b/pkg/naming/zookeeper/zookeeper.go index 778e8a5a3..9e50f130f 100644 --- a/pkg/naming/zookeeper/zookeeper.go +++ b/pkg/naming/zookeeper/zookeeper.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "strings" "sync" "sync/atomic" "time" @@ -15,10 +14,6 @@ import ( "github.com/go-zookeeper/zk" ) -const ( - RootPath = "/" -) - type Config struct { // Endpoints is a list of URLs. Endpoints []string `json:"endpoints"` @@ -176,7 +171,9 @@ func (z *ZookeeperBuilder) Register(ctx context.Context, ins *naming.Instance) ( continue } } - case <-time.After(time.Second): + case <-ctx.Done(): + ch <- struct{}{} + return } } }() @@ -226,26 +223,14 @@ func (z *ZookeeperBuilder) register(ctx context.Context, ins *naming.Instance) ( log.Warn(fmt.Sprintf("register, fail to registerPerServer node error:(%v)", err)) } - var svrAddr string for _, val := range ins.Addrs { - if strings.HasPrefix(val, "grpc://") { - svrAddr = strings.TrimPrefix(val, "grpc://") - break + err = z.registerEphServer(prefix, "/"+val, ins) + if err != nil { + log.Warn(fmt.Sprintf("registerServer, fail to RegisterEphServer node error:(%v)", err)) + } else { + log.Info(fmt.Sprintf("registerServer, succeed to RegistServer node.")) } } - if svrAddr == "" { - errInfo := fmt.Sprintf("register, error occur, grpc svrAddr is null") - log.Error(errInfo) - return errors.New(errInfo) - } - - err = z.registerEphServer(prefix, RootPath+svrAddr, ins) - if err != nil { - log.Error(fmt.Sprintf("registerServer, fail to RegisterEphServer node error:(%v)", err)) - //return - } else { - log.Info(fmt.Sprintf("registerServer, succeed to RegistServer node.")) - } return nil } @@ -255,35 +240,25 @@ func (z *ZookeeperBuilder) unregister(ins *naming.Instance) (err error) { log.Info("zookeeper unregister enter, instance Addrs:(%v)", ins.Addrs) prefix := z.keyPrefix(ins) - var svrAddr string for _, val := range ins.Addrs { - if strings.HasPrefix(val, "grpc://") { - svrAddr = strings.TrimPrefix(val, "grpc://") - break - } - } - if svrAddr == "" { - errInfo := fmt.Sprintf("unregister, error occur, grpc svrAddr is null") - log.Error(errInfo) - return errors.New(errInfo) - } - - strNode := prefix + RootPath + svrAddr - exists, _, err := z.cli.Exists(strNode) - if err != nil { - log.Error("zk.Conn.Exists node:(%v), error:(%s)", strNode, err.Error()) - return err - } - if exists { - _, s, err := z.cli.Get(strNode) + strNode := prefix + "/" + val + exists, _, err := z.cli.Exists(strNode) if err != nil { - log.Error("zk.Conn.Get node:(%s), error:(%s)", strNode, err.Error()) + log.Error("zk.Conn.Exists node:(%v), error:(%s)", strNode, err.Error()) return err } - return z.cli.Delete(strNode, s.Version) + if exists { + _, s, err := z.cli.Get(strNode) + if err != nil { + log.Error("zk.Conn.Get node:(%s), error:(%s)", strNode, err.Error()) + return err + } + return z.cli.Delete(strNode, s.Version) + } + + log.Info(fmt.Sprintf("unregister, client.Delete:(%v), appid:(%v), hostname:(%v) success", strNode, ins.AppID, ins.Hostname)) } - log.Info(fmt.Sprintf("unregister, client.Delete:(%v), appid:(%v), hostname:(%v) success", strNode, ins.AppID, ins.Hostname)) return } @@ -336,7 +311,7 @@ func (a *appInfo) fetchstore(appID string) (err error) { //for range childs for _, child := range childs { - strNode = prefix + RootPath + child + strNode = prefix + "/" + child resp, _, err := a.zkb.cli.Get(strNode) if err != nil { log.Error("zookeeper: fetch client.Get(%s) error(%v)", strNode, err)