您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關dubbo-go中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
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的作用是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。