請求次數 | 請求前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加權輪詢負載均衡內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
上一篇:詳解玩轉直播系列之消息模塊演進
下一篇:Go語言設計模式之結構型模式