這篇文章將為大家詳細講解有關dubbo-go中roundRobinLoadBalance的作用是什么,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
創(chuàng)新互聯(lián)建站作為成都網站建設公司,專注網站建設公司、網站設計,有關企業(yè)網站設計方案、改版、費用等問題,行業(yè)涉及成都雨棚定制等多個領域,已為上千家企業(yè)服務,得到了客戶的尊重與認可。
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方法創(chuàng)建了roundRobinLoadBalance
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的執(zhí)行selectedWeightRobin.Current(totalWeight),返回selectedInvoker
roundRobinLoadBalance的NewRoundRobinLoadBalance方法創(chuàng)建了roundRobinLoadBalance;其Select方法遍歷invokers,通過weightRobin.increaseCurrent()作為currentWeight,若currentWeight大于maxCurrentWeight則更新maxCurrentWeight,設置selectedInvoker為當前invoker,設置selectedWeightRobin為當前weightRobin
關于dubbo-go中roundRobinLoadBalance的作用是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。