Go   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了GoLang实现一致性哈希算法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

直接上代码,windows7,go1.7下直接运行。

package main

import (
    "fmt"
    "sort"
    "strconv"
    "hash/crc32"
    "sync"
)

const DEFAULT_REPLICAS = 160

type HashRing []uint32

func (c HashRing) Len() int {
    return len(C)
}

func (c HashRing) Less(i,j int) bool {
    return c[i] < c[j]
}

func (c HashRing) Swap(i,j int) {
    c[i],c[j] = c[j],c[i]
}

type Node struct {
    Id       int
    Ip       String
    Port     int
    HostName String
    Weight   int
}

func NewNode(id int,ip String,port int,name String,weight int) *Node {
    return &Node{
        Id:       id,Ip:       ip,Port:     port,HostName: name,Weight:   weight,}
}

type Consistent struct {
    Nodes     @H_628_4@map[uint32]Node
    numReps   int
    resources @H_628_4@map[int]bool
    ring      HashRing
    sync.RWMutex
}

func NewConsistent() *Consistent {
    return &Consistent{
        Nodes:     @H_21_32@make(@H_628_4@map[uint32]NodE),numReps:   DEFAULT_REPLICAS,resources: @H_21_32@make(@H_628_4@map[int]bool),ring:      HashRing{},}
}

func (c *Consistent) Add(node *NodE) bool {
    c.Lock()
    defer c.Unlock()

    if _,ok := c.resources[node.Id]; ok {
        return false
    }

    count := c.numReps * node.Weight
    for i := 0; i < count; i++ {
        str := c.joinstr(i,nodE)
        c.Nodes[c.hashStr(str)] = *(nodE)
    }
    c.resources[node.Id] = true
    c.sortHashRing()
    return true
}

func (c *Consistent) sortHashRing() {
    c.ring = HashRing{}
    for k := range c.Nodes {
        c.ring = append(c.ring,k)
    }
    sort.sort(c.ring)
}

func (c *Consistent) joinstr(i int,node *NodE) String {
    return node.Ip + "*" + strconv.Itoa(node.Weight) +
        "-" + strconv.Itoa(i) +
        "-" + strconv.Itoa(node.Id)
}

// MurMurHash算法 :https://github.com/spaolacci/murmur3
func (c *Consistent) hashStr(key String) uint32 {
    return crc32.checksumIEEE([]byte(key))
}

func (c *Consistent) Get(key String) Node {
    c.RLock()
    defer c.RUnlock()

    hash := c.hashStr(key)
    i := c.search(hash)

    return c.Nodes[c.ring[i]]
}

func (c *Consistent) search(hash uint32) int {

    i := sort.Search(len(c.ring),func(i int) bool { return c.ring[i] >= hash })
    if i < len(c.ring) {
        if i == len(c.ring)-1 {
            return 0
        } else {
            return i
        }
    } else {
        return len(c.ring) - 1
    }
}

func (c *Consistent) Remove(node *NodE) {
    c.Lock()
    defer c.Unlock()

    if _,ok := c.resources[node.Id]; !ok {
        return
    }

    delete(c.resources,node.Id)

    count := c.numReps * node.Weight
    for i := 0; i < count; i++ {
        str := c.joinstr(i,nodE)
        delete(c.Nodes,c.hashStr(str))
    }
    c.sortHashRing()
}

func main() {

    cHashRing := NewConsistent()

    for i := 0; i < 10; i++ {
        si := fmt.Sprintf("%d",i)
        cHashRing.Add(NewNode(i,"172.18.1."+si, 8080,"host_"+si, 1))
    }

    for k,v := range cHashRing.Nodes {
        fmt.Println("Hash:",k," IP:",v.Ip)
    }

    ipMap := @H_21_32@make(@H_628_4@map[String]int, 0)
    for i := 0; i < 1000; i++ {
        si := fmt.Sprintf("key%d",i)
        k := cHashRing.Get(si)
        if _,ok := ipMap[k.Ip]; ok {
            ipMap[k.Ip] += 1
        } else {
            ipMap[k.Ip] = 1
        }
    }

    for k,v := range ipMap {
        fmt.Println("Node IP:"," count:",v)
    }

}

大佬总结

以上是大佬教程为你收集整理的GoLang实现一致性哈希算法全部内容,希望文章能够帮你解决GoLang实现一致性哈希算法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。