一个很简单的 golang 一致性hash 库consistent
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package main
import (
"log"
"github.com/lafikl/consistent"
)
func main() {
c := consistent.New()
// adds the hosts to the ring
c.Add("127.0.0.1:8000")
c.Add("92.0.0.1:8000")
// Returns the host that owns `key`.
//
// As described in https://en.wikipedia.org/wiki/Consistent_hashing
//
// It returns ErrNoHosts if the ring has no hosts in it.
host, err := c.Get("/app.html")
if err != nil {
log.Fatal(err)
}
log.Println(host)
}
项目地址: lafikl/consistent
本文网址: https://golangnote.com/topic/173.html 转摘请注明来源