千家信息网

dubbo-go中roundRobinLoadBalance的作用是什么

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,这篇文章将为大家详细讲解有关dubbo-go中roundRobinLoadBalance的作用是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。ro
千家信息网最后更新 2025年01月31日dubbo-go中roundRobinLoadBalance的作用是什么

这篇文章将为大家详细讲解有关dubbo-go中roundRobinLoadBalance的作用是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

roundRobinLoadBalance

dubbo-go-v1.4.2/cluster/loadbalance/round_robin.go

const (        // RoundRobin ...        RoundRobin = "roundrobin"        // COMPLETE ...        COMPLETE = 0        // UPDATING ...        UPDATING = 1)var (        methodWeightMap sync.Map          // [string]invokers        state           = int32(COMPLETE) // update lock acquired ?        recyclePeriod   = 60 * time.Second.Nanoseconds())func init() {        extension.SetLoadbalance(RoundRobin, NewRoundRobinLoadBalance)}type roundRobinLoadBalance struct{}// NewRoundRobinLoadBalance ...func NewRoundRobinLoadBalance() cluster.LoadBalance {        return &roundRobinLoadBalance{}}
  • roundRobinLoadBalance的NewRoundRobinLoadBalance方法创建了roundRobinLoadBalance

Select

dubbo-go-v1.4.2/cluster/loadbalance/round_robin.go

func (lb *roundRobinLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker {        count := len(invokers)        if count == 0 {                return nil        }        if count == 1 {                return invokers[0]        }        key := invokers[0].GetUrl().Path + "." + invocation.MethodName()        cache, _ := methodWeightMap.LoadOrStore(key, &cachedInvokers{})        cachedInvokers := cache.(*cachedInvokers)        var (                clean               = false                totalWeight         = int64(0)                maxCurrentWeight    = int64(math.MinInt64)                now                 = time.Now()                selectedInvoker     protocol.Invoker                selectedWeightRobin *weightedRoundRobin        )        for _, invoker := range invokers {                var weight = GetWeight(invoker, invocation)                if weight < 0 {                        weight = 0                }                identifier := invoker.GetUrl().Key()                loaded, found := cachedInvokers.LoadOrStore(identifier, &weightedRoundRobin{weight: weight})                weightRobin := loaded.(*weightedRoundRobin)                if !found {                        clean = true                }                if weightRobin.Weight() != weight {                        weightRobin.setWeight(weight)                }                currentWeight := weightRobin.increaseCurrent()                weightRobin.lastUpdate = &now                if currentWeight > maxCurrentWeight {                        maxCurrentWeight = currentWeight                        selectedInvoker = invoker                        selectedWeightRobin = weightRobin                }                totalWeight += weight        }        cleanIfRequired(clean, cachedInvokers, &now)        if selectedWeightRobin != nil {                selectedWeightRobin.Current(totalWeight)                return selectedInvoker        }        // should never happen        return invokers[0]}
  • Select方法遍历invokers,通过weightRobin.increaseCurrent()作为currentWeight,若currentWeight大于maxCurrentWeight则更新maxCurrentWeight,设置selectedInvoker为当前invoker,设置selectedWeightRobin为当前weightRobin;之后对于selectedWeightRobin不为nil的执行selectedWeightRobin.Current(totalWeight),返回selectedInvoker

小结

roundRobinLoadBalance的NewRoundRobinLoadBalance方法创建了roundRobinLoadBalance;其Select方法遍历invokers,通过weightRobin.increaseCurrent()作为currentWeight,若currentWeight大于maxCurrentWeight则更新maxCurrentWeight,设置selectedInvoker为当前invoker,设置selectedWeightRobin为当前weightRobin

关于dubbo-go中roundRobinLoadBalance的作用是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

0