請求次數(shù) | 請求前currentWelght | 選中的節(jié)點 | 請求后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 // 服務(wù)器地址 weight int //權(quán)重值 currentWeight int //節(jié)點當(dāng)前權(quán)重 effectiveWeight int //有效權(quán)重 } 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 統(tǒng)計所有有效權(quán)重之和 total += w.effectiveWeight //step 2 變更節(jié)點臨時權(quán)重為的節(jié)點臨時權(quán)重+節(jié)點有效權(quán)重 w.currentWeight += w.effectiveWeight //step 3 有效權(quán)重默認(rèn)與權(quán)重相同,通訊異常時-1, 通訊成功+1,直到恢復(fù)到weight大小 if w.effectiveWeight w.weight { w.effectiveWeight++ } //step 4 選擇最大臨時權(quán)重點節(jié)點 if best == nil || w.currentWeight > best.currentWeight { best = w } } if best == nil { return "" } //step 5 變更臨時權(quán)重為 臨時權(quán)重-有效權(quán)重之和 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()) }
測試結(jié)果
$ 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 權(quán)重兩倍。而從答應(yīng)結(jié)果上看,符合要求
到此這篇關(guān)于Golang加權(quán)輪詢負(fù)載均衡的實現(xiàn)的文章就介紹到這了,更多相關(guān)Golang加權(quán)輪詢負(fù)載均衡內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:吐魯番 雞西 汕頭 蘭州 梅河口 銅川 欽州 重慶
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Golang加權(quán)輪詢負(fù)載均衡的實現(xiàn)》,本文關(guān)鍵詞 Golang,加權(quán),輪詢,負(fù)載,均衡,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。