loadbalancer

package
Version: v0.0.0-...-de569a9 Latest Latest
Warning

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

Go to latest
Published: May 21, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 默认虚拟结点数
	DefaultVirualSpots = 400
)

一致性哈希算法

Variables

View Source
var (
	ErrNotExistService = errors.New("service instances are not exist")
)

Functions

This section is empty.

Types

type HashLoadBalancer

type HashLoadBalancer struct {
}

一致性哈希负载均衡器

func (*HashLoadBalancer) SelectServiceByKey

func (h *HashLoadBalancer) SelectServiceByKey(services []*InstanceInfo, key string) (*InstanceInfo, error)

根据key选择服务器

type HashRing

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

哈希环

func NewHashRing

func NewHashRing() *HashRing

构建哈希环

func (*HashRing) AddNode

func (h *HashRing) AddNode(nodeKey string, weight int)

添加服务结点

func (*HashRing) AddNodes

func (h *HashRing) AddNodes(nodeWeight map[string]int)

批量添加服务结点

func (*HashRing) GetNode

func (h *HashRing) GetNode(s string) string

获取指定值所在环中的服务节点

func (*HashRing) RemoveNode

func (h *HashRing) RemoveNode(nodeKey string)

移除服务结点

func (*HashRing) UpdateNode

func (h *HashRing) UpdateNode(nodeKey string, weight int)

更新服务结点

type InstanceInfo

type InstanceInfo struct {
	// 当前权重
	CurWeight int
	// 权重
	Weight int
	// 地址
	Address string
}

服务实例结构体

type LoadBalancer

type LoadBalancer interface {
	// 选择服务器
	SelectService(services []*InstanceInfo) (*InstanceInfo, error)
	// 根据key选择服务器
	SelectServiceByKey(service []*InstanceInfo, key string) (*InstanceInfo, error)
}

负载均衡器

type RandomLoadBalancer

type RandomLoadBalancer struct {
}

随机负载均衡器

func (*RandomLoadBalancer) SelectService

func (r *RandomLoadBalancer) SelectService(services []*InstanceInfo) (*InstanceInfo, error)

选择服务器

type WeightRoundRobinLoadBalancer

type WeightRoundRobinLoadBalancer struct {
}

权重平滑负载均衡器

func (*WeightRoundRobinLoadBalancer) SelectService

func (w *WeightRoundRobinLoadBalancer) SelectService(services []*InstanceInfo) (*InstanceInfo, error)

选择服务器 每次当请求到来,选取服务实例时,该策略会遍历服务实例队列中的所有服务实例。 对于每个服务实例,让它的CurWeight 值加上 Weight 值;同时累加所有服务实例的Weight 值,将其保存为Total。 遍历完所有服务实例之后,如果某个服务实例的CurWeight最大,就选择这个服务实例处理本次请求,最后把该服务实例的 CurWeight 减去 Total 值

Jump to

Keyboard shortcuts

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