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加權輪詢負載均衡內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet! 

推薦閱讀: