乐趣区

关于golang:聊聊dubbogoproxy的ConsulRegistryLoad

本文次要钻研一下 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

  • dubbo-go-proxy
退出移动版