您好,登錄后才能下訂單哦!
這篇文章主要介紹“dubbo-go中leastActiveLoadBalance的用法”,在日常操作中,相信很多人在dubbo-go中leastActiveLoadBalance的用法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”dubbo-go中leastActiveLoadBalance的用法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
本文主要研究一下dubbo-go的leastActiveLoadBalance
dubbo-go-v1.4.2/cluster/loadbalance/least_active.go
const ( // LeastActive ... LeastActive = "leastactive" ) func init() { extension.SetLoadbalance(LeastActive, NewLeastActiveLoadBalance) } type leastActiveLoadBalance struct { } // NewLeastActiveLoadBalance ... func NewLeastActiveLoadBalance() cluster.LoadBalance { return &leastActiveLoadBalance{} }
leastActiveLoadBalance的NewLeastActiveLoadBalance方法創建leastActiveLoadBalance
dubbo-go-v1.4.2/cluster/loadbalance/least_active.go
func (lb *leastActiveLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker { count := len(invokers) if count == 0 { return nil } if count == 1 { return invokers[0] } var ( leastActive int32 = -1 // The least active value of all invokers totalWeight int64 // The number of invokers having the same least active value (LEAST_ACTIVE) firstWeight int64 // Initial value, used for comparison leastCount int // The number of invokers having the same least active value (LEAST_ACTIVE) leastIndexes = make([]int, count) // The index of invokers having the same least active value (LEAST_ACTIVE) sameWeight = true // Every invoker has the same weight value? ) for i := 0; i < count; i++ { invoker := invokers[i] // Active number active := protocol.GetMethodStatus(invoker.GetUrl(), invocation.MethodName()).GetActive() // current weight (maybe in warmUp) weight := GetWeight(invoker, invocation) // There are smaller active services if leastActive == -1 || active < leastActive { leastActive = active leastIndexes[0] = i leastCount = 1 // next available leastIndex offset totalWeight = weight firstWeight = weight sameWeight = true } else if active == leastActive { leastIndexes[leastCount] = i totalWeight += weight leastCount++ if sameWeight && (i > 0) && weight != firstWeight { sameWeight = false } } } if leastCount == 1 { return invokers[0] } if !sameWeight && totalWeight > 0 { offsetWeight := rand.Int63n(totalWeight) + 1 for i := 0; i < leastCount; i++ { leastIndex := leastIndexes[i] offsetWeight -= GetWeight(invokers[i], invocation) if offsetWeight <= 0 { return invokers[leastIndex] } } } index := leastIndexes[rand.Intn(leastCount)] return invokers[index] }
Select方法遍歷invokers,挨個通過protocol.GetMethodStatus(invoker.GetUrl(), invocation.MethodName()).GetActive()獲取active信息,并通過GetWeight(invoker, invocation)獲取weight,然后計算leastCount、totalWeight、sameWeight;對于leastCount為1的返回invokers[0],對于sameWeight為false且totalWeight大于0的,遍歷leastIndexes,計算offsetWeight,若offsetWeight小于等于0,則返回invokers[leastIndex],否則通過leastIndexes[rand.Intn(leastCount)]計算index,返回invokers[index]
dubbo-go-v1.4.2/cluster/loadbalance/util.go
// GetWeight ... func GetWeight(invoker protocol.Invoker, invocation protocol.Invocation) int64 { url := invoker.GetUrl() weight := url.GetMethodParamInt64(invocation.MethodName(), constant.WEIGHT_KEY, constant.DEFAULT_WEIGHT) if weight > 0 { //get service register time an do warm up time now := time.Now().Unix() timestamp := url.GetParamInt(constant.REMOTE_TIMESTAMP_KEY, now) if uptime := now - timestamp; uptime > 0 { warmup := url.GetParamInt(constant.WARMUP_KEY, constant.DEFAULT_WARMUP) if uptime < warmup { if ww := float64(uptime) / float64(warmup) / float64(weight); ww < 1 { weight = 1 } else if int64(ww) <= weight { weight = int64(ww) } } } } return weight }
GetWeight方法通過url.GetMethodParamInt64(invocation.MethodName(), constant.WEIGHT_KEY, constant.DEFAULT_WEIGHT)獲取weight,若weight大于0,則計算warmup,重新計算weight值
leastActiveLoadBalance的NewLeastActiveLoadBalance方法創建leastActiveLoadBalance;Select方法遍歷invokers,挨個通過protocol.GetMethodStatus(invoker.GetUrl(), invocation.MethodName()).GetActive()獲取active信息,并通過GetWeight(invoker, invocation)獲取weight,然后計算leastCount、totalWeight、sameWeight,最后計算index
到此,關于“dubbo-go中leastActiveLoadBalance的用法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。