您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)dubbo-go中randomLoadBalance的作用是什么,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
dubbo-go-v1.4.2/cluster/loadbalance/random.go
const ( name = "random" ) func init() { extension.SetLoadbalance(name, NewRandomLoadBalance) } type randomLoadBalance struct { } // NewRandomLoadBalance ... func NewRandomLoadBalance() cluster.LoadBalance { return &randomLoadBalance{} }
randomLoadBalance的NewRandomLoadBalance方法創(chuàng)建randomLoadBalance
dubbo-go-v1.4.2/cluster/loadbalance/random.go
func (lb *randomLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker { var length int if length = len(invokers); length == 1 { return invokers[0] } sameWeight := true weights := make([]int64, length) firstWeight := GetWeight(invokers[0], invocation) totalWeight := firstWeight weights[0] = firstWeight for i := 1; i < length; i++ { weight := GetWeight(invokers[i], invocation) weights[i] = weight totalWeight += weight if sameWeight && weight != firstWeight { sameWeight = false } } if totalWeight > 0 && !sameWeight { // If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight. offset := rand.Int63n(totalWeight) for i := 0; i < length; i++ { offset -= weights[i] if offset < 0 { return invokers[i] } } } // If all invokers have the same weight value or totalWeight=0, return evenly. return invokers[rand.Intn(length)] }
Select方法先判斷invokers數(shù)量,若只有一個(gè)則返回invokers[0];之后遍歷invokers計(jì)算totalWeight及sameWeight,若totalWeight大于0且sameWeight為false則使用rand.Int63n(totalWeight)隨機(jī)一個(gè)offset,之后遍歷weights,用offset挨個(gè)去減weights[i],若offset小于0,則返回invokers[i];若都沒有選中,則返回invokers[rand.Intn(length)]
randomLoadBalance的NewRandomLoadBalance方法創(chuàng)建randomLoadBalance;其Select方法使用了帶weight的方法,具體就是使用rand.Int63n(totalWeight)隨機(jī)一個(gè)offset,之后遍歷weights,用offset挨個(gè)去減weights[i],若offset小于0,則返回invokers[i]
上述就是小編為大家分享的dubbo-go中randomLoadBalance的作用是什么了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。