关于golang:聊聊dubbogoproxy的ZookeeperRegistryLoad

39次阅读

共计 2348 个字符,预计需要花费 6 分钟才能阅读完成。

本文次要钻研一下 dubbo-go-proxy 的 ZookeeperRegistryLoad

Loader

dubbo-go-proxy/pkg/registry/load.go

// Loader this interface defined for load services from different kinds registry, such as nacos,consul,zookeeper.
type Loader interface {
    // LoadAllServices load all services registered in registry
    LoadAllServices() ([]*common.URL, error)
    // GetCluster get the registry name
    GetCluster() (string, error)
}

Loader 接口定义了 LoadAllServices、GetCluster 办法

ZookeeperRegistryLoad

dubbo-go-proxy/pkg/registry/zookeeper.go

const (rootPath = "/dubbo")

func init() {var _ Loader = new(ZookeeperRegistryLoad)
}

// ZookeeperRegistryLoad load dubbo apis from zookeeper registry
type ZookeeperRegistryLoad struct {
    zkName  string
    client  *zookeeper.ZookeeperClient
    Address string
    cluster string
}

func newZookeeperRegistryLoad(address, cluster string) (Loader, error) {newClient, err := zookeeper.NewZookeeperClient("zkClient", strings.Split(address, ","), 15*time.Second)
    if err != nil {logger.Warnf("newZookeeperClient error:%v", err)
        return nil, err
    }

    r := &ZookeeperRegistryLoad{
        Address: address,
        client:  newClient,
        cluster: cluster,
    }

    return r, nil
}

ZookeeperRegistryLoad 定义了 zkName、client、Address、cluster 属性;newZookeeperRegistryLoad 依据 address 及 cluster 来创立 ZookeeperRegistryLoad

GetCluster

dubbo-go-proxy/pkg/registry/zookeeper.go

// nolint
func (crl *ZookeeperRegistryLoad) GetCluster() (string, error) {return crl.cluster, nil}

GetCluster 办法返回 cluster 属性

LoadAllServices

dubbo-go-proxy/pkg/registry/zookeeper.go

// LoadAllServices load all services from zookeeper registry
func (crl *ZookeeperRegistryLoad) LoadAllServices() ([]*common.URL, error) {children, err := crl.client.GetChildren(rootPath)
    if err != nil {logger.Errorf("[zookeeper registry] get zk children error:%v", err)
        return nil, err
    }
    var urls []*common.URL
    for _, _interface := range children {providerStr := path.Join(rootPath, "/", _interface, "/", "providers")
        urlStrs, err := crl.client.GetChildren(providerStr)
        if err != nil {logger.Errorf("[zookeeper registry] get zk children \"%s\"error:%v", providerStr, err)
            return nil, err
        }
        for _, url := range urlStrs {dubboURL, err := common.NewURL(url)
            if err != nil {logger.Warnf("[zookeeper registry] transfer zk info to url error:%v", err)
                continue
            }
            urls = append(urls, dubboURL)
        }
    }
    return urls, nil
}

LoadAllServices 通过 client.GetChildren(rootPath) 获取 children,之后遍历 children,挨个获取 provider 信息通过 common.NewURL(url) 构建 dubboURL

小结

ZookeeperRegistryLoad 定义了 zkName、client、Address、cluster 属性;newZookeeperRegistryLoad 依据 address 及 cluster 来创立 ZookeeperRegistryLoad;它实现了 Loader 接口,提供了 GetCluster、LoadAllServices 办法。

doc

  • dubbo-go-proxy

正文完
 0