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

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

View File

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