聊聊dubbo-go-proxy的ZookeeperRegistryLoad


本文摘自网络,作者,侵删。

本文主要研究一下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

本文来自:Segmentfault

感谢作者:codecraft

查看原文:聊聊dubbo-go-proxy的ZookeeperRegistryLoad

相关阅读 >>

Go语言不可比较类型与map问题

Golang 空指针怎么检查

Go实战--使用Golang开发windows gui桌面程序(lxn/walk)

stream:我们为何要从python转到Go语言?

Go 语言中的一些非常规优化

Golang-匿名函数

Golang使用for循环的一个小技巧

一个Golang vue使用websocket 的例子

Golang如何升级?

Golang获取两个日期的自然天之差

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...