升级接口Server接口约束,支持修改服务器节点配置

This commit is contained in:
白茶清欢 2025-05-23 22:38:59 +08:00
parent e1597328d2
commit 48c8c1652d
4 changed files with 61 additions and 14 deletions

22
abstract/IServer.go Normal file
View File

@ -0,0 +1,22 @@
// Package abstract ...
//
// Description : abstract ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2025-05-23 22:30
package abstract
import (
"git.zhangdeman.cn/gateway/balance/define"
"git.zhangdeman.cn/zhangdeman/exception"
)
// IServer 服务节点集合的接口约束
type IServer interface {
Add(node *define.SeverNode) // 在集群中添加一个节点
Modify(node *define.SeverNode) // 修改集群中一个节点的信息
Remove(nodeID string) // 在集群中移除一个节点
Get() (string, exception.IException) // 获取一个访问节点
ChangeBalance(d IBalance) // 修改负载均衡策略
}

View File

@ -13,9 +13,12 @@ package define
//
// Date : 2:46 下午 2021/4/1
type SeverNode struct {
ID string `json:"id"` // 机器编号
Host string `json:"host"` // ip
Port int `json:"port"` // 端口
Weight float64 `json:"weight"` // 权重
Status int `json:"status"` // 状态
ID string `json:"id"` // 机器编号
Host string `json:"host"` // ip
Port int `json:"port"` // 端口
Weight float64 `json:"weight"` // 权重
Status int `json:"status"` // 状态
Count uint64 `json:"count"` // 节点访问次数, 用于实现最小访问次数的策略
CostTime uint64 `json:"cost_time"` // 节点访问耗时, 用于实现最小访问耗时的策略
FailureCount uint64 `json:"failure_count"` // 节点访问失败次数, 用于实现最小访问失败次数的策略
}

View File

@ -10,9 +10,9 @@ package balance
import (
"git.zhangdeman.cn/gateway/balance/abstract"
"git.zhangdeman.cn/gateway/balance/define"
"git.zhangdeman.cn/zhangdeman/easylock"
"git.zhangdeman.cn/zhangdeman/exception"
"reflect"
"sync"
)
// NewServer ...
@ -22,7 +22,7 @@ import (
// Date : 6:51 下午 2021/4/1
func NewServer(nodeList []*define.SeverNode, d abstract.IBalance) *Server {
return &Server{
lock: easylock.NewLock(),
lock: &sync.RWMutex{},
NodeList: nodeList,
Balance: d,
}
@ -34,7 +34,7 @@ func NewServer(nodeList []*define.SeverNode, d abstract.IBalance) *Server {
//
// Date : 2:59 下午 2021/4/1
type Server struct {
lock easylock.EasyLock
lock *sync.RWMutex
NodeList []*define.SeverNode
Balance abstract.IBalance
}
@ -45,9 +45,9 @@ type Server struct {
//
// Date : 3:00 下午 2021/4/1
func (s *Server) Add(node *define.SeverNode) {
_ = s.lock.Lock()
s.lock.Lock()
defer func() {
_ = s.lock.Unlock()
s.lock.Unlock()
}()
s.NodeList = append(s.NodeList, node)
}
@ -58,9 +58,9 @@ func (s *Server) Add(node *define.SeverNode) {
//
// Date : 5:09 下午 2021/4/1
func (s *Server) Remove(nodeID string) {
_ = s.lock.Lock()
s.lock.Lock()
defer func() {
_ = s.lock.Unlock()
s.lock.Unlock()
}()
for nodeIndex, item := range s.NodeList {
if item.ID == nodeID {
@ -76,13 +76,35 @@ func (s *Server) Remove(nodeID string) {
//
// Date : 5:17 下午 2021/4/1
func (s *Server) Get() (string, exception.IException) {
_ = s.lock.RLock()
s.lock.RLock()
defer func() {
_ = s.lock.RUnlock()
s.lock.RUnlock()
}()
return s.Balance.Get(s.NodeList)
}
func (s *Server) Modify(node *define.SeverNode) {
if nil == node {
return
}
s.lock.RLock()
defer func() {
s.lock.RUnlock()
}()
findExistNode := false
for nodeIndex, item := range s.NodeList {
if item.ID == node.ID {
s.NodeList[nodeIndex] = node
findExistNode = true
break
}
}
if !findExistNode {
// 没找到已存在节点, 自动降级为添加
s.Add(node)
}
}
// ChangeBalance 修改负载均衡策略
func (s *Server) ChangeBalance(d abstract.IBalance) {
if nil == d {