loadbalance

package
v0.0.0-...-869c34b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultCheckMethod xx
	DefaultCheckMethod = 0
	// DefaultCheckTimeout xx
	DefaultCheckTimeout = 2
	// DefaultCheckMaxErrNum xx
	DefaultCheckMaxErrNum = 2
	// DefaultCheckInterval xx
	DefaultCheckInterval = 5
)

default check setting

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckConf

type CheckConf struct {
	// contains filtered or unexported fields
}

CheckConf 客户端服务发现负载均衡配置

func NewCheckConf

func NewCheckConf(format string, conf map[string]string) (*CheckConf, error)

NewCheckConf 创建主动探测的负载均衡配置 CheckConf

func (*CheckConf) Attach

func (s *CheckConf) Attach(o Observer)

Attach 绑定

func (*CheckConf) GetConf

func (s *CheckConf) GetConf() []string

GetConf 获取配置

func (*CheckConf) NotifyAllObservers

func (s *CheckConf) NotifyAllObservers()

NotifyAllObservers 通知所有的

func (*CheckConf) UpdateConf

func (s *CheckConf) UpdateConf(conf []string)

UpdateConf 更新配置时,通知监听者也更新

func (*CheckConf) WatchConf

func (s *CheckConf) WatchConf()

WatchConf 更新配置时,通知监听者也更新

type Conf

type Conf interface {
	Attach(o Observer)        // 绑定
	GetConf() []string        // 获取配置
	WatchConf()               // 监听
	UpdateConf(conf []string) // 更新
}

Conf 服务发现配置主题

type ConsistentHashBanlance

type ConsistentHashBanlance struct {
	// contains filtered or unexported fields
}

ConsistentHashBanlance 一致性Hash负载均衡

func NewConsistentHashBanlance

func NewConsistentHashBanlance(replicas int, fn Hash) *ConsistentHashBanlance

NewConsistentHashBanlance 创建

func (*ConsistentHashBanlance) Add

func (c *ConsistentHashBanlance) Add(params ...string) error

Add 方法用来添加缓存节点,参数为节点 key,比如使用 IP

func (*ConsistentHashBanlance) Get

func (c *ConsistentHashBanlance) Get(key string) (string, error)

Get 方法根据给定的对象获取最靠近它的那个节点

func (*ConsistentHashBanlance) IsEmpty

func (c *ConsistentHashBanlance) IsEmpty() bool

IsEmpty 验证是否为空

func (*ConsistentHashBanlance) SetConf

func (c *ConsistentHashBanlance) SetConf(conf Conf)

SetConf 设置

func (*ConsistentHashBanlance) Update

func (c *ConsistentHashBanlance) Update()

Update 更新配置,通知其他...

type Hash

type Hash func(data []byte) uint32

Hash 哈希函数

type LbType

type LbType int

LbType 加权策略

const (
	// LbRandom 随机负载均衡
	LbRandom LbType = iota
	// LbRoundRobin 轮询负载
	LbRoundRobin
	// LbWeightRoundRobin 加权轮询
	LbWeightRoundRobin
	// LbConsistentHash 一致性hash负载均衡
	LbConsistentHash
)

type LoadBalance

type LoadBalance interface {
	Add(...string) error
	Get(string) (string, error)

	// 后期服务发现
	Update()
}

LoadBalance 负载均衡策略接口

func FactorWithConf

func FactorWithConf(lbType LbType, mConf Conf) LoadBalance

FactorWithConf 观察者模式结合负载均衡器工厂方法

func Factory

func Factory(lbType LbType) LoadBalance

Factory 负载策略工厂方法 默认使用随机负载均衡策略

type LoadBalanceObserver

type LoadBalanceObserver struct {
	ModuleConf *ZKConf
}

LoadBalanceObserver 观察者

func NewLoadBalanceObserver

func NewLoadBalanceObserver(conf *ZKConf) *LoadBalanceObserver

NewLoadBalanceObserver 创建 NewLoadBalanceObserver

func (*LoadBalanceObserver) Update

func (l *LoadBalanceObserver) Update()

Update 更新配置

type Observer

type Observer interface {
	Update()
}

Observer 观察者接口

