聊聊dubbo-go-proxy的ConsulRegistryLoad

codecraft · · 424 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

本文主要研究一下dubbo-go-proxy的ConsulRegistryLoad

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方法

ConsulRegistryLoad

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

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

const (
    dubboAPIFilter = "dubbo in Tags"
)

// ConsulRegistryLoad load dubbo apis from consul registry
type ConsulRegistryLoad struct {
    Address string
    // Consul client.
    client  *consul.Client
    cluster string
}

func newConsulRegistryLoad(address, cluster string) (Loader, error) {
    config := &consul.Config{Address: address}
    client, err := consul.NewClient(config)
    if err != nil {
        return nil, err
    }

    r := &ConsulRegistryLoad{
        Address: address,
        client:  client,
        cluster: cluster,
    }

    return r, nil
}
ConsulRegistryLoad定义了Address、client、cluster属性;newConsulRegistryLoad方法根据address及cluster来创建ConsulRegistryLoad

GetCluster

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

func (crl *ConsulRegistryLoad) GetCluster() (string, error) {
    return crl.cluster, nil
}
GetCluster方法返回ConsulRegistryLoad的cluster属性

LoadAllServices

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

func (crl *ConsulRegistryLoad) LoadAllServices() ([]*common.URL, error) {
    agentServices, err := crl.client.Agent().ServicesWithFilter(dubboAPIFilter)
    if err != nil {
        logger.Error("consul load all apis error:%v", err)
        return nil, perrors.Wrap(err, "consul load all apis")
    }
    var urls []*common.URL
    for _, service := range agentServices {
        url, err := crl.transfer2Url(*service)
        if err != nil {
            logger.Warnf("consul transfer service to url error:%v", err)
            continue
        }
        urls = append(urls, url)
    }
    return urls, nil
}
LoadAllServices方法通过ConsulRegistryLoad的client.Agent().ServicesWithFilter来获取agentServices,之后遍历agentServices通过transfer2Url方法转换为url

transfer2Url

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

func (crl *ConsulRegistryLoad) transfer2Url(service consul.AgentService) (*common.URL, error) {
    var params = url.Values{}
    var protocol string

    for _, tag := range service.Tags {
        kv := strings.Split(tag, "=")
        if len(kv) != 2 {
            continue
        }
        params.Add(kv[0], kv[1])
    }

    if url, ok := service.Meta["url"]; ok {
        protocol = strings.Split(url, ":")[0]
    }

    methodsParam := strings.Split(params.Get(constant.METHODS_KEY), ",")
    var methods = make([]string, len(methodsParam))
    for _, method := range methodsParam {
        if method != "" {
            methods = append(methods, method)
        }
    }
    url := common.NewURLWithOptions(common.WithPort(strconv.Itoa(service.Port)),
        common.WithProtocol(protocol), common.WithMethods(methods),
        common.WithIp(service.Address), common.WithParams(params))

    return url, nil
}
transfer2Url方法遍历service.Tags追加到params,之后解析service.Meta["url"],再解析params.Get(constant.METHODS_KEY),最后通过common.NewURLWithOptions构建url

小结

ConsulRegistryLoad定义了Address、client、cluster属性;newConsulRegistryLoad方法根据address及cluster来创建ConsulRegistryLoad;它实现了Loader接口,提供了GetCluster、LoadAllServices方法。

doc


有疑问加站长微信联系(非本文作者)

本文来自:Segmentfault

感谢作者:codecraft

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

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

424 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传