package main
import (
"crypto/sha1"
"sync"
"math"
"sort"
"strconv"
"fmt"
)
const (
node1 = "192.168.1.1"
node2 = "192.168.1.2"
node3 = "192.168.1.3"
)
const (
//DefaultVirualSpots default virual spots
DefaultVirualSpots = 400
)
//节点信息
type node struct {
nodeKey string //节点的key
spotValue uint32 //节点的槽位值
}
type nodesArray []node
func (p nodesArray) Len() int { return len(p) }
func (p nodesArray) Less(i, j int) bool { return p[i].spotValue < p[j].spotValue }
func (p nodesArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p nodesArray) Sort() { sort.Sort(p) }
//HashRing store nodes and weigths
type HashRing struct {
virualSpots int //虚拟节点
nodes nodesArray //节点数组(槽位)
weights map[string]int //节点信息对应其权重
mu sync.RWMutex
}
//NewHashRing create a hash ring with virual spots
//初始化Hash环
func NewHashRing() *HashRing {
spots := DefaultVirualSpots
h := &HashRing{
virualS
golang实现一致性哈希算法
最新推荐文章于 2024-07-16 23:26:30 发布

最低0.47元/天 解锁文章
940

被折叠的 条评论
为什么被折叠?



