您好,登錄后才能下訂單哦!
這篇文章主要講解了“Golang怎么實現加權輪詢負載均衡”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Golang怎么實現加權輪詢負載均衡”吧!
實現加權輪詢負載均衡思路
加權輪詢負載均衡代碼
測試代碼
代碼實現一個加權負載均衡
Weight 初始化時對節點約定的權重
currentWeight 節點臨時權重,每輪都會變化
effectiveWeight 節點有效權重,默認與Weight相同
totalWeight 所有節點有效權重之和:sum(effectiveWeight)
代碼實現一個加權負載均衡
currentWeight = currentWeight+effecitveWeight
選中最大的 currentWeight 節點為選中節點
currentWeight = currentWeight-totalWeight (4+3+2=9)
所以我們能夠 在表格模擬運行情況:
請求次數 | 請求前currentWelght | 選中的節點 | 請求后currentWelght |
---|---|---|---|
1 | [serverA=4,serverB=3,serverC=2] | serverA | [serverA=-1,serverB=6,serverC=4] |
2 | [serverA=-1,serverB=6,serverC=4] | serverB | [serverA=3,serverB=0,serverC=6] |
3 | [serverA=3,serverB=0,serverC=6] | serverc | [serverA=7,serverB=3,serverC=-1] |
4 | [serverA=7,serverB=3,serverC=-1] | serverA | [serverA=2,serverB=6,serverC=1] |
5 | [serverA=2,serverB=6,serverC=1] | serverB | [serverA=6,serverB=0,serverC=3] |
6 | [serverA=6,serverB=0,serverC=3] | serverA | [serverA=1,serverB=3,serverC=5] |
7 | [serverA=1,serverB=3,serverC=5] | serverc | [serverA=5,serverB=6,serverC=-2] |
package load_balance import ( "errors" "strconv" ) type WeightRoundRobinBalance struct { curIndex int rss []*WeightNode rsw []int //觀察主體 conf LoadBalanceConf } // 配置主題 type LoadBalanceConf interface { GetConf() []string WatchConf() UpdateConf(conf []string) } type WeightNode struct { addr string // 服務器地址 weight int //權重值 currentWeight int //節點當前權重 effectiveWeight int //有效權重 } func (r *WeightRoundRobinBalance) Add(params ...string) error { if len(params) != 2 { return errors.New("param len need 2") } parInt, err := strconv.ParseInt(params[1], 10, 64) if err != nil { return err } node := &WeightNode{addr: params[0], weight: int(parInt)} node.effectiveWeight = node.weight r.rss = append(r.rss, node) return nil } func (r *WeightRoundRobinBalance) Next() string { total := 0 var best *WeightNode for i := 0; i < len(r.rss); i++ { w := r.rss[i] //step 1 統計所有有效權重之和 total += w.effectiveWeight //step 2 變更節點臨時權重為的節點臨時權重+節點有效權重 w.currentWeight += w.effectiveWeight //step 3 有效權重默認與權重相同,通訊異常時-1, 通訊成功+1,直到恢復到weight大小 if w.effectiveWeight < w.weight { w.effectiveWeight++ } //step 4 選擇最大臨時權重點節點 if best == nil || w.currentWeight > best.currentWeight { best = w } } if best == nil { return "" } //step 5 變更臨時權重為 臨時權重-有效權重之和 best.currentWeight -= total return best.addr } func (r *WeightRoundRobinBalance) Get(key string) (string, error) { return r.Next(), nil } func (r *WeightRoundRobinBalance) SetConf(conf LoadBalanceConf) { r.conf = conf }
package load_balance import ( "fmt" "testing" ) func TestLB(t *testing.T) { rb := &WeightRoundRobinBalance{} rb.Add("127.0.0.1:2003", "4") //0 // rb.Add("127.0.0.1:2004", "3") //1 rb.Add("127.0.0.1:2005", "2") //2 fmt.Println(rb.Next()) fmt.Println(rb.Next()) fmt.Println(rb.Next()) fmt.Println(rb.Next()) fmt.Println(rb.Next()) fmt.Println(rb.Next()) fmt.Println(rb.Next()) fmt.Println(rb.Next()) fmt.Println(rb.Next()) fmt.Println(rb.Next()) fmt.Println(rb.Next()) fmt.Println(rb.Next()) fmt.Println(rb.Next()) fmt.Println(rb.Next()) }
測試結果
$ go test
127.0.0.1:2003
127.0.0.1:2005
127.0.0.1:2003
127.0.0.1:2003
127.0.0.1:2005
127.0.0.1:2003
127.0.0.1:2003
127.0.0.1:2005
127.0.0.1:2003
127.0.0.1:2003
127.0.0.1:2005
127.0.0.1:2003
127.0.0.1:2003
127.0.0.1:2005
PASS
ok gateway/_test/demo 0.080s## 127.0.0.1:2003 為 127.0.0.1:2005 權重兩倍。而從答應結果上看,符合要求
感謝各位的閱讀,以上就是“Golang怎么實現加權輪詢負載均衡”的內容了,經過本文的學習后,相信大家對Golang怎么實現加權輪詢負載均衡這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。