type RandomBalance

type RandomBalance struct {
	// contains filtered or unexported fields
}

RandomBalance 存放服务器列表和当前使用服务器下标

func (*RandomBalance) Add

func (r *RandomBalance) Add(params ...string) error

Add 添加服务器

func (*RandomBalance) Get

func (r *RandomBalance) Get(key string) (string, error)

Get 对 Next 进一步包装

func (*RandomBalance) Next

func (r *RandomBalance) Next() string

Next 从服务器列表随机获取一台服务器

func (*RandomBalance) SetConf

func (r *RandomBalance) SetConf(conf Conf)

SetConf 初始化配置

func (*RandomBalance) Update

func (r *RandomBalance) Update()

Update 更新配置

type RoundRobinBalance

type RoundRobinBalance struct {
	// contains filtered or unexported fields
}

RoundRobinBalance 轮询负载均衡

func (*RoundRobinBalance) Add

func (r *RoundRobinBalance) Add(params ...string) error

Add 添加服务 一次添加一个服务地址

func (*RoundRobinBalance) Get

func (r *RoundRobinBalance) Get(key string) (string, error)

Get 对Next方法进一步封装

func (*RoundRobinBalance) Next

func (r *RoundRobinBalance) Next() string

Next 轮询方式获取服务器

func (*RoundRobinBalance) SetConf

func (r *RoundRobinBalance) SetConf(conf Conf)

SetConf 配置观察主题

func (*RoundRobinBalance) Update

func (r *RoundRobinBalance) Update()

Update 更新,通知其他...

type UInt32Slice

type UInt32Slice []uint32

UInt32Slice 存储排序节点

func (UInt32Slice) Len

func (s UInt32Slice) Len() int

Len 返回长度

func (UInt32Slice) Less

func (s UInt32Slice) Less(i, j int) bool

Less 比较函数

func (UInt32Slice) Swap

func (s UInt32Slice) Swap(i, j int)

Swap 交换函数

type WeightNode

type WeightNode struct {
	Weight int // 权重
	// contains filtered or unexported fields
}

WeightNode 权重节点

type WeightRoundRobinBalance

type WeightRoundRobinBalance struct {
	// contains filtered or unexported fields
}

WeightRoundRobinBalance 加权轮询

func (*WeightRoundRobinBalance) Add

func (r *WeightRoundRobinBalance) Add(params ...string) error

Add 添加下游服务 params 接收两个string参数 第一个为服务器Ip 第二个是该服务节点权重值

func (*WeightRoundRobinBalance) Get

func (r *WeightRoundRobinBalance) Get(key string) (string, error)

Get Next方法的进一步包装

func (*WeightRoundRobinBalance) Next

func (r *WeightRoundRobinBalance) Next() string

Next 加权获取下游服务

func (*WeightRoundRobinBalance) SetConf

func (r *WeightRoundRobinBalance) SetConf(conf Conf)

SetConf 设置服务发现

func (*WeightRoundRobinBalance) Update

func (r *WeightRoundRobinBalance) Update()

Update 更新,通知其他...

type ZKConf

type ZKConf struct {
	Observers []Observer // 观察者列表
	// contains filtered or unexported fields
}

ZKConf zookeeper 客户端服务发现配置

func NewLoadBalanceZkConf

func NewLoadBalanceZkConf(
	format, path string, zkHosts []string, conf map[string]string) (*ZKConf, error)

NewLoadBalanceZkConf 创建 ZKConf

func (*ZKConf) Attach

func (s *ZKConf) Attach(o Observer)

Attach 把观察者绑定到 Observer 上面

func (*ZKConf) GetConf

func (s *ZKConf) GetConf() []string

GetConf 获取当前 ZK 注册中心的服务列表

func (*ZKConf) NotifyAllObservers

func (s *ZKConf) NotifyAllObservers()

NotifyAllObservers 通知所有的 Observer

func (*ZKConf) UpdateConf

func (s *ZKConf) UpdateConf(conf []string)

UpdateConf 更新配置时,通知监听者也更新

func (*ZKConf) WatchConf

func (s *ZKConf) WatchConf()

WatchConf 更新配置,通知监听者也更新

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL