网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

dubbo-go中leastActiveLoadBalance的用法

这篇文章主要介绍“dubbo-go中leastActiveLoadBalance的用法”,在日常操作中,相信很多人在dubbo-go中leastActiveLoadBalance的用法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”dubbo-go中leastActiveLoadBalance的用法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

成都创新互联成立与2013年,是专业互联网技术服务公司,拥有项目做网站、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元中宁做网站,已为上家服务,为中宁各地企业和个人服务,联系电话:028-86922220

本文主要研究一下dubbo-go的leastActiveLoadBalance

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

Select

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]

GetWeight

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的用法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


网站题目:dubbo-go中leastActiveLoadBalance的用法
文章来源:http://bjjierui.cn/article/ppodps.html

其他资讯