您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“dubbo-go中ConsistentHashLoadBalance的用法”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
本文主要研究一下dubbo-go的ConsistentHashLoadBalance
dubbo-go-v1.4.2/cluster/loadbalance/consistent_hash.go
const ( // ConsistentHash ... ConsistentHash = "consistenthash" // HashNodes ... HashNodes = "hash.nodes" // HashArguments ... HashArguments = "hash.arguments" ) var ( selectors = make(map[string]*ConsistentHashSelector) re = regexp.MustCompile(constant.COMMA_SPLIT_PATTERN) ) func init() { extension.SetLoadbalance(ConsistentHash, NewConsistentHashLoadBalance) } // ConsistentHashLoadBalance ... type ConsistentHashLoadBalance struct { } // NewConsistentHashLoadBalance ... func NewConsistentHashLoadBalance() cluster.LoadBalance { return &ConsistentHashLoadBalance{} }
ConsistentHashLoadBalance的init方法設(shè)置了名為consistenthash的ConsistentHashLoadBalance到extension中
dubbo-go-v1.4.2/cluster/loadbalance/consistent_hash.go
// Select ... func (lb *ConsistentHashLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker { methodName := invocation.MethodName() key := invokers[0].GetUrl().ServiceKey() + "." + methodName // hash the invokers bs := make([]byte, 0) for _, invoker := range invokers { b, err := json.Marshal(invoker) if err != nil { return nil } bs = append(bs, b...) } hashCode := crc32.ChecksumIEEE(bs) selector, ok := selectors[key] if !ok || selector.hashCode != hashCode { selectors[key] = newConsistentHashSelector(invokers, methodName, hashCode) selector = selectors[key] } return selector.Select(invocation) }
Select方法遍歷invokers挨個執(zhí)行json.Marshal(invoker),將bytes[]添加到bs中,之后通過crc32.ChecksumIEEE(bs)計算hashCode,然后對比selectors[key]的hashCode與計算出來的hashCode是否一致,不一致則通過newConsistentHashSelector重新設(shè)置一個,最后執(zhí)行selector.Select(invocation)
dubbo-go-v1.4.2/cluster/loadbalance/consistent_hash.go
// ConsistentHashSelector ... type ConsistentHashSelector struct { hashCode uint32 replicaNum int virtualInvokers map[uint32]protocol.Invoker keys Uint32Slice argumentIndex []int }
ConsistentHashSelector定義了hashCode、replicaNum、virtualInvokers、keys、argumentIndex屬性
dubbo-go-v1.4.2/cluster/loadbalance/consistent_hash.go
func newConsistentHashSelector(invokers []protocol.Invoker, methodName string, hashCode uint32) *ConsistentHashSelector { selector := &ConsistentHashSelector{} selector.virtualInvokers = make(map[uint32]protocol.Invoker) selector.hashCode = hashCode url := invokers[0].GetUrl() selector.replicaNum = int(url.GetMethodParamInt(methodName, HashNodes, 160)) indices := re.Split(url.GetMethodParam(methodName, HashArguments, "0"), -1) for _, index := range indices { i, err := strconv.Atoi(index) if err != nil { return nil } selector.argumentIndex = append(selector.argumentIndex, i) } for _, invoker := range invokers { u := invoker.GetUrl() address := u.Ip + ":" + u.Port for i := 0; i < selector.replicaNum/4; i++ { digest := md5.Sum([]byte(address + strconv.Itoa(i))) for j := 0; j < 4; j++ { key := selector.hash(digest, j) selector.keys = append(selector.keys, key) selector.virtualInvokers[key] = invoker } } } sort.Sort(selector.keys) return selector }
newConsistentHashSelector方法實例化ConsistentHashSelector,并初始化virtualInvokers、hashCode、argumentIndex、keys、virtualInvokers屬性
dubbo-go-v1.4.2/cluster/loadbalance/consistent_hash.go
// Select ... func (c *ConsistentHashSelector) Select(invocation protocol.Invocation) protocol.Invoker { key := c.toKey(invocation.Arguments()) digest := md5.Sum([]byte(key)) return c.selectForKey(c.hash(digest, 0)) } func (c *ConsistentHashSelector) toKey(args []interface{}) string { var sb strings.Builder for i := range c.argumentIndex { if i >= 0 && i < len(args) { fmt.Fprint(&sb, args[i].(string)) } } return sb.String() } func (c *ConsistentHashSelector) selectForKey(hash uint32) protocol.Invoker { idx := sort.Search(len(c.keys), func(i int) bool { return c.keys[i] >= hash }) if idx == len(c.keys) { idx = 0 } return c.virtualInvokers[c.keys[idx]] } func (c *ConsistentHashSelector) hash(digest [16]byte, i int) uint32 { return uint32((digest[3+i*4]&0xFF)<<24) | uint32((digest[2+i*4]&0xFF)<<16) | uint32((digest[1+i*4]&0xFF)<<8) | uint32(digest[i*4]&0xFF)&0xFFFFFFF }
Select方法通過c.toKey(invocation.Arguments())獲取key,再通過md5.Sum([]byte(key))計算digest,最后通過c.selectForKey(c.hash(digest, 0))選取Invoker
ConsistentHashLoadBalance的Select方法遍歷invokers挨個執(zhí)行json.Marshal(invoker),將bytes[]添加到bs中,之后通過crc32.ChecksumIEEE(bs)計算hashCode,然后對比selectors[key]的hashCode與計算出來的hashCode是否一致,不一致則通過newConsistentHashSelector重新設(shè)置一個,最后執(zhí)行selector.Select(invocation)
“dubbo-go中ConsistentHashLoadBalance的用法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。