增加负载均衡策略修改的方法

This commit is contained in:
白茶清欢 2025-05-23 22:16:30 +08:00
parent e85811512f
commit 640e022a40
2 changed files with 19 additions and 7 deletions

View File

@ -1,23 +1,23 @@
// Package dispatch...
// Package abstract ...
//
// Description : dispatch...
//
// Author : go_developer@163.com<张德满>
//
// Date : 2021-04-01 5:53 下午
package dispatch
package abstract
import (
"git.zhangdeman.cn/gateway/balance/define"
"git.zhangdeman.cn/zhangdeman/exception"
)
// IDispatch 负载均衡的接口定义
// IBalance 负载均衡的接口定义
//
// Author : go_developer@163.com<张德满>
//
// Date : 2:44 下午 2021/4/1
type IDispatch interface {
type IBalance interface {
// Get 获取一个节点
Get(nodeList []*define.SeverNode) (string, exception.IException)
}

View File

@ -8,10 +8,11 @@
package balance
import (
"git.zhangdeman.cn/gateway/balance/abstract"
"git.zhangdeman.cn/gateway/balance/define"
"git.zhangdeman.cn/gateway/balance/dispatch"
"git.zhangdeman.cn/zhangdeman/easylock"
"git.zhangdeman.cn/zhangdeman/exception"
"reflect"
)
// NewServer ...
@ -19,7 +20,7 @@ import (
// Author : go_developer@163.com<张德满>
//
// Date : 6:51 下午 2021/4/1
func NewServer(nodeList []*define.SeverNode, d dispatch.IDispatch) *Server {
func NewServer(nodeList []*define.SeverNode, d abstract.IBalance) *Server {
return &Server{
lock: easylock.NewLock(),
NodeList: nodeList,
@ -35,7 +36,7 @@ func NewServer(nodeList []*define.SeverNode, d dispatch.IDispatch) *Server {
type Server struct {
lock easylock.EasyLock
NodeList []*define.SeverNode
Balance dispatch.IDispatch
Balance abstract.IBalance
}
// Add 添加一个Server
@ -81,3 +82,14 @@ func (s *Server) Get() (string, exception.IException) {
}()
return s.Balance.Get(s.NodeList)
}
// ChangeBalance 修改负载均衡策略
func (s *Server) ChangeBalance(d abstract.IBalance) {
if nil == d {
return
}
if reflect.ValueOf(d).IsNil() {
return
}
s.Balance = d
}