佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Golang加權(quán)輪詢負(fù)載均衡的實現(xiàn)

Golang加權(quán)輪詢負(fù)載均衡的實現(xiàn)

熱門標(biāo)簽:Linux服務(wù)器 呼叫中心市場需求 網(wǎng)站排名優(yōu)化 AI電銷 地方門戶網(wǎng)站 服務(wù)外包 百度競價排名 鐵路電話系統(tǒng)

實現(xiàn)加權(quán)輪詢負(fù)載均衡思路

代碼實現(xiàn)一個加權(quán)負(fù)載均衡

  • Weight            初始化時對節(jié)點約定的權(quán)重
  • currentWeight     節(jié)點臨時權(quán)重,每輪都會變化
  • effectiveWeight   節(jié)點有效權(quán)重,默認(rèn)與Weight相同
  • totalWeight       所有節(jié)點有效權(quán)重之和:sum(effectiveWeight)

代碼實現(xiàn)一個加權(quán)負(fù)載均衡

  • currentWeight = currentWeight+effecitveWeight
  • 選中最大的 currentWeight 節(jié)點為選中節(jié)點
  • currentWeight = currentWeight-totalWeight  (4+3+2=9)

所以我們能夠 在表格模擬運行情況:

請求次數(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]

加權(quán)輪詢負(fù)載均衡代碼

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)文章希望大家以后多多支持腳本之家! 

您可能感興趣的文章:
  • Golang實現(xiàn)四種負(fù)載均衡的算法(隨機(jī),輪詢等)
  • Golang 實現(xiàn)簡單隨機(jī)負(fù)載均衡
  • golang 實現(xiàn)一個負(fù)載均衡案例(隨機(jī),輪訓(xùn))
  • Django高并發(fā)負(fù)載均衡實現(xiàn)原理詳解
  • golang grpc 負(fù)載均衡的方法

標(biāo)簽:湘潭 蘭州 崇左 衡水 銅川 湖南 仙桃 黃山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Golang加權(quán)輪詢負(fù)載均衡的實現(xiàn)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    芒康县| 休宁县| 曲阳县| 合肥市| 秦皇岛市| 乌恰县| 滨州市| 肥乡县| 雷州市| 伊宁市| 西乌珠穆沁旗| 浮梁县| 晋宁县| 平南县| 策勒县| 两当县| 金乡县| 木兰县| 文水县| 岳阳县| 高青县| 铁岭市| 松桃| 都匀市| 齐河县| 濉溪县| 古交市| 奉节县| 邵武市| 连山| 永修县| 平塘县| 扶沟县| 榕江县| 柘城县| 额敏县| 浮山县| 朝阳市| 青冈县| 武乡县| 山西省|