type uints []uint32// Len returns the length of the uints array.func(x uints)Len()int{
returnlen(x)}// Less returns true if element i is less than element j.func(x uints)Less(i, j int)bool{
return x[i]< x[j]}// Swap exchanges elements i and j.func(x uints)Swap(i, j int){
x[i], x[j]= x[j], x[i]}
定义一致性哈希环数据结构
type Consistent struct{
circle map[uint32]string// 索引和key的map
members map[string]bool// 快速查找key的set
sortedHashes uints // 索引的有序数组
replicas int// 虚拟节点数
count int64// 节点总数
sync.RWMutex // 读写锁}
编写 初始化函数
根据传入的虚拟节点数初始化
// 编写 初始化函数funcNewConsistent(replicas int)*Consistent {
c :=new(Consistent)
c.replicas = replicas
c.circle =make(map[uint32]string)
c.members =make(map[string]bool)return c
}