增加第一个简版的轮询负载均衡

This commit is contained in:
白茶清欢 2021-10-19 14:48:46 +08:00
parent 3aaffbeab2
commit 75b557b07f
6 changed files with 178 additions and 2 deletions

55
balance/base.go Normal file
View File

@ -0,0 +1,55 @@
// Package balance...
//
// Description : balance...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-19 2:26 下午
package balance
import "sync"
// base ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:26 下午 2021/10/19
type base struct {
lock *sync.RWMutex
}
// Lock ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:27 下午 2021/10/19
func (b *base) Lock() {
b.lock.Lock()
}
// Unlock ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:27 下午 2021/10/19
func (b *base) Unlock() {
b.lock.Unlock()
}
// RLock ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:28 下午 2021/10/19
func (b *base) RLock() {
b.lock.RLock()
}
// RUnlock ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:28 下午 2021/10/19
func (b *base) RUnlock() {
b.lock.RUnlock()
}

19
balance/define/base.go Normal file
View File

@ -0,0 +1,19 @@
// Package define...
//
// Description : define...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-19 12:43 下午
package define
const (
// ServerNodeStatusNormal 服务节点状态正常
ServerNodeStatusNormal = 0
// ServerNodeStatusLoss 探活逻辑无法和服务建立连接
ServerNodeStatusLoss = 1
// ServerNodeStatusException 指定时间区间内出现过多 5xx
ServerNodeStatusException = 2
// ServerNodeStatusRemove 通过调用函数的方法,将节点移除
ServerNodeStatusRemove = 3
)

20
balance/define/node.go Normal file
View File

@ -0,0 +1,20 @@
// Package define...
//
// Description : define...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-19 12:36 下午
package define
// ServerNode 服务器节点的定义
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:36 下午 2021/10/19
type ServerNode struct {
HostIP string `json:"host_ip"` // 机器IP
Port int `json:"port"` // 机器端口
Status int `json:"status"` // 机器状态
Weight int `json:"weight"` // 机器权重
}

76
balance/poll.go Normal file
View File

@ -0,0 +1,76 @@
// Package balance ...
//
// Description : 轮询机制的负载均衡
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-19 12:35 下午
package balance
import (
"sync"
"git.zhangdeman.cn/zhangdeman/gopkg/balance/define"
"github.com/pkg/errors"
)
// NewPoll 获取轮询实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:40 下午 2021/10/19
func NewPoll(serverList []*define.ServerNode) (*Poll, error) {
if nil == serverList || len(serverList) == 0 {
return nil, errors.New("server list is empty")
}
return &Poll{
base: base{
lock: &sync.RWMutex{},
},
serverList: serverList,
currentServerIndex: 0,
}, nil
}
// Poll ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:41 下午 2021/10/19
type Poll struct {
base
serverList []*define.ServerNode
currentServerIndex int
}
// GetServerNode 获取服务节点
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:43 下午 2021/10/19
func (p *Poll) GetServerNode() (*define.ServerNode, error) {
if len(p.serverList) == 0 {
return nil, errors.New("server list is empty")
}
p.Lock()
defer p.Unlock()
var (
serverNode *define.ServerNode
)
// 循环次数
loopTimes := 0
for loopTimes < len(p.serverList) {
loopTimes++
p.currentServerIndex = (p.currentServerIndex + 1) % len(p.serverList)
if serverNode = p.serverList[p.currentServerIndex]; serverNode.Status != define.ServerNodeStatusNormal {
continue
}
break
}
if nil == serverNode || serverNode.Status != define.ServerNodeStatusNormal {
return nil, errors.New("has no server node be used")
}
return serverNode, nil
}

4
go.mod
View File

@ -10,6 +10,7 @@ require (
github.com/go-redis/redis_rate/v9 v9.1.1
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/pkg/errors v0.9.1
github.com/shirou/gopsutil v3.21.9+incompatible
github.com/spaolacci/murmur3 v1.1.0
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.9.0
@ -21,6 +22,7 @@ require (
)
require (
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
@ -28,6 +30,7 @@ require (
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21 // indirect
github.com/eapache/queue v1.1.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
@ -53,7 +56,6 @@ require (
github.com/pierrec/lz4 v2.6.0+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/shirou/gopsutil v3.21.9+incompatible // indirect
github.com/tidwall/match v1.0.3 // indirect
github.com/tidwall/pretty v1.1.0 // indirect
github.com/tklauser/go-sysconf v0.3.9 // indirect

6
go.sum
View File

@ -2,6 +2,8 @@ github.com/Shopify/sarama v1.29.1 h1:wBAacXbYVLmWieEA/0X/JagDdCZ8NVFOfS6l6+2u5S0
github.com/Shopify/sarama v1.29.1/go.mod h1:mdtqvCSg8JOxk8PmpTNGyo6wzd4BMm4QXSfDnTXmgkE=
github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWsokNbMijUGhmcoBJc=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
@ -31,6 +33,8 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.7.4 h1:QmUZXrvJ9qZ3GfWvQ+2wnW/1ePrTEJqPKMYEU3lD/DM=
github.com/gin-gonic/gin v1.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY=
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
@ -207,6 +211,7 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@ -218,7 +223,6 @@ golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71 h1:ikCpsnYR+Ew0vu99XlDp55lGgDJdIMx3f4a18jfse/s=
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=