登录
首页 >  Golang >  Go问答

不提供 http 服务时 golang 客户端负载均衡器

来源:stackoverflow

时间:2024-04-23 21:27:35 310浏览 收藏

大家好,今天本人给大家带来文章《不提供 http 服务时 golang 客户端负载均衡器》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!

问题内容

作为一个golang n00b,我有一个 go 程序,它将消息读取到 kafka 中,修改它们,然后将它们发布到列表中的 http 端点之一。

到目前为止,我们做了一些非常基本的随机循环

cur := rand.Int() % len(httpEndpointList)

我想改进这一点,并根据端点的响应时间或类似内容增加端点的权重。

我研究过库,但我发现的所有库都是使用 http.handle 编写的,可用作中间件。例如,请参阅 oxy lib roundrobin

就我而言,我不提供 http 请求。

有什么想法可以在我的 golang 程序中实现这种更高级的客户端负载平衡吗?

我想避免在我的环境中使用另一个 haproxy 或类似的。


解决方案


有一个非常简单的加权随机选择算法:

package main

import (
    "fmt"
    "math/rand"
)

type endpoint struct {
    url    string
    weight int
}

func randomweightedselector(endpoints []endpoint) endpoint {
    // this first loop should be optimised so it only gets computed once
    max := 0
    for _, endpoint := range endpoints {
        max = max + endpoint.weight
    }

    r := rand.intn(max)
    for _, endpoint := range endpoints {
        if r < endpoint.weight {
            return endpoint
        } else {
            r = r - endpoint.weight
        }
    }
    // should never get to this point because r is smaller than max
    return endpoint{}
}

func main() {
    endpoints := []endpoint{
        {weight: 1, url: "https://web1.example.com"},
        {weight: 2, url: "https://web2.example.com"},
    }

    count1 := 0
    count2 := 0

    for i := 0; i < 100; i++ {
        switch randomweightedselector(endpoints).url {
        case "https://web1.example.com":
            count1++
        case "https://web2.example.com":
            count2++
        }
    }
    fmt.println("times web1: ", count1)
    fmt.println("times web2: ", count2)
}

在可以优化的情况下,这是最幼稚的。当然,对于生产来说,您不应该每次都计算最大值,但除此之外,这基本上就是解决方案。

这是一个更专业和面向对象的版本,不会每次都重新计算最大值:

package main

import (
    "fmt"
    "math/rand"
)

type Endpoint struct {
    URL    string
    Weight int
}

type RandomWeightedSelector struct {
    max       int
    endpoints []Endpoint
}

func (rws *RandomWeightedSelector) AddEndpoint(endpoint Endpoint) {
    rws.endpoints = append(rws.endpoints, endpoint)
    rws.max += endpoint.Weight
}

func (rws *RandomWeightedSelector) Select() Endpoint {
    r := rand.Intn(rws.max)
    for _, endpoint := range rws.endpoints {
        if r < endpoint.Weight {
            return endpoint
        } else {
            r = r - endpoint.Weight
        }
    }
    // should never get to this point because r is smaller than max
    return Endpoint{}
}

func main() {
    var rws RandomWeightedSelector
    rws.AddEndpoint(Endpoint{Weight: 1, URL: "https://web1.example.com"})
    rws.AddEndpoint(Endpoint{Weight: 2, URL: "https://web2.example.com"})

    count1 := 0
    count2 := 0

    for i := 0; i < 100; i++ {
        switch rws.Select().URL {
        case "https://web1.example.com":
            count1++
        case "https://web2.example.com":
            count2++
        }
    }
    fmt.Println("Times web1: ", count1)
    fmt.Println("Times web2: ", count2)
}

对于基于端点延迟等指标更新权重的部分,我将创建一个不同的对象,使用此指标来更新 randomweightedselector 对象中的权重。我认为一起实施它会违背单一责任。

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>