# Golang实现四种负载均衡算法

### 随机负载

```package load_balance
import (
"errors"
"math/rand"
)
//随机负载均衡
type RandomBalance struct {
curIndex int
}
func (r *RandomBalance) Add(params ...string) error {
if len(params) == 0 {
return errors.New("params len 1 at least")
}
return nil
}
func (r *RandomBalance) Next() string {
return ""
}
}
func (r *RandomBalance) Get(string) (string, error) {
return r.Next(), nil
}

### 轮询负载

```package load_balance
import "errors"
//轮询负载均衡
type RoundRobinBalance struct {
curIndex int
}
func (r *RoundRobinBalance) Add(params ...string) error {
if len(params) == 0 {
return errors.New("params len 1 at least")
}
return nil
}
func (r *RoundRobinBalance) Next() string {
return ""
}
if r.curIndex >= lens {
r.curIndex = 0
}
r.curIndex = (r.curIndex + 1) % lens
}
func (r *RoundRobinBalance) Get(string) (string, error) {
return r.Next(), nil
}

### 加权轮询负载

```package load_balance
import (
"errors"
"strconv"
)
type WeightRoundRobinBalance struct {
curIndex int
rsw      []int
}
type WeightNode struct {
Weight          int //初始化时对节点约定的权重
currentWeight   int //节点临时权重，每轮都会变化
effectiveWeight int //有效权重, 默认与weight相同 , totalWeight = sum(effectiveWeight)  //出现故障就-1
}
//1, currentWeight = currentWeight + effectiveWeight
//2, 选中最大的currentWeight节点为选中节点
//3, currentWeight = currentWeight - totalWeight
func (r *WeightRoundRobinBalance) Add(params ...string) error {
if len(params) != 2 {
return errors.New("params len need 2")
}
parInt, err := strconv.ParseInt(params[1], 10, 64)
if err != nil {
return err
}
node := &WeightNode{
Weight: int(parInt),
}
node.effectiveWeight = node.Weight
return nil
}
func (r *WeightRoundRobinBalance) Next() string {
var best *WeightNode
total := 0
for i := 0; i < len(r.rss); i++ {
//1 计算所有有效权重
total += w.effectiveWeight
//2 修改当前节点临时权重
w.currentWeight += w.effectiveWeight
//3 有效权重默认与权重相同，通讯异常时-1, 通讯成功+1，直到恢复到weight大小
if w.effectiveWeight < w.Weight {
w.effectiveWeight++
}
//4 选中最大临时权重节点
if best == nil || w.currentWeight > best.currentWeight {
best = w
}
}
if best == nil {
return ""
}
//5 变更临时权重为 临时权重-有效权重之和
best.currentWeight -= total
}
func (r *WeightRoundRobinBalance) Get(string) (string, error) {
return r.Next(), nil
}
func (r *WeightRoundRobinBalance) Update()  {
}

### 一致性hash

```package load_balance
import (
"errors"
"hash/crc32"
"sort"
"strconv"
"sync"
)
//1 单调性（唯一） 2平衡性 (数据 目标元素均衡) 3分散性(散列)
type Hash func(data []byte) uint32
type UInt32Slice []uint32
func (s UInt32Slice) Len() int {
return len(s)
}
func (s UInt32Slice) Less(i, j int) bool {
return s[i] < s[j]
}
func (s UInt32Slice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
type ConsistentHashBalance struct {
mux      sync.RWMutex
hash     Hash
replicas int               //复制因子
keys     UInt32Slice       //已排序的节点hash切片
hashMap  map[uint32]string //节点哈希和key的map, 键是hash值，值是节点key
}
func NewConsistentHashBalance(replicas int, fn Hash) *ConsistentHashBalance {
m := &ConsistentHashBalance{
replicas: replicas,
hash:     fn,
hashMap:  make(map[uint32]string),
}
if m.hash == nil {
//最多32位，保证是一个2^32-1环
m.hash = crc32.ChecksumIEEE
}
return m
}
func (c *ConsistentHashBalance) IsEmpty() bool {
return len(c.keys) == 0
}
func (c *ConsistentHashBalance) Add(params ...string) error {
if len(params) == 0 {
return errors.New("param len 1 at least")
}
c.mux.Lock()
defer c.mux.Unlock()
// 结合复制因子计算所有虚拟节点的hash值，并存入m.keys中，同时在m.hashMap中保存哈希值和key的映射
for i := 0; i < c.replicas; i++ {
c.keys = append(c.keys, hash)
}
// 对所有虚拟节点的哈希值进行排序，方便之后进行二分查找
sort.Sort(c.keys)
return nil
}
// Get 方法根据给定的对象获取最靠近它的那个节点
func (c *ConsistentHashBalance) Get(key string) (string, error) {
if c.IsEmpty() {
return "", errors.New("node is empty")
}
hash := c.hash([]byte(key))
// 通过二分查找获取最优节点，第一个"服务器hash"值大于"数据hash"值的就是最优"服务器节点"
idx := sort.Search(len(c.keys), func(i int) bool { return c.keys[i] >= hash })
// 如果查找结果 大于 服务器节点哈希数组的最大索引，表示此时该对象哈希值位于最后一个节点之后，那么放入第一个节点中
if idx == len(c.keys) {
idx = 0
}
c.mux.RLock()
defer c.mux.RUnlock()
return c.hashMap[c.keys[idx]], nil
}

### 封装

```package load_balance
Get(string)(string, error)
}

```package load_balance
type LbType int
const (
LbRandom LbType = iota
LbRoundRobin
LbWeightRoundRobin
LbConsistentHash
)
switch lbType {
case LbRandom:
return &RandomBalance{}
case LbConsistentHash:
return NewConsistentHashBalance(10, nil)
case LbRoundRobin:
return &RoundRobinBalance{}
case LbWeightRoundRobin:
return &WeightRoundRobinBalance{}
default:
return &RandomBalance{}
}
}