新聞中心
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)dubbo-go中randomLoadBalance的作用是什么,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、靖西ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的靖西網(wǎng)站制作公司
randomLoadBalance
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
Select
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)]
小結(jié)
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)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享題目:dubbo-go中randomLoadBalance的作用是什么
標(biāo)題路徑:http://www.ef60e0e.cn/article/ghhjcs.